Skip to content

Commit

Permalink
Fixed whitespace on entities and char refs in XmlParser. (#3781)
Browse files Browse the repository at this point in the history
* Fixed whitespace on entities and char refs in XmlParser.

* Polish. Updated MavenParserTest#malformedPom.
  • Loading branch information
traceyyoshima authored Dec 7, 2023
1 parent a928dc2 commit 726abc6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
Expand All @@ -28,6 +29,7 @@
import org.openrewrite.maven.tree.*;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.tree.ParseError;
import org.openrewrite.xml.tree.Xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -2240,6 +2242,7 @@ void exclusions() {

@Test
void malformedPom() {
@Language("xml")
String malformedPomXml = """
<project>
<groupId>com.mycompany.app</groupId>
Expand All @@ -2250,9 +2253,8 @@ void malformedPom() {
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>&gt;
<version>4.11</version>
</dependency>
</dependencies>
</project>
""";
assertThat(MavenParser.builder().build().parse(malformedPomXml))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ public Xml visitContent(XMLParser.ContentContext ctx) {
return charData;
} else if (ctx.reference() != null) {
if (ctx.reference().EntityRef() != null) {
cursor += ctx.reference().EntityRef().getSymbol().getStopIndex() + 1;
String prefix = prefix(ctx);
cursor = ctx.reference().EntityRef().getSymbol().getStopIndex() + 1;
return new Xml.CharData(randomId(),
"",
prefix,
Markers.EMPTY,
false,
ctx.reference().EntityRef().getText(),
"");
} else if (ctx.reference().CharRef() != null) {
cursor += ctx.reference().CharRef().getSymbol().getStopIndex() + 1;
String prefix = prefix(ctx);
cursor = ctx.reference().CharRef().getSymbol().getStopIndex() + 1;
return new Xml.CharData(randomId(),
"",
prefix,
Markers.EMPTY,
false,
ctx.reference().CharRef().getText(),
Expand Down
30 changes: 27 additions & 3 deletions rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ void utf8BOM() {
xml(
"""
%s<?xml version="1.0" encoding="UTF-8"?><test></test>
""".formatted("\uFEFF"))
""".formatted("\uFEFF")
)
);
}

Expand All @@ -308,7 +309,8 @@ void preserveSpaceBeforeAttributeAssignment() {
xml(
"""
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?><blah></blah>
""")
"""
)
);
}

Expand All @@ -321,7 +323,29 @@ void linkWithQuestionMark() {
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="/name/other?link"?>
<blah></blah>
""")
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/3442")
@Test
void preserveWhitespaceOnEntities() {
rewriteRun(
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<message><text>&lt;?xml version='1.0' encoding='UTF-8'?&gt;&#13;
&lt;note&gt;&#13;
&lt;to&gt;Tove&lt;/to&gt;&#13;
&lt;from&gt;Jani&lt;/from&gt;&#13;
&lt;heading&gt;Reminder&lt;/heading&gt;&#13;
&lt;body&gt;Don't forget me this weekend!&lt;/body&gt;&#13;
&lt;/note&gt;&#13;
&#13;
</text></message>
"""
)
);
}
}

0 comments on commit 726abc6

Please sign in to comment.