KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > tools > plugin > extractor > beanshell > BeanshellMojoDescriptorExtractor


1 package org.apache.maven.tools.plugin.extractor.beanshell;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import bsh.EvalError;
20 import bsh.Interpreter;
21
22 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
23 import org.apache.maven.plugin.descriptor.MojoDescriptor;
24 import org.apache.maven.plugin.descriptor.PluginDescriptor;
25 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
26 import org.apache.maven.tools.plugin.extractor.ExtractionException;
27
28 import java.io.File JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 /**
37  * @todo share constants
38  * @todo add example usage tag that can be shown in the doco
39  * @todo need to add validation directives so that systems embedding maven2 can
40  * get validation directives to help users in IDEs.
41  */

42 public class BeanshellMojoDescriptorExtractor
43     extends AbstractScriptedMojoDescriptorExtractor
44 {
45     private MojoDescriptor createMojoDescriptor( String JavaDoc basedir, String JavaDoc resource, PluginDescriptor pluginDescriptor )
46         throws InvalidPluginDescriptorException
47     {
48         MojoDescriptor mojoDescriptor = new MojoDescriptor();
49         mojoDescriptor.setPluginDescriptor( pluginDescriptor );
50
51         mojoDescriptor.setLanguage( "bsh" );
52         mojoDescriptor.setComponentConfigurator( "bsh" );
53
54         mojoDescriptor.setImplementation( resource );
55
56         Interpreter interpreter = new Interpreter();
57
58         try
59         {
60             interpreter.set( "file", new File JavaDoc( basedir, resource ) );
61
62             interpreter.set( "mojoDescriptor", mojoDescriptor );
63
64             interpreter.eval( new InputStreamReader JavaDoc( getClass().getResourceAsStream( "/extractor.bsh" ) ) );
65         }
66         catch ( EvalError evalError )
67         {
68             throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
69         }
70
71         return mojoDescriptor;
72     }
73
74     protected String JavaDoc getScriptFileExtension()
75     {
76         return ".bsh";
77     }
78
79     protected List JavaDoc extractMojoDescriptors( Map JavaDoc scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
80         throws ExtractionException, InvalidPluginDescriptorException
81     {
82         List JavaDoc descriptors = new ArrayList JavaDoc();
83
84         for ( Iterator JavaDoc mapIterator = scriptFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); )
85         {
86             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) mapIterator.next();
87
88             String JavaDoc basedir = (String JavaDoc) entry.getKey();
89             Set JavaDoc metadataFiles = (Set JavaDoc) entry.getValue();
90
91             for ( Iterator JavaDoc it = metadataFiles.iterator(); it.hasNext(); )
92             {
93                 File JavaDoc scriptFile = (File JavaDoc) it.next();
94
95                 String JavaDoc relativePath = null;
96
97                 if ( basedir.endsWith( "/" ) )
98                 {
99                     basedir = basedir.substring( 0, basedir.length() - 2 );
100                 }
101
102                 relativePath = scriptFile.getPath().substring( basedir.length() );
103
104                 relativePath = relativePath.replace( '\\', '/' );
105
106                 MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, pluginDescriptor );
107                 descriptors.add( mojoDescriptor );
108             }
109         }
110
111         return descriptors;
112     }
113 }
Popular Tags