Skip to content

Commit

Permalink
Document limitation with comment in DTD
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops committed Dec 17, 2018
1 parent 9730a31 commit 27461e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 us 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,44 @@ public void testDtd() throws Exception {
assertNoData(docType, Location.START, Location.END, Location.NAME, Location.VALUE);
}

/**
* Detailed in SONARXML-73
*/
@Test
public void testCommentInDoctypeProduceWrongLocations() throws Exception {
Document document = XmlFile.create(
"<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE menu [\n" +
"<!--\n" +
"Some comment\n" +
"-->\n" +
"<!ELEMENT menu (modulo)* >\n" +
"]>\n" +
"<menu value=\"foo\"></menu>").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, event are not at the correct place
assertRange(lastChild, Location.NODE, 7, 0, 7, 25);

document = XmlFile.create(
"<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE menu [\n" +
"<!--" + /* extra space before NewLine */ " " + "\n" +
"Some comment\n" +
"-->\n" +
"<!ELEMENT menu (modulo)* >\n" +
"]>\n" +
"<menu value=\"foo\"></menu>").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("<!-- comment --><tag/>").getDocument();
Expand Down

0 comments on commit 27461e6

Please sign in to comment.