1 package org.codehaus.mojo.dashboard.report.plugin.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
66
67
68
69
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
116
117
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
133
134
135
136
137
138
139
140
141
142
143
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 }