Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MappingUtils |
|
| 1.2;1,2 | ||||
MappingUtils$PropertiesInterpolationValueSource |
|
| 1.2;1,2 |
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.java 947587 2010-05-24 10:17:47Z dennisl $ | |
48 | */ | |
49 | 0 | 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 | 118 | String value = expression; |
63 | ||
64 | // FIXME: This is BAD! Accessors SHOULD NOT change the behavior of the object. | |
65 | 118 | artifact.isSnapshot(); |
66 | ||
67 | 118 | RegexBasedInterpolator interpolator = new RegexBasedInterpolator( "\\@\\{(", ")?([^}]+)\\}@" ); |
68 | 118 | interpolator.addValueSource( new ObjectBasedValueSource( artifact ) ); |
69 | 118 | interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) ); |
70 | ||
71 | 118 | Properties classifierMask = new Properties(); |
72 | 118 | classifierMask.setProperty( "classifier", "" ); |
73 | ||
74 | // Support for special expressions, like @{dashClassifier?}@, see MWAR-212 | |
75 | 118 | String classifier = artifact.getClassifier(); |
76 | 118 | if ( classifier != null ) |
77 | { | |
78 | 7 | classifierMask.setProperty( "dashClassifier?", "-" + classifier ); |
79 | 7 | classifierMask.setProperty( "dashClassifier", "-" + classifier ); |
80 | } | |
81 | else | |
82 | { | |
83 | 111 | classifierMask.setProperty( "dashClassifier?", "" ); |
84 | 111 | classifierMask.setProperty( "dashClassifier", "" ); |
85 | } | |
86 | ||
87 | 118 | interpolator.addValueSource( new PropertiesBasedValueSource ( classifierMask ) ); |
88 | ||
89 | 118 | value = interpolator.interpolate( value, "__artifact" ); |
90 | ||
91 | 118 | return value; |
92 | } | |
93 | ||
94 | ||
95 | /** | |
96 | * Internal implementation of {@link ValueSource} | |
97 | */ | |
98 | 0 | static class PropertiesInterpolationValueSource |
99 | implements ValueSource | |
100 | { | |
101 | ||
102 | private final Properties properties; | |
103 | ||
104 | public PropertiesInterpolationValueSource( Properties properties ) | |
105 | 0 | { |
106 | 0 | this.properties = properties; |
107 | 0 | } |
108 | ||
109 | public Object getValue( String key ) | |
110 | { | |
111 | 0 | return properties.getProperty( key ); |
112 | } | |
113 | ||
114 | public void clearFeedback() | |
115 | { | |
116 | // nothing here | |
117 | ||
118 | 0 | } |
119 | ||
120 | public List getFeedback() | |
121 | { | |
122 | // nothing here just NPE free | |
123 | 0 | return Collections.EMPTY_LIST; |
124 | } | |
125 | ||
126 | } | |
127 | ||
128 | } |