View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.workspace;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.io.Reader;
28  import java.net.URL;
29  
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.eclipse.Messages;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.xml.Xpp3Dom;
34  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
35  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36  
37  /**
38   * an Eclipse code style file
39   * 
40   * @author dtran
41   */
42  
43  public class EclipseCodeFormatterProfile
44  {
45      private static final String ELT_PROFILE = "profile";
46  
47      /**
48       * String presentation of the formatter with EOLs are escaped so that it can be embedded in a property value
49       */
50      private String content;
51  
52      private String profileName;
53  
54      public EclipseCodeFormatterProfile init( URL url, String profileName )
55          throws MojoExecutionException
56      {
57  
58          this.profileName = profileName;
59  
60          if ( this.profileName == null )
61          {
62              loadDefaultProfileName( url );
63          }
64  
65          this.convertFormatterToString( url );
66  
67          return this;
68      }
69  
70      private void loadDefaultProfileName( URL url )
71          throws MojoExecutionException
72      {
73          Reader reader = null;
74          try
75          {
76              reader = new InputStreamReader( url.openStream() );
77              Xpp3Dom dom = Xpp3DomBuilder.build( reader );
78  
79              Xpp3Dom[] existingProfiles = dom.getChildren( ELT_PROFILE );
80              if ( existingProfiles.length != 0 )
81              {
82                  Xpp3Dom firstProfile = existingProfiles[0];
83                  this.profileName = firstProfile.getAttribute( "name" );
84              }
85          }
86          catch ( XmlPullParserException e )
87          {
88              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); //$NON-NLS-1$
89          }
90          catch ( IOException e )
91          {
92              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); //$NON-NLS-1$
93          }
94          finally
95          {
96              IOUtil.close( reader );
97          }
98      }
99  
100     private void convertFormatterToString( URL url )
101         throws MojoExecutionException
102     {
103         InputStream is = null;
104 
105         ByteArrayOutputStream os = new ByteArrayOutputStream();
106 
107         try
108         {
109             is = url.openStream();
110 
111             IOUtil.copy( is, os );
112         }
113         catch ( IOException e )
114         {
115             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantreadfile", url.toString() ), e ); //$NON-NLS-1$
116         }
117         finally
118         {
119             IOUtil.close( is );
120         }
121 
122         content = os.toString();
123 
124     }
125 
126     public String getContent()
127     {
128         return this.content;
129     }
130 
131     public String getProfileName()
132     {
133         return this.profileName;
134     }
135 
136 }