Skip to content

Commit 6c76a1a

Browse files
committed
Forced use of Woodstox parser for FormatCommand and fixed bug
1 parent 4eecd14 commit 6c76a1a

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ picocli = "4.7.0"
1515
slf4j = "2.0.6"
1616
guice = "5.1.0"
1717
dom4j = "2.1.4"
18+
woodstox = "7.1.0"
1819

1920
[libraries]
2021
autovalue-annotations = { module = "com.google.auto.value:auto-value-annotations", version.ref = "auto-value" }
@@ -27,6 +28,7 @@ contrast-sarif = "com.contrastsecurity:java-sarif:2.0"
2728
gson = "com.google.code.gson:gson:2.9.0"
2829
guice = { module = "com.google.inject:guice", version.ref = "guice" }
2930
immutables = "org.immutables:value:2.9.0"
31+
woodstox = { module = "com.fasterxml.woodstox:woodstox-core", version.ref = "woodstox" }
3032
jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" }
3133
jackson-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }
3234
javadiff = "io.github.java-diff-utils:java-diff-utils:4.12"

plugins/codemodder-plugin-maven/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ dependencies {
3131
implementation(libs.diff.match.patch)
3232
implementation(libs.slf4j.simple)
3333
implementation(libs.slf4j.api)
34+
implementation(libs.woodstox)
3435
}

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/MavenProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* <p>a. We skip parent finding if there's not a relativePath declaration (this is by design), so
2020
* sometimes pom finding will fail on purpose b. there are several flags on ProjectModelFactory
21-
* which aren't applied. They relate to verisons, upgrading and particularly: Actives Profiles c. If
21+
* which aren't applied. They relate to versions, upgrading and particularly: Actives Profiles c. If
2222
* you need anything declared in a ~/.m2/settings.xml, we don't support that (e.g., passwords or
2323
* proxies) d. Haven't tested, but I'm almost sure that it wouldn't work on any repo other than
2424
* central e. We allow on this module to do online resolution. HOWEVER by default its offline f. You

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/FormatCommand.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static io.github.pixee.security.XMLInputFactorySecurity.hardenFactory;
44

5+
import com.ctc.wstx.evt.CompactStartElement;
6+
import com.ctc.wstx.stax.WstxInputFactory;
57
import java.io.ByteArrayInputStream;
68
import java.io.IOException;
79
import java.io.InputStream;
@@ -38,7 +40,7 @@ class FormatCommand extends AbstractCommand {
3840
private static final Logger LOGGER = LoggerFactory.getLogger(FormatCommand.class);
3941

4042
/** StAX InputFactory */
41-
private XMLInputFactory inputFactory = hardenFactory(XMLInputFactory.newInstance());
43+
private XMLInputFactory inputFactory = WstxInputFactory.newInstance();
4244

4345
/** StAX OutputFactory */
4446
private XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
@@ -270,6 +272,10 @@ private void parseXmlAndCharset(POMDocument pomFile) throws XMLStreamException,
270272
int elementStart = 0;
271273
List<XMLEvent> prevEvents = new ArrayList<>();
272274

275+
System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
276+
System.out.println(inputFactory.getClass());
277+
System.out.println(eventReader.getClass());
278+
System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
273279
while (eventReader.hasNext()) {
274280
XMLEvent event = eventReader.nextEvent();
275281

@@ -324,8 +330,15 @@ private void parseXmlAndCharset(POMDocument pomFile) throws XMLStreamException,
324330
String originalPomCharsetString =
325331
new String(pomFile.getOriginalPom(), pomFile.getCharset());
326332

327-
String untrimmedOriginalContent =
328-
originalPomCharsetString.substring(elementStart, offset);
333+
String untrimmedOriginalContent = "";
334+
// is self closing element, tag is contained within the offset of the next element
335+
if (prevEvents.get(prevEvents.size() - 1) instanceof CompactStartElement) {
336+
untrimmedOriginalContent =
337+
originalPomCharsetString.substring(
338+
offset, eventReader.peek().getLocation().getCharacterOffset());
339+
} else {
340+
untrimmedOriginalContent = originalPomCharsetString.substring(elementStart, offset);
341+
}
329342

330343
String trimmedOriginalContent = untrimmedOriginalContent.trim();
331344

0 commit comments

Comments
 (0)