1 51 package org.apache.fop.apps; 52 53 import org.apache.fop.configuration.ConfigurationReader; 55 import org.apache.fop.fo.ElementMapping; 56 import org.apache.fop.fo.FOTreeBuilder; 57 import org.apache.fop.render.Renderer; 58 import org.apache.fop.tools.DocumentInputSource; 59 import org.apache.fop.tools.DocumentReader; 60 61 import org.apache.avalon.framework.logger.ConsoleLogger; 63 import org.apache.avalon.framework.logger.Logger; 64 65 import org.w3c.dom.Document ; 67 68 import org.xml.sax.ContentHandler ; 70 import org.xml.sax.InputSource ; 71 import org.xml.sax.SAXException ; 72 import org.xml.sax.XMLReader ; 73 74 import java.io.BufferedReader ; 76 import java.io.InputStreamReader ; 77 import java.io.InputStream ; 78 import java.io.IOException ; 79 import java.io.OutputStream ; 80 import java.util.Enumeration ; 81 import java.util.HashMap ; 82 import java.util.Vector ; 83 84 124 public class Driver { 125 126 129 public static final int RENDER_PDF = 1; 130 131 134 public static final int RENDER_AWT = 2; 135 136 139 public static final int RENDER_MIF = 3; 140 141 144 public static final int RENDER_XML = 4; 145 146 149 public static final int RENDER_PRINT = 5; 150 151 154 public static final int RENDER_PCL = 6; 155 156 159 public static final int RENDER_PS = 7; 160 161 164 public static final int RENDER_TXT = 8; 165 166 169 public static final int RENDER_SVG = 9; 170 171 174 private FOTreeBuilder _treeBuilder; 175 176 179 private Renderer _renderer; 180 181 184 private InputSource _source; 185 186 189 private OutputStream _stream; 190 191 194 private XMLReader _reader; 195 196 199 private boolean _errorDump = false; 200 201 204 private Logger log; 205 206 public static final String getParserClassName() { 207 try { 208 return javax.xml.parsers.SAXParserFactory.newInstance().newSAXParser().getXMLReader().getClass().getName(); 209 } catch (javax.xml.parsers.ParserConfigurationException e) { 210 return null; 211 } catch (org.xml.sax.SAXException e) { 212 return null; 213 } 214 } 215 216 219 public Driver() { 220 _stream = null; 221 _treeBuilder = new FOTreeBuilder(); 222 setupDefaultMappings(); 223 } 224 225 public Driver(InputSource source, OutputStream stream) { 226 this(); 227 _source = source; 228 _stream = stream; 229 } 230 231 public void setLogger(Logger logger) { 232 log = logger; 233 } 234 235 private Logger getLogger() { 236 if(log == null) { 237 log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); 238 log.error("Logger not set"); 239 } 240 241 return log; 242 } 243 244 249 public synchronized void reset() { 250 _source = null; 251 _stream = null; 252 _reader = null; 253 _treeBuilder.reset(); 254 } 255 256 261 public FormattingResults getResults() { 262 try { 263 return _treeBuilder.getStreamRenderer().getResults(); 264 } catch (NullPointerException e) { 265 return null; 266 } 267 } 268 269 public boolean hasData() { 270 return (_treeBuilder.hasData()); 271 } 272 273 277 public void setErrorDump(boolean dump) { 278 _errorDump = dump; 279 } 280 281 287 public void setOutputStream(OutputStream stream) { 288 _stream = stream; 289 } 290 291 296 public void setInputSource(InputSource source) { 297 _source = source; 298 } 299 300 304 public void setXMLReader(XMLReader reader) { 305 _reader = reader; 306 } 307 308 312 public void setupDefaultMappings() { 313 addElementMapping("org.apache.fop.fo.StandardElementMapping"); 314 addElementMapping("org.apache.fop.svg.SVGElementMapping"); 315 addElementMapping("org.apache.fop.extensions.ExtensionElementMapping"); 316 317 Enumeration providers = 319 Service.providers(org.apache.fop.fo.ElementMapping.class); 320 if (providers != null) { 321 while (providers.hasMoreElements()) { 322 String str = (String )providers.nextElement(); 323 try { 324 addElementMapping(str); 325 } catch (IllegalArgumentException e) {} 326 327 } 328 } 329 } 330 331 345 public void setRenderer(int renderer) throws IllegalArgumentException { 346 switch (renderer) { 347 case RENDER_PDF: 348 setRenderer(new org.apache.fop.render.pdf.PDFRenderer()); 349 break; 350 case RENDER_AWT: 351 throw new IllegalArgumentException ("Use renderer form of setRenderer() for AWT"); 352 case RENDER_PRINT: 353 throw new IllegalArgumentException ("Use renderer form of setRenderer() for PRINT"); 354 case RENDER_PCL: 355 setRenderer(new org.apache.fop.render.pcl.PCLRenderer()); 356 break; 357 case RENDER_PS: 358 setRenderer(new org.apache.fop.render.ps.PSRenderer()); 359 break; 360 case RENDER_TXT: 361 setRenderer(new org.apache.fop.render.txt.TXTRenderer()); 362 break; 363 case RENDER_MIF: 364 setRenderer(new org.apache.fop.render.mif.MIFRenderer()); 365 break; 366 case RENDER_XML: 367 setRenderer(new org.apache.fop.render.xml.XMLRenderer()); 368 break; 369 case RENDER_SVG: 370 setRenderer(new org.apache.fop.render.svg.SVGRenderer()); 371 break; 372 default: 373 throw new IllegalArgumentException ("Unknown renderer type"); 374 } 375 376 } 377 378 382 public void setRenderer(Renderer renderer) { 383 renderer.setLogger(getLogger()); 384 _renderer = renderer; 385 } 386 387 public Renderer getRenderer() { 388 return _renderer; 389 } 390 391 396 public void setRenderer(String rendererClassName, String version) { 397 setRenderer(rendererClassName); 398 } 399 400 408 public void setRenderer(String rendererClassName) 409 throws IllegalArgumentException { 410 try { 411 _renderer = 412 (Renderer)Class.forName(rendererClassName).newInstance(); 413 _renderer.setProducer(Version.getVersion()); 414 } catch (ClassNotFoundException e) { 415 throw new IllegalArgumentException ("Could not find " 416 + rendererClassName); 417 } 418 catch (InstantiationException e) { 419 throw new IllegalArgumentException ("Could not instantiate " 420 + rendererClassName); 421 } 422 catch (IllegalAccessException e) { 423 throw new IllegalArgumentException ("Could not access " 424 + rendererClassName); 425 } 426 catch (ClassCastException e) { 427 throw new IllegalArgumentException (rendererClassName 428 + " is not a renderer"); 429 } 430 } 431 432 438 public void addElementMapping(ElementMapping mapping) { 439 mapping.addToBuilder(_treeBuilder); 440 } 441 442 445 public void addElementMapping(String mappingClassName) 446 throws IllegalArgumentException { 447 try { 448 ElementMapping mapping = 449 (ElementMapping)Class.forName(mappingClassName).newInstance(); 450 addElementMapping(mapping); 451 } catch (ClassNotFoundException e) { 452 throw new IllegalArgumentException ("Could not find " 453 + mappingClassName); 454 } 455 catch (InstantiationException e) { 456 throw new IllegalArgumentException ("Could not instantiate " 457 + mappingClassName); 458 } 459 catch (IllegalAccessException e) { 460 throw new IllegalArgumentException ("Could not access " 461 + mappingClassName); 462 } 463 catch (ClassCastException e) { 464 throw new IllegalArgumentException (mappingClassName 465 + " is not an ElementMapping"); 466 } 467 } 468 469 476 public ContentHandler getContentHandler() { 477 if (!(_renderer instanceof org.apache.fop.render.awt.AWTRenderer 478 || _renderer instanceof org.apache.fop.render.PrintRenderer)) { 479 if (_stream == null) throw new NullPointerException ("OutputStream has not been set. Set before getting the ContentHandler"); 480 } 481 if (_renderer == null) throw new NullPointerException ("The renderer has not been set. Set before getting the ContentHandler"); 482 StreamRenderer streamRenderer = new StreamRenderer(_stream, _renderer); 483 streamRenderer.setLogger(getLogger()); 484 _treeBuilder.setLogger(getLogger()); 485 _treeBuilder.setStreamRenderer(streamRenderer); 486 487 return _treeBuilder; 488 } 489 490 494 public synchronized void render(XMLReader parser, InputSource source) 495 throws FOPException { 496 parser.setContentHandler(getContentHandler()); 497 try { 498 parser.parse(source); 499 } catch (SAXException e) { 500 if (e.getException() instanceof FOPException) { 501 throw (FOPException)e.getException(); 502 } else { 503 throw new FOPException(e); 504 } 505 } 506 catch (IOException e) { 507 throw new FOPException(e); 508 } 509 } 510 511 514 public synchronized void render(Document document) 515 throws FOPException { 516 DocumentInputSource source = new DocumentInputSource(document); 517 DocumentReader reader = new DocumentReader(); 518 render(reader, source); 519 } 520 521 524 public void dumpError(Exception e) { 525 if (_errorDump) { 526 Logger log = getLogger(); 527 if (e instanceof SAXException ) { 528 log.error("", e); 529 if (((SAXException )e).getException() != null) { 530 log.error("", ((SAXException )e).getException()); 531 } 532 } else if (e instanceof FOPException) { 533 e.printStackTrace(); 534 if (((FOPException)e).getException() != null) { 535 log.error("", ((FOPException)e).getException()); 536 } 537 } else { 538 log.error("", e); 539 } 540 } 541 } 542 543 547 public synchronized void run() throws IOException , FOPException { 548 if (_renderer == null) { 549 setRenderer(RENDER_PDF); 550 } 551 552 if (_source == null) { 553 throw new FOPException("InputSource is not set."); 554 } 555 556 if (_reader == null) { 557 if (!(_source instanceof DocumentInputSource)) { 558 _reader = ConfigurationReader.createParser(); 559 } 560 } 561 562 if (_source instanceof DocumentInputSource) { 563 render(((DocumentInputSource)_source).getDocument()); 564 } else { 565 render(_reader, _source); 566 } 567 } 568 569 } 570 571 575 578 class Service { 579 580 static HashMap providerMap = new HashMap (); 581 582 public static synchronized Enumeration providers(Class cls) { 583 ClassLoader cl = cls.getClassLoader(); 584 String serviceFile = "META-INF/services/" + cls.getName(); 585 586 588 Vector v = (Vector )providerMap.get(serviceFile); 589 if (v != null) 590 return v.elements(); 591 592 v = new Vector (); 593 providerMap.put(serviceFile, v); 594 595 Enumeration e; 596 try { 597 e = cl.getResources(serviceFile); 598 } catch (IOException ioe) { 599 return v.elements(); 600 } 601 602 while (e.hasMoreElements()) { 603 try { 604 java.net.URL u = (java.net.URL )e.nextElement(); 605 607 InputStream is = u.openStream(); 608 InputStreamReader r = new InputStreamReader (is, "UTF-8"); 609 BufferedReader br = new BufferedReader (r); 610 611 String line = br.readLine(); 612 while (line != null) { 613 try { 614 int idx = line.indexOf('#'); 616 if (idx != -1) 617 line = line.substring(0, idx); 618 619 line = line.trim(); 621 622 if (line.length() == 0) { 624 line = br.readLine(); 625 continue; 626 } 627 629 v.add(line); 633 } catch (Exception ex) { 634 } 636 637 line = br.readLine(); 638 } 639 } catch (Exception ex) { 640 } 642 643 } 644 return v.elements(); 645 } 646 647 } 648 649 | Popular Tags |