View Javadoc

1   package org.alfresco.maven.plugin.amp.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 org.apache.maven.artifact.Artifact;
23  import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource;
24  import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
25  import org.codehaus.plexus.util.interpolation.ValueSource;
26  
27  import java.util.Properties;
28  
29  /***
30   * Utilities used to evaluate expression.
31   * <p/>
32   * TODO: this comes from the assembly plugin; refactor when it's shared.
33   * <p/>
34   * The expression might use any fied of the {@link Artifact} interface. Some
35   * examples might be:
36   * <ul>
37   * <li>${artifactId}-${version}.${extension}</li>
38   * <li>${artifactId}.${extension}</li>
39   * </ul>
40   *
41   * @author Stephane Nicoll
42   */
43  public class MappingUtils
44  {
45  
46      /***
47       * Evaluates the specified expression for the given artifact.
48       *
49       * @param expression the expression to evaluate
50       * @param artifact   the artifact to use as value object for tokens
51       * @return expression the evaluated expression
52       */
53      public static String evaluateFileNameMapping( String expression, Artifact artifact )
54  
55      {
56          String value = expression;
57  
58          // FIXME: This is BAD! Accessors SHOULD NOT change the behavior of the object.
59          artifact.isSnapshot();
60  
61          RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
62  
63          interpolator.addValueSource( new ObjectBasedValueSource( artifact ) );
64          interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) );
65  
66          Properties classifierMask = new Properties();
67          classifierMask.setProperty( "classifier", "" );
68  
69          interpolator.addValueSource( new PropertiesInterpolationValueSource( classifierMask ) );
70  
71          value = interpolator.interpolate( value, "__artifact" );
72  
73          return value;
74      }
75  
76  
77      static class PropertiesInterpolationValueSource
78          implements ValueSource
79      {
80  
81          private final Properties properties;
82  
83          public PropertiesInterpolationValueSource( Properties properties )
84          {
85              this.properties = properties;
86          }
87  
88          public Object getValue( String key )
89          {
90              return properties.getProperty( key );
91          }
92  
93      }
94  
95  }