KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > tools > model > PluginMetadataParser


1 package org.apache.maven.plugin.tools.model;
2
3 import org.apache.maven.plugin.descriptor.DuplicateParameterException;
4 import org.apache.maven.plugin.descriptor.MojoDescriptor;
5 import org.apache.maven.plugin.descriptor.Parameter;
6 import org.apache.maven.plugin.tools.model.io.xpp3.PluginMetadataXpp3Reader;
7 import org.codehaus.plexus.component.repository.ComponentRequirement;
8 import org.codehaus.plexus.util.IOUtil;
9 import org.codehaus.plexus.util.StringUtils;
10 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
11
12 import java.io.File JavaDoc;
13 import java.io.FileReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 public class PluginMetadataParser
22 {
23     public static final String JavaDoc IMPL_BASE_PLACEHOLDER = "<REPLACE-WITH-MOJO-PATH>";
24     
25     public Set JavaDoc parseMojoDescriptors( File JavaDoc metadataFile )
26         throws PluginMetadataParseException
27     {
28         Set JavaDoc descriptors = new HashSet JavaDoc();
29         
30         Reader reader = null;
31         
32         try
33         {
34             reader = new FileReader JavaDoc( metadataFile );
35             
36             PluginMetadataXpp3Reader metadataReader = new PluginMetadataXpp3Reader();
37             
38             PluginMetadata pluginMetadata = metadataReader.read( reader );
39             
40             List JavaDoc mojos = pluginMetadata.getMojos();
41             
42             if ( mojos != null && !mojos.isEmpty() )
43             {
44                 for ( Iterator JavaDoc it = mojos.iterator(); it.hasNext(); )
45                 {
46                     Mojo mojo = (Mojo) it.next();
47                     
48                     MojoDescriptor descriptor = asDescriptor( metadataFile, mojo );
49                     
50                     descriptors.add( descriptor );
51                 }
52             }
53         }
54         catch ( IOException JavaDoc e )
55         {
56             throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
57         }
58         catch ( XmlPullParserException e )
59         {
60             throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
61         }
62         finally
63         {
64             IOUtil.close( reader );
65         }
66         
67         return descriptors;
68     }
69
70     private MojoDescriptor asDescriptor( File JavaDoc metadataFile, Mojo mojo )
71         throws PluginMetadataParseException
72     {
73         MojoDescriptor descriptor = new MojoDescriptor();
74         
75         descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
76         
77         descriptor.setGoal( mojo.getGoal() );
78         descriptor.setPhase( mojo.getPhase() );
79         descriptor.setDependencyResolutionRequired( mojo.getRequiresDependencyResolution() );
80         descriptor.setAggregator( mojo.isAggregator() );
81         descriptor.setInheritedByDefault( mojo.isInheritByDefault() );
82         descriptor.setDirectInvocationOnly( mojo.isRequiresDirectInvocation() );
83         descriptor.setOnlineRequired( mojo.isRequiresOnline() );
84         descriptor.setProjectRequired( mojo.isRequiresProject() );
85         descriptor.setRequiresReports( mojo.isRequiresReports() );
86         descriptor.setDescription( mojo.getDescription() );
87         descriptor.setDeprecated( mojo.getDeprecation() );
88         
89         LifecycleExecution le = mojo.getExecution();
90         if ( le != null )
91         {
92             descriptor.setExecuteLifecycle( le.getLifecycle() );
93             descriptor.setExecutePhase( le.getPhase() );
94         }
95         
96         List JavaDoc parameters = mojo.getParameters();
97         
98         if ( parameters != null && !parameters.isEmpty() )
99         {
100             for ( Iterator JavaDoc it = parameters.iterator(); it.hasNext(); )
101             {
102                 org.apache.maven.plugin.tools.model.Parameter param = (org.apache.maven.plugin.tools.model.Parameter) it.next();
103                 
104                 Parameter dParam = new Parameter();
105                 dParam.setAlias( param.getAlias() );
106                 dParam.setDeprecated( param.getDeprecation() );
107                 dParam.setDescription( param.getDescription() );
108                 dParam.setEditable( !param.isReadonly() );
109                 dParam.setExpression( param.getExpression() );
110                 dParam.setDefaultValue( param.getDefaultValue() );
111                 
112                 String JavaDoc property = param.getProperty();
113                 if ( StringUtils.isNotEmpty( property ) )
114                 {
115                     dParam.setName( property );
116                 }
117                 else
118                 {
119                     dParam.setName( param.getName() );
120                 }
121                 
122                 if ( StringUtils.isEmpty( dParam.getName() ) )
123                 {
124                     throw new PluginMetadataParseException( metadataFile, "Mojo: \'" + mojo.getGoal() + "\' has a parameter without either property or name attributes. Please specify one." );
125                 }
126
127                 dParam.setRequired( param.isRequired() );
128                 dParam.setType( param.getType() );
129                 
130                 try
131                 {
132                     descriptor.addParameter( dParam );
133                 }
134                 catch ( DuplicateParameterException e )
135                 {
136                     throw new PluginMetadataParseException( metadataFile, "Duplicate parameters detected for mojo: " + mojo.getGoal(), e );
137                 }
138             }
139         }
140         
141         List JavaDoc components = mojo.getComponents();
142         
143         if ( components != null && !components.isEmpty() )
144         {
145             for ( Iterator JavaDoc it = components.iterator(); it.hasNext(); )
146             {
147                 Component component = (Component) it.next();
148                 
149                 ComponentRequirement cr = new ComponentRequirement();
150                 cr.setRole( component.getRole() );
151                 cr.setRoleHint( component.getHint() );
152                 
153                 descriptor.addRequirement( cr );
154             }
155         }
156         
157         return descriptor;
158     }
159
160 }
161
Popular Tags