mirror of https://github.com/apache/maven.git
[API] Expose InputLocation formatter in the XmlFactory (#1616)
This commit is contained in:
parent
473b5374f0
commit
4369fa8dff
|
@ -21,6 +21,7 @@ package org.apache.maven.api.services.xml;
|
|||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.maven.api.annotations.Experimental;
|
||||
import org.apache.maven.api.annotations.Nonnull;
|
||||
|
@ -47,6 +48,9 @@ public interface XmlWriterRequest<T> {
|
|||
@Nonnull
|
||||
T getContent();
|
||||
|
||||
@Nullable
|
||||
Function<Object, String> getInputLocationFormatter();
|
||||
|
||||
static <T> XmlWriterRequestBuilder<T> builder() {
|
||||
return new XmlWriterRequestBuilder<>();
|
||||
}
|
||||
|
@ -56,6 +60,7 @@ public interface XmlWriterRequest<T> {
|
|||
OutputStream outputStream;
|
||||
Writer writer;
|
||||
T content;
|
||||
Function<Object, String> inputLocationFormatter;
|
||||
|
||||
public XmlWriterRequestBuilder<T> path(Path path) {
|
||||
this.path = path;
|
||||
|
@ -77,8 +82,13 @@ public interface XmlWriterRequest<T> {
|
|||
return this;
|
||||
}
|
||||
|
||||
public XmlWriterRequestBuilder<T> inputLocationFormatter(Function<Object, String> inputLocationFormatter) {
|
||||
this.inputLocationFormatter = inputLocationFormatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
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> {
|
||||
|
@ -86,12 +96,19 @@ public interface XmlWriterRequest<T> {
|
|||
final OutputStream outputStream;
|
||||
final Writer writer;
|
||||
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.outputStream = outputStream;
|
||||
this.writer = writer;
|
||||
this.content = content;
|
||||
this.inputLocationFormatter = inputLocationFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -113,6 +130,11 @@ public interface XmlWriterRequest<T> {
|
|||
public T getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Object, String> getInputLocationFormatter() {
|
||||
return inputLocationFormatter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -152,21 +152,4 @@ public class InputLocation implements Serializable, InputLocationTracker {
|
|||
|
||||
return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations);
|
||||
} // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection )
|
||||
|
||||
/**
|
||||
* Class StringFormatter.
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface StringFormatter {
|
||||
|
||||
// -----------/
|
||||
// - Methods -/
|
||||
// -----------/
|
||||
|
||||
/**
|
||||
* Method toString.
|
||||
*/
|
||||
String toString(InputLocation location);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.io.Writer;
|
|||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.maven.api.annotations.Nonnull;
|
||||
import org.apache.maven.api.di.Named;
|
||||
|
@ -89,17 +90,22 @@ public class DefaultModelXmlFactory implements ModelXmlFactory {
|
|||
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);
|
||||
}
|
||||
if (writer != null) {
|
||||
new MavenStaxWriter().write(writer, content);
|
||||
w.write(writer, content);
|
||||
} else if (outputStream != null) {
|
||||
new MavenStaxWriter().write(outputStream, content);
|
||||
w.write(outputStream, content);
|
||||
} else {
|
||||
try (OutputStream os = Files.newOutputStream(path)) {
|
||||
new MavenStaxWriter().write(outputStream, content);
|
||||
w.write(outputStream, content);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -67,14 +67,7 @@ public class MavenXpp3Writer {
|
|||
*/
|
||||
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
|
||||
delegate.setStringFormatter(
|
||||
stringFormatter != 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);
|
||||
stringFormatter != null ? location -> stringFormatter.toString(new InputLocation(location)) : null);
|
||||
} // -- void setStringFormatter( InputLocation.StringFormatter )
|
||||
|
||||
/**
|
||||
|
|
|
@ -57,6 +57,7 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
@ -114,7 +115,7 @@ public class ${className} {
|
|||
/**
|
||||
* Field stringFormatter.
|
||||
*/
|
||||
protected InputLocation.StringFormatter stringFormatter;
|
||||
protected Function<InputLocation, String> stringFormatter;
|
||||
|
||||
#end
|
||||
//-----------/
|
||||
|
@ -161,9 +162,9 @@ public class ${className} {
|
|||
*
|
||||
* @param stringFormatter
|
||||
*/
|
||||
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
|
||||
public void setStringFormatter(Function<InputLocation, String> stringFormatter) {
|
||||
this.stringFormatter = stringFormatter;
|
||||
} //-- void setStringFormatter(InputLocation.StringFormatter)
|
||||
} //-- void setStringFormatter(Function<InputLocation, String>)
|
||||
|
||||
#end
|
||||
/**
|
||||
|
@ -432,7 +433,7 @@ public class ${className} {
|
|||
*/
|
||||
protected String toString(InputLocation location) {
|
||||
if (stringFormatter != null) {
|
||||
return stringFormatter.toString(location);
|
||||
return stringFormatter.apply(location);
|
||||
}
|
||||
if (location.getSource() != null) {
|
||||
return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
|
||||
|
|
Loading…
Reference in New Issue