Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.xml.ModelXmlFactory;
Expand Down Expand Up @@ -121,22 +122,29 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
Path path = request.getPath();
OutputStream outputStream = request.getOutputStream();
Writer writer = request.getWriter();
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();

if (writer == null && outputStream == null && path == null) {
throw new IllegalArgumentException("writer, outputStream or path must be non null");
}

try {
MavenStaxWriter w = new MavenStaxWriter();
if (inputLocationFormatter != null) {
w.setStringFormatter((Function) inputLocationFormatter);
MavenStaxWriter xmlWriter = new MavenStaxWriter();
xmlWriter.setAddLocationInformation(false);

Function<Object, String> formatter = request.getInputLocationFormatter();
if (formatter != null) {
xmlWriter.setAddLocationInformation(true);
Function<InputLocation, String> adapter = formatter::apply;
xmlWriter.setStringFormatter(adapter);
}

if (writer != null) {
w.write(writer, content);
xmlWriter.write(writer, content);
} else if (outputStream != null) {
w.write(outputStream, content);
xmlWriter.write(outputStream, content);
} else {
try (OutputStream os = Files.newOutputStream(path)) {
w.write(os, content);
xmlWriter.write(os, content);
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
package org.apache.maven.impl;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.function.Function;

import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.xml.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.services.xml.XmlWriterRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -122,4 +126,62 @@ void testMalformedModelVersion() throws Exception {
Model model = factory.read(request);
assertEquals("invalid.version", model.getModelVersion());
}

@Test
void testWriteWithoutFormatterDisablesLocationTracking() throws Exception {
// minimal valid model we can round-trip
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>g</groupId>
<artifactId>a</artifactId>
<version>1</version>
</project>""";

Model model = factory.read(XmlReaderRequest.builder()
.reader(new StringReader(xml))
.strict(true)
.build());

StringWriter out = new StringWriter();
factory.write(XmlWriterRequest.<Model>builder()
.writer(out)
.content(model)
// no formatter -> tracking should be OFF
.build());

String result = out.toString();
assertFalse(result.contains("LOC_MARK"), "Unexpected marker found in output");
}

@Test
void testWriteWithFormatterEnablesLocationTracking() throws Exception {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>g</groupId>
<artifactId>a</artifactId>
<version>1</version>
</project>""";

Model model = factory.read(XmlReaderRequest.builder()
.reader(new StringReader(xml))
.strict(true)
.build());

StringWriter out = new StringWriter();
Function<Object, String> formatter = o -> "LOC_MARK";

factory.write(XmlWriterRequest.<Model>builder()
.writer(out)
.content(model)
.inputLocationFormatter(formatter)
.build());

String result = out.toString();
// Presence of our formatter's output proves tracking was enabled and formatter applied
assertTrue(result.contains("LOC_MARK"), "Expected formatter marker in output when tracking is enabled");
}
}