1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| package org.w3c.tidy.ant; |
55 |
| |
56 |
| import java.io.BufferedInputStream; |
57 |
| import java.io.BufferedOutputStream; |
58 |
| import java.io.ByteArrayOutputStream; |
59 |
| import java.io.File; |
60 |
| import java.io.FileInputStream; |
61 |
| import java.io.FileOutputStream; |
62 |
| import java.io.IOException; |
63 |
| import java.io.InputStream; |
64 |
| import java.io.OutputStream; |
65 |
| import java.io.PrintWriter; |
66 |
| import java.util.ArrayList; |
67 |
| import java.util.Iterator; |
68 |
| import java.util.List; |
69 |
| import java.util.Properties; |
70 |
| |
71 |
| import org.apache.tools.ant.BuildException; |
72 |
| import org.apache.tools.ant.DirectoryScanner; |
73 |
| import org.apache.tools.ant.Project; |
74 |
| import org.apache.tools.ant.Task; |
75 |
| import org.apache.tools.ant.types.FileSet; |
76 |
| import org.apache.tools.ant.types.Parameter; |
77 |
| import org.apache.tools.ant.util.FileNameMapper; |
78 |
| import org.apache.tools.ant.util.FlatFileNameMapper; |
79 |
| import org.apache.tools.ant.util.IdentityMapper; |
80 |
| import org.w3c.tidy.Tidy; |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| public class JTidyTask extends Task |
167 |
| { |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| private List filesets = new ArrayList(); |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| private File destdir; |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| private File destfile; |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| private File srcfile; |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| private boolean failonerror; |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| private boolean flatten; |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| private Tidy tidy; |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| private Properties props; |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| private File properties; |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
8
| public void setDestdir(File destdir)
|
218 |
| { |
219 |
8
| this.destdir = destdir;
|
220 |
| } |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
7
| public void setDestfile(File destfile)
|
226 |
| { |
227 |
7
| this.destfile = destfile;
|
228 |
| } |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
11
| public void setSrcfile(File srcfile)
|
234 |
| { |
235 |
11
| this.srcfile = srcfile;
|
236 |
| } |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
4
| public void setFailonerror(boolean failonerror)
|
242 |
| { |
243 |
4
| this.failonerror = failonerror;
|
244 |
| } |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
3
| public void setFlatten(boolean flatten)
|
250 |
| { |
251 |
3
| this.flatten = flatten;
|
252 |
| } |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
4
| public void setProperties(File properties)
|
258 |
| { |
259 |
4
| this.properties = properties;
|
260 |
| } |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
7
| public void addFileset(FileSet fileSet)
|
267 |
| { |
268 |
7
| filesets.add(fileSet);
|
269 |
| } |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
1
| public void addConfiguredParameter(Parameter prop)
|
276 |
| { |
277 |
1
| props.setProperty(prop.getName(), prop.getValue());
|
278 |
| } |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
20
| public void init()
|
284 |
| { |
285 |
20
| super.init();
|
286 |
| |
287 |
| |
288 |
20
| tidy = new Tidy();
|
289 |
20
| props = new Properties();
|
290 |
| } |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
19
| protected void validateParameters() throws BuildException
|
297 |
| { |
298 |
19
| if (srcfile == null && filesets.size() == 0)
|
299 |
| { |
300 |
1
| throw new BuildException("Specify at least srcfile or a fileset.");
|
301 |
| } |
302 |
18
| if (srcfile != null && filesets.size() > 0)
|
303 |
| { |
304 |
1
| throw new BuildException("You can't specify both srcfile and nested filesets.");
|
305 |
| } |
306 |
| |
307 |
17
| if (destfile == null && destdir == null)
|
308 |
| { |
309 |
1
| throw new BuildException("One of destfile or destdir must be set.");
|
310 |
| } |
311 |
| |
312 |
16
| if (srcfile == null && destfile != null)
|
313 |
| { |
314 |
1
| throw new BuildException("You only can use destfile with srcfile.");
|
315 |
| } |
316 |
| |
317 |
15
| if (srcfile != null && srcfile.isDirectory())
|
318 |
| { |
319 |
1
| throw new BuildException("srcfile can't be a directory.");
|
320 |
| } |
321 |
| |
322 |
14
| if (properties != null && properties.isDirectory())
|
323 |
| { |
324 |
1
| throw new BuildException("Invalid properties file specified: " + properties.getPath());
|
325 |
| } |
326 |
| |
327 |
| } |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
13
| public void execute() throws BuildException
|
334 |
| { |
335 |
| |
336 |
13
| validateParameters();
|
337 |
| |
338 |
| |
339 |
11
| if (this.properties != null)
|
340 |
| { |
341 |
2
| try
|
342 |
| { |
343 |
2
| this.props.load(new FileInputStream(this.properties));
|
344 |
| } |
345 |
| catch (IOException e) |
346 |
| { |
347 |
1
| throw new BuildException("Unable to load properties file " + properties, e);
|
348 |
| } |
349 |
| } |
350 |
| |
351 |
| |
352 |
10
| tidy.setErrout(new PrintWriter(new ByteArrayOutputStream()));
|
353 |
| |
354 |
10
| tidy.setConfigurationFromProps(props);
|
355 |
| |
356 |
10
| if (this.srcfile != null)
|
357 |
| { |
358 |
| |
359 |
5
| executeSingle();
|
360 |
| } |
361 |
| else |
362 |
| { |
363 |
| |
364 |
5
| executeSet();
|
365 |
| } |
366 |
| } |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
5
| protected void executeSingle()
|
372 |
| { |
373 |
| |
374 |
5
| if (!srcfile.exists())
|
375 |
| { |
376 |
1
| throw new BuildException("Could not find source file " + srcfile.getAbsolutePath() + ".");
|
377 |
| } |
378 |
| |
379 |
4
| if (destfile == null)
|
380 |
| { |
381 |
| |
382 |
3
| destfile = new File(destdir, srcfile.getName());
|
383 |
| } |
384 |
| |
385 |
4
| processFile(srcfile, destfile);
|
386 |
| } |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
5
| protected void executeSet()
|
392 |
| { |
393 |
| |
394 |
5
| FileNameMapper mapper = null;
|
395 |
5
| if (flatten)
|
396 |
| { |
397 |
3
| mapper = new FlatFileNameMapper();
|
398 |
| } |
399 |
| else |
400 |
| { |
401 |
2
| mapper = new IdentityMapper();
|
402 |
| } |
403 |
| |
404 |
5
| mapper.setTo(this.destdir.getAbsolutePath());
|
405 |
| |
406 |
5
| Iterator iterator = filesets.iterator();
|
407 |
5
| while (iterator.hasNext())
|
408 |
| { |
409 |
5
| FileSet fileSet = (FileSet) iterator.next();
|
410 |
5
| DirectoryScanner directoryScanner = fileSet.getDirectoryScanner(getProject());
|
411 |
5
| String[] sourceFiles = directoryScanner.getIncludedFiles();
|
412 |
5
| File inputdir = directoryScanner.getBasedir();
|
413 |
| |
414 |
5
| mapper.setFrom(inputdir.getAbsolutePath());
|
415 |
| |
416 |
5
| for (int j = 0; j < sourceFiles.length; j++)
|
417 |
| { |
418 |
11
| String[] mapped = mapper.mapFileName(sourceFiles[j]);
|
419 |
| |
420 |
11
| processFile(new File(inputdir, sourceFiles[j]), new File(this.destdir, mapped[0]));
|
421 |
| } |
422 |
| } |
423 |
| } |
424 |
| |
425 |
| |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
17
| protected void processFile(File inputFile, File outputFile)
|
431 |
| { |
432 |
| |
433 |
17
| log("Processing " + inputFile.getAbsolutePath(), Project.MSG_DEBUG);
|
434 |
| |
435 |
17
| InputStream is;
|
436 |
17
| OutputStream os;
|
437 |
17
| try
|
438 |
| { |
439 |
17
| is = new BufferedInputStream(new FileInputStream(inputFile));
|
440 |
| } |
441 |
| catch (IOException e) |
442 |
| { |
443 |
1
| throw new BuildException("Unable to open file " + inputFile);
|
444 |
| } |
445 |
| |
446 |
16
| try
|
447 |
| { |
448 |
16
| outputFile.getParentFile().mkdirs();
|
449 |
16
| outputFile.createNewFile();
|
450 |
15
| os = new BufferedOutputStream(new FileOutputStream(outputFile));
|
451 |
| } |
452 |
| catch (IOException e) |
453 |
| { |
454 |
1
| throw new BuildException("Unable to open destination file " + outputFile, e);
|
455 |
| } |
456 |
| |
457 |
15
| tidy.parse(is, os);
|
458 |
| |
459 |
15
| try
|
460 |
| { |
461 |
15
| is.close();
|
462 |
| } |
463 |
| catch (IOException e1) |
464 |
| { |
465 |
| |
466 |
| } |
467 |
15
| try
|
468 |
| { |
469 |
15
| os.flush();
|
470 |
15
| os.close();
|
471 |
| } |
472 |
| catch (IOException e1) |
473 |
| { |
474 |
| |
475 |
| } |
476 |
| |
477 |
| |
478 |
15
| if (tidy.getParseErrors() > 0 && !tidy.getForceOutput())
|
479 |
| { |
480 |
5
| outputFile.delete();
|
481 |
| } |
482 |
| |
483 |
15
| if (failonerror && tidy.getParseErrors() > 0)
|
484 |
| { |
485 |
1
| throw new BuildException("Tidy was unable to process file "
|
486 |
| + inputFile |
487 |
| + ", " |
488 |
| + tidy.getParseErrors() |
489 |
| + " returned."); |
490 |
| } |
491 |
| |
492 |
| } |
493 |
| } |