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