View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from writer-stax.vm
3   //  template, any modifications will be overwritten.
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       //- Class/Member Variables -/
53      //--------------------------/
54  
55      /**
56       * Default namespace.
57       */
58      private static final String NAMESPACE = "http://maven.apache.org/SETTINGS/1.2.0";
59  
60      /**
61       * Default schemaLocation.
62       */
63      private static final String SCHEMA_LOCATION = "http://maven.apache.org/xsd/settings-1.2.0.xsd";
64  
65      /**
66       * Field namespace.
67       */
68      private String namespace = NAMESPACE;
69  
70      /**
71       * Field schemaLocation.
72       */
73      private String schemaLocation = SCHEMA_LOCATION;
74  
75      /**
76       * Field fileComment.
77       */
78      private String fileComment = null;
79  
80      private boolean addLocationInformation = true;
81  
82      /**
83       * Field stringFormatter.
84       */
85      protected InputLocation.StringFormatter stringFormatter;
86  
87        //-----------/
88       //- Methods -/
89      //-----------/
90  
91      /**
92       * Method setNamespace.
93       *
94       * @param namespace the namespace to use.
95       */
96      public void setNamespace(String namespace) {
97          this.namespace = Objects.requireNonNull(namespace);
98      } //-- void setNamespace(String)
99  
100     /**
101      * Method setSchemaLocation.
102      *
103      * @param schemaLocation the schema location to use.
104      */
105     public void setSchemaLocation(String schemaLocation) {
106         this.schemaLocation = Objects.requireNonNull(schemaLocation);
107         } //-- void setSchemaLocation(String)
108 
109     /**
110      * Method setFileComment.
111      *
112      * @param fileComment a fileComment object.
113      */
114     public void setFileComment(String fileComment) {
115         this.fileComment = fileComment;
116     } //-- void setFileComment(String)
117 
118     /**
119      * Method setAddLocationInformation.
120      */
121     public void setAddLocationInformation(boolean addLocationInformation) {
122         this.addLocationInformation = addLocationInformation;
123     } //-- void setAddLocationInformation(String)
124 
125     /**
126      * Method setStringFormatter.
127      *
128      * @param stringFormatter
129      */
130     public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
131         this.stringFormatter = stringFormatter;
132     } //-- void setStringFormatter(InputLocation.StringFormatter)
133 
134     /**
135      * Method write.
136      *
137      * @param writer a writer object
138      * @param settings a Settings object
139      * @throws IOException IOException if any
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     } //-- void write(Writer, Settings)
151 
152     /**
153      * Method write.
154      *
155      * @param stream a stream object
156      * @param settings a Settings object
157      * @throws IOException IOException if any
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     } //-- void write(OutputStream, Settings)
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             serializer.writeEndElement();
295         }
296     }
297 
298     private void writeRepositoryBase(String tagName, RepositoryBase repositoryBase, XMLStreamWriter serializer)
299         throws IOException, XMLStreamException {
300         if (repositoryBase != null) {
301             serializer.writeStartElement(namespace, tagName);
302             writeTag("name", null, repositoryBase.getName(), serializer, repositoryBase);
303             writeTag("url", null, repositoryBase.getUrl(), serializer, repositoryBase);
304             writeTag("layout", "default", repositoryBase.getLayout(), serializer, repositoryBase);
305             writeTag("id", "default", repositoryBase.getId(), serializer, repositoryBase);
306             serializer.writeEndElement();
307         }
308     }
309 
310     private void writeRepository(String tagName, Repository repository, XMLStreamWriter serializer)
311         throws IOException, XMLStreamException {
312         if (repository != null) {
313             serializer.writeStartElement(namespace, tagName);
314             writeRepositoryPolicy("releases", repository.getReleases(), serializer);
315             writeRepositoryPolicy("snapshots", repository.getSnapshots(), serializer);
316             writeTag("name", null, repository.getName(), serializer, repository);
317             writeTag("url", null, repository.getUrl(), serializer, repository);
318             writeTag("layout", "default", repository.getLayout(), serializer, repository);
319             writeTag("id", "default", repository.getId(), serializer, repository);
320             serializer.writeEndElement();
321         }
322     }
323 
324     private void writeRepositoryPolicy(String tagName, RepositoryPolicy repositoryPolicy, XMLStreamWriter serializer)
325         throws IOException, XMLStreamException {
326         if (repositoryPolicy != null) {
327             serializer.writeStartElement(namespace, tagName);
328             writeTag("enabled", "true", repositoryPolicy.isEnabled() ? null : "false", serializer, repositoryPolicy);
329             writeTag("updatePolicy", null, repositoryPolicy.getUpdatePolicy(), serializer, repositoryPolicy);
330             writeTag("checksumPolicy", null, repositoryPolicy.getChecksumPolicy(), serializer, repositoryPolicy);
331             serializer.writeEndElement();
332         }
333     }
334 
335     private void writeActivationProperty(String tagName, ActivationProperty activationProperty, XMLStreamWriter serializer)
336         throws IOException, XMLStreamException {
337         if (activationProperty != null) {
338             serializer.writeStartElement(namespace, tagName);
339             writeTag("name", null, activationProperty.getName(), serializer, activationProperty);
340             writeTag("value", null, activationProperty.getValue(), serializer, activationProperty);
341             serializer.writeEndElement();
342         }
343     }
344 
345     private void writeActivationOS(String tagName, ActivationOS activationOS, XMLStreamWriter serializer)
346         throws IOException, XMLStreamException {
347         if (activationOS != null) {
348             serializer.writeStartElement(namespace, tagName);
349             writeTag("name", null, activationOS.getName(), serializer, activationOS);
350             writeTag("family", null, activationOS.getFamily(), serializer, activationOS);
351             writeTag("arch", null, activationOS.getArch(), serializer, activationOS);
352             writeTag("version", null, activationOS.getVersion(), serializer, activationOS);
353             serializer.writeEndElement();
354         }
355     }
356 
357     private void writeActivationFile(String tagName, ActivationFile activationFile, XMLStreamWriter serializer)
358         throws IOException, XMLStreamException {
359         if (activationFile != null) {
360             serializer.writeStartElement(namespace, tagName);
361             writeTag("missing", null, activationFile.getMissing(), serializer, activationFile);
362             writeTag("exists", null, activationFile.getExists(), serializer, activationFile);
363             serializer.writeEndElement();
364         }
365     }
366 
367     @FunctionalInterface
368     private interface ElementWriter<T> {
369         public void write(T t) throws IOException, XMLStreamException;
370     }
371 
372     private <T> void writeList(String tagName, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException {
373         writeList(tagName, false, list, serializer, locationTracker, writer);
374     }
375 
376     private <T> void writeList(String tagName, boolean flat, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException {
377         if (list != null && !list.isEmpty()) {
378             if (!flat) {
379                 serializer.writeStartElement(namespace, tagName);
380             }
381             int index = 0;
382             InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
383             for (T t : list) {
384                 writer.write(t);
385                 writeLocationTracking(location, Integer.valueOf(index++), serializer);
386             }
387             if (!flat) {
388                 serializer.writeEndElement();
389             }
390         }
391     }
392 
393     private <T> void writeProperties(String tagName, Map<String, String> props, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException {
394         if (props != null && !props.isEmpty()) {
395             serializer.writeStartElement(namespace, tagName);
396             InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
397             for (Map.Entry<String, String> entry : props.entrySet()) {
398                 String key = entry.getKey();
399                 writeTag(key, null, entry.getValue(), serializer, null);
400                 writeLocationTracking(location, key, serializer);
401             }
402             serializer.writeEndElement();
403         }
404     }
405 
406     private void writeDom(XmlNode dom, XMLStreamWriter serializer) throws IOException, XMLStreamException {
407         if (dom != null) {
408             serializer.writeStartElement(namespace, dom.getName());
409             for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
410                 if (attr.getKey().startsWith("xml:")) {
411                     serializer.writeAttribute("http://www.w3.org/XML/1998/namespace",
412                     attr.getKey().substring(4), attr.getValue());
413                 } else {
414                     serializer.writeAttribute(attr.getKey(), attr.getValue());
415                 }
416             }
417             for (XmlNode child : dom.getChildren()) {
418                 writeDom(child, serializer);
419             }
420             String value = dom.getValue();
421             if (value != null) {
422                 serializer.writeCharacters(value);
423             }
424             serializer.writeEndElement();
425             if (addLocationInformation && dom.getInputLocation() instanceof InputLocation && dom.getChildren().isEmpty()) {
426                 serializer.writeComment(toString((InputLocation) dom.getInputLocation()));
427             }
428         }
429     }
430 
431     private void writeTag(String tagName, String defaultValue, String value, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException {
432         if (value != null && !Objects.equals(defaultValue, value)) {
433             serializer.writeStartElement(namespace, tagName);
434             serializer.writeCharacters(value);
435             serializer.writeEndElement();
436             writeLocationTracking(locationTracker, tagName, serializer);
437         }
438     }
439 
440     private void writeAttr(String attrName, String value, XMLStreamWriter serializer) throws IOException, XMLStreamException {
441         if (value != null) {
442             serializer.writeAttribute(attrName, value);
443         }
444     }
445 
446     /**
447      * Method writeLocationTracking.
448      *
449      * @param locationTracker
450      * @param serializer
451      * @param key
452      * @throws IOException
453      */
454     protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XMLStreamWriter serializer) throws IOException, XMLStreamException {
455         if (addLocationInformation) {
456             InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key);
457             if (location != null) {
458                 serializer.writeComment(toString(location));
459             }
460         }
461     } //-- void writeLocationTracking(InputLocationTracker, Object, XMLStreamWriter)
462 
463     /**
464      * Method toString.
465      *
466      * @param location
467      * @return String
468      */
469     protected String toString(InputLocation location) {
470         if (stringFormatter != null) {
471             return stringFormatter.toString(location);
472         }
473         if (location.getSource() != null) {
474             return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
475         } else {
476             return " " + location.getLineNumber() + " ";
477         }
478     } //-- String toString(InputLocation)
479 
480     static class IndentingXMLStreamWriter extends StreamWriterDelegate {
481 
482         int depth = 0;
483         boolean hasChildren = false;
484 
485         public IndentingXMLStreamWriter(XMLStreamWriter parent) {
486             super(parent);
487         }
488 
489         @Override
490         public void writeEmptyElement(String localName) throws XMLStreamException {
491             indent();
492             super.writeEmptyElement(localName);
493             hasChildren = true;
494         }
495 
496         @Override
497         public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
498             indent();
499             super.writeEmptyElement(namespaceURI, localName);
500             hasChildren = true;
501         }
502 
503         @Override
504         public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
505             indent();
506             super.writeEmptyElement(prefix, localName, namespaceURI);
507             hasChildren = true;
508         }
509 
510         @Override
511         public void writeStartElement(String localName) throws XMLStreamException {
512             indent();
513             super.writeStartElement(localName);
514             depth++;
515             hasChildren = false;
516         }
517 
518         @Override
519         public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
520             indent();
521             super.writeStartElement(namespaceURI, localName);
522             depth++;
523             hasChildren = false;
524         }
525 
526         @Override
527         public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
528             indent();
529             super.writeStartElement(prefix, localName, namespaceURI);
530             depth++;
531             hasChildren = false;
532         }
533 
534         @Override
535         public void writeEndElement() throws XMLStreamException {
536             depth--;
537             if (hasChildren) {
538                 indent();
539             }
540             super.writeEndElement();
541             hasChildren = true;
542         }
543 
544         private void indent() throws XMLStreamException {
545             super.writeCharacters("\n");
546             for (int i = 0; i < depth; i++) {
547                 super.writeCharacters("  ");
548             }
549         }
550     }
551 }