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 public class AntTargetConverter
46 extends AbstractConfigurationConverter
47 {
48 public static final String MAVEN_EXPRESSION_EVALUATOR_ID = "maven.expressionEvaluator";
49
50 public static final String ROLE = ConfigurationConverter.class.getName();
51
52
53
54
55 public boolean canConvert( Class type )
56 {
57 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 Object retValue = fromExpression( configuration, expressionEvaluator, type );
69 if ( retValue != null )
70 {
71 return retValue;
72 }
73
74 Class implementation = getClassForImplementationHint( type, configuration, classLoader );
75
76 retValue = instantiateObject( implementation );
77
78 if ( !( retValue instanceof Target ) )
79 {
80 retValue = new Target();
81 }
82
83 processConfiguration( (Target) retValue, configuration, expressionEvaluator );
84
85 return retValue;
86 }
87
88 private void processConfiguration( Target target, PlexusConfiguration configuration,
89 ExpressionEvaluator expressionEvaluator )
90 throws ComponentConfigurationException
91 {
92 Project project = new Project();
93 project.setName( "DummyProject" );
94
95 target.setName( "" );
96 target.setProject( project );
97 String[] attributeNames = configuration.getAttributeNames();
98 for ( int i = 0; i < attributeNames.length; i++ )
99 {
100 String attributeName = attributeNames[i];
101 String attributeValue = configuration.getAttribute( attributeNames[i], null );
102
103 addAttributes( target, attributeName, attributeValue );
104 }
105 project.addTarget( target );
106
107 project.addReference( MAVEN_EXPRESSION_EVALUATOR_ID, expressionEvaluator );
108
109 initDefinitions( project, target );
110
111 processConfiguration( null, project, target, configuration );
112
113 project.init();
114 }
115
116 private void processConfiguration( RuntimeConfigurable parentWrapper, Project project, Target target,
117 PlexusConfiguration configuration )
118 throws ComponentConfigurationException
119 {
120 int items = configuration.getChildCount();
121
122 Object parent = parentWrapper == null ? null : parentWrapper.getProxy();
123
124 for ( int i = 0; i < items; i++ )
125 {
126 PlexusConfiguration childConfiguration = configuration.getChild( i );
127 UnknownElement task = new UnknownElement( childConfiguration.getName() );
128 task.setProject( project );
129 task.setNamespace( "" );
130 task.setQName( childConfiguration.getName() );
131 task.setTaskType( ProjectHelper.genComponentName( task.getNamespace(), childConfiguration.getName() ) );
132 task.setTaskName( childConfiguration.getName() );
133 task.setOwningTarget( target );
134 task.init();
135
136 if ( parent != null )
137 {
138 ( (UnknownElement) parent ).addChild( task );
139 }
140 else
141 {
142 target.addTask( task );
143 }
144
145 RuntimeConfigurable wrapper = new RuntimeConfigurable( task, task.getTaskName() );
146
147 try
148 {
149 if ( childConfiguration.getValue() != null )
150 {
151 wrapper.addText( childConfiguration.getValue() );
152 }
153 }
154 catch ( PlexusConfigurationException e )
155 {
156 throw new ComponentConfigurationException( "Error reading text value from element '"
157 + childConfiguration.getName() + "'", e );
158 }
159
160 String[] attrNames = childConfiguration.getAttributeNames();
161
162 for ( int a = 0; a < attrNames.length; a++ )
163 {
164 try
165 {
166 String v = childConfiguration.getAttribute( attrNames[a] );
167 wrapper.setAttribute( attrNames[a], v );
168 }
169 catch ( PlexusConfigurationException e )
170 {
171 throw new ComponentConfigurationException( "Error getting attribute '" + attrNames[a]
172 + "' of tag '" + childConfiguration.getName() + "'", e );
173 }
174 }
175
176 if ( parentWrapper != null )
177 {
178 parentWrapper.addChild( wrapper );
179 }
180
181 processConfiguration( wrapper, project, target, childConfiguration );
182 }
183 }
184
185 protected void initDefinitions( Project project, Target unused )
186 {
187 ComponentHelper componentHelper = ComponentHelper.getComponentHelper( project );
188
189 componentHelper.initDefaultDefinitions();
190 }
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 if ( StringUtils.isEmpty( attributeName ) )
207 {
208 return;
209 }
210 if ( StringUtils.isEmpty( attributeValue ) )
211 {
212 return;
213 }
214
215 if ( attributeName.toLowerCase().equals( "name" ) )
216 {
217 tasks.setName( attributeValue );
218 }
219 if ( attributeName.toLowerCase().equals( "unless" ) )
220 {
221 tasks.setUnless( attributeValue );
222 }
223 if ( attributeName.toLowerCase().equals( "description" ) )
224 {
225 tasks.setDescription( attributeValue );
226 }
227 if ( attributeName.toLowerCase().equals( "if" ) )
228 {
229 tasks.setIf( attributeValue );
230 }
231 }
232 }