View Javadoc

1   package org.apache.maven.plugin.war.util;
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  import java.util.Collections;
23  import java.util.List;
24  import java.util.Properties;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.codehaus.plexus.interpolation.InterpolationException;
28  import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
29  import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
30  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
31  import org.codehaus.plexus.interpolation.ValueSource;
32  
33  /**
34   * Utilities used to evaluate expression.
35   * <p/>
36   * TODO: this comes from the assembly plugin; refactor when it's shared.
37   * <p/>
38   * The expression might use any field of the {@link Artifact} interface. Some
39   * examples might be:
40   * <ul>
41   * <li>@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@</li>
42   * <li>@{artifactId}@-@{version}@.@{extension}@</li>
43   * <li>@{artifactId}@.@{extension}@</li>
44   * </ul>
45   *
46   * @author Stephane Nicoll
47   * @version $Id: MappingUtils.html 867902 2013-06-30 13:58:43Z olamy $
48   */
49  public class MappingUtils
50  {
51  
52      /**
53       * Evaluates the specified expression for the given artifact.
54       *
55       * @param expression the expression to evaluate
56       * @param artifact   the artifact to use as value object for tokens
57       * @return expression the evaluated expression
58       */
59      public static String evaluateFileNameMapping( String expression, Artifact artifact )
60          throws InterpolationException
61      {
62          String value = expression;
63  
64          // FIXME: This is BAD! Accessors SHOULD NOT change the behavior of the object.
65          artifact.isSnapshot();
66  
67          RegexBasedInterpolator interpolator = new RegexBasedInterpolator( "\\@\\{(", ")?([^}]+)\\}@" );
68          interpolator.addValueSource( new ObjectBasedValueSource( artifact ) );
69          interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) );
70  
71          Properties classifierMask = new Properties();
72          classifierMask.setProperty( "classifier", "" );
73  
74          // Support for special expressions, like @{dashClassifier?}@, see MWAR-212
75          String classifier = artifact.getClassifier();
76          if ( classifier != null )
77          {
78              classifierMask.setProperty( "dashClassifier?", "-" + classifier );
79              classifierMask.setProperty( "dashClassifier", "-" + classifier );
80          }
81          else
82          {
83              classifierMask.setProperty( "dashClassifier?", "" );
84              classifierMask.setProperty( "dashClassifier", "" );
85          }
86  
87          interpolator.addValueSource( new PropertiesBasedValueSource ( classifierMask ) );
88  
89          value = interpolator.interpolate( value, "__artifact" );
90  
91          return value;
92      }
93  
94  
95      /**
96       * Internal implementation of {@link ValueSource}
97       */
98      static class PropertiesInterpolationValueSource
99          implements ValueSource
100     {
101 
102         private final Properties properties;
103 
104         public PropertiesInterpolationValueSource( Properties properties )
105         {
106             this.properties = properties;
107         }
108 
109         public Object getValue( String key )
110         {
111             return properties.getProperty( key );
112         }
113 
114         public void clearFeedback()
115         {
116             // nothing here
117             
118         }
119 
120         public List getFeedback()
121         {
122             // nothing here just NPE free
123             return Collections.EMPTY_LIST;
124         }
125 
126     }
127 
128 }