View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from writer.vm
3   //  template, any modifications will be overwritten.
4   // ==============================================================
5   package org.apache.maven.plugin.lifecycle.io.xpp3;
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 org.apache.maven.api.annotations.Generated;
22  import org.apache.maven.api.xml.XmlNode;
23  import org.apache.maven.internal.xml.XmlNodeBuilder;
24  import org.apache.maven.plugin.lifecycle.LifecycleConfiguration;
25  import org.apache.maven.plugin.lifecycle.Lifecycle;
26  import org.apache.maven.plugin.lifecycle.Phase;
27  import org.apache.maven.plugin.lifecycle.Execution;
28  import org.codehaus.plexus.util.ReaderFactory;
29  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
30  import org.codehaus.plexus.util.xml.pull.MXParser;
31  import org.codehaus.plexus.util.xml.pull.MXSerializer;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
33  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34  import org.codehaus.plexus.util.xml.pull.XmlSerializer;
35  
36  @Generated
37  public class LifecycleMappingsXpp3Writer
38  {
39        //--------------------------/
40       //- Class/Member Variables -/
41      //--------------------------/
42  
43      /**
44       * Field NAMESPACE.
45       */
46      private static final String NAMESPACE = null;
47  
48      /**
49       * Field fileComment.
50       */
51      private String fileComment = null;
52  
53  
54        //-----------/
55       //- Methods -/
56      //-----------/
57  
58      /**
59       * Method setFileComment.
60       *
61       * @param fileComment a fileComment object.
62       */
63      public void setFileComment( String fileComment )
64      {
65          this.fileComment = fileComment;
66      } //-- void setFileComment( String )
67  
68      /**
69       * Method write.
70       *
71       * @param writer a writer object
72       * @param lifecycleConfiguration a LifecycleConfiguration object
73       * @throws java.io.IOException java.io.IOException if any
74       */
75      public void write( Writer writer, LifecycleConfiguration lifecycleConfiguration )
76          throws java.io.IOException
77      {
78          XmlSerializer serializer = new MXSerializer();
79          serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "  " );
80          serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
81          serializer.setOutput( writer );
82          serializer.startDocument( lifecycleConfiguration.getModelEncoding(), null );
83          writeLifecycleConfiguration( "lifecycles", lifecycleConfiguration, serializer );
84          serializer.endDocument();
85      } //-- void write( Writer, LifecycleConfiguration )
86  
87      /**
88       * Method write.
89       *
90       * @param stream a stream object
91       * @param lifecycleConfiguration a LifecycleConfiguration object
92       * @throws java.io.IOException java.io.IOException if any
93       */
94      public void write( OutputStream stream, LifecycleConfiguration lifecycleConfiguration )
95          throws java.io.IOException
96      {
97          XmlSerializer serializer = new MXSerializer();
98          serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "  " );
99          serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
100         serializer.setOutput( stream, lifecycleConfiguration.getModelEncoding() );
101         serializer.startDocument( lifecycleConfiguration.getModelEncoding(), null );
102         writeLifecycleConfiguration( "lifecycles", lifecycleConfiguration, serializer );
103         serializer.endDocument();
104     } //-- void write( OutputStream, LifecycleConfiguration )
105 
106     /**
107      * Method writeDomToSerializer.
108      *
109      * @param dom a dom object.
110      * @param serializer a serializer object.
111      * @throws java.io.IOException java.io.IOException if any.
112      */
113     protected void writeDomToSerializer( org.apache.maven.api.xml.XmlNode dom, XmlSerializer serializer )
114         throws java.io.IOException
115     {
116         serializer.startTag( NAMESPACE, dom.getName() );
117 
118         for ( Map.Entry<String, String> attribute : dom.getAttributes().entrySet() )
119         {
120             serializer.attribute( NAMESPACE, attribute.getKey(), attribute.getValue() );
121         }
122         for ( XmlNode aChild : dom.getChildren() )
123         {
124             writeDomToSerializer( aChild, serializer );
125         }
126 
127         String value = dom.getValue();
128         if ( value != null )
129         {
130             serializer.text( value );
131         }
132 
133         serializer.endTag( NAMESPACE, dom.getName() );
134 
135     } //-- void writeDomToSerializer( org.apache.maven.api.xml.XmlNode, XmlSerializer )
136 
137 
138     private void writeLifecycleConfiguration( String tagName, LifecycleConfiguration lifecycleConfiguration, XmlSerializer serializer )
139         throws IOException
140     {
141         if ( lifecycleConfiguration != null )
142         {
143             if ( this.fileComment != null )
144             {
145                 serializer.comment(this.fileComment);
146             }
147             serializer.setPrefix( "", "http://maven.apache.org/POM/4.0.0" );
148             serializer.setPrefix( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
149             serializer.startTag( NAMESPACE, tagName );
150             serializer.attribute( "", "xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" );
151             writeList( "lifecycles", true, lifecycleConfiguration.getLifecycles(), serializer, t -> writeLifecycle( "lifecycle", t, serializer ) );
152             serializer.endTag( NAMESPACE, tagName );
153         }
154     }
155 
156     private void writeLifecycle( String tagName, Lifecycle lifecycle, XmlSerializer serializer )
157         throws IOException
158     {
159         if ( lifecycle != null )
160         {
161             serializer.startTag( NAMESPACE, tagName );
162             writeTag( "id", null, lifecycle.getId(), serializer );
163             writeList( "phases", false, lifecycle.getPhases(), serializer, t -> writePhase( "phase", t, serializer ) );
164             serializer.endTag( NAMESPACE, tagName );
165         }
166     }
167 
168     private void writePhase( String tagName, Phase phase, XmlSerializer serializer )
169         throws IOException
170     {
171         if ( phase != null )
172         {
173             serializer.startTag( NAMESPACE, tagName );
174             writeTag( "id", null, phase.getId(), serializer );
175             writeList( "executions", false, phase.getExecutions(), serializer, t -> writeExecution( "execution", t, serializer ) );
176             writeDom( phase.getConfiguration(), serializer );
177             serializer.endTag( NAMESPACE, tagName );
178         }
179     }
180 
181     private void writeExecution( String tagName, Execution execution, XmlSerializer serializer )
182         throws IOException
183     {
184         if ( execution != null )
185         {
186             serializer.startTag( NAMESPACE, tagName );
187             writeDom( execution.getConfiguration(), serializer );
188             writeList( "goals", execution.getGoals(), serializer, t -> writeTag( "goal", "new java.util.ArrayList/*<String>*/()", t, serializer ) );
189             serializer.endTag( NAMESPACE, tagName );
190         }
191     }
192 
193     @FunctionalInterface
194     private interface ElementWriter<T>
195     {
196         public void write( T t ) throws IOException;
197     }
198 
199     private <T> void writeList( String tagName, List<T> list, XmlSerializer serializer, ElementWriter<T> writer )
200         throws IOException
201     {
202         writeList( tagName, false, list, serializer, writer );
203     }
204 
205     private <T> void writeList( String tagName, boolean flat, List<T> list, XmlSerializer serializer, ElementWriter<T> writer )
206         throws IOException
207     {
208         if ( list != null && !list.isEmpty() )
209         {
210             if ( !flat )
211             {
212                 serializer.startTag( NAMESPACE, tagName );
213             }
214             for ( T t : list )
215             {
216                 writer.write( t );
217             }
218             if ( !flat )
219             {
220                 serializer.endTag( NAMESPACE, tagName );
221             }
222         }
223     }
224 
225     private <T> void writeProperties( String tagName, Map<String, String> props, XmlSerializer serializer )
226             throws IOException
227     {
228         if ( props != null && !props.isEmpty() )
229         {
230             serializer.startTag( NAMESPACE, tagName );
231             for ( Map.Entry<String, String> entry : props.entrySet() )
232             {
233                 writeTag( entry.getKey(), null, entry.getValue(), serializer );
234             }
235             serializer.endTag( NAMESPACE, tagName );
236         }
237     }
238 
239     private void writeDom( XmlNode dom, XmlSerializer serializer )
240             throws IOException
241     {
242         if ( dom != null )
243         {
244             serializer.startTag( NAMESPACE, dom.getName() );
245             for ( Map.Entry<String, String> attr : dom.getAttributes().entrySet() )
246             {
247                 serializer.attribute( NAMESPACE, attr.getKey(), attr.getValue() );
248             }
249             for ( XmlNode child : dom.getChildren() )
250             {
251                 writeDom( child, serializer );
252             }
253             String value = dom.getValue();
254             if ( value != null )
255             {
256                 serializer.text( value );
257             }
258             serializer.endTag( NAMESPACE, dom.getName() );
259         }
260     }
261 
262     private void writeTag( String tagName, String defaultValue, String value, XmlSerializer serializer )
263         throws IOException
264     {
265         if ( value != null && !Objects.equals( defaultValue, value ) )
266         {
267             serializer.startTag( NAMESPACE, tagName ).text( value ).endTag( NAMESPACE, tagName );
268         }
269     }
270 
271     private void writeAttr( String attrName, String value, XmlSerializer serializer )
272         throws IOException
273     {
274         if ( value != null )
275         {
276             serializer.attribute( NAMESPACE, attrName, value );
277         }
278     }
279 
280 }