0

I have a project in which I try to run queries on XML files using in.jlibs.jlibs-xmldog.2.2.1.

The XPath that I'm using is

(//server[@id=123])[1]

and the file to be queried looks like this

<servers>
    <server id="123" name="servername3">
        <cpu_count>3</cpu_count>
        <ram_gb>3</ram_gb>
        <mac>aabbccaabbcc</mac>
        <domain>test.internal</domain>
        <ip>192.168.1.20</ip>
    </server>
    <server id="124" name="servername4">
        <cpu_count>4</cpu_count>
        <ram_gb>4</ram_gb>
        <mac>aabbccaabbcc</mac>
        <domain>test.internal</domain>
        <ip>192.168.1.20</ip>
    </server>
    <server id="125" name="servername5">
        <cpu_count>5</cpu_count>
        <ram_gb>5</ram_gb>
        <mac>aabbccaabbcc</mac>
        <domain>test.internal</domain>
        <ip>192.168.1.20</ip>
    </server>
</servers>

As you can see, there is already only one result that is returned by the inner xpath statement, but I have some reasons for which I need to do the query like this. Anyway, the same behaviour happens if I have more results returned by the inner statement. I've tested the xpath online and it seems to be valid.

Code:

public static void main(String[] args) throws Exception{
    final String filePath = "C:\\Users\\NCsorba\\Desktop\\xml\\sample_11433_entries.xml";
    final String xPathExpression = "(//server[@id=123])[1]";

    DefaultNamespaceContext namespaceContext = new DefaultNamespaceContext();
    namespaceContext.declarePrefix("xsd", Namespaces.URI_XSD);
    XMLDog xmlDog = new XMLDog(namespaceContext);

    Expression compiledXPath = xmlDog.addXPath(xPathExpression);
    XPathResults xPathResults = xmlDog.sniff(new InputSource(filePath)); // exception thrown

    // System.out.println(xPathResults.getResult(compiledXPath));
}

Here is the stack trace:

   Exception in thread "main" java.lang.NegativeArraySizeException
        at jlibs.core.util.LongTreeMap$Values.toArray(LongTreeMap.java:534)
        at java.util.ArrayList.<init>(ArrayList.java:178)
        at jlibs.xml.sax.dog.expr.nodset.PathEvaluation.finished(PathExpression.java:218)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.fireFinished(LocationEvaluation.java:215)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.finished(LocationEvaluation.java:208)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.resultPrepared(LocationEvaluation.java:190)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.expired(LocationEvaluation.java:176)
        at jlibs.xml.sax.dog.path.EventID.expireList(EventID.java:244)
        at jlibs.xml.sax.dog.path.EventID.expire(EventID.java:230)
        at jlibs.xml.sax.dog.path.EventID.pop(EventID.java:322)
        at jlibs.xml.sax.dog.sniff.Event.firePop(Event.java:266)
        at jlibs.xml.sax.dog.sniff.Event.pop(Event.java:712)
        at jlibs.xml.sax.dog.sniff.Event.onEndDocument(Event.java:587)
        at jlibs.xml.sax.dog.sniff.SAXHandler.endDocument(SAXHandler.java:75)
        at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
        at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:189)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:174)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:205)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:212)
        at com.hp.oo.content.actions.xml.Main.main(Main.java:24)
        --------------- linked to ------------------
    javax.xml.xpath.XPathException: java.lang.NegativeArraySizeException
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:192)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:174)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:205)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:212)
        at com.hp.oo.content.actions.xml.Main.main(Main.java:24)
        Caused by: java.lang.NegativeArraySizeException
        at jlibs.core.util.LongTreeMap$Values.toArray(LongTreeMap.java:534)
        at java.util.ArrayList.<init>(ArrayList.java:178)
        at jlibs.xml.sax.dog.expr.nodset.PathEvaluation.finished(PathExpression.java:218)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.fireFinished(LocationEvaluation.java:215)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.finished(LocationEvaluation.java:208)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.resultPrepared(LocationEvaluation.java:190)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.expired(LocationEvaluation.java:176)
        at jlibs.xml.sax.dog.path.EventID.expireList(EventID.java:244)
        at jlibs.xml.sax.dog.path.EventID.expire(EventID.java:230)
        at jlibs.xml.sax.dog.path.EventID.pop(EventID.java:322)
        at jlibs.xml.sax.dog.sniff.Event.firePop(Event.java:266)
        at jlibs.xml.sax.dog.sniff.Event.pop(Event.java:712)
        at jlibs.xml.sax.dog.sniff.Event.onEndDocument(Event.java:587)
        at jlibs.xml.sax.dog.sniff.SAXHandler.endDocument(SAXHandler.java:75)
        at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
        at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:189)
        ... 4 more
        --------------- linked to ------------------
    javax.xml.xpath.XPathException: javax.xml.xpath.XPathException: java.lang.NegativeArraySizeException
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:176)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:205)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:212)
        at com.hp.oo.content.actions.xml.Main.main(Main.java:24)
    Caused by: javax.xml.xpath.XPathException: java.lang.NegativeArraySizeException
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:192)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:174)
        ... 3 more
    Caused by: java.lang.NegativeArraySizeException
        at jlibs.core.util.LongTreeMap$Values.toArray(LongTreeMap.java:534)
        at java.util.ArrayList.<init>(ArrayList.java:178)
        at jlibs.xml.sax.dog.expr.nodset.PathEvaluation.finished(PathExpression.java:218)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.fireFinished(LocationEvaluation.java:215)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.finished(LocationEvaluation.java:208)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.resultPrepared(LocationEvaluation.java:190)
        at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.expired(LocationEvaluation.java:176)
        at jlibs.xml.sax.dog.path.EventID.expireList(EventID.java:244)
        at jlibs.xml.sax.dog.path.EventID.expire(EventID.java:230)
        at jlibs.xml.sax.dog.path.EventID.pop(EventID.java:322)
        at jlibs.xml.sax.dog.sniff.Event.firePop(Event.java:266)
        at jlibs.xml.sax.dog.sniff.Event.pop(Event.java:712)
        at jlibs.xml.sax.dog.sniff.Event.onEndDocument(Event.java:587)
        at jlibs.xml.sax.dog.sniff.SAXHandler.endDocument(SAXHandler.java:75)
        at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
        at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:189)
        ... 4 more

I'll appreciate any help! Thanks!

Norberth Csorba
  • 312
  • 1
  • 9
  • That line of [XMLDog library](https://github.com/santhosh-tekuri/jlibs/blob/master/xmldog/src/main/java/jlibs/xml/sax/dog/XMLDog.java) is for parsing the source. Your posted sample is missing the closing tag for the document element. – Alejandro Jan 17 '20 at 15:54
  • Yeah, the post was not formatted well: the closing tag was there. – Norberth Csorba Jan 20 '20 at 08:03
  • There seems to be a potential logic error in [LongTreeMap.java:356](https://github.com/santhosh-tekuri/jlibs/blob/master/core/src/main/java/jlibs/core/util/LongTreeMap.java) as field called _size_ might have its value decremented even if given entry is not found. That still might not solve the problem as the result of the above xpath should be 1, not 0. – Norberth Csorba Jan 21 '20 at 15:59

0 Answers0