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