From 80ef751c615a298e90929865bf09f90e61c068b7 Mon Sep 17 00:00:00 2001 From: Michael Gumowski Date: Mon, 17 Dec 2018 18:19:16 +0100 Subject: [PATCH] Document limitation with comment in DTD --- .../analyzer/commons/xml/XmlParser.java | 6 ++- .../analyzer/commons/xml/XmlParserTest.java | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/xml-parsing/src/main/java/org/sonarsource/analyzer/commons/xml/XmlParser.java b/xml-parsing/src/main/java/org/sonarsource/analyzer/commons/xml/XmlParser.java index 0d506a81..82034292 100644 --- a/xml-parsing/src/main/java/org/sonarsource/analyzer/commons/xml/XmlParser.java +++ b/xml-parsing/src/main/java/org/sonarsource/analyzer/commons/xml/XmlParser.java @@ -192,9 +192,13 @@ private void finalizePreviousNode(XmlFilePosition endLocation) { private XMLStreamReader getXmlStreamReader() throws XMLStreamException { Reader reader = new StringReader(content); - XMLInputFactory factory = XMLInputFactory.newInstance(); + // forcing the XMLInputFactory implementation class, in order to be sure that we are going to use the adequate + // stream reader while retrieving locations + XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory(); + factory.setProperty(XMLInputFactory.SUPPORT_DTD, false); factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); + factory.setProperty(XMLInputFactory.IS_VALIDATING, false); return factory.createXMLStreamReader(reader); } diff --git a/xml-parsing/src/test/java/org/sonarsource/analyzer/commons/xml/XmlParserTest.java b/xml-parsing/src/test/java/org/sonarsource/analyzer/commons/xml/XmlParserTest.java index 57d2666a..85149622 100644 --- a/xml-parsing/src/test/java/org/sonarsource/analyzer/commons/xml/XmlParserTest.java +++ b/xml-parsing/src/test/java/org/sonarsource/analyzer/commons/xml/XmlParserTest.java @@ -261,6 +261,45 @@ public void testDtd() throws Exception { assertNoData(docType, Location.START, Location.END, Location.NAME, Location.VALUE); } + /** + * Detailed in SONARXML-73, should be fixed with https://github.com/FasterXML/woodstox/issues/67 + */ + @Test + public void testCommentInDoctypeProduceWrongLocations() throws Exception { + Document document = XmlFile.create( + "\n" + + "\n" + + "\n" + + "]>\n" + + "").getDocument(); + assertThat(document.getChildNodes().getLength()).isEqualTo(2); + DocumentType documentType = (DocumentType) document.getFirstChild(); + assertRange(documentType, Location.NODE, 2, 0, 7, 2); + Node lastChild = document.getLastChild(); + // location of the node is wrong, should be line 8, events are not at the correct place + // See https://github.com/FasterXML/woodstox/issues/67 + assertRange(lastChild, Location.NODE, 7, 0, 7, 25); + + document = XmlFile.create( + "\n" + + "\n" + + "\n" + + "]>\n" + + "").getDocument(); + assertThat(document.getChildNodes().getLength()).isEqualTo(2); + documentType = (DocumentType) document.getFirstChild(); + assertRange(documentType, Location.NODE, 2, 0, 7, 2); + lastChild = document.getLastChild(); + // location is correct + assertRange(lastChild, Location.NODE, 8, 0, 8, 25); + } + @Test public void testComment() throws Exception { Document document = XmlFile.create("").getDocument();