View Javadoc
1   package org.apache.maven.jxr;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.jxr.ant.DirectoryScanner;
23  import org.apache.maven.jxr.log.Log;
24  import org.apache.maven.jxr.pacman.FileManager;
25  import org.apache.maven.jxr.pacman.PackageManager;
26  
27  import java.io.IOException;
28  import java.nio.file.Path;
29  import java.nio.file.Paths;
30  import java.util.List;
31  import java.util.Locale;
32  
33  /**
34   * Main entry point into Maven used to kick off the XReference code building.
35   *
36   * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
37   * @version $Id$
38   */
39  public class JXR
40  {
41      /**
42       * The Log.
43       */
44      private Log log;
45  
46      /**
47       * The default list of include patterns to use.
48       */
49      private static final String[] DEFAULT_INCLUDES = { "**/*.java" };
50  
51      /**
52       * Path to destination.
53       */
54      private Path destDir;
55  
56      private Locale locale;
57  
58      private String inputEncoding;
59  
60      private String outputEncoding;
61  
62      /**
63       * Relative path to javadocs, suitable for hyperlinking.
64       */
65      private Path javadocLinkDir;
66  
67      /**
68       * Handles taking .java files and changing them into html. "More than meets
69       * the eye!" :)
70       */
71      private JavaCodeTransform transformer;
72  
73      /**
74       * The revision of the module currently being processed.
75       */
76      private String revision;
77  
78      /**
79       * The list of exclude patterns to use.
80       */
81      private String[] excludes = null;
82  
83      /**
84       * The list of include patterns to use.
85       */
86      private String[] includes = DEFAULT_INCLUDES;
87  
88      /**
89       * Now that we have instantiated everything. Process this JXR task.
90       *
91       * @param packageManager
92       * @param sourceDir
93       * @param bottom
94       * @throws IOException
95       */
96      public void processPath( PackageManager packageManager, Path sourceDir, String bottom )
97          throws IOException
98      {
99          this.transformer = new JavaCodeTransform( packageManager );
100 
101         DirectoryScannerScanner.html#DirectoryScanner">DirectoryScanner ds = new DirectoryScanner();
102         // I'm not sure why we don't use the directoryScanner in packageManager,
103         // but since we don't we need to set includes/excludes here as well
104         ds.setExcludes( excludes );
105         ds.setIncludes( includes );
106         ds.addDefaultExcludes();
107 
108         ds.setBasedir( sourceDir.toString() );
109         ds.scan();
110 
111         //now get the list of included files
112 
113         String[] files = ds.getIncludedFiles();
114 
115         for ( String file : files )
116         {
117             Path sourceFile = sourceDir.resolve( file );
118 
119             if ( isJavaFile( sourceFile.toString() ) )
120             {
121                 String newFileName = file.replaceFirst( ".java$", ".html" );
122                 
123                 transform( sourceFile, this.destDir.resolve( newFileName ), bottom );
124             }
125         }
126     }
127 
128     /**
129      * Check to see if the file is a Java source file.
130      *
131      * @param filename The name of the file to check
132      * @return <code>true</code> if the file is a Java file
133      */
134     public static boolean isJavaFile( String filename )
135     {
136         return filename.endsWith( ".java" );
137     }
138 
139     /**
140      * Check to see if the file is an HTML file.
141      *
142      * @param filename The name of the file to check
143      * @return <code>true</code> if the file is an HTML file
144      */
145     public static boolean isHtmlFile( String filename )
146     {
147         return filename.endsWith( ".html" );
148     }
149 
150     /**
151      * @param dest
152      */
153     public void setDest( Path dest )
154     {
155         this.destDir = dest;
156     }
157 
158     /**
159      * @param locale
160      */
161     public void setLocale( Locale locale )
162     {
163         this.locale = locale;
164     }
165 
166     /**
167      * @param inputEncoding
168      */
169     public void setInputEncoding( String inputEncoding )
170     {
171         this.inputEncoding = inputEncoding;
172     }
173 
174     /**
175      * @param outputEncoding
176      */
177     public void setOutputEncoding( String outputEncoding )
178     {
179         this.outputEncoding = outputEncoding;
180     }
181 
182     /**
183      * @param javadocLinkDir
184      */
185     public void setJavadocLinkDir( Path javadocLinkDir )
186     {
187         // get a relative link to the javadocs
188         this.javadocLinkDir = javadocLinkDir;
189     }
190 
191     /**
192      * @param transformer
193      */
194     public void setTransformer( JavaCodeTransform transformer )
195     {
196         this.transformer = transformer;
197     }
198 
199     /**
200      * @param revision
201      */
202     public void setRevision( String revision )
203     {
204         this.revision = revision;
205     }
206 
207     /**
208      * @param log
209      */
210     public void setLog( Log log )
211     {
212         this.log = log;
213     }
214 
215     /**
216      * @param sourceDirs
217      * @param templateDir
218      * @param windowTitle
219      * @param docTitle
220      * @param bottom
221      * @throws IOException
222      * @throws JxrException
223      */
224     public void xref( List<String> sourceDirs, String templateDir, String windowTitle, String docTitle, String bottom )
225         throws IOException, JxrException
226     {
227         // first collect package and class info
228         FileManagerr.html#FileManager">FileManager fileManager = new FileManager();
229         fileManager.setEncoding( inputEncoding );
230 
231         PackageManageranager.html#PackageManager">PackageManager pkgmgr = new PackageManager( log, fileManager );
232         pkgmgr.setExcludes( excludes );
233         pkgmgr.setIncludes( includes );
234 
235         // go through each source directory and xref the java files
236         for ( String dir : sourceDirs )
237         {
238             Path path = Paths.get( dir ).toRealPath();
239 
240             pkgmgr.process( path );
241 
242             processPath( pkgmgr, path, bottom );
243         }
244 
245         // once we have all the source files xref'd, create the index pages
246         DirectoryIndexertml#DirectoryIndexer">DirectoryIndexer indexer = new DirectoryIndexer( pkgmgr, destDir.toString() );
247         indexer.setOutputEncoding( outputEncoding );
248         indexer.setTemplateDir( templateDir );
249         indexer.setWindowTitle( windowTitle );
250         indexer.setDocTitle( docTitle );
251         indexer.setBottom( bottom );
252         indexer.process( log );
253     }
254 
255     // ----------------------------------------------------------------------
256     // private methods
257     // ----------------------------------------------------------------------
258     /**
259      * Given a source file transform it into HTML and write it to the
260      * destination (dest) file.
261      *
262      * @param sourceFile The java source file
263      * @param destFile The directory to put the HTML into
264      * @param bottom The bottom footer text just as in the package pages
265      * @throws IOException Thrown if the transform can't happen for some reason.
266      */
267     private void transform( Path sourceFile, Path destFile, String bottom )
268         throws IOException
269     {
270         log.debug( sourceFile + " -> " + destFile );
271 
272         // get a relative link to the javadocs
273         Path javadoc = javadocLinkDir != null ? getRelativeLink( destFile.getParent(), javadocLinkDir ) : null;
274         transformer.transform( sourceFile, destFile, locale, inputEncoding, outputEncoding, javadoc,
275             this.revision, bottom );
276     }
277 
278     /**
279      * Creates a relative link from one directory to another.
280      *
281      * Example:
282      * given <code>/foo/bar/baz/oink</code>
283      * and <code>/foo/bar/schmoo</code>
284      *
285      * this method will return a string of <code>"../../schmoo/"</code>
286      *
287      * @param fromDir The directory from which the link is relative.
288      * @param toDir The directory into which the link points.
289      * @return a String of format <code>"../../schmoo/"</code>
290      */
291     private static Path getRelativeLink( Path fromDir, Path toDir )
292     {
293         return fromDir.relativize( toDir );
294     }
295 
296     public void setExcludes( String[] excludes )
297     {
298         this.excludes = excludes;
299     }
300 
301 
302     public void setIncludes( String[] includes )
303     {
304         if ( includes == null )
305         {
306             // We should not include non-java files, so we use a sensible default pattern
307             this.includes = DEFAULT_INCLUDES;
308         }
309         else
310         {
311             this.includes = includes;
312         }
313     }
314 }