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