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.pacman.PackageManager;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
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   */
38  public class JXR
39  {
40      private static final Logger LOGGER = LoggerFactory.getLogger( JXR.class );
41      
42      private final PackageManager pkgmgr;
43  
44      /**
45       * Handles taking .java files and changing them into html. "More than meets
46       * the eye!" :)
47       */
48      private final JavaCodeTransform transformer;
49      
50      /**
51       * The default list of include patterns to use.
52       */
53      private static final String[] DEFAULT_INCLUDES = { "**/*.java" };
54  
55      /**
56       * Path to destination.
57       */
58      private Path destDir;
59  
60      private Locale locale;
61  
62      private String inputEncoding;
63  
64      private String outputEncoding;
65  
66      /**
67       * Relative path to javadocs, suitable for hyperlinking.
68       */
69      private Path javadocLinkDir;
70  
71  
72      /**
73       * The revision of the module currently being processed.
74       */
75      private String revision;
76  
77      /**
78       * The list of exclude patterns to use.
79       */
80      private String[] excludes = null;
81  
82      /**
83       * The list of include patterns to use.
84       */
85      private String[] includes = DEFAULT_INCLUDES;
86      
87      public JXR( PackageManager pkgmgr, JavaCodeTransform transformer )
88      {
89          this.pkgmgr = pkgmgr;
90          this.transformer = transformer;
91      }
92  
93      /**
94       * Now that we have instantiated everything. Process this JXR task.
95       *
96       * @param packageManager
97       * @param sourceDir
98       * @param bottom
99       * @throws IOException
100      */
101     public void processPath( PackageManager packageManager, Path sourceDir, String bottom )
102         throws IOException
103     {
104         DirectoryScanner ds = new DirectoryScanner();
105         // I'm not sure why we don't use the directoryScanner in packageManager,
106         // but since we don't we need to set includes/excludes here as well
107         ds.setExcludes( excludes );
108         ds.setIncludes( includes );
109         ds.addDefaultExcludes();
110 
111         ds.setBasedir( sourceDir.toString() );
112         ds.scan();
113 
114         //now get the list of included files
115 
116         String[] files = ds.getIncludedFiles();
117 
118         for ( String file : files )
119         {
120             Path sourceFile = sourceDir.resolve( file );
121 
122             if ( isJavaFile( sourceFile.toString() ) )
123             {
124                 String newFileName = file.replaceFirst( ".java$", ".html" );
125                 
126                 transform( sourceFile, this.destDir.resolve( newFileName ), bottom );
127             }
128         }
129     }
130 
131     /**
132      * Check to see if the file is a Java source file.
133      *
134      * @param filename The name of the file to check
135      * @return <code>true</code> if the file is a Java file
136      */
137     public static boolean isJavaFile( String filename )
138     {
139         return filename.endsWith( ".java" );
140     }
141 
142     /**
143      * Check to see if the file is an HTML file.
144      *
145      * @param filename The name of the file to check
146      * @return <code>true</code> if the file is an HTML file
147      */
148     public static boolean isHtmlFile( String filename )
149     {
150         return filename.endsWith( ".html" );
151     }
152 
153     /**
154      * @param dest
155      */
156     public void setDest( Path dest )
157     {
158         this.destDir = dest;
159     }
160 
161     /**
162      * @param locale
163      */
164     public void setLocale( Locale locale )
165     {
166         this.locale = locale;
167     }
168 
169     /**
170      * @param inputEncoding
171      */
172     public void setInputEncoding( String inputEncoding )
173     {
174         this.inputEncoding = inputEncoding;
175     }
176 
177     /**
178      * @param outputEncoding
179      */
180     public void setOutputEncoding( String outputEncoding )
181     {
182         this.outputEncoding = outputEncoding;
183     }
184 
185     /**
186      * @param javadocLinkDir
187      */
188     public void setJavadocLinkDir( Path javadocLinkDir )
189     {
190         // get a relative link to the javadocs
191         this.javadocLinkDir = javadocLinkDir;
192     }
193 
194     /**
195      * @param revision
196      */
197     public void setRevision( String revision )
198     {
199         this.revision = revision;
200     }
201 
202     /**
203      * @param sourceDirs
204      * @param templateDir
205      * @param windowTitle
206      * @param docTitle
207      * @param bottom
208      * @throws IOException
209      * @throws JxrException
210      */
211     public void xref( List<String> sourceDirs, String templateDir, String windowTitle, String docTitle, String bottom )
212         throws IOException, JxrException
213     {
214         pkgmgr.setExcludes( excludes );
215         pkgmgr.setIncludes( includes );
216 
217         // go through each source directory and xref the java files
218         for ( String dir : sourceDirs )
219         {
220             Path path = Paths.get( dir ).toRealPath();
221 
222             pkgmgr.process( path );
223 
224             processPath( pkgmgr, path, bottom );
225         }
226 
227         // once we have all the source files xref'd, create the index pages
228         DirectoryIndexer indexer = new DirectoryIndexer( pkgmgr, destDir.toString() );
229         indexer.setOutputEncoding( outputEncoding );
230         indexer.setTemplateDir( templateDir );
231         indexer.setWindowTitle( windowTitle );
232         indexer.setDocTitle( docTitle );
233         indexer.setBottom( bottom );
234         indexer.process();
235     }
236 
237     // ----------------------------------------------------------------------
238     // private methods
239     // ----------------------------------------------------------------------
240     /**
241      * Given a source file transform it into HTML and write it to the
242      * destination (dest) file.
243      *
244      * @param sourceFile The java source file
245      * @param destFile The directory to put the HTML into
246      * @param bottom The bottom footer text just as in the package pages
247      * @throws IOException Thrown if the transform can't happen for some reason.
248      */
249     private void transform( Path sourceFile, Path destFile, String bottom )
250         throws IOException
251     {
252         LOGGER.debug( sourceFile + " -> " + destFile );
253 
254         // get a relative link to the javadocs
255         Path javadoc = javadocLinkDir != null ? getRelativeLink( destFile.getParent(), javadocLinkDir ) : null;
256         transformer.transform( sourceFile, destFile, locale, inputEncoding, outputEncoding, javadoc,
257             this.revision, bottom );
258     }
259 
260     /**
261      * Creates a relative link from one directory to another.
262      *
263      * Example:
264      * given <code>/foo/bar/baz/oink</code>
265      * and <code>/foo/bar/schmoo</code>
266      *
267      * this method will return a string of <code>"../../schmoo/"</code>
268      *
269      * @param fromDir The directory from which the link is relative.
270      * @param toDir The directory into which the link points.
271      * @return a String of format <code>"../../schmoo/"</code>
272      */
273     private static Path getRelativeLink( Path fromDir, Path toDir )
274     {
275         return fromDir.relativize( toDir );
276     }
277 
278     public void setExcludes( String[] excludes )
279     {
280         this.excludes = excludes;
281     }
282 
283 
284     public void setIncludes( String[] includes )
285     {
286         if ( includes == null )
287         {
288             // We should not include non-java files, so we use a sensible default pattern
289             this.includes = DEFAULT_INCLUDES;
290         }
291         else
292         {
293             this.includes = includes;
294         }
295     }
296 }