View Javadoc

1   package org.apache.maven.plugin.eclipse.writers.workspace;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.io.Reader;
8   import java.net.URL;
9   
10  import org.apache.maven.plugin.MojoExecutionException;
11  import org.apache.maven.plugin.eclipse.Messages;
12  import org.codehaus.plexus.util.IOUtil;
13  import org.codehaus.plexus.util.xml.Xpp3Dom;
14  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
15  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
16  
17  /**
18   * an Eclipse code style file
19   * 
20   * @author dtran
21   */
22  
23  public class EclipseCodeFormatterProfile
24  {
25      private static final String ELT_PROFILE = "profile";
26  
27      /**
28       * String presentation of the formatter with EOLs are escaped so that it can be embedded in a property value
29       */
30      private String content;
31  
32      private String profileName;
33  
34      public EclipseCodeFormatterProfile init( URL url, String profileName )
35          throws MojoExecutionException
36      {
37  
38          this.profileName = profileName;
39  
40          if ( this.profileName == null )
41          {
42              loadDefaultProfileName( url );
43          }
44  
45          this.convertFormatterToString( url );
46  
47          return this;
48      }
49  
50      private void loadDefaultProfileName( URL url )
51          throws MojoExecutionException
52      {
53          Reader reader = null;
54          try
55          {
56              reader = new InputStreamReader( url.openStream() );
57              Xpp3Dom dom = Xpp3DomBuilder.build( reader );
58  
59              Xpp3Dom[] existingProfiles = dom.getChildren( ELT_PROFILE );
60              if ( existingProfiles.length != 0 )
61              {
62                  Xpp3Dom firstProfile = existingProfiles[0];
63                  this.profileName = firstProfile.getAttribute( "name" );
64              }
65          }
66          catch ( XmlPullParserException e )
67          {
68              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); //$NON-NLS-1$
69          }
70          catch ( IOException e )
71          {
72              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); //$NON-NLS-1$
73          }
74          finally
75          {
76              IOUtil.close( reader );
77          }
78      }
79  
80      private void convertFormatterToString( URL url )
81          throws MojoExecutionException
82      {
83          InputStream is = null;
84  
85          ByteArrayOutputStream os = new ByteArrayOutputStream();
86  
87          try
88          {
89              is = url.openStream();
90  
91              IOUtil.copy( is, os );
92          }
93          catch ( IOException e )
94          {
95              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantreadfile", url.toString() ), e ); //$NON-NLS-1$
96          }
97          finally
98          {
99              IOUtil.close( is );
100         }
101 
102         content = os.toString();
103 
104     }
105 
106     public String getContent()
107     {
108         return this.content;
109     }
110 
111     public String getProfileName()
112     {
113         return this.profileName;
114     }
115 
116 }