View Javadoc

1   /*
2    * $Id$
3    */
4   
5   package org.apache.maven.profiles.io.xpp3;
6   
7     //---------------------------------/
8    //- Imported classes and packages -/
9   //---------------------------------/
10  
11  import java.io.Writer;
12  import java.text.DateFormat;
13  import java.util.Iterator;
14  import java.util.Locale;
15  import org.apache.maven.profiles.Activation;
16  import org.apache.maven.profiles.ActivationFile;
17  import org.apache.maven.profiles.ActivationOS;
18  import org.apache.maven.profiles.ActivationProperty;
19  import org.apache.maven.profiles.Profile;
20  import org.apache.maven.profiles.ProfilesRoot;
21  import org.apache.maven.profiles.Repository;
22  import org.apache.maven.profiles.RepositoryBase;
23  import org.apache.maven.profiles.RepositoryPolicy;
24  import org.codehaus.plexus.util.xml.pull.MXSerializer;
25  import org.codehaus.plexus.util.xml.pull.XmlSerializer;
26  
27  /**
28   * Class ProfilesXpp3Writer.
29   * 
30   * @version $Revision$ $Date$
31   */
32  public class ProfilesXpp3Writer {
33  
34  
35        //--------------------------/
36       //- Class/Member Variables -/
37      //--------------------------/
38  
39      /**
40       * Field NAMESPACE.
41       */
42      private String NAMESPACE;
43  
44  
45        //-----------/
46       //- Methods -/
47      //-----------/
48  
49      /**
50       * Method write.
51       * 
52       * @param writer
53       * @param profilesRoot
54       * @throws java.io.IOException
55       */
56      public void write( Writer writer, ProfilesRoot profilesRoot )
57          throws java.io.IOException
58      {
59          XmlSerializer serializer = new MXSerializer();
60          serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "  " );
61          serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
62          serializer.setOutput( writer );
63          serializer.startDocument( profilesRoot.getModelEncoding(), null );
64          writeProfilesRoot( profilesRoot, "profilesXml", serializer );
65          serializer.endDocument();
66      } //-- void write( Writer, ProfilesRoot ) 
67  
68      /**
69       * Method writeActivation.
70       * 
71       * @param activation
72       * @param serializer
73       * @param tagName
74       * @throws java.io.IOException
75       */
76      private void writeActivation( Activation activation, String tagName, XmlSerializer serializer )
77          throws java.io.IOException
78      {
79          if ( activation != null )
80          {
81              serializer.startTag( NAMESPACE, tagName );
82              if ( activation.isActiveByDefault() != false )
83              {
84                  serializer.startTag( NAMESPACE, "activeByDefault" ).text( String.valueOf( activation.isActiveByDefault() ) ).endTag( NAMESPACE, "activeByDefault" );
85              }
86              if ( activation.getJdk() != null )
87              {
88                  serializer.startTag( NAMESPACE, "jdk" ).text( activation.getJdk() ).endTag( NAMESPACE, "jdk" );
89              }
90              if ( activation.getOs() != null )
91              {
92                  writeActivationOS( (ActivationOS) activation.getOs(), "os", serializer );
93              }
94              if ( activation.getProperty() != null )
95              {
96                  writeActivationProperty( (ActivationProperty) activation.getProperty(), "property", serializer );
97              }
98              if ( activation.getFile() != null )
99              {
100                 writeActivationFile( (ActivationFile) activation.getFile(), "file", serializer );
101             }
102             serializer.endTag( NAMESPACE, tagName );
103         }
104     } //-- void writeActivation( Activation, String, XmlSerializer ) 
105 
106     /**
107      * Method writeActivationFile.
108      * 
109      * @param activationFile
110      * @param serializer
111      * @param tagName
112      * @throws java.io.IOException
113      */
114     private void writeActivationFile( ActivationFile activationFile, String tagName, XmlSerializer serializer )
115         throws java.io.IOException
116     {
117         if ( activationFile != null )
118         {
119             serializer.startTag( NAMESPACE, tagName );
120             if ( activationFile.getMissing() != null )
121             {
122                 serializer.startTag( NAMESPACE, "missing" ).text( activationFile.getMissing() ).endTag( NAMESPACE, "missing" );
123             }
124             if ( activationFile.getExists() != null )
125             {
126                 serializer.startTag( NAMESPACE, "exists" ).text( activationFile.getExists() ).endTag( NAMESPACE, "exists" );
127             }
128             serializer.endTag( NAMESPACE, tagName );
129         }
130     } //-- void writeActivationFile( ActivationFile, String, XmlSerializer ) 
131 
132     /**
133      * Method writeActivationOS.
134      * 
135      * @param activationOS
136      * @param serializer
137      * @param tagName
138      * @throws java.io.IOException
139      */
140     private void writeActivationOS( ActivationOS activationOS, String tagName, XmlSerializer serializer )
141         throws java.io.IOException
142     {
143         if ( activationOS != null )
144         {
145             serializer.startTag( NAMESPACE, tagName );
146             if ( activationOS.getName() != null )
147             {
148                 serializer.startTag( NAMESPACE, "name" ).text( activationOS.getName() ).endTag( NAMESPACE, "name" );
149             }
150             if ( activationOS.getFamily() != null )
151             {
152                 serializer.startTag( NAMESPACE, "family" ).text( activationOS.getFamily() ).endTag( NAMESPACE, "family" );
153             }
154             if ( activationOS.getArch() != null )
155             {
156                 serializer.startTag( NAMESPACE, "arch" ).text( activationOS.getArch() ).endTag( NAMESPACE, "arch" );
157             }
158             if ( activationOS.getVersion() != null )
159             {
160                 serializer.startTag( NAMESPACE, "version" ).text( activationOS.getVersion() ).endTag( NAMESPACE, "version" );
161             }
162             serializer.endTag( NAMESPACE, tagName );
163         }
164     } //-- void writeActivationOS( ActivationOS, String, XmlSerializer ) 
165 
166     /**
167      * Method writeActivationProperty.
168      * 
169      * @param activationProperty
170      * @param serializer
171      * @param tagName
172      * @throws java.io.IOException
173      */
174     private void writeActivationProperty( ActivationProperty activationProperty, String tagName, XmlSerializer serializer )
175         throws java.io.IOException
176     {
177         if ( activationProperty != null )
178         {
179             serializer.startTag( NAMESPACE, tagName );
180             if ( activationProperty.getName() != null )
181             {
182                 serializer.startTag( NAMESPACE, "name" ).text( activationProperty.getName() ).endTag( NAMESPACE, "name" );
183             }
184             if ( activationProperty.getValue() != null )
185             {
186                 serializer.startTag( NAMESPACE, "value" ).text( activationProperty.getValue() ).endTag( NAMESPACE, "value" );
187             }
188             serializer.endTag( NAMESPACE, tagName );
189         }
190     } //-- void writeActivationProperty( ActivationProperty, String, XmlSerializer ) 
191 
192     /**
193      * Method writeProfile.
194      * 
195      * @param profile
196      * @param serializer
197      * @param tagName
198      * @throws java.io.IOException
199      */
200     private void writeProfile( Profile profile, String tagName, XmlSerializer serializer )
201         throws java.io.IOException
202     {
203         if ( profile != null )
204         {
205             serializer.startTag( NAMESPACE, tagName );
206             if ( profile.getId() != null )
207             {
208                 serializer.startTag( NAMESPACE, "id" ).text( profile.getId() ).endTag( NAMESPACE, "id" );
209             }
210             if ( profile.getActivation() != null )
211             {
212                 writeActivation( (Activation) profile.getActivation(), "activation", serializer );
213             }
214             if ( profile.getProperties() != null && profile.getProperties().size() > 0 )
215             {
216                 serializer.startTag( NAMESPACE, "properties" );
217                 for ( Iterator iter = profile.getProperties().keySet().iterator(); iter.hasNext(); )
218                 {
219                     String key = (String) iter.next();
220                     String value = (String) profile.getProperties().get( key );
221                     serializer.startTag( NAMESPACE, "" + key + "" ).text( value ).endTag( NAMESPACE, "" + key + "" );
222                 }
223                 serializer.endTag( NAMESPACE, "properties" );
224             }
225             if ( profile.getRepositories() != null && profile.getRepositories().size() > 0 )
226             {
227                 serializer.startTag( NAMESPACE, "repositories" );
228                 for ( Iterator iter = profile.getRepositories().iterator(); iter.hasNext(); )
229                 {
230                     Repository o = (Repository) iter.next();
231                     writeRepository( o, "repository", serializer );
232                 }
233                 serializer.endTag( NAMESPACE, "repositories" );
234             }
235             if ( profile.getPluginRepositories() != null && profile.getPluginRepositories().size() > 0 )
236             {
237                 serializer.startTag( NAMESPACE, "pluginRepositories" );
238                 for ( Iterator iter = profile.getPluginRepositories().iterator(); iter.hasNext(); )
239                 {
240                     Repository o = (Repository) iter.next();
241                     writeRepository( o, "pluginRepository", serializer );
242                 }
243                 serializer.endTag( NAMESPACE, "pluginRepositories" );
244             }
245             serializer.endTag( NAMESPACE, tagName );
246         }
247     } //-- void writeProfile( Profile, String, XmlSerializer ) 
248 
249     /**
250      * Method writeProfilesRoot.
251      * 
252      * @param profilesRoot
253      * @param serializer
254      * @param tagName
255      * @throws java.io.IOException
256      */
257     private void writeProfilesRoot( ProfilesRoot profilesRoot, String tagName, XmlSerializer serializer )
258         throws java.io.IOException
259     {
260         if ( profilesRoot != null )
261         {
262             serializer.startTag( NAMESPACE, tagName );
263             if ( profilesRoot.getProfiles() != null && profilesRoot.getProfiles().size() > 0 )
264             {
265                 serializer.startTag( NAMESPACE, "profiles" );
266                 for ( Iterator iter = profilesRoot.getProfiles().iterator(); iter.hasNext(); )
267                 {
268                     Profile o = (Profile) iter.next();
269                     writeProfile( o, "profile", serializer );
270                 }
271                 serializer.endTag( NAMESPACE, "profiles" );
272             }
273             if ( profilesRoot.getActiveProfiles() != null && profilesRoot.getActiveProfiles().size() > 0 )
274             {
275                 serializer.startTag( NAMESPACE, "activeProfiles" );
276                 for ( Iterator iter = profilesRoot.getActiveProfiles().iterator(); iter.hasNext(); )
277                 {
278                     String activeProfile = (String) iter.next();
279                     serializer.startTag( NAMESPACE, "activeProfile" ).text( activeProfile ).endTag( NAMESPACE, "activeProfile" );
280                 }
281                 serializer.endTag( NAMESPACE, "activeProfiles" );
282             }
283             serializer.endTag( NAMESPACE, tagName );
284         }
285     } //-- void writeProfilesRoot( ProfilesRoot, String, XmlSerializer ) 
286 
287     /**
288      * Method writeRepository.
289      * 
290      * @param repository
291      * @param serializer
292      * @param tagName
293      * @throws java.io.IOException
294      */
295     private void writeRepository( Repository repository, String tagName, XmlSerializer serializer )
296         throws java.io.IOException
297     {
298         if ( repository != null )
299         {
300             serializer.startTag( NAMESPACE, tagName );
301             if ( repository.getReleases() != null )
302             {
303                 writeRepositoryPolicy( (RepositoryPolicy) repository.getReleases(), "releases", serializer );
304             }
305             if ( repository.getSnapshots() != null )
306             {
307                 writeRepositoryPolicy( (RepositoryPolicy) repository.getSnapshots(), "snapshots", serializer );
308             }
309             if ( repository.getId() != null )
310             {
311                 serializer.startTag( NAMESPACE, "id" ).text( repository.getId() ).endTag( NAMESPACE, "id" );
312             }
313             if ( repository.getName() != null )
314             {
315                 serializer.startTag( NAMESPACE, "name" ).text( repository.getName() ).endTag( NAMESPACE, "name" );
316             }
317             if ( repository.getUrl() != null )
318             {
319                 serializer.startTag( NAMESPACE, "url" ).text( repository.getUrl() ).endTag( NAMESPACE, "url" );
320             }
321             if ( repository.getLayout() != null && !repository.getLayout().equals( "default" ) )
322             {
323                 serializer.startTag( NAMESPACE, "layout" ).text( repository.getLayout() ).endTag( NAMESPACE, "layout" );
324             }
325             serializer.endTag( NAMESPACE, tagName );
326         }
327     } //-- void writeRepository( Repository, String, XmlSerializer ) 
328 
329     /**
330      * Method writeRepositoryBase.
331      * 
332      * @param repositoryBase
333      * @param serializer
334      * @param tagName
335      * @throws java.io.IOException
336      */
337     private void writeRepositoryBase( RepositoryBase repositoryBase, String tagName, XmlSerializer serializer )
338         throws java.io.IOException
339     {
340         if ( repositoryBase != null )
341         {
342             serializer.startTag( NAMESPACE, tagName );
343             if ( repositoryBase.getId() != null )
344             {
345                 serializer.startTag( NAMESPACE, "id" ).text( repositoryBase.getId() ).endTag( NAMESPACE, "id" );
346             }
347             if ( repositoryBase.getName() != null )
348             {
349                 serializer.startTag( NAMESPACE, "name" ).text( repositoryBase.getName() ).endTag( NAMESPACE, "name" );
350             }
351             if ( repositoryBase.getUrl() != null )
352             {
353                 serializer.startTag( NAMESPACE, "url" ).text( repositoryBase.getUrl() ).endTag( NAMESPACE, "url" );
354             }
355             if ( repositoryBase.getLayout() != null && !repositoryBase.getLayout().equals( "default" ) )
356             {
357                 serializer.startTag( NAMESPACE, "layout" ).text( repositoryBase.getLayout() ).endTag( NAMESPACE, "layout" );
358             }
359             serializer.endTag( NAMESPACE, tagName );
360         }
361     } //-- void writeRepositoryBase( RepositoryBase, String, XmlSerializer ) 
362 
363     /**
364      * Method writeRepositoryPolicy.
365      * 
366      * @param repositoryPolicy
367      * @param serializer
368      * @param tagName
369      * @throws java.io.IOException
370      */
371     private void writeRepositoryPolicy( RepositoryPolicy repositoryPolicy, String tagName, XmlSerializer serializer )
372         throws java.io.IOException
373     {
374         if ( repositoryPolicy != null )
375         {
376             serializer.startTag( NAMESPACE, tagName );
377             if ( repositoryPolicy.isEnabled() != true )
378             {
379                 serializer.startTag( NAMESPACE, "enabled" ).text( String.valueOf( repositoryPolicy.isEnabled() ) ).endTag( NAMESPACE, "enabled" );
380             }
381             if ( repositoryPolicy.getUpdatePolicy() != null )
382             {
383                 serializer.startTag( NAMESPACE, "updatePolicy" ).text( repositoryPolicy.getUpdatePolicy() ).endTag( NAMESPACE, "updatePolicy" );
384             }
385             if ( repositoryPolicy.getChecksumPolicy() != null )
386             {
387                 serializer.startTag( NAMESPACE, "checksumPolicy" ).text( repositoryPolicy.getChecksumPolicy() ).endTag( NAMESPACE, "checksumPolicy" );
388             }
389             serializer.endTag( NAMESPACE, tagName );
390         }
391     } //-- void writeRepositoryPolicy( RepositoryPolicy, String, XmlSerializer ) 
392 
393 
394 }