[API] Expose InputLocation formatter in the XmlFactory (#1616)

This commit is contained in:
Guillaume Nodet 2024-08-11 00:21:50 +02:00 committed by GitHub
parent 473b5374f0
commit 4369fa8dff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 34 deletions

View File

@ -21,6 +21,7 @@ package org.apache.maven.api.services.xml;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.function.Function;
import org.apache.maven.api.annotations.Experimental; import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nonnull;
@ -47,6 +48,9 @@ public interface XmlWriterRequest<T> {
@Nonnull @Nonnull
T getContent(); T getContent();
@Nullable
Function<Object, String> getInputLocationFormatter();
static <T> XmlWriterRequestBuilder<T> builder() { static <T> XmlWriterRequestBuilder<T> builder() {
return new XmlWriterRequestBuilder<>(); return new XmlWriterRequestBuilder<>();
} }
@ -56,6 +60,7 @@ public interface XmlWriterRequest<T> {
OutputStream outputStream; OutputStream outputStream;
Writer writer; Writer writer;
T content; T content;
Function<Object, String> inputLocationFormatter;
public XmlWriterRequestBuilder<T> path(Path path) { public XmlWriterRequestBuilder<T> path(Path path) {
this.path = path; this.path = path;
@ -77,8 +82,13 @@ public interface XmlWriterRequest<T> {
return this; return this;
} }
public XmlWriterRequestBuilder<T> inputLocationFormatter(Function<Object, String> inputLocationFormatter) {
this.inputLocationFormatter = inputLocationFormatter;
return this;
}
public XmlWriterRequest<T> build() { public XmlWriterRequest<T> build() {
return new DefaultXmlWriterRequest<>(path, outputStream, writer, content); return new DefaultXmlWriterRequest<>(path, outputStream, writer, content, inputLocationFormatter);
} }
private static class DefaultXmlWriterRequest<T> implements XmlWriterRequest<T> { private static class DefaultXmlWriterRequest<T> implements XmlWriterRequest<T> {
@ -86,12 +96,19 @@ public interface XmlWriterRequest<T> {
final OutputStream outputStream; final OutputStream outputStream;
final Writer writer; final Writer writer;
final T content; final T content;
final Function<Object, String> inputLocationFormatter;
DefaultXmlWriterRequest(Path path, OutputStream outputStream, Writer writer, T content) { DefaultXmlWriterRequest(
Path path,
OutputStream outputStream,
Writer writer,
T content,
Function<Object, String> inputLocationFormatter) {
this.path = path; this.path = path;
this.outputStream = outputStream; this.outputStream = outputStream;
this.writer = writer; this.writer = writer;
this.content = content; this.content = content;
this.inputLocationFormatter = inputLocationFormatter;
} }
@Override @Override
@ -113,6 +130,11 @@ public interface XmlWriterRequest<T> {
public T getContent() { public T getContent() {
return content; return content;
} }
@Override
public Function<Object, String> getInputLocationFormatter() {
return inputLocationFormatter;
}
} }
} }
} }

View File

@ -152,21 +152,4 @@ public class InputLocation implements Serializable, InputLocationTracker {
return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations); return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations);
} // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection ) } // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection )
/**
* Class StringFormatter.
*
* @version $Revision$ $Date$
*/
public interface StringFormatter {
// -----------/
// - Methods -/
// -----------/
/**
* Method toString.
*/
String toString(InputLocation location);
}
} }

View File

@ -25,6 +25,7 @@ import java.io.Writer;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.function.Function;
import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named; import org.apache.maven.api.di.Named;
@ -89,17 +90,22 @@ public class DefaultModelXmlFactory implements ModelXmlFactory {
Path path = request.getPath(); Path path = request.getPath();
OutputStream outputStream = request.getOutputStream(); OutputStream outputStream = request.getOutputStream();
Writer writer = request.getWriter(); Writer writer = request.getWriter();
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
if (writer == null && outputStream == null && path == null) { if (writer == null && outputStream == null && path == null) {
throw new IllegalArgumentException("writer, outputStream or path must be non null"); throw new IllegalArgumentException("writer, outputStream or path must be non null");
} }
try { try {
MavenStaxWriter w = new MavenStaxWriter();
if (inputLocationFormatter != null) {
w.setStringFormatter((Function) inputLocationFormatter);
}
if (writer != null) { if (writer != null) {
new MavenStaxWriter().write(writer, content); w.write(writer, content);
} else if (outputStream != null) { } else if (outputStream != null) {
new MavenStaxWriter().write(outputStream, content); w.write(outputStream, content);
} else { } else {
try (OutputStream os = Files.newOutputStream(path)) { try (OutputStream os = Files.newOutputStream(path)) {
new MavenStaxWriter().write(outputStream, content); w.write(outputStream, content);
} }
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -67,14 +67,7 @@ public class MavenXpp3Writer {
*/ */
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) { public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
delegate.setStringFormatter( delegate.setStringFormatter(
stringFormatter != null stringFormatter != null ? location -> stringFormatter.toString(new InputLocation(location)) : null);
? new org.apache.maven.api.model.InputLocation.StringFormatter() {
@Override
public String toString(org.apache.maven.api.model.InputLocation location) {
return stringFormatter.toString(new InputLocation(location));
}
}
: null);
} // -- void setStringFormatter( InputLocation.StringFormatter ) } // -- void setStringFormatter( InputLocation.StringFormatter )
/** /**

View File

@ -57,6 +57,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter; import javax.xml.stream.XMLStreamWriter;
@ -114,7 +115,7 @@ public class ${className} {
/** /**
* Field stringFormatter. * Field stringFormatter.
*/ */
protected InputLocation.StringFormatter stringFormatter; protected Function<InputLocation, String> stringFormatter;
#end #end
//-----------/ //-----------/
@ -161,9 +162,9 @@ public class ${className} {
* *
* @param stringFormatter * @param stringFormatter
*/ */
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) { public void setStringFormatter(Function<InputLocation, String> stringFormatter) {
this.stringFormatter = stringFormatter; this.stringFormatter = stringFormatter;
} //-- void setStringFormatter(InputLocation.StringFormatter) } //-- void setStringFormatter(Function<InputLocation, String>)
#end #end
/** /**
@ -432,7 +433,7 @@ public class ${className} {
*/ */
protected String toString(InputLocation location) { protected String toString(InputLocation location) {
if (stringFormatter != null) { if (stringFormatter != null) {
return stringFormatter.toString(location); return stringFormatter.apply(location);
} }
if (location.getSource() != null) { if (location.getSource() != null) {
return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' '; return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';