Skip to content

Commit 28e2f84

Browse files
committed
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.
1 parent 667714d commit 28e2f84

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
@@ -30,6 +30,7 @@
3030
import org.apache.maven.api.annotations.Nonnull;
3131
import org.apache.maven.api.di.Named;
3232
import org.apache.maven.api.di.Singleton;
33+
import org.apache.maven.api.model.InputLocation;
3334
import org.apache.maven.api.model.InputSource;
3435
import org.apache.maven.api.model.Model;
3536
import org.apache.maven.api.services.xml.ModelXmlFactory;
@@ -121,22 +122,29 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
121122
Path path = request.getPath();
122123
OutputStream outputStream = request.getOutputStream();
123124
Writer writer = request.getWriter();
124-
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
125+
125126
if (writer == null && outputStream == null && path == null) {
126127
throw new IllegalArgumentException("writer, outputStream or path must be non null");
127128
}
129+
128130
try {
129-
MavenStaxWriter w = new MavenStaxWriter();
130-
if (inputLocationFormatter != null) {
131-
w.setStringFormatter((Function) inputLocationFormatter);
131+
final MavenStaxWriter xmlWriter = new MavenStaxWriter();
132+
xmlWriter.setAddLocationInformation(false);
133+
134+
final Function<Object, String> fmtformatter = request.getInputLocationFormatter();
135+
if (fmtformatter != null) {
136+
xmlWriter.setAddLocationInformation(true);
137+
final Function<InputLocation, String> adapter = fmtformatter::apply;
138+
xmlWriter.setStringFormatter(adapter);
132139
}
140+
133141
if (writer != null) {
134-
w.write(writer, content);
142+
xmlWriter.write(writer, content);
135143
} else if (outputStream != null) {
136-
w.write(outputStream, content);
144+
xmlWriter.write(outputStream, content);
137145
} else {
138146
try (OutputStream os = Files.newOutputStream(path)) {
139-
w.write(os, content);
147+
xmlWriter.write(os, content);
140148
}
141149
}
142150
} 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)