Skip to content

Commit f49b95e

Browse files
Fix #10939: DefaultModelXmlFactory: make location tracking opt-in—disabled by def… (#11092) (#11198)
DefaultModelXmlFactory: make location tracking opt-in—disabled by default, enabled when an InputLocationFormatter is provided. Adapt formatter to Function<InputLocation,String> and write to the actually opened stream for path output. Fixes #10939. (cherry picked from commit 3338476) Co-authored-by: Arturo Bernal <[email protected]>
1 parent 60fdc47 commit f49b95e

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.maven.api.annotations.Nonnull;
4040
import org.apache.maven.api.di.Named;
4141
import org.apache.maven.api.di.Singleton;
42+
import org.apache.maven.api.model.InputLocation;
4243
import org.apache.maven.api.model.InputSource;
4344
import org.apache.maven.api.model.Model;
4445
import org.apache.maven.api.services.xml.ModelXmlFactory;
@@ -156,22 +157,29 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
156157
Path path = request.getPath();
157158
OutputStream outputStream = request.getOutputStream();
158159
Writer writer = request.getWriter();
159-
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
160+
160161
if (writer == null && outputStream == null && path == null) {
161162
throw new IllegalArgumentException("writer, outputStream or path must be non null");
162163
}
164+
163165
try {
164-
MavenStaxWriter w = new MavenStaxWriter();
165-
if (inputLocationFormatter != null) {
166-
w.setStringFormatter((Function) inputLocationFormatter);
166+
MavenStaxWriter xmlWriter = new MavenStaxWriter();
167+
xmlWriter.setAddLocationInformation(false);
168+
169+
Function<Object, String> formatter = request.getInputLocationFormatter();
170+
if (formatter != null) {
171+
xmlWriter.setAddLocationInformation(true);
172+
Function<InputLocation, String> adapter = formatter::apply;
173+
xmlWriter.setStringFormatter(adapter);
167174
}
175+
168176
if (writer != null) {
169-
w.write(writer, content);
177+
xmlWriter.write(writer, content);
170178
} else if (outputStream != null) {
171-
w.write(outputStream, content);
179+
xmlWriter.write(outputStream, content);
172180
} else {
173181
try (OutputStream os = Files.newOutputStream(path)) {
174-
w.write(os, content);
182+
xmlWriter.write(os, content);
175183
}
176184
}
177185
} catch (Exception e) {

impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultModelXmlFactoryTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
package org.apache.maven.impl;
2020

2121
import java.io.StringReader;
22+
import java.io.StringWriter;
23+
import java.util.function.Function;
2224

2325
import org.apache.maven.api.model.Model;
2426
import org.apache.maven.api.services.xml.XmlReaderException;
2527
import org.apache.maven.api.services.xml.XmlReaderRequest;
28+
import org.apache.maven.api.services.xml.XmlWriterRequest;
2629
import org.junit.jupiter.api.BeforeEach;
2730
import org.junit.jupiter.api.Test;
2831

2932
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertFalse;
3034
import static org.junit.jupiter.api.Assertions.assertThrows;
3135
import static org.junit.jupiter.api.Assertions.assertTrue;
3236

@@ -122,4 +126,62 @@ void testMalformedModelVersion() throws Exception {
122126
Model model = factory.read(request);
123127
assertEquals("invalid.version", model.getModelVersion());
124128
}
129+
130+
@Test
131+
void testWriteWithoutFormatterDisablesLocationTracking() throws Exception {
132+
// minimal valid model we can round-trip
133+
String xml =
134+
"""
135+
<project xmlns="http://maven.apache.org/POM/4.0.0">
136+
<modelVersion>4.0.0</modelVersion>
137+
<groupId>g</groupId>
138+
<artifactId>a</artifactId>
139+
<version>1</version>
140+
</project>""";
141+
142+
Model model = factory.read(XmlReaderRequest.builder()
143+
.reader(new StringReader(xml))
144+
.strict(true)
145+
.build());
146+
147+
StringWriter out = new StringWriter();
148+
factory.write(XmlWriterRequest.<Model>builder()
149+
.writer(out)
150+
.content(model)
151+
// no formatter -> tracking should be OFF
152+
.build());
153+
154+
String result = out.toString();
155+
assertFalse(result.contains("LOC_MARK"), "Unexpected marker found in output");
156+
}
157+
158+
@Test
159+
void testWriteWithFormatterEnablesLocationTracking() throws Exception {
160+
String xml =
161+
"""
162+
<project xmlns="http://maven.apache.org/POM/4.0.0">
163+
<modelVersion>4.0.0</modelVersion>
164+
<groupId>g</groupId>
165+
<artifactId>a</artifactId>
166+
<version>1</version>
167+
</project>""";
168+
169+
Model model = factory.read(XmlReaderRequest.builder()
170+
.reader(new StringReader(xml))
171+
.strict(true)
172+
.build());
173+
174+
StringWriter out = new StringWriter();
175+
Function<Object, String> formatter = o -> "LOC_MARK";
176+
177+
factory.write(XmlWriterRequest.<Model>builder()
178+
.writer(out)
179+
.content(model)
180+
.inputLocationFormatter(formatter)
181+
.build());
182+
183+
String result = out.toString();
184+
// Presence of our formatter's output proves tracking was enabled and formatter applied
185+
assertTrue(result.contains("LOC_MARK"), "Expected formatter marker in output when tracking is enabled");
186+
}
125187
}

0 commit comments

Comments
 (0)