Обновление JTidy сломало документ xpaths - PullRequest
3 голосов
/ 07 октября 2009

Я только что обновился до последней версии jtidy, вышедшей в октябре, и, похоже, он по неизвестным причинам сломал мой объект документа. Это мой код:

tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setShowErrors(0);
tidy.setQuiet(true);
tidy.setMakeClean(true);

URL url = new URL(url_string);
Document doc = tidy.parseDOM(url.openStream(), null);

String xpath_string = "//table[@id='links']//a";
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xpath_string);
NodeList n = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);

И это ошибка, которую я получаю:

javax.xml.transform.TransformerException: -1
    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
    at IndoorClimbing.main(IndoorClimbing.java:55)
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
    at com.sun.org.apache.xml.internal.dtm.ref.ExpandedNameTable.getType(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase.indexNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.addNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.nextNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase._firstch(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase.getFirstChild(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBaseTraversers$ChildTraverser.first(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.AxesWalker.getNextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown Source)
    ... 6 more
---------
java.lang.ArrayIndexOutOfBoundsException: -1
    at com.sun.org.apache.xml.internal.dtm.ref.ExpandedNameTable.getType(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase.indexNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.addNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.nextNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase._firstch(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase.getFirstChild(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBaseTraversers$ChildTraverser.first(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.AxesWalker.getNextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
    at IndoorClimbing.main(IndoorClimbing.java:55)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(Unknown Source)
    at IndoorClimbing.main(IndoorClimbing.java:55)
Caused by: javax.xml.transform.TransformerException: -1
    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(Unknown Source)
    ... 2 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
    at com.sun.org.apache.xml.internal.dtm.ref.ExpandedNameTable.getType(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase.indexNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.addNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.dom2dtm.DOM2DTM.nextNode(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase._firstch(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase.getFirstChild(Unknown Source)
    at com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBaseTraversers$ChildTraverser.first(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.AxesWalker.getNextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown Source)
    ... 6 more

Ошибка возникает в последней строке кода при попытке создать NodeList. У кого-нибудь были такие проблемы с новой версией JTidy?

Ответы [ 2 ]

4 голосов
/ 06 декабря 2009

Была похожая проблема. Найден довольно глупый обходной путь (для повторного анализа вывода jtidy), который предлагает проблему с jTidy.

document = tidy.parseDOM(rstream, null); 

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());

Document doc = db.parse(is);

Это заняло у меня несколько часов; надеюсь, это поможет.

0 голосов
/ 07 октября 2009

Поскольку ошибка возникает в com.sun.org.apache, я не думаю, что это проблема JTidy.

Попробуйте сократить ваш пример, чтобы вы могли подать отчет об ошибке в проекте XalanJ .

...