|
25 | 25 | import java.net.URL; |
26 | 26 | import java.nio.file.Files; |
27 | 27 | import java.nio.file.Path; |
| 28 | +import java.util.Objects; |
28 | 29 | import java.util.function.Function; |
29 | 30 |
|
30 | 31 | import org.apache.maven.api.annotations.Nonnull; |
31 | 32 | import org.apache.maven.api.di.Named; |
32 | 33 | import org.apache.maven.api.di.Singleton; |
| 34 | +import org.apache.maven.api.model.InputLocation; |
33 | 35 | import org.apache.maven.api.model.InputSource; |
34 | 36 | import org.apache.maven.api.model.Model; |
35 | 37 | import org.apache.maven.api.services.xml.ModelXmlFactory; |
@@ -115,28 +117,38 @@ private Model doRead(XmlReaderRequest request) throws XmlReaderException { |
115 | 117 | } |
116 | 118 |
|
117 | 119 | @Override |
118 | | - public void write(XmlWriterRequest<Model> request) throws XmlWriterException { |
119 | | - requireNonNull(request, "request"); |
120 | | - Model content = requireNonNull(request.getContent(), "content"); |
121 | | - Path path = request.getPath(); |
122 | | - OutputStream outputStream = request.getOutputStream(); |
123 | | - Writer writer = request.getWriter(); |
124 | | - Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter(); |
| 120 | + public void write(final XmlWriterRequest<Model> request) throws XmlWriterException { |
| 121 | + Objects.requireNonNull(request, "request can not be null"); |
| 122 | + final Model content = Objects.requireNonNull(request.getContent(), "content can not be null"); |
| 123 | + |
| 124 | + final Path path = request.getPath(); |
| 125 | + final OutputStream outputStream = request.getOutputStream(); |
| 126 | + final Writer writer = request.getWriter(); |
| 127 | + |
125 | 128 | if (writer == null && outputStream == null && path == null) { |
126 | 129 | throw new IllegalArgumentException("writer, outputStream or path must be non null"); |
127 | 130 | } |
| 131 | + |
128 | 132 | try { |
129 | | - MavenStaxWriter w = new MavenStaxWriter(); |
130 | | - if (inputLocationFormatter != null) { |
131 | | - w.setStringFormatter((Function) inputLocationFormatter); |
| 133 | + // OFF by default |
| 134 | + final MavenStaxWriter xmlWriter = new MavenStaxWriter(); |
| 135 | + xmlWriter.setAddLocationInformation(false); |
| 136 | + |
| 137 | + // Opt-in when a formatter is provided; adapt to the required type |
| 138 | + final Function<Object, String> fmt = request.getInputLocationFormatter(); |
| 139 | + if (fmt != null) { |
| 140 | + xmlWriter.setAddLocationInformation(true); |
| 141 | + final Function<InputLocation, String> adapter = fmt::apply; |
| 142 | + xmlWriter.setStringFormatter(adapter); |
132 | 143 | } |
| 144 | + |
133 | 145 | if (writer != null) { |
134 | | - w.write(writer, content); |
| 146 | + xmlWriter.write(writer, content); |
135 | 147 | } else if (outputStream != null) { |
136 | | - w.write(outputStream, content); |
| 148 | + xmlWriter.write(outputStream, content); |
137 | 149 | } else { |
138 | 150 | try (OutputStream os = Files.newOutputStream(path)) { |
139 | | - w.write(os, content); |
| 151 | + xmlWriter.write(os, content); |
140 | 152 | } |
141 | 153 | } |
142 | 154 | } catch (Exception e) { |
|
0 commit comments