1 | |
package org.apache.maven.plugin.antrun.components; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.tools.ant.ComponentHelper; |
23 | |
import org.apache.tools.ant.Project; |
24 | |
import org.apache.tools.ant.ProjectHelper; |
25 | |
import org.apache.tools.ant.RuntimeConfigurable; |
26 | |
import org.apache.tools.ant.Target; |
27 | |
import org.apache.tools.ant.UnknownElement; |
28 | |
import org.codehaus.plexus.component.configurator.ComponentConfigurationException; |
29 | |
import org.codehaus.plexus.component.configurator.ConfigurationListener; |
30 | |
import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter; |
31 | |
import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter; |
32 | |
import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup; |
33 | |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; |
34 | |
import org.codehaus.plexus.configuration.PlexusConfiguration; |
35 | |
import org.codehaus.plexus.configuration.PlexusConfigurationException; |
36 | |
import org.codehaus.plexus.util.StringUtils; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 3 | public class AntTargetConverter |
46 | |
extends AbstractConfigurationConverter |
47 | |
{ |
48 | |
public static final String MAVEN_EXPRESSION_EVALUATOR_ID = "maven.expressionEvaluator"; |
49 | |
|
50 | 3 | public static final String ROLE = ConfigurationConverter.class.getName(); |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public boolean canConvert( Class type ) |
56 | |
{ |
57 | 12 | return Target.class.isAssignableFrom( type ); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public Object fromConfiguration( ConverterLookup converterLookup, PlexusConfiguration configuration, Class type, |
64 | |
Class baseType, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator, |
65 | |
ConfigurationListener listener ) |
66 | |
throws ComponentConfigurationException |
67 | |
{ |
68 | 3 | Object retValue = fromExpression( configuration, expressionEvaluator, type ); |
69 | 3 | if ( retValue != null ) |
70 | |
{ |
71 | 0 | return retValue; |
72 | |
} |
73 | |
|
74 | 3 | Class implementation = getClassForImplementationHint( type, configuration, classLoader ); |
75 | |
|
76 | 3 | retValue = instantiateObject( implementation ); |
77 | |
|
78 | 3 | if ( !( retValue instanceof Target ) ) |
79 | |
{ |
80 | 3 | retValue = new Target(); |
81 | |
} |
82 | |
|
83 | 3 | processConfiguration( (Target) retValue, configuration, expressionEvaluator ); |
84 | |
|
85 | 3 | return retValue; |
86 | |
} |
87 | |
|
88 | |
private void processConfiguration( Target target, PlexusConfiguration configuration, |
89 | |
ExpressionEvaluator expressionEvaluator ) |
90 | |
throws ComponentConfigurationException |
91 | |
{ |
92 | 3 | Project project = new Project(); |
93 | 3 | project.setName( "DummyProject" ); |
94 | |
|
95 | 3 | target.setName( "" ); |
96 | 3 | target.setProject( project ); |
97 | 3 | String[] attributeNames = configuration.getAttributeNames(); |
98 | 8 | for ( int i = 0; i < attributeNames.length; i++ ) |
99 | |
{ |
100 | 5 | String attributeName = attributeNames[i]; |
101 | 5 | String attributeValue = configuration.getAttribute( attributeNames[i], null ); |
102 | |
|
103 | 5 | addAttributes( target, attributeName, attributeValue ); |
104 | |
} |
105 | 3 | project.addTarget( target ); |
106 | |
|
107 | 3 | project.addReference( MAVEN_EXPRESSION_EVALUATOR_ID, expressionEvaluator ); |
108 | |
|
109 | 3 | initDefinitions( project, target ); |
110 | |
|
111 | 3 | processConfiguration( null, project, target, configuration ); |
112 | |
|
113 | 3 | project.init(); |
114 | 3 | } |
115 | |
|
116 | |
private void processConfiguration( RuntimeConfigurable parentWrapper, Project project, Target target, |
117 | |
PlexusConfiguration configuration ) |
118 | |
throws ComponentConfigurationException |
119 | |
{ |
120 | 6 | int items = configuration.getChildCount(); |
121 | |
|
122 | 6 | Object parent = parentWrapper == null ? null : parentWrapper.getProxy(); |
123 | |
|
124 | 9 | for ( int i = 0; i < items; i++ ) |
125 | |
{ |
126 | 3 | PlexusConfiguration childConfiguration = configuration.getChild( i ); |
127 | 3 | UnknownElement task = new UnknownElement( childConfiguration.getName() ); |
128 | 3 | task.setProject( project ); |
129 | 3 | task.setNamespace( "" ); |
130 | 3 | task.setQName( childConfiguration.getName() ); |
131 | 3 | task.setTaskType( ProjectHelper.genComponentName( task.getNamespace(), childConfiguration.getName() ) ); |
132 | 3 | task.setTaskName( childConfiguration.getName() ); |
133 | 3 | task.setOwningTarget( target ); |
134 | 3 | task.init(); |
135 | |
|
136 | 3 | if ( parent != null ) |
137 | |
{ |
138 | 0 | ( (UnknownElement) parent ).addChild( task ); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 3 | target.addTask( task ); |
143 | |
} |
144 | |
|
145 | 3 | RuntimeConfigurable wrapper = new RuntimeConfigurable( task, task.getTaskName() ); |
146 | |
|
147 | |
try |
148 | |
{ |
149 | 3 | if ( childConfiguration.getValue() != null ) |
150 | |
{ |
151 | 3 | wrapper.addText( childConfiguration.getValue() ); |
152 | |
} |
153 | |
} |
154 | 0 | catch ( PlexusConfigurationException e ) |
155 | |
{ |
156 | 0 | throw new ComponentConfigurationException( "Error reading text value from element '" |
157 | |
+ childConfiguration.getName() + "'", e ); |
158 | 3 | } |
159 | |
|
160 | 3 | String[] attrNames = childConfiguration.getAttributeNames(); |
161 | |
|
162 | 4 | for ( int a = 0; a < attrNames.length; a++ ) |
163 | |
{ |
164 | |
try |
165 | |
{ |
166 | 1 | String v = childConfiguration.getAttribute( attrNames[a] ); |
167 | 1 | wrapper.setAttribute( attrNames[a], v ); |
168 | |
} |
169 | 0 | catch ( PlexusConfigurationException e ) |
170 | |
{ |
171 | 0 | throw new ComponentConfigurationException( "Error getting attribute '" + attrNames[a] |
172 | |
+ "' of tag '" + childConfiguration.getName() + "'", e ); |
173 | 1 | } |
174 | |
} |
175 | |
|
176 | 3 | if ( parentWrapper != null ) |
177 | |
{ |
178 | 0 | parentWrapper.addChild( wrapper ); |
179 | |
} |
180 | |
|
181 | 3 | processConfiguration( wrapper, project, target, childConfiguration ); |
182 | |
} |
183 | 6 | } |
184 | |
|
185 | |
protected void initDefinitions( Project project, Target unused ) |
186 | |
{ |
187 | 3 | ComponentHelper componentHelper = ComponentHelper.getComponentHelper( project ); |
188 | |
|
189 | 3 | componentHelper.initDefaultDefinitions(); |
190 | 3 | } |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
private static void addAttributes( Target tasks, String attributeName, String attributeValue ) |
205 | |
{ |
206 | 5 | if ( StringUtils.isEmpty( attributeName ) ) |
207 | |
{ |
208 | 0 | return; |
209 | |
} |
210 | 5 | if ( StringUtils.isEmpty( attributeValue ) ) |
211 | |
{ |
212 | 0 | return; |
213 | |
} |
214 | |
|
215 | 5 | if ( attributeName.toLowerCase().equals( "name" ) ) |
216 | |
{ |
217 | 0 | tasks.setName( attributeValue ); |
218 | |
} |
219 | 5 | if ( attributeName.toLowerCase().equals( "unless" ) ) |
220 | |
{ |
221 | 2 | tasks.setUnless( attributeValue ); |
222 | |
} |
223 | 5 | if ( attributeName.toLowerCase().equals( "description" ) ) |
224 | |
{ |
225 | 0 | tasks.setDescription( attributeValue ); |
226 | |
} |
227 | 5 | if ( attributeName.toLowerCase().equals( "if" ) ) |
228 | |
{ |
229 | 0 | tasks.setIf( attributeValue ); |
230 | |
} |
231 | 5 | } |
232 | |
} |