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