1   package org.apache.maven.jxr;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  import org.apache.maven.jxr.pacman.ClassType;
32  import org.apache.maven.jxr.pacman.PackageManager;
33  import org.apache.maven.jxr.pacman.PackageType;
34  import org.apache.velocity.Template;
35  import org.apache.velocity.VelocityContext;
36  import org.apache.velocity.app.VelocityEngine;
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  public class DirectoryIndexer
63  {
64      
65  
66  
67  
68      static final String INDEX = "package-summary.html";
69  
70      
71  
72  
73      private String root;
74  
75      
76  
77  
78      private PackageManager packageManager;
79  
80      
81  
82  
83      private String outputEncoding;
84  
85      private String templateDir;
86  
87      private String windowTitle;
88  
89      private String docTitle;
90  
91      private String bottom;
92  
93      
94  
95  
96  
97  
98  
99      public DirectoryIndexer( PackageManager packageManager, String root )
100     {
101         this.packageManager = packageManager;
102         this.root = root;
103     }
104 
105     
106 
107 
108 
109 
110     public void setOutputEncoding( String outputEncoding )
111     {
112         this.outputEncoding = outputEncoding;
113     }
114 
115     
116 
117 
118     public String getOutputEncoding()
119     {
120         return outputEncoding;
121     }
122 
123     
124 
125 
126 
127 
128 
129     public void setTemplateDir( String templateDir )
130     {
131         this.templateDir = templateDir;
132     }
133 
134     
135 
136 
137     public String getTemplateDir()
138     {
139         return templateDir;
140     }
141 
142     
143 
144 
145 
146 
147 
148     public void setWindowTitle( String windowTitle )
149     {
150         this.windowTitle = windowTitle;
151     }
152 
153     
154 
155 
156 
157 
158     public String getWindowTitle()
159     {
160         return windowTitle;
161     }
162 
163     
164 
165 
166 
167 
168 
169     public void setDocTitle( String docTitle )
170     {
171         this.docTitle = docTitle;
172     }
173 
174     
175 
176 
177 
178 
179     public String getDocTitle()
180     {
181         return docTitle;
182     }
183 
184     
185 
186 
187 
188 
189 
190     public void setBottom( String bottom )
191     {
192         this.bottom = bottom;
193     }
194 
195     
196 
197 
198 
199 
200     public String getBottom()
201     {
202         return bottom;
203     }
204 
205     
206 
207 
208 
209 
210     public void process()
211         throws JxrException
212     {
213         ProjectInfo info = getProjectInfo();
214 
215         VelocityEngine engine = new VelocityEngine();
216         setProperties( engine );
217         try
218         {
219             engine.init();
220         }
221         catch ( Exception e )
222         {
223             throw new JxrException( "Error initializing Velocity", e );
224         }
225 
226         VelocityContext context = new VelocityContext();
227         context.put( "outputEncoding", getOutputEncoding() );
228         context.put( "windowTitle", getWindowTitle() );
229         context.put( "docTitle", getDocTitle() );
230         context.put( "bottom", getBottom() );
231         context.put( "info", info );
232 
233         doVelocity( "index", root, context, engine );
234         doVelocity( "overview-frame", root, context, engine );
235         doVelocity( "allclasses-frame", root, context, engine );
236         doVelocity( "overview-summary", root, context, engine );
237 
238         for ( PackageInfo pkgInfo : info.getAllPackages().values() )
239         {
240             VelocityContext subContext = new VelocityContext( context );
241             subContext.put( "pkgInfo", pkgInfo );
242 
243             String outDir = root + '/' + pkgInfo.getDir();
244             doVelocity( "package-summary", outDir, subContext, engine );
245             doVelocity( "package-frame", outDir, subContext, engine );
246         }
247     }
248 
249     
250 
251 
252     private void setProperties( VelocityEngine engine )
253     {
254         Path templateDirFile = Paths.get( getTemplateDir() );
255         if ( templateDirFile.isAbsolute() )
256         {
257             
258             engine.setProperty( "resource.loader", "file" );
259             engine.setProperty( "file.resource.loader.class",
260                                 "org.apache.velocity.runtime.resource.loader.FileResourceLoader" );
261             engine.setProperty( "file.resource.loader.path", templateDirFile.toString() );
262         }
263         else
264         {
265             
266             engine.setProperty( "resource.loader", "classpath" );
267             engine.setProperty( "classpath.resource.loader.class",
268                                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
269         }
270         
271         engine.setProperty( "velocimacro.library", "" );
272 
273 
274     }
275 
276     
277 
278 
279     private void doVelocity( String templateName, String outDir, VelocityContext context, VelocityEngine engine )
280         throws JxrException
281     {
282         
283         File file = new File( outDir, templateName + ".html" );
284         file.getParentFile().mkdirs();
285 
286         try ( Writer writer = new OutputStreamWriter( new FileOutputStream( file ), getOutputEncoding() ) )
287         {
288             
289             StringBuilder templateFile = new StringBuilder();
290             File templateDirFile = new File( getTemplateDir() );
291             if ( !templateDirFile.isAbsolute() )
292             {
293                 
294                 templateFile.append( getTemplateDir() );
295                 templateFile.append( '/' );
296             }
297             templateFile.append( templateName );
298             templateFile.append( ".vm" );
299             Template template = engine.getTemplate( templateFile.toString() );
300 
301             
302             template.merge( context, writer );
303             writer.flush();
304         }
305         catch ( Exception e )
306         {
307             throw new JxrException( "Error merging velocity template", e );
308         }
309     }
310 
311     
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326     ProjectInfo getProjectInfo()
327     {
328         Map<String, PackageInfo> allPackages = new TreeMap<>();
329         Map<String, ClassInfo> allClasses = new TreeMap<>();
330 
331         for ( PackageType pkg : packageManager.getPackageTypes() )
332         {
333             String pkgName = pkg.getName();
334             String pkgDir = pkgName.replace( '.', '/' );
335             String rootRef = pkgName.replaceAll( "[^\\.]+(\\.|$)", "../" );
336 
337             
338             
339             if ( pkgName.length() == 0 )
340             {
341                 pkgName = "(default package)";
342                 pkgDir = ".";
343                 rootRef = "./";
344             }
345 
346             Map<String, ClassInfo> pkgClasses = new TreeMap<>();
347             for ( ClassType clazz : pkg.getClassTypes() )
348             {
349                 String className = clazz.getName();
350                 
351                 ClassInfo classInfo = new ClassInfo( className, pkgDir );
352                 
353                 classInfo.setFilename( clazz.getFilename() );
354 
355                 pkgClasses.put( className, classInfo );
356                 
357                 
358                 
359                 allClasses.put( className + "#" + pkgName, classInfo );
360             }
361 
362             PackageInfo pkgInfo = new PackageInfo( pkgName, pkgDir );
363             pkgInfo.setClasses( pkgClasses );
364             pkgInfo.setRootRef( rootRef );
365 
366             allPackages.put( pkgName, pkgInfo );
367         }
368 
369         return new ProjectInfo( allPackages, allClasses );
370     }
371     
372     
373 
374 
375 
376 
377     public static class ProjectInfo
378     {
379         private final Map<String, PackageInfo> allPackages;
380         
381         private final Map<String, ClassInfo> allClasses;
382 
383         public ProjectInfo( Map<String, PackageInfo> allPackages, Map<String, ClassInfo> allClasses )
384         {
385             this.allPackages = allPackages;
386             this.allClasses = allClasses;
387         }
388 
389         public Map<String, PackageInfo> getAllPackages()
390         {
391             return allPackages;
392         }
393         
394         public Map<String, ClassInfo> getAllClasses()
395         {
396             return allClasses;
397         }
398     }
399     
400     
401 
402 
403 
404 
405     public static class PackageInfo
406     {
407         private final String name;
408         
409         private final String dir;
410 
411         Map<String, ClassInfo> classes;
412         
413         private String rootRef;
414         
415         public PackageInfo( String name, String dir )
416         {
417             this.name = name;
418             this.dir = dir;
419         }
420         
421         public String getName()
422         {
423             return name;
424         }
425         
426         public String getDir()
427         {
428             return dir;
429         }
430         
431         public void setClasses( Map<String, ClassInfo> classes )
432         {
433             this.classes = classes;
434         }
435         
436         public Map<String, ClassInfo> getClasses()
437         {
438             return classes;
439         }
440         
441         public void setRootRef( String rootRef )
442         {
443             this.rootRef = rootRef;
444         }
445         
446         public String getRootRef()
447         {
448             return rootRef;
449         }
450     }
451     
452     
453 
454 
455 
456 
457 
458     public static class ClassInfo
459     {
460         private final String name;
461         
462         private final String dir;
463         
464         private String filename;
465 
466         public ClassInfo( String name, String dir )
467         {
468             super();
469             this.name = name;
470             this.dir = dir;
471         }
472         
473         public String getName()
474         {
475             return name;
476         }
477         
478         public String getDir()
479         {
480             return dir;
481         }
482         
483         public void setFilename( String filename )
484         {
485             this.filename = filename;
486         }
487         
488         public String getFilename()
489         {
490             return filename;
491         }
492     }
493 }