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 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
35
36
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
46
47
48 private final JavaCodeTransform transformer;
49
50
51
52
53 private static final String[] DEFAULT_INCLUDES = { "**/*.java" };
54
55
56
57
58 private Path destDir;
59
60 private Locale locale;
61
62 private String inputEncoding;
63
64 private String outputEncoding;
65
66
67
68
69 private Path javadocLinkDir;
70
71
72
73
74
75 private String revision;
76
77
78
79
80 private String[] excludes = null;
81
82
83
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
95
96
97
98
99
100
101 public void processPath( PackageManager packageManager, Path sourceDir, String bottom )
102 throws IOException
103 {
104 DirectoryScanner ds = new DirectoryScanner();
105
106
107 ds.setExcludes( excludes );
108 ds.setIncludes( includes );
109 ds.addDefaultExcludes();
110
111 ds.setBasedir( sourceDir.toString() );
112 ds.scan();
113
114
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
133
134
135
136
137 public static boolean isJavaFile( String filename )
138 {
139 return filename.endsWith( ".java" );
140 }
141
142
143
144
145
146
147
148 public static boolean isHtmlFile( String filename )
149 {
150 return filename.endsWith( ".html" );
151 }
152
153
154
155
156 public void setDest( Path dest )
157 {
158 this.destDir = dest;
159 }
160
161
162
163
164 public void setLocale( Locale locale )
165 {
166 this.locale = locale;
167 }
168
169
170
171
172 public void setInputEncoding( String inputEncoding )
173 {
174 this.inputEncoding = inputEncoding;
175 }
176
177
178
179
180 public void setOutputEncoding( String outputEncoding )
181 {
182 this.outputEncoding = outputEncoding;
183 }
184
185
186
187
188 public void setJavadocLinkDir( Path javadocLinkDir )
189 {
190
191 this.javadocLinkDir = javadocLinkDir;
192 }
193
194
195
196
197 public void setRevision( String revision )
198 {
199 this.revision = revision;
200 }
201
202
203
204
205
206
207
208
209
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
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
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
239
240
241
242
243
244
245
246
247
248
249 private void transform( Path sourceFile, Path destFile, String bottom )
250 throws IOException
251 {
252 LOGGER.debug( sourceFile + " -> " + destFile );
253
254
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
262
263
264
265
266
267
268
269
270
271
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
289 this.includes = DEFAULT_INCLUDES;
290 }
291 else
292 {
293 this.includes = includes;
294 }
295 }
296 }