KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > tools > AbstractQDoxMojo


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

19 package org.apache.avalon.fortress.tools;
20
21 import java.io.File JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.maven.model.FileSet;
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.codehaus.plexus.util.DirectoryScanner;
32
33 import com.thoughtworks.qdox.JavaDocBuilder;
34 import com.thoughtworks.qdox.model.DefaultDocletTagFactory;
35 import com.thoughtworks.qdox.model.DocletTagFactory;
36 import com.thoughtworks.qdox.model.JavaClass;
37 import com.thoughtworks.qdox.model.JavaSource;
38
39 /**
40  * An abstract class that provides the common logic of getting directory and
41  * file-set configuration information from Maven before creating the list of classes
42  * and source files that will be parsed by QDox.
43  *
44  * Classes extending this class must call the <code>execute()</code> method, after which
45  * the <code>allSources</code> and <code>allClasses</code> fields are filled in.
46  *
47  * @author <a HREF="mailto:dev@excalibur.apache.org">The Excalibur Team</a>
48  * @version CVS $Revision: 1.1 $ $Date: 2005/12/25 08:29:44 $
49  */

50 public class AbstractQDoxMojo extends AbstractMojo
51 {
52
53     /**
54      * The directory where the generated directories and files are placed
55      * @parameter expression="${project.build.outputDirectory}"
56      */

57     private File JavaDoc destDir;
58
59     /**
60      * Define the set of directory and file name inclusion/exclusion patterns. If ommitted,
61      * a default fileset is constructed using the baseDir and defaultIncludes configuration
62      * @parameter
63      */

64     private FileSet[] filesets;
65
66     /**
67      * If filesets are not defined, a default one is created using this directory
68      * @parameter expression="${basedir}"
69      */

70     private String JavaDoc baseDir;
71
72     /**
73      * If filesets are not defined, a default one is created using this inclusion pattern
74      * @parameter expression="**\/*.java"
75      */

76     private String JavaDoc defaultIncludes;
77
78     protected HashMap JavaDoc fileMap = new HashMap JavaDoc();
79
80     protected ArrayList JavaDoc allSources = new ArrayList JavaDoc();
81
82     protected ArrayList JavaDoc allClasses = new ArrayList JavaDoc();
83
84     public void execute() throws MojoExecutionException, MojoFailureException
85     {
86         validateAttributes();
87         buildFileMap();
88         JavaDocBuilder builder = new JavaDocBuilder( createDocletTagFactory() );
89
90         // Add a classloader that has the taskdef's classpath.
91
builder.getClassLibrary().addClassLoader( getClass().getClassLoader() );
92         mergeBuilderSources( builder );
93         JavaSource[] sources = builder.getSources();
94         processSources( sources );
95     }
96
97     protected void buildFileMap()
98     {
99         for ( int i = 0; i < filesets.length; i++ )
100         {
101             FileSet fs = filesets[ i ];
102             DirectoryScanner ds = new DirectoryScanner();
103             ds.setBasedir( fs.getDirectory() );
104             if ( fs.getIncludes().size() > 0 )
105             {
106                 ds.setIncludes( (String JavaDoc[]) fs.getIncludes().toArray( new String JavaDoc[0] ) );
107             }
108             if ( fs.getExcludes().size() > 0 )
109             {
110                 ds.setExcludes( (String JavaDoc[]) fs.getExcludes().toArray( new String JavaDoc[0] ) );
111             }
112             ds.scan();
113             String JavaDoc[] srcFiles = ds.getIncludedFiles();
114             buildFileMap( new File JavaDoc( fs.getDirectory() ), srcFiles );
115         }
116     }
117
118     protected void buildFileMap( File JavaDoc directory, String JavaDoc[] sourceFiles )
119     {
120         for ( int i = 0; i < sourceFiles.length; i++ )
121         {
122             File JavaDoc src = new File JavaDoc( directory, sourceFiles[ i ] );
123             fileMap.put( src.getAbsolutePath(), src );
124         }
125     }
126
127     protected DocletTagFactory createDocletTagFactory()
128     {
129         return new DefaultDocletTagFactory();
130     }
131
132     private void mergeBuilderSources( JavaDocBuilder builder )
133     {
134         for ( Iterator JavaDoc iterator = fileMap.keySet().iterator(); iterator.hasNext(); )
135         {
136             String JavaDoc sourceFile = (String JavaDoc) iterator.next();
137             builder.addSourceTree( (File JavaDoc) fileMap.get( sourceFile ) );
138
139         }
140     }
141
142     protected void processSources( JavaSource[] sources )
143     {
144         for ( int i = 0; i < sources.length; i++ )
145         {
146             JavaSource source = sources[ i ];
147             allSources.add( source );
148             JavaClass[] classes = source.getClasses();
149             processClasses( classes );
150         }
151     }
152
153     protected void processClasses( JavaClass[] classes )
154     {
155         for ( int j = 0; j < classes.length; j++ )
156         {
157             JavaClass clazz = classes[ j ];
158             allClasses.add( clazz );
159         }
160     }
161
162     protected void validateAttributes() throws MojoExecutionException
163     {
164         if ( filesets == null || filesets.length == 0 )
165         {
166             filesets = createDefaultFileset();
167         }
168     }
169
170     protected FileSet[] createDefaultFileset()
171     {
172         FileSet[] defaultFileSet = new FileSet[] {new FileSet()};
173         List JavaDoc defaultPatternList = new ArrayList JavaDoc();
174         
175         defaultPatternList.add(defaultIncludes);
176         defaultFileSet[0].setIncludes(defaultPatternList);
177         defaultFileSet[0].setDirectory(baseDir);
178         
179         return defaultFileSet;
180     }
181
182     public File JavaDoc getDestDir()
183     {
184         return destDir;
185     }
186 }
187
Popular Tags