1
2
3
4
5 package org.apache.maven.settings.v4;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.io.Reader;
11 import java.io.Writer;
12 import java.text.DateFormat;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.Properties;
20 import java.util.Set;
21 import java.util.function.Function;
22 import javax.xml.stream.XMLOutputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import org.apache.maven.api.annotations.Generated;
26 import org.apache.maven.api.settings.InputLocation;
27 import org.apache.maven.api.settings.InputLocationTracker;
28 import org.apache.maven.api.xml.XmlNode;
29 import org.apache.maven.internal.xml.XmlNodeBuilder;
30 import org.apache.maven.api.settings.TrackableBase;
31 import org.apache.maven.api.settings.IdentifiableBase;
32 import org.apache.maven.api.settings.Settings;
33 import org.apache.maven.api.settings.Proxy;
34 import org.apache.maven.api.settings.Server;
35 import org.apache.maven.api.settings.Mirror;
36 import org.apache.maven.api.settings.Profile;
37 import org.apache.maven.api.settings.Activation;
38 import org.apache.maven.api.settings.RepositoryBase;
39 import org.apache.maven.api.settings.Repository;
40 import org.apache.maven.api.settings.RepositoryPolicy;
41 import org.apache.maven.api.settings.ActivationProperty;
42 import org.apache.maven.api.settings.ActivationOS;
43 import org.apache.maven.api.settings.ActivationFile;
44 import org.apache.maven.api.settings.InputSource;
45 import org.codehaus.stax2.util.StreamWriterDelegate;
46
47 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI;
48
49 @Generated
50 public class SettingsStaxWriter {
51
52
53
54
55
56
57
58
59 private static final String NAMESPACE = "http://maven.apache.org/SETTINGS/1.2.0";
60
61
62
63
64 private static final String SCHEMA_LOCATION = "http://maven.apache.org/xsd/settings-1.2.0.xsd";
65
66
67
68
69 private String namespace = NAMESPACE;
70
71
72
73
74 private String schemaLocation = SCHEMA_LOCATION;
75
76
77
78
79 private String fileComment = null;
80
81 private boolean addLocationInformation = true;
82
83
84
85
86 protected Function<InputLocation, String> stringFormatter;
87
88
89
90
91
92
93
94
95
96
97 public void setNamespace(String namespace) {
98 this.namespace = Objects.requireNonNull(namespace);
99 }
100
101
102
103
104
105
106 public void setSchemaLocation(String schemaLocation) {
107 this.schemaLocation = Objects.requireNonNull(schemaLocation);
108 }
109
110
111
112
113
114
115 public void setFileComment(String fileComment) {
116 this.fileComment = fileComment;
117 }
118
119
120
121
122 public void setAddLocationInformation(boolean addLocationInformation) {
123 this.addLocationInformation = addLocationInformation;
124 }
125
126
127
128
129
130
131 public void setStringFormatter(Function<InputLocation, String> stringFormatter) {
132 this.stringFormatter = stringFormatter;
133 }
134
135
136
137
138
139
140
141
142 public void write(Writer writer, Settings settings) throws IOException, XMLStreamException {
143 XMLOutputFactory factory = new com.ctc.wstx.stax.WstxOutputFactory();
144 factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
145 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
146 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM, true);
147 XMLStreamWriter serializer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(writer));
148 serializer.writeStartDocument(settings.getModelEncoding(), null);
149 writeSettings("settings", settings, serializer);
150 serializer.writeEndDocument();
151 }
152
153
154
155
156
157
158
159
160 public void write(OutputStream stream, Settings settings) throws IOException, XMLStreamException {
161 XMLOutputFactory factory = new com.ctc.wstx.stax.WstxOutputFactory();
162 factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
163 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
164 factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM, true);
165 XMLStreamWriter serializer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(stream, settings.getModelEncoding()));
166 serializer.writeStartDocument(settings.getModelEncoding(), null);
167 writeSettings("settings", settings, serializer);
168 serializer.writeEndDocument();
169 }
170
171 private void writeTrackableBase(String tagName, TrackableBase trackableBase, XMLStreamWriter serializer)
172 throws IOException, XMLStreamException {
173 if (trackableBase != null) {
174 serializer.writeStartElement(namespace, tagName);
175 serializer.writeEndElement();
176 }
177 }
178
179 private void writeIdentifiableBase(String tagName, IdentifiableBase identifiableBase, XMLStreamWriter serializer)
180 throws IOException, XMLStreamException {
181 if (identifiableBase != null) {
182 serializer.writeStartElement(namespace, tagName);
183 writeTag("id", "default", identifiableBase.getId(), serializer, identifiableBase);
184 serializer.writeEndElement();
185 }
186 }
187
188 private void writeSettings(String tagName, Settings settings, XMLStreamWriter serializer)
189 throws IOException, XMLStreamException {
190 if (settings != null) {
191 if (this.fileComment != null) {
192 serializer.writeCharacters("\n");
193 serializer.writeComment(this.fileComment);
194 serializer.writeCharacters("\n");
195 }
196 serializer.writeStartElement("", tagName, namespace);
197 serializer.writeNamespace("", namespace);
198 serializer.writeNamespace("xsi", W3C_XML_SCHEMA_INSTANCE_NS_URI);
199 serializer.writeAttribute(W3C_XML_SCHEMA_INSTANCE_NS_URI, "schemaLocation", namespace + " " + schemaLocation);
200 writeTag("localRepository", null, settings.getLocalRepository(), serializer, settings);
201 writeTag("interactiveMode", "true", settings.isInteractiveMode() ? null : "false", serializer, settings);
202 writeTag("usePluginRegistry", "false", settings.isUsePluginRegistry() ? "true" : null, serializer, settings);
203 writeTag("offline", "false", settings.isOffline() ? "true" : null, serializer, settings);
204 writeList("proxies", false, settings.getProxies(), serializer, settings,
205 t -> writeProxy("proxy", t, serializer));
206 writeList("servers", false, settings.getServers(), serializer, settings,
207 t -> writeServer("server", t, serializer));
208 writeList("mirrors", false, settings.getMirrors(), serializer, settings,
209 t -> writeMirror("mirror", t, serializer));
210 writeList("repositories", false, settings.getRepositories(), serializer, settings,
211 t -> writeRepository("repository", t, serializer));
212 writeList("pluginRepositories", false, settings.getPluginRepositories(), serializer, settings,
213 t -> writeRepository("pluginRepository", t, serializer));
214 writeList("profiles", false, settings.getProfiles(), serializer, settings,
215 t -> writeProfile("profile", t, serializer));
216 writeList("activeProfiles", settings.getActiveProfiles(), serializer, settings,
217 t -> writeTag("activeProfile", null, t, serializer, null));
218 writeList("pluginGroups", settings.getPluginGroups(), serializer, settings,
219 t -> writeTag("pluginGroup", null, t, serializer, null));
220 serializer.writeEndElement();
221 }
222 }
223
224 private void writeProxy(String tagName, Proxy proxy, XMLStreamWriter serializer)
225 throws IOException, XMLStreamException {
226 if (proxy != null) {
227 serializer.writeStartElement(namespace, tagName);
228 writeTag("active", "true", proxy.getActiveString(), serializer, proxy);
229 writeTag("protocol", "http", proxy.getProtocol(), serializer, proxy);
230 writeTag("username", null, proxy.getUsername(), serializer, proxy);
231 writeTag("password", null, proxy.getPassword(), serializer, proxy);
232 writeTag("port", "8080", proxy.getPortString(), serializer, proxy);
233 writeTag("host", null, proxy.getHost(), serializer, proxy);
234 writeTag("nonProxyHosts", null, proxy.getNonProxyHosts(), serializer, proxy);
235 writeTag("id", "default", proxy.getId(), serializer, proxy);
236 serializer.writeEndElement();
237 }
238 }
239
240 private void writeServer(String tagName, Server server, XMLStreamWriter serializer)
241 throws IOException, XMLStreamException {
242 if (server != null) {
243 serializer.writeStartElement(namespace, tagName);
244 writeTag("username", null, server.getUsername(), serializer, server);
245 writeTag("password", null, server.getPassword(), serializer, server);
246 writeTag("privateKey", null, server.getPrivateKey(), serializer, server);
247 writeTag("passphrase", null, server.getPassphrase(), serializer, server);
248 writeTag("filePermissions", null, server.getFilePermissions(), serializer, server);
249 writeTag("directoryPermissions", null, server.getDirectoryPermissions(), serializer, server);
250 writeDom(server.getConfiguration(), serializer);
251 writeTag("id", "default", server.getId(), serializer, server);
252 serializer.writeEndElement();
253 }
254 }
255
256 private void writeMirror(String tagName, Mirror mirror, XMLStreamWriter serializer)
257 throws IOException, XMLStreamException {
258 if (mirror != null) {
259 serializer.writeStartElement(namespace, tagName);
260 writeTag("mirrorOf", null, mirror.getMirrorOf(), serializer, mirror);
261 writeTag("name", null, mirror.getName(), serializer, mirror);
262 writeTag("url", null, mirror.getUrl(), serializer, mirror);
263 writeTag("layout", "default", mirror.getLayout(), serializer, mirror);
264 writeTag("mirrorOfLayouts", "default,legacy", mirror.getMirrorOfLayouts(), serializer, mirror);
265 writeTag("blocked", "false", mirror.isBlocked() ? "true" : null, serializer, mirror);
266 writeTag("id", "default", mirror.getId(), serializer, mirror);
267 serializer.writeEndElement();
268 }
269 }
270
271 private void writeProfile(String tagName, Profile profile, XMLStreamWriter serializer)
272 throws IOException, XMLStreamException {
273 if (profile != null) {
274 serializer.writeStartElement(namespace, tagName);
275 writeActivation("activation", profile.getActivation(), serializer);
276 writeProperties("properties", profile.getProperties(), serializer, profile);
277 writeList("repositories", false, profile.getRepositories(), serializer, profile,
278 t -> writeRepository("repository", t, serializer));
279 writeList("pluginRepositories", false, profile.getPluginRepositories(), serializer, profile,
280 t -> writeRepository("pluginRepository", t, serializer));
281 writeTag("id", "default", profile.getId(), serializer, profile);
282 serializer.writeEndElement();
283 }
284 }
285
286 private void writeActivation(String tagName, Activation activation, XMLStreamWriter serializer)
287 throws IOException, XMLStreamException {
288 if (activation != null) {
289 serializer.writeStartElement(namespace, tagName);
290 writeTag("activeByDefault", "false", activation.isActiveByDefault() ? "true" : null, serializer, activation);
291 writeTag("jdk", null, activation.getJdk(), serializer, activation);
292 writeActivationOS("os", activation.getOs(), serializer);
293 writeActivationProperty("property", activation.getProperty(), serializer);
294 writeActivationFile("file", activation.getFile(), serializer);
295 writeTag("packaging", null, activation.getPackaging(), serializer, activation);
296 serializer.writeEndElement();
297 }
298 }
299
300 private void writeRepositoryBase(String tagName, RepositoryBase repositoryBase, XMLStreamWriter serializer)
301 throws IOException, XMLStreamException {
302 if (repositoryBase != null) {
303 serializer.writeStartElement(namespace, tagName);
304 writeTag("name", null, repositoryBase.getName(), serializer, repositoryBase);
305 writeTag("url", null, repositoryBase.getUrl(), serializer, repositoryBase);
306 writeTag("layout", "default", repositoryBase.getLayout(), serializer, repositoryBase);
307 writeTag("id", "default", repositoryBase.getId(), serializer, repositoryBase);
308 serializer.writeEndElement();
309 }
310 }
311
312 private void writeRepository(String tagName, Repository repository, XMLStreamWriter serializer)
313 throws IOException, XMLStreamException {
314 if (repository != null) {
315 serializer.writeStartElement(namespace, tagName);
316 writeRepositoryPolicy("releases", repository.getReleases(), serializer);
317 writeRepositoryPolicy("snapshots", repository.getSnapshots(), serializer);
318 writeTag("name", null, repository.getName(), serializer, repository);
319 writeTag("url", null, repository.getUrl(), serializer, repository);
320 writeTag("layout", "default", repository.getLayout(), serializer, repository);
321 writeTag("id", "default", repository.getId(), serializer, repository);
322 serializer.writeEndElement();
323 }
324 }
325
326 private void writeRepositoryPolicy(String tagName, RepositoryPolicy repositoryPolicy, XMLStreamWriter serializer)
327 throws IOException, XMLStreamException {
328 if (repositoryPolicy != null) {
329 serializer.writeStartElement(namespace, tagName);
330 writeTag("enabled", "true", repositoryPolicy.isEnabled() ? null : "false", serializer, repositoryPolicy);
331 writeTag("updatePolicy", null, repositoryPolicy.getUpdatePolicy(), serializer, repositoryPolicy);
332 writeTag("checksumPolicy", null, repositoryPolicy.getChecksumPolicy(), serializer, repositoryPolicy);
333 serializer.writeEndElement();
334 }
335 }
336
337 private void writeActivationProperty(String tagName, ActivationProperty activationProperty, XMLStreamWriter serializer)
338 throws IOException, XMLStreamException {
339 if (activationProperty != null) {
340 serializer.writeStartElement(namespace, tagName);
341 writeTag("name", null, activationProperty.getName(), serializer, activationProperty);
342 writeTag("value", null, activationProperty.getValue(), serializer, activationProperty);
343 serializer.writeEndElement();
344 }
345 }
346
347 private void writeActivationOS(String tagName, ActivationOS activationOS, XMLStreamWriter serializer)
348 throws IOException, XMLStreamException {
349 if (activationOS != null) {
350 serializer.writeStartElement(namespace, tagName);
351 writeTag("name", null, activationOS.getName(), serializer, activationOS);
352 writeTag("family", null, activationOS.getFamily(), serializer, activationOS);
353 writeTag("arch", null, activationOS.getArch(), serializer, activationOS);
354 writeTag("version", null, activationOS.getVersion(), serializer, activationOS);
355 serializer.writeEndElement();
356 }
357 }
358
359 private void writeActivationFile(String tagName, ActivationFile activationFile, XMLStreamWriter serializer)
360 throws IOException, XMLStreamException {
361 if (activationFile != null) {
362 serializer.writeStartElement(namespace, tagName);
363 writeTag("missing", null, activationFile.getMissing(), serializer, activationFile);
364 writeTag("exists", null, activationFile.getExists(), serializer, activationFile);
365 serializer.writeEndElement();
366 }
367 }
368
369 @FunctionalInterface
370 private interface ElementWriter<T> {
371 public void write(T t) throws IOException, XMLStreamException;
372 }
373
374 private <T> void writeList(String tagName, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException {
375 writeList(tagName, false, list, serializer, locationTracker, writer);
376 }
377
378 private <T> void writeList(String tagName, boolean flat, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException {
379 if (list != null && !list.isEmpty()) {
380 if (!flat) {
381 serializer.writeStartElement(namespace, tagName);
382 }
383 int index = 0;
384 InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
385 for (T t : list) {
386 writer.write(t);
387 writeLocationTracking(location, Integer.valueOf(index++), serializer);
388 }
389 if (!flat) {
390 serializer.writeEndElement();
391 }
392 }
393 }
394
395 private <T> void writeProperties(String tagName, Map<String, String> props, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException {
396 if (props != null && !props.isEmpty()) {
397 serializer.writeStartElement(namespace, tagName);
398 InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
399 for (Map.Entry<String, String> entry : props.entrySet()) {
400 String key = entry.getKey();
401 writeTag(key, null, entry.getValue(), serializer, null);
402 writeLocationTracking(location, key, serializer);
403 }
404 serializer.writeEndElement();
405 }
406 }
407
408 private void writeDom(XmlNode dom, XMLStreamWriter serializer) throws IOException, XMLStreamException {
409 if (dom != null) {
410 serializer.writeStartElement(namespace, dom.getName());
411 for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
412 if (attr.getKey().startsWith("xml:")) {
413 serializer.writeAttribute("http://www.w3.org/XML/1998/namespace",
414 attr.getKey().substring(4), attr.getValue());
415 } else {
416 serializer.writeAttribute(attr.getKey(), attr.getValue());
417 }
418 }
419 for (XmlNode child : dom.getChildren()) {
420 writeDom(child, serializer);
421 }
422 String value = dom.getValue();
423 if (value != null) {
424 serializer.writeCharacters(value);
425 }
426 serializer.writeEndElement();
427 if (addLocationInformation && dom.getInputLocation() instanceof InputLocation && dom.getChildren().isEmpty()) {
428 serializer.writeComment(toString((InputLocation) dom.getInputLocation()));
429 }
430 }
431 }
432
433 private void writeTag(String tagName, String defaultValue, String value, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException {
434 if (value != null && !Objects.equals(defaultValue, value)) {
435 serializer.writeStartElement(namespace, tagName);
436 serializer.writeCharacters(value);
437 serializer.writeEndElement();
438 writeLocationTracking(locationTracker, tagName, serializer);
439 }
440 }
441
442 private void writeAttr(String attrName, String value, XMLStreamWriter serializer) throws IOException, XMLStreamException {
443 if (value != null) {
444 serializer.writeAttribute(attrName, value);
445 }
446 }
447
448
449
450
451
452
453
454
455
456 protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XMLStreamWriter serializer) throws IOException, XMLStreamException {
457 if (addLocationInformation) {
458 InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key);
459 if (location != null) {
460 serializer.writeComment(toString(location));
461 }
462 }
463 }
464
465
466
467
468
469
470
471 protected String toString(InputLocation location) {
472 if (stringFormatter != null) {
473 return stringFormatter.apply(location);
474 }
475 if (location.getSource() != null) {
476 return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
477 } else {
478 return " " + location.getLineNumber() + " ";
479 }
480 }
481
482 static class IndentingXMLStreamWriter extends StreamWriterDelegate {
483
484 int depth = 0;
485 boolean hasChildren = false;
486
487 public IndentingXMLStreamWriter(XMLStreamWriter parent) {
488 super(parent);
489 }
490
491 @Override
492 public void writeEmptyElement(String localName) throws XMLStreamException {
493 indent();
494 super.writeEmptyElement(localName);
495 hasChildren = true;
496 }
497
498 @Override
499 public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
500 indent();
501 super.writeEmptyElement(namespaceURI, localName);
502 hasChildren = true;
503 }
504
505 @Override
506 public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
507 indent();
508 super.writeEmptyElement(prefix, localName, namespaceURI);
509 hasChildren = true;
510 }
511
512 @Override
513 public void writeStartElement(String localName) throws XMLStreamException {
514 indent();
515 super.writeStartElement(localName);
516 depth++;
517 hasChildren = false;
518 }
519
520 @Override
521 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
522 indent();
523 super.writeStartElement(namespaceURI, localName);
524 depth++;
525 hasChildren = false;
526 }
527
528 @Override
529 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
530 indent();
531 super.writeStartElement(prefix, localName, namespaceURI);
532 depth++;
533 hasChildren = false;
534 }
535
536 @Override
537 public void writeEndElement() throws XMLStreamException {
538 depth--;
539 if (hasChildren) {
540 indent();
541 }
542 super.writeEndElement();
543 hasChildren = true;
544 }
545
546 private void indent() throws XMLStreamException {
547 super.writeCharacters("\n");
548 for (int i = 0; i < depth; i++) {
549 super.writeCharacters(" ");
550 }
551 }
552 }
553 }