001: package org.apache.maven.plugin.descriptor;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.codehaus.plexus.component.repository.ComponentDependency;
023: import org.codehaus.plexus.component.repository.ComponentRequirement;
024: import org.codehaus.plexus.configuration.PlexusConfiguration;
025: import org.codehaus.plexus.configuration.PlexusConfigurationException;
026: import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
027: import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
028: import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
029:
030: import java.io.IOException;
031: import java.io.Reader;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
037: * @version $Id: PluginDescriptorBuilder.java 513701 2007-03-02 10:55:05Z kenney $
038: */
039: public class PluginDescriptorBuilder {
040: public PluginDescriptor build(Reader reader)
041: throws PlexusConfigurationException {
042: return build(reader, null);
043: }
044:
045: public PluginDescriptor build(Reader reader, String source)
046: throws PlexusConfigurationException {
047: PlexusConfiguration c = buildConfiguration(reader);
048:
049: PluginDescriptor pluginDescriptor = new PluginDescriptor();
050:
051: pluginDescriptor.setSource(source);
052: pluginDescriptor.setGroupId(c.getChild("groupId").getValue());
053: pluginDescriptor.setArtifactId(c.getChild("artifactId")
054: .getValue());
055: pluginDescriptor.setVersion(c.getChild("version").getValue());
056: pluginDescriptor.setGoalPrefix(c.getChild("goalPrefix")
057: .getValue());
058:
059: pluginDescriptor.setName(c.getChild("name").getValue());
060: pluginDescriptor.setDescription(c.getChild("description")
061: .getValue());
062:
063: String isolatedRealm = c.getChild("isolatedRealm").getValue();
064:
065: if (isolatedRealm != null) {
066: pluginDescriptor.setIsolatedRealm(Boolean.valueOf(
067: isolatedRealm).booleanValue());
068: }
069:
070: String inheritedByDefault = c.getChild("inheritedByDefault")
071: .getValue();
072:
073: if (inheritedByDefault != null) {
074: pluginDescriptor.setInheritedByDefault(Boolean.valueOf(
075: inheritedByDefault).booleanValue());
076: }
077:
078: // ----------------------------------------------------------------------
079: // Components
080: // ----------------------------------------------------------------------
081:
082: PlexusConfiguration[] mojoConfigurations = c.getChild("mojos")
083: .getChildren("mojo");
084:
085: for (int i = 0; i < mojoConfigurations.length; i++) {
086: PlexusConfiguration component = mojoConfigurations[i];
087:
088: MojoDescriptor mojoDescriptor = buildComponentDescriptor(
089: component, pluginDescriptor);
090:
091: pluginDescriptor.addMojo(mojoDescriptor);
092: }
093:
094: // ----------------------------------------------------------------------
095: // Dependencies
096: // ----------------------------------------------------------------------
097:
098: PlexusConfiguration[] dependencyConfigurations = c.getChild(
099: "dependencies").getChildren("dependency");
100:
101: List dependencies = new ArrayList();
102:
103: for (int i = 0; i < dependencyConfigurations.length; i++) {
104: PlexusConfiguration d = dependencyConfigurations[i];
105:
106: ComponentDependency cd = new ComponentDependency();
107:
108: cd.setArtifactId(d.getChild("artifactId").getValue());
109:
110: cd.setGroupId(d.getChild("groupId").getValue());
111:
112: cd.setType(d.getChild("type").getValue());
113:
114: cd.setVersion(d.getChild("version").getValue());
115:
116: dependencies.add(cd);
117: }
118:
119: pluginDescriptor.setDependencies(dependencies);
120:
121: return pluginDescriptor;
122: }
123:
124: public MojoDescriptor buildComponentDescriptor(
125: PlexusConfiguration c, PluginDescriptor pluginDescriptor)
126: throws PlexusConfigurationException {
127: MojoDescriptor mojo = new MojoDescriptor();
128: mojo.setPluginDescriptor(pluginDescriptor);
129:
130: mojo.setGoal(c.getChild("goal").getValue());
131:
132: mojo.setImplementation(c.getChild("implementation").getValue());
133:
134: PlexusConfiguration langConfig = c.getChild("language");
135:
136: if (langConfig != null) {
137: mojo.setLanguage(langConfig.getValue());
138: }
139:
140: PlexusConfiguration configuratorConfig = c
141: .getChild("configurator");
142:
143: if (configuratorConfig != null) {
144: mojo
145: .setComponentConfigurator(configuratorConfig
146: .getValue());
147: }
148:
149: PlexusConfiguration composerConfig = c.getChild("composer");
150:
151: if (composerConfig != null) {
152: mojo.setComponentComposer(composerConfig.getValue());
153: }
154:
155: String since = c.getChild("since").getValue();
156:
157: if (since != null) {
158: mojo.setSince(since);
159: }
160:
161: String phase = c.getChild("phase").getValue();
162:
163: if (phase != null) {
164: mojo.setPhase(phase);
165: }
166:
167: String executePhase = c.getChild("executePhase").getValue();
168:
169: if (executePhase != null) {
170: mojo.setExecutePhase(executePhase);
171: }
172:
173: String executeMojo = c.getChild("executeGoal").getValue();
174:
175: if (executeMojo != null) {
176: mojo.setExecuteGoal(executeMojo);
177: }
178:
179: String executeLifecycle = c.getChild("executeLifecycle")
180: .getValue();
181:
182: if (executeLifecycle != null) {
183: mojo.setExecuteLifecycle(executeLifecycle);
184: }
185:
186: mojo.setInstantiationStrategy(c.getChild(
187: "instantiationStrategy").getValue());
188:
189: mojo.setDescription(c.getChild("description").getValue());
190:
191: String dependencyResolution = c.getChild(
192: "requiresDependencyResolution").getValue();
193:
194: if (dependencyResolution != null) {
195: mojo.setDependencyResolutionRequired(dependencyResolution);
196: }
197:
198: String directInvocationOnly = c.getChild(
199: "requiresDirectInvocation").getValue();
200:
201: if (directInvocationOnly != null) {
202: mojo.setDirectInvocationOnly(Boolean.valueOf(
203: directInvocationOnly).booleanValue());
204: }
205:
206: String requiresProject = c.getChild("requiresProject")
207: .getValue();
208:
209: if (requiresProject != null) {
210: mojo.setProjectRequired(Boolean.valueOf(requiresProject)
211: .booleanValue());
212: }
213:
214: String requiresReports = c.getChild("requiresReports")
215: .getValue();
216:
217: if (requiresReports != null) {
218: mojo.setRequiresReports(Boolean.valueOf(requiresReports)
219: .booleanValue());
220: }
221:
222: String aggregator = c.getChild("aggregator").getValue();
223:
224: if (aggregator != null) {
225: mojo.setAggregator(Boolean.valueOf(aggregator)
226: .booleanValue());
227: }
228:
229: String requiresOnline = c.getChild("requiresOnline").getValue();
230:
231: if (requiresOnline != null) {
232: mojo.setOnlineRequired(Boolean.valueOf(requiresOnline)
233: .booleanValue());
234: }
235:
236: String inheritedByDefault = c.getChild("inheritedByDefault")
237: .getValue();
238:
239: if (inheritedByDefault != null) {
240: mojo.setInheritedByDefault(Boolean.valueOf(
241: inheritedByDefault).booleanValue());
242: }
243:
244: // ----------------------------------------------------------------------
245: // Parameters
246: // ----------------------------------------------------------------------
247:
248: PlexusConfiguration[] parameterConfigurations = c.getChild(
249: "parameters").getChildren("parameter");
250:
251: List parameters = new ArrayList();
252:
253: for (int i = 0; i < parameterConfigurations.length; i++) {
254: PlexusConfiguration d = parameterConfigurations[i];
255:
256: Parameter parameter = new Parameter();
257:
258: parameter.setName(d.getChild("name").getValue());
259:
260: parameter.setAlias(d.getChild("alias").getValue());
261:
262: parameter.setType(d.getChild("type").getValue());
263:
264: String required = d.getChild("required").getValue();
265:
266: parameter.setRequired(Boolean.valueOf(required)
267: .booleanValue());
268:
269: PlexusConfiguration editableConfig = d.getChild("editable");
270:
271: // we need the null check for pre-build legacy plugins...
272: if (editableConfig != null) {
273: String editable = d.getChild("editable").getValue();
274:
275: parameter.setEditable(editable == null
276: || Boolean.valueOf(editable).booleanValue());
277: }
278:
279: parameter.setDescription(d.getChild("description")
280: .getValue());
281:
282: parameter
283: .setDeprecated(d.getChild("deprecated").getValue());
284:
285: parameter.setImplementation(d.getChild("implementation")
286: .getValue());
287:
288: parameters.add(parameter);
289: }
290:
291: mojo.setParameters(parameters);
292:
293: // TODO: this should not need to be handed off...
294:
295: // ----------------------------------------------------------------------
296: // Configuration
297: // ----------------------------------------------------------------------
298:
299: mojo.setMojoConfiguration(c.getChild("configuration"));
300:
301: // TODO: Go back to this when we get the container ready to configure mojos...
302: // mojo.setConfiguration( c.getChild( "configuration" ) );
303:
304: // ----------------------------------------------------------------------
305: // Requirements
306: // ----------------------------------------------------------------------
307:
308: PlexusConfiguration[] requirements = c.getChild("requirements")
309: .getChildren("requirement");
310:
311: for (int i = 0; i < requirements.length; i++) {
312: PlexusConfiguration requirement = requirements[i];
313:
314: ComponentRequirement cr = new ComponentRequirement();
315:
316: cr.setRole(requirement.getChild("role").getValue());
317:
318: cr
319: .setRoleHint(requirement.getChild("role-hint")
320: .getValue());
321:
322: cr.setFieldName(requirement.getChild("field-name")
323: .getValue());
324:
325: mojo.addRequirement(cr);
326: }
327:
328: return mojo;
329: }
330:
331: // ----------------------------------------------------------------------
332: //
333: // ----------------------------------------------------------------------
334:
335: public PlexusConfiguration buildConfiguration(Reader configuration)
336: throws PlexusConfigurationException {
337: try {
338: return new XmlPlexusConfiguration(Xpp3DomBuilder
339: .build(configuration));
340: } catch (IOException e) {
341: throw new PlexusConfigurationException(
342: "Error creating configuration", e);
343: } catch (XmlPullParserException e) {
344: throw new PlexusConfigurationException(
345: "Error creating configuration", e);
346: }
347: }
348: }
|