View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.utils;
2   
3   /*
4    * Copyright 2008 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Iterator;
22  import java.util.StringTokenizer;
23  
24  import javax.xml.parsers.DocumentBuilderFactory;
25  
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.xpath.XPathAPI;
29  import org.apache.xpath.objects.XObject;
30  import org.w3c.dom.Document;
31  
32  import org.codehaus.plexus.util.FileUtils;
33  import org.codehaus.plexus.util.StringInputStream;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.interpolation.MapBasedValueSource;
36  import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource;
37  import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  
40  
41  public class MavenUtils
42  {
43      
44      private static MavenUtils mavenUtils = null;
45      
46      /**
47       * Creation forbidden...
48       */
49      private MavenUtils()
50      {
51          super();
52          
53      }
54      
55      public static MavenUtils getInstance()
56      {
57          if (mavenUtils == null){
58              mavenUtils = new MavenUtils();
59          }
60          return mavenUtils;
61      }
62      
63      /**
64       * 
65       * @param project
66       * @param pluginArtifact
67       * @param optionName
68       * @param defaultValue
69       * @return
70       */
71      public String getConfiguration( MavenProject project, String pluginArtifact, String pluginGroupId,
72                                       String optionName, String defaultValue )
73      {
74          String result = null;
75          String value = "";
76          try
77          {
78              value = getMavenPluginConfiguration( project, pluginArtifact, pluginGroupId, optionName, "" );
79              if ( value != null && value.length() > 0 )
80              {
81                  if ( value.indexOf( "$" ) > -1 )
82                  {
83                      result = getInterpolatorValue( project, value );
84                  }
85                  else
86                  {
87  
88                      File dir = new File( value );
89                      boolean isExists = dir.exists();
90                      if ( !isExists )
91                      {
92                          File resultFile = FileUtils.resolveFile( project.getBasedir(), value );
93                          result = resultFile.getAbsolutePath();
94                      }
95                      else
96                      {
97                          result = value;
98                      }
99                  }
100             }
101             else
102             {
103                 result = getInterpolatorValue( project, defaultValue );
104             }
105         }
106         catch ( IOException e )
107         {
108             result = null;
109         }
110         return result;
111     }
112 
113     /**
114      * 
115      * @param project
116      * @param value
117      * @return
118      */
119     private String getInterpolatorValue( MavenProject project, String value )
120     {
121 
122         RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
123         interpolator.addValueSource( new ObjectBasedValueSource( project ) );
124         interpolator.addValueSource( new MapBasedValueSource( project.getProperties() ) );
125 
126         String result = interpolator.interpolate( value, "project" );
127 
128         return result;
129     }
130 
131     /**
132      * Return the optionName value defined in a project for a given artifactId plugin.
133      * 
134      * @param project
135      *            not null
136      * @param pluginArtifact
137      *            not null
138      * @param optionName
139      *            an Xpath expression from the plugin <code>&lt;configuration/&gt;</code>
140      * @param defaultValue
141      * @return the value for the option name (comma separated value in the case of list) or null if not found
142      * @throws IOException
143      *             if any
144      */
145     private String getMavenPluginConfiguration( MavenProject project, String pluginArtifact,
146                                                        String pluginGroupId, String optionName, String defaultValue )
147         throws IOException
148     {
149         for ( Iterator it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
150         {
151             Plugin plugin = (Plugin) it.next();
152 
153             if ( ( plugin.getGroupId().equals( pluginGroupId ) ) && ( plugin.getArtifactId().equals( pluginArtifact ) ) )
154             {
155                 Xpp3Dom pluginConf = (Xpp3Dom) plugin.getConfiguration();
156 
157                 if ( pluginConf != null )
158                 {
159                     StringBuffer sb = new StringBuffer();
160                     try
161                     {
162                         Document doc =
163                             DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
164                                                                                              new StringInputStream(
165                                                                                                                     pluginConf.toString() ) );
166 
167                         XObject obj = XPathAPI.eval( doc, "//configuration//" + optionName );
168 
169                         if ( StringUtils.isNotEmpty( obj.toString() ) )
170                         {
171                             StringTokenizer token = new StringTokenizer( obj.toString(), "\n " );
172                             while ( token.hasMoreTokens() )
173                             {
174                                 sb.append( token.nextToken().trim() );
175                                 if ( token.hasMoreElements() )
176                                 {
177                                     sb.append( "," );
178                                 }
179                             }
180                             return sb.toString();
181                         }
182                     }
183                     catch ( Exception e )
184                     {
185                         throw new IOException( "Exception occured" + e.getMessage() );
186                     }
187                 }
188             }
189         }
190 
191         return defaultValue;
192     }
193 }