1 51 package org.apache.fop.tools.anttasks; 52 53 import org.apache.tools.ant.BuildException; 55 import org.apache.tools.ant.DirectoryScanner; 56 import org.apache.tools.ant.Project; 57 import org.apache.tools.ant.Task; 58 import org.apache.tools.ant.types.FileSet; 59 60 import org.xml.sax.XMLReader ; 62 63 import java.io.File ; 65 import java.io.OutputStream ; 66 import java.util.List ; 67 import java.util.Map ; 68 69 import org.apache.fop.messaging.MessageHandler; 71 import org.apache.fop.apps.Options; 72 import org.apache.fop.apps.Starter; 73 import org.apache.fop.apps.InputHandler; 74 import org.apache.fop.apps.FOInputHandler; 75 import org.apache.fop.apps.Driver; 76 import org.apache.fop.apps.FOPException; 77 import org.apache.fop.configuration.Configuration; 78 79 import org.apache.avalon.framework.logger.ConsoleLogger; 81 import org.apache.avalon.framework.logger.Logger; 82 import org.apache.tools.ant.util.GlobPatternMapper; 83 84 97 public class Fop extends Task { 98 99 File foFile; 100 List filesets = new java.util.ArrayList (); 101 File outFile; 102 File outDir; 103 String format; File baseDir; 105 File userConfig; 106 int messageType = Project.MSG_VERBOSE; 107 boolean logFiles = true; 108 109 113 public void setUserconfig (File userConfig) { 114 this.userConfig = userConfig; 115 } 116 117 121 public void setFofile(File foFile) { 122 this.foFile = foFile; 123 } 124 125 128 public File getFofile() { 129 return foFile; 130 } 131 132 135 public void addFileset(FileSet set) { 136 filesets.add(set); 137 } 138 139 143 public void setOutfile(File outFile) { 144 this.outFile = outFile; 145 } 146 147 150 public File getOutfile() { 151 return this.outFile; 152 } 153 154 158 public void setOutdir(File outDir) { 159 this.outDir = outDir; 160 } 161 162 165 public File getOutdir() { 166 return this.outDir; 167 } 168 169 172 public void setFormat(String format) { 173 this.format = format; 174 } 175 176 179 public String getFormat() { 180 return this.format; 181 } 182 183 187 public void setMessagelevel(String messageLevel) { 188 if (messageLevel.equalsIgnoreCase("info")) { 189 messageType = Project.MSG_INFO; 190 } else if (messageLevel.equalsIgnoreCase("verbose")) { 191 messageType = Project.MSG_VERBOSE; 192 } else if (messageLevel.equalsIgnoreCase("debug")) { 193 messageType = Project.MSG_DEBUG; 194 } else if (messageLevel.equalsIgnoreCase("err") || messageLevel.equalsIgnoreCase("error")) { 195 messageType = Project.MSG_ERR; 196 } else if (messageLevel.equalsIgnoreCase("warn")) { 197 messageType = Project.MSG_WARN; 198 } else { 199 log("messagelevel set to unknown value \"" + messageLevel + 200 "\"", Project.MSG_ERR); 201 throw new BuildException("unknown messagelevel"); 202 } 203 } 204 205 209 public int getMessageType() { 210 return messageType; 211 } 212 213 217 public void setBasedir(File baseDir) { 218 this.baseDir = baseDir; 219 } 220 221 224 public File getBasedir() { 225 return (baseDir != null) ? baseDir : project.resolveFile("."); 226 } 227 228 232 public void setLogFiles(boolean aBoolean) { 233 logFiles = aBoolean; 234 } 235 236 public boolean getLogFiles() { 237 return logFiles; 238 } 239 240 243 public void execute() throws BuildException { 244 int logLevel = ConsoleLogger.LEVEL_INFO; 245 switch (getMessageType()) { 246 case Project.MSG_DEBUG : logLevel = ConsoleLogger.LEVEL_DEBUG; break; 247 case Project.MSG_INFO : logLevel = ConsoleLogger.LEVEL_INFO; break; 248 case Project.MSG_WARN : logLevel = ConsoleLogger.LEVEL_WARN; break; 249 case Project.MSG_ERR : logLevel = ConsoleLogger.LEVEL_ERROR; break; 250 case Project.MSG_VERBOSE: logLevel = ConsoleLogger.LEVEL_DEBUG; break; 251 } 252 Logger log = new ConsoleLogger(logLevel); 253 MessageHandler.setScreenLogger(log); 254 try { 255 Starter starter = new FOPTaskStarter(this, log, logFiles); 256 starter.run(); 257 } catch (FOPException ex) { 258 throw new BuildException(ex); 259 } 260 261 } 262 263 } 264 265 class FOPTaskStarter extends Starter { 266 Fop task; 267 Logger log; 268 boolean logFiles; 269 270 FOPTaskStarter(Fop task, Logger aLogger, boolean aLogFiles) throws FOPException { 271 this.task = task; 272 log = aLogger; 273 logFiles = aLogFiles; 274 } 275 276 private int determineRenderer(String format) { 277 if ((format == null) || 278 format.equalsIgnoreCase("application/pdf") || 279 format.equalsIgnoreCase("pdf")) { 280 return Driver.RENDER_PDF; 281 } else if (format.equalsIgnoreCase("application/postscript") || 282 format.equalsIgnoreCase("ps")) { 283 return Driver.RENDER_PS; 284 } else if (format.equalsIgnoreCase("application/vnd.mif") || 285 format.equalsIgnoreCase("mif")) { 286 return Driver.RENDER_MIF; 287 } else if (format.equalsIgnoreCase("application/vnd.gp-PCL") || 288 format.equalsIgnoreCase("pcl")) { 289 return Driver.RENDER_PCL; 290 } else if (format.equalsIgnoreCase("text/plain") || 291 format.equalsIgnoreCase("txt")) { 292 return Driver.RENDER_TXT; 293 } else if (format.equalsIgnoreCase("text/xml") || 294 format.equalsIgnoreCase("at") || 295 format.equalsIgnoreCase("xml")) { 296 return Driver.RENDER_XML; 297 } else { 298 String err = "Couldn't determine renderer to use: "+format; 299 throw new BuildException(err); 300 } 301 } 302 303 private String determineExtension(int renderer) { 304 switch (renderer) { 305 case Driver.RENDER_PDF: 306 return ".pdf"; 307 case Driver.RENDER_PS: 308 return ".ps"; 309 case Driver.RENDER_MIF: 310 return ".mif"; 311 case Driver.RENDER_PCL: 312 return ".pcl"; 313 case Driver.RENDER_TXT: 314 return ".txt"; 315 case Driver.RENDER_XML: 316 return ".xml"; 317 default: 318 String err = "Unknown renderer: "+renderer; 319 throw new BuildException(err); 320 } 321 } 322 323 private File replaceExtension(File file, String expectedExt, 324 String newExt) { 325 String name = file.getName(); 326 if (name.toLowerCase().endsWith(expectedExt)) { 327 name = name.substring(0, name.length() - expectedExt.length()); 328 } 329 name = name.concat(newExt); 330 return new File (file.getParentFile(), name); 331 } 332 333 public void run() throws FOPException { 334 if (task.userConfig != null) { 335 new Options (task.userConfig); 336 } 337 338 try { 339 if (task.getFofile() != null) { 340 if (task.getBasedir() != null) { 341 Configuration.put("baseDir", 342 task.getBasedir().toURL(). 343 toExternalForm()); 344 } else { 345 Configuration.put("baseDir", 346 task.getFofile().getParentFile().toURL(). 347 toExternalForm()); 348 } 349 } 350 task.log("Using base directory: " + 351 Configuration.getValue("baseDir"), Project.MSG_DEBUG); 352 } catch (Exception e) { 353 354 task.log("Error setting base directory: " + e, Project.MSG_ERR); 355 } 356 357 int rint = determineRenderer(task.getFormat()); 358 String newExtension = determineExtension(rint); 359 360 int actioncount = 0; 361 362 if (task.getFofile() != null) { 364 if (task.getFofile().exists()) { 365 File outf = task.getOutfile(); 366 if (outf == null) { 367 throw new BuildException("outfile is required when fofile is used"); 368 } 369 if (task.getOutdir() != null) { 370 outf = new File (task.getOutdir(), outf.getName()); 371 } 372 render(task.getFofile(), outf, rint); 373 actioncount++; 374 } 375 } 376 377 GlobPatternMapper mapper = new GlobPatternMapper(); 378 mapper.setFrom("*.fo"); 379 mapper.setTo("*" + newExtension); 380 381 for (int i = 0; i < task.filesets.size(); i++) { 383 FileSet fs = (FileSet) task.filesets.get(i); 384 DirectoryScanner ds = fs.getDirectoryScanner(task.getProject()); 385 String [] files = ds.getIncludedFiles(); 386 387 388 for (int j = 0; j < files.length; j++) { 389 File f = new File (fs.getDir(task.getProject()), files[j]); 390 File outf = null; 391 if (task.getOutdir() != null && files[j].endsWith(".fo")) { 392 String [] sa = mapper.mapFileName(files[j]); 393 outf = new File (task.getOutdir(), sa[0]); 394 } else { 395 outf = replaceExtension(f, ".fo", newExtension); 396 if (task.getOutdir() != null) { 397 outf = new File (task.getOutdir(), outf.getName()); 398 } 399 } 400 try { 401 if (task.getBasedir() != null) { 402 Configuration.put("baseDir", 403 task.getBasedir().toURL(). 404 toExternalForm()); 405 } else { 406 Configuration.put("baseDir", 407 fs.getDir(task.getProject()).toURL(). 408 toExternalForm()); 409 } 410 task.log("Using base directory: " + 411 Configuration.getValue("baseDir"), Project.MSG_DEBUG); 412 } catch (Exception e) { 413 task.log("Error setting base directory: " + e, Project.MSG_ERR); 414 } 415 416 render(f, outf, rint); 417 actioncount++; 418 } 419 } 420 421 if (actioncount == 0) { 422 task.log( 423 "No files processed. No files were selected by the filesets and no fofile was set." , 424 Project.MSG_WARN); 425 } 426 } 427 428 private void render(File foFile, File outFile, 429 int renderer) throws FOPException { 430 InputHandler inputHandler = new FOInputHandler(foFile); 431 XMLReader parser = inputHandler.getParser(); 432 433 OutputStream out = null; 434 try { 435 File dir = outFile.getParentFile(); 436 dir.mkdirs(); 437 out = new java.io.FileOutputStream (outFile); 438 } catch (Exception ex) { 439 throw new BuildException(ex); 440 } 441 442 if (logFiles) task.log(foFile + " -> " + outFile, Project.MSG_INFO); 443 444 try { 445 Driver driver = new Driver(inputHandler.getInputSource(), out); 446 driver.setLogger(log); 447 driver.setRenderer(renderer); 448 if (renderer == Driver.RENDER_XML) { 449 Map rendererOptions = new java.util.HashMap (); 450 rendererOptions.put("fineDetail", new Boolean (true)); 451 driver.getRenderer().setOptions(rendererOptions); 452 } 453 driver.setXMLReader(parser); 454 driver.run(); 455 out.close(); 456 } catch (Exception ex) { 457 throw new BuildException(ex); 458 } 459 } 460 461 } 462 463 | Popular Tags |