1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.jxr;
20
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.util.List;
25 import java.util.Locale;
26
27 import org.apache.maven.jxr.ant.DirectoryScanner;
28 import org.apache.maven.jxr.pacman.PackageManager;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37 public class JXR {
38 private static final Logger LOGGER = LoggerFactory.getLogger(JXR.class);
39
40 private final PackageManager pkgmgr;
41
42
43
44
45
46 private final JavaCodeTransform transformer;
47
48
49
50
51 private static final String[] DEFAULT_INCLUDES = {"**/*.java"};
52
53
54
55
56 private Path destDir;
57
58 private Locale locale;
59
60 private String inputEncoding;
61
62 private String outputEncoding;
63
64
65
66
67 private Path javadocLinkDir;
68
69
70
71
72 private String revision;
73
74
75
76
77 private String[] excludes = null;
78
79
80
81
82 private String[] includes = DEFAULT_INCLUDES;
83
84 public JXR(PackageManager pkgmgr, JavaCodeTransform transformer) {
85 this.pkgmgr = pkgmgr;
86 this.transformer = transformer;
87 }
88
89
90
91
92
93
94
95
96
97 public void processPath(PackageManager packageManager, Path sourceDir, String bottom) throws IOException {
98 DirectoryScanner ds = new DirectoryScanner();
99
100
101 ds.setExcludes(excludes);
102 ds.setIncludes(includes);
103 ds.addDefaultExcludes();
104
105 ds.setBasedir(sourceDir.toString());
106 ds.scan();
107
108
109
110 String[] files = ds.getIncludedFiles();
111
112 for (String file : files) {
113 Path sourceFile = sourceDir.resolve(file);
114
115 if (isJavaFile(sourceFile.toString())) {
116 String newFileName = file.replaceFirst(".java$", ".html");
117
118 transform(sourceFile, this.destDir.resolve(newFileName), bottom);
119 }
120 }
121 }
122
123
124
125
126
127
128
129 public static boolean isJavaFile(String filename) {
130 return filename.endsWith(".java");
131 }
132
133
134
135
136
137
138
139 public static boolean isHtmlFile(String filename) {
140 return filename.endsWith(".html");
141 }
142
143
144
145
146
147
148 public void setDest(Path dest) {
149 this.destDir = dest;
150 }
151
152
153
154
155
156
157 public void setLocale(Locale locale) {
158 this.locale = locale;
159 }
160
161
162
163
164
165
166 public void setInputEncoding(String inputEncoding) {
167 this.inputEncoding = inputEncoding;
168 }
169
170
171
172
173
174
175 public void setOutputEncoding(String outputEncoding) {
176 this.outputEncoding = outputEncoding;
177 }
178
179
180
181
182
183 public void setJavadocLinkDir(Path javadocLinkDir) {
184
185 this.javadocLinkDir = javadocLinkDir;
186 }
187
188
189
190
191
192
193 public void setRevision(String revision) {
194 this.revision = revision;
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208 public void xref(List<String> sourceDirs, String templateDir, String windowTitle, String docTitle, String bottom)
209 throws IOException, JxrException {
210 pkgmgr.setExcludes(excludes);
211 pkgmgr.setIncludes(includes);
212
213
214 for (String dir : sourceDirs) {
215 Path path = Paths.get(dir).toRealPath();
216
217 pkgmgr.process(path);
218
219 processPath(pkgmgr, path, bottom);
220 }
221
222
223 DirectoryIndexer indexer = new DirectoryIndexer(pkgmgr, destDir.toString());
224 indexer.setOutputEncoding(outputEncoding);
225 indexer.setTemplateDir(templateDir);
226 indexer.setWindowTitle(windowTitle);
227 indexer.setDocTitle(docTitle);
228 indexer.setBottom(bottom);
229 indexer.process();
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244 private void transform(Path sourceFile, Path destFile, String bottom) throws IOException {
245 LOGGER.debug(sourceFile + " -> " + destFile);
246
247
248 Path javadoc = javadocLinkDir != null ? getRelativeLink(destFile.getParent(), javadocLinkDir) : null;
249 transformer.transform(
250 sourceFile, destFile, locale, inputEncoding, outputEncoding, javadoc, this.revision, bottom);
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 private static Path getRelativeLink(Path fromDir, Path toDir) {
267 return fromDir.relativize(toDir);
268 }
269
270 public void setExcludes(String[] excludes) {
271 this.excludes = excludes;
272 }
273
274 public void setIncludes(String[] includes) {
275 if (includes == null) {
276
277 this.includes = DEFAULT_INCLUDES;
278 } else {
279 this.includes = includes;
280 }
281 }
282 }