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