KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > tools > plugin > extractor > ant > AntMojoDescriptorExtractor


1 package org.apache.maven.tools.plugin.extractor.ant;
2
3 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
4 import org.apache.maven.plugin.descriptor.MojoDescriptor;
5 import org.apache.maven.plugin.descriptor.Parameter;
6 import org.apache.maven.plugin.descriptor.PluginDescriptor;
7 import org.apache.maven.plugin.tools.model.PluginMetadataParseException;
8 import org.apache.maven.plugin.tools.model.PluginMetadataParser;
9 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
10 import org.apache.maven.tools.plugin.extractor.ExtractionException;
11 import org.codehaus.plexus.util.StringUtils;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 public class AntMojoDescriptorExtractor
21     extends AbstractScriptedMojoDescriptorExtractor
22 {
23
24     private static final String JavaDoc METADATA_FILE_EXTENSION = ".mojos.xml";
25
26     private static final String JavaDoc SCRIPT_FILE_EXTENSION = ".build.xml";
27
28     protected List JavaDoc extractMojoDescriptorsFromMetadata( Map JavaDoc metadataFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
29         throws ExtractionException, InvalidPluginDescriptorException
30     {
31         List JavaDoc descriptors = new ArrayList JavaDoc();
32
33         PluginMetadataParser parser = new PluginMetadataParser();
34
35         for ( Iterator JavaDoc mapIterator = metadataFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); )
36         {
37             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) mapIterator.next();
38
39             String JavaDoc basedir = (String JavaDoc) entry.getKey();
40             Set JavaDoc metadataFiles = (Set JavaDoc) entry.getValue();
41
42             for ( Iterator JavaDoc it = metadataFiles.iterator(); it.hasNext(); )
43             {
44                 File JavaDoc metadataFile = (File JavaDoc) it.next();
45
46                 String JavaDoc basename = metadataFile.getName();
47                 basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() );
48
49                 File JavaDoc scriptFile = new File JavaDoc( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION );
50
51                 if ( !scriptFile.exists() )
52                 {
53                     throw new InvalidPluginDescriptorException( "Found orphaned plugin metadata file: "
54                         + metadataFile );
55                 }
56
57                 String JavaDoc relativePath = null;
58
59                 if ( basedir.endsWith( "/" ) )
60                 {
61                     basedir = basedir.substring( 0, basedir.length() - 2 );
62                 }
63
64                 relativePath = scriptFile.getPath().substring( basedir.length() );
65
66                 relativePath = relativePath.replace( '\\', '/' );
67
68                 try
69                 {
70                     Set JavaDoc mojoDescriptors = parser.parseMojoDescriptors( metadataFile );
71
72                     for ( Iterator JavaDoc discoveredMojoIterator = mojoDescriptors.iterator(); discoveredMojoIterator
73                         .hasNext(); )
74                     {
75                         MojoDescriptor descriptor = (MojoDescriptor) discoveredMojoIterator.next();
76
77                         Map JavaDoc paramMap = descriptor.getParameterMap();
78
79                         if ( !paramMap.containsKey( "basedir" ) )
80                         {
81                             Parameter param = new Parameter();
82                             param.setName( "basedir" );
83                             param.setAlias( "ant.basedir" );
84                             param.setExpression( "${antBasedir}" );
85                             param.setDefaultValue( "${basedir}" );
86                             param.setType( "java.io.File" );
87                             param.setDescription( "The base directory from which to execute the Ant script." );
88                             param.setEditable( true );
89                             param.setRequired( true );
90
91                             descriptor.addParameter( param );
92                         }
93
94                         if ( !paramMap.containsKey( "antMessageLevel" ) )
95                         {
96                             Parameter param = new Parameter();
97                             param.setName( "messageLevel" );
98                             param.setAlias( "ant.messageLevel" );
99                             param.setExpression( "${antMessageLevel}" );
100                             param.setDefaultValue( "info" );
101                             param.setType( "java.lang.String" );
102                             param.setDescription( "The message-level used to tune the verbosity of Ant logging." );
103                             param.setEditable( true );
104                             param.setRequired( false );
105
106                             descriptor.addParameter( param );
107                         }
108
109                         String JavaDoc implementation = relativePath;
110
111                         String JavaDoc dImpl = descriptor.getImplementation();
112                         if ( StringUtils.isNotEmpty( dImpl ) )
113                         {
114                             implementation = relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() );
115                         }
116                         
117                         descriptor.setImplementation( implementation );
118
119                         descriptor.setLanguage( "ant-mojo" );
120                         descriptor.setComponentComposer( "map-oriented" );
121                         descriptor.setComponentConfigurator( "map-oriented" );
122
123                         descriptor.setPluginDescriptor( pluginDescriptor );
124
125                         descriptors.add( descriptor );
126                     }
127                 }
128                 catch ( PluginMetadataParseException e )
129                 {
130                     throw new ExtractionException( "Error extracting mojo descriptor from script: "
131                         + metadataFile, e );
132                 }
133             }
134         }
135
136         return descriptors;
137     }
138
139     protected String JavaDoc getScriptFileExtension()
140     {
141         return SCRIPT_FILE_EXTENSION;
142     }
143     
144     protected String JavaDoc getMetadataFileExtension()
145     {
146         return METADATA_FILE_EXTENSION;
147     }
148 }
149
Popular Tags