KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > PluginParameterExpressionEvaluator


1 package org.apache.maven.plugin;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
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 org.apache.maven.execution.MavenSession;
20 import org.apache.maven.plugin.descriptor.MojoDescriptor;
21 import org.apache.maven.plugin.descriptor.PluginDescriptor;
22 import org.apache.maven.project.MavenProject;
23 import org.apache.maven.project.path.PathTranslator;
24 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
25 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
26 import org.codehaus.plexus.logging.Logger;
27 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
28
29 import java.io.File JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl</a>
36  * @version $Id: PluginParameterExpressionEvaluator.java 348489 2005-11-23 17:01:09Z jdcasey $
37  * @todo belong in MavenSession, so it only gets created once?
38  */

39 public class PluginParameterExpressionEvaluator
40     implements ExpressionEvaluator
41 {
42     private static final Map JavaDoc BANNED_EXPRESSIONS;
43
44     private static final Map JavaDoc DEPRECATED_EXPRESSIONS;
45
46     static
47     {
48         Map JavaDoc deprecated = new HashMap JavaDoc();
49
50         deprecated.put( "project.build.resources", "project.resources" );
51         deprecated.put( "project.build.testResources", "project.testResources" );
52
53         DEPRECATED_EXPRESSIONS = deprecated;
54
55         Map JavaDoc banned = new HashMap JavaDoc();
56
57         BANNED_EXPRESSIONS = banned;
58     }
59
60     private final PathTranslator pathTranslator;
61
62     private final MavenSession context;
63
64     private final Logger logger;
65
66     private final MojoExecution mojoExecution;
67
68     private final MavenProject project;
69
70     private final String JavaDoc basedir;
71
72     private final Properties JavaDoc properties;
73
74     public PluginParameterExpressionEvaluator( MavenSession context,
75                                                MojoExecution mojoExecution,
76                                                PathTranslator pathTranslator,
77                                                Logger logger,
78                                                MavenProject project,
79                                                Properties JavaDoc properties )
80     {
81         this.context = context;
82         this.mojoExecution = mojoExecution;
83         this.pathTranslator = pathTranslator;
84         this.logger = logger;
85         this.project = project;
86         this.properties = properties;
87
88         String JavaDoc basedir = null;
89
90         if ( project != null )
91         {
92             File JavaDoc projectFile = project.getFile();
93
94             // this should always be the case for non-super POM instances...
95
if ( projectFile != null )
96             {
97                 basedir = projectFile.getParentFile().getAbsolutePath();
98             }
99         }
100
101         if ( basedir == null )
102         {
103             basedir = System.getProperty( "user.dir" );
104         }
105
106         this.basedir = basedir;
107     }
108
109     public Object JavaDoc evaluate( String JavaDoc expr )
110         throws ExpressionEvaluationException
111     {
112         Object JavaDoc value = null;
113
114         if ( expr == null )
115         {
116             return null;
117         }
118         
119         String JavaDoc expression = stripTokens( expr );
120         if ( expression.equals( expr ) )
121         {
122             int index = expr.indexOf( "${" );
123             if ( index >= 0 )
124             {
125                 int lastIndex = expr.indexOf( "}", index );
126                 if ( lastIndex >= 0 )
127                 {
128                     String JavaDoc retVal = expr.substring( 0, index );
129                     
130                     if ( index > 0 && expr.charAt( index - 1 ) == '$' )
131                     {
132                         retVal += expr.substring( index + 1, lastIndex + 1 );
133                     }
134                     else
135                     {
136                         retVal += evaluate( expr.substring( index, lastIndex + 1 ) );
137                     }
138                     
139                     retVal += evaluate( expr.substring( lastIndex + 1 ) );
140                     return retVal;
141                 }
142             }
143
144             // Was not an expression
145
if ( expression.indexOf( "$$" ) > -1 )
146             {
147                 return expression.replaceAll( "\\$\\$", "\\$" );
148             }
149             else
150             {
151                 return expression;
152             }
153         }
154
155         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
156         if ( BANNED_EXPRESSIONS.containsKey( expression ) )
157         {
158             throw new ExpressionEvaluationException( "The parameter expression: \'" + expression +
159                 "\' used in mojo: \'" + mojoDescriptor.getGoal() + "\' is banned. Use \'" +
160                 BANNED_EXPRESSIONS.get( expression ) + "\' instead." );
161         }
162         else if ( DEPRECATED_EXPRESSIONS.containsKey( expression ) )
163         {
164             logger.warn( "The parameter expression: \'" + expression + "\' used in mojo: \'" +
165                 mojoDescriptor.getGoal() + "\' has been deprecated. Use \'" + DEPRECATED_EXPRESSIONS.get( expression ) +
166                 "\' instead." );
167         }
168
169         if ( "localRepository".equals( expression ) )
170         {
171             value = context.getLocalRepository();
172         }
173         else if ( "session".equals( expression ) )
174         {
175             value = context;
176         }
177         else if ( "reactorProjects".equals( expression ) )
178         {
179             value = context.getSortedProjects();
180         }
181         else if ( "reports".equals( expression ) )
182         {
183             value = mojoExecution.getReports();
184         }
185         else if ( "project".equals( expression ) )
186         {
187             value = project;
188         }
189         else if ( "executedProject".equals( expression ) )
190         {
191             value = project.getExecutionProject();
192         }
193         else if ( expression.startsWith( "project" ) )
194         {
195             try
196             {
197                 int pathSeparator = expression.indexOf( "/" );
198
199                 if ( pathSeparator > 0 )
200                 {
201                     String JavaDoc pathExpression = expression.substring( 0, pathSeparator );
202                     value = ReflectionValueExtractor.evaluate( pathExpression, project );
203                     value = value + expression.substring( pathSeparator );
204                 }
205                 else
206                 {
207                     value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), project );
208                 }
209             }
210             catch ( Exception JavaDoc e )
211             {
212                 // TODO: don't catch exception
213
throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
214                                                          e );
215             }
216         }
217         else if ( expression.startsWith( "plugin" ) )
218         {
219             try
220             {
221                 int pathSeparator = expression.indexOf( "/" );
222
223                 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
224
225                 if ( pathSeparator > 0 )
226                 {
227                     String JavaDoc pathExpression = expression.substring( 1, pathSeparator );
228                     value = ReflectionValueExtractor.evaluate( pathExpression, pluginDescriptor );
229                     value = value + expression.substring( pathSeparator );
230                 }
231                 else
232                 {
233                     value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), pluginDescriptor );
234                 }
235             }
236             catch ( Exception JavaDoc e )
237             {
238                 // TODO: don't catch exception
239
throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
240                                                          e );
241             }
242         }
243         else if ( "settings".equals( expression ) )
244         {
245             value = context.getSettings();
246         }
247         else if ( expression.startsWith( "settings" ) )
248         {
249             try
250             {
251                 int pathSeparator = expression.indexOf( "/" );
252
253                 if ( pathSeparator > 0 )
254                 {
255                     String JavaDoc pathExpression = expression.substring( 1, pathSeparator );
256                     value = ReflectionValueExtractor.evaluate( pathExpression, context.getSettings() );
257                     value = value + expression.substring( pathSeparator );
258                 }
259                 else
260                 {
261                     value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), context.getSettings() );
262                 }
263             }
264             catch ( Exception JavaDoc e )
265             {
266                 // TODO: don't catch exception
267
throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
268                                                          e );
269             }
270         }
271         else if ( "basedir".equals( expression ) )
272         {
273             value = basedir;
274         }
275         else if ( expression.startsWith( "basedir" ) )
276         {
277             int pathSeparator = expression.indexOf( "/" );
278
279             if ( pathSeparator > 0 )
280             {
281                 value = basedir + expression.substring( pathSeparator );
282             }
283             else
284             {
285                 logger.error( "Got expression '" + expression + "' that was not recognised" );
286             }
287         }
288
289         if ( value == null )
290         {
291             // Check POM-level properties before we default over to system properties.
292
if ( project != null && project.getProperties() != null )
293             {
294                 value = project.getProperties().getProperty( expression );
295             }
296
297             if ( value == null && properties != null )
298             {
299                 // We will attempt to get nab a system property as a way to specify a
300
// parameter to a plugins. My particular case here is allowing the surefire
301
// plugin to run a single test so I want to specify that class on the cli
302
// as a parameter.
303

304                 value = properties.getProperty( expression );
305             }
306         }
307
308         if ( value instanceof String JavaDoc )
309         {
310             // TODO: without #, this could just be an evaluate call...
311

312             String JavaDoc val = (String JavaDoc) value;
313
314             int exprStartDelimiter = val.indexOf( "${" );
315
316             if ( exprStartDelimiter >= 0 )
317             {
318                 if ( exprStartDelimiter > 0 )
319                 {
320                     value = val.substring( 0, exprStartDelimiter ) + evaluate( val.substring( exprStartDelimiter ) );
321                 }
322                 else
323                 {
324                     value = evaluate( val.substring( exprStartDelimiter ) );
325                 }
326             }
327         }
328
329         return value;
330     }
331
332     private String JavaDoc stripTokens( String JavaDoc expr )
333     {
334         if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 )
335         {
336             expr = expr.substring( 2, expr.length() - 1 );
337         }
338         return expr;
339     }
340
341     public File JavaDoc alignToBaseDirectory( File JavaDoc file )
342     {
343         File JavaDoc basedir;
344
345         if ( project != null && project.getFile() != null )
346         {
347             basedir = project.getFile().getParentFile();
348         }
349         else
350         {
351             basedir = new File JavaDoc( "." ).getAbsoluteFile().getParentFile();
352         }
353
354         return new File JavaDoc( pathTranslator.alignToBaseDirectory( file.getPath(), basedir ) );
355     }
356
357 }
358
Popular Tags