1 17 18 19 20 package org.apache.fop.apps; 21 22 import java.awt.color.ColorSpace ; 23 import java.awt.color.ICC_ColorSpace ; 24 import java.awt.color.ICC_Profile ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.net.MalformedURLException ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import javax.xml.transform.Source ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.URIResolver ; 37 import javax.xml.transform.stream.StreamSource ; 38 39 import org.xml.sax.SAXException ; 40 41 import org.apache.avalon.framework.configuration.Configuration; 42 import org.apache.avalon.framework.configuration.ConfigurationException; 43 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 44 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 48 import org.apache.fop.fo.ElementMapping; 49 import org.apache.fop.fo.ElementMappingRegistry; 50 import org.apache.fop.hyphenation.HyphenationTreeResolver; 51 import org.apache.fop.image.ImageFactory; 52 import org.apache.fop.layoutmgr.LayoutManagerMaker; 53 import org.apache.fop.render.RendererFactory; 54 import org.apache.fop.render.XMLHandlerRegistry; 55 import org.apache.fop.util.ContentHandlerFactoryRegistry; 56 57 62 public class FopFactory { 63 64 65 private static final float DEFAULT_SOURCE_RESOLUTION = 72.0f; 67 private static final String DEFAULT_PAGE_HEIGHT = "11in"; 68 69 private static final String DEFAULT_PAGE_WIDTH = "8.26in"; 70 71 72 private static Log log = LogFactory.getLog(FopFactory.class); 73 74 75 private RendererFactory rendererFactory = new RendererFactory(); 76 77 78 private XMLHandlerRegistry xmlHandlers = new XMLHandlerRegistry(); 79 80 81 private ElementMappingRegistry elementMappingRegistry; 82 83 84 private ContentHandlerFactoryRegistry contentHandlerFactoryRegistry 85 = new ContentHandlerFactoryRegistry(); 86 87 88 private URIResolver foURIResolver = new FOURIResolver(); 89 90 private URIResolver uriResolver = null; 91 92 93 private HyphenationTreeResolver hyphResolver; 94 95 private ImageFactory imageFactory = new ImageFactory(); 96 97 98 private Configuration userConfig = null; 99 100 101 private String fontBaseURL; 102 103 109 private boolean strictValidation = true; 110 111 112 private boolean enableBase14Kerning = false; 113 114 115 private float sourceResolution = DEFAULT_SOURCE_RESOLUTION; 116 private String pageHeight = DEFAULT_PAGE_HEIGHT; 117 private String pageWidth = DEFAULT_PAGE_WIDTH; 118 119 120 private boolean breakIndentInheritanceOnReferenceAreaBoundary = false; 121 122 123 private LayoutManagerMaker lmMakerOverride = null; 124 125 private Set ignoredNamespaces = new java.util.HashSet (); 126 127 128 private Map colorSpaceMap = null; 129 130 133 protected FopFactory() { 134 this.elementMappingRegistry = new ElementMappingRegistry(this); 135 this.colorSpaceMap = Collections.synchronizedMap(new java.util.HashMap ()); 137 } 138 139 143 public static FopFactory newInstance() { 144 return new FopFactory(); 145 } 146 147 153 public FOUserAgent newFOUserAgent() { 154 FOUserAgent userAgent = new FOUserAgent(this); 155 return userAgent; 156 } 157 158 168 public Fop newFop(String outputFormat) throws FOPException { 169 return new Fop(outputFormat, newFOUserAgent()); 170 } 171 172 185 public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException { 186 if (userAgent == null) { 187 throw new NullPointerException ("The userAgent parameter must not be null!"); 188 } 189 return new Fop(outputFormat, userAgent); 190 } 191 192 203 public Fop newFop(String outputFormat, OutputStream stream) throws FOPException { 204 return new Fop(outputFormat, newFOUserAgent(), stream); 205 } 206 207 222 public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream) 223 throws FOPException { 224 if (userAgent == null) { 225 throw new NullPointerException ("The userAgent parameter must not be null!"); 226 } 227 return new Fop(outputFormat, userAgent, stream); 228 } 229 230 239 public Fop newFop(FOUserAgent userAgent) throws FOPException { 240 if (userAgent.getRendererOverride() == null 241 && userAgent.getFOEventHandlerOverride() == null) { 242 throw new IllegalStateException ("Either the overriding renderer or the overriding" 243 + " FOEventHandler must be set when this factory method is used!"); 244 } 245 return newFop(null, userAgent); 246 } 247 248 249 public RendererFactory getRendererFactory() { 250 return this.rendererFactory; 251 } 252 253 254 public XMLHandlerRegistry getXMLHandlerRegistry() { 255 return this.xmlHandlers; 256 } 257 258 259 public ElementMappingRegistry getElementMappingRegistry() { 260 return this.elementMappingRegistry; 261 } 262 263 264 public ContentHandlerFactoryRegistry getContentHandlerFactoryRegistry() { 265 return this.contentHandlerFactoryRegistry; 266 } 267 268 269 public ImageFactory getImageFactory() { 270 return this.imageFactory; 271 } 272 273 277 public void addElementMapping(ElementMapping elementMapping) { 278 this.elementMappingRegistry.addElementMapping(elementMapping); 279 } 280 281 286 public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) { 287 this.lmMakerOverride = lmMaker; 288 } 289 290 294 public LayoutManagerMaker getLayoutManagerMakerOverride() { 295 return this.lmMakerOverride; 296 } 297 298 302 public void setFontBaseURL(String fontBaseURL) { 303 this.fontBaseURL = fontBaseURL; 304 } 305 306 307 public String getFontBaseURL() { 308 return this.fontBaseURL; 309 } 310 311 316 public void setURIResolver(URIResolver resolver) { 317 this.uriResolver = resolver; 318 } 319 320 324 public URIResolver getURIResolver() { 325 return this.uriResolver; 326 } 327 328 329 public HyphenationTreeResolver getHyphenationTreeResolver() { 330 return this.hyphResolver; 331 } 332 333 338 public void setStrictValidation(boolean validateStrictly) { 339 this.strictValidation = validateStrictly; 340 } 341 342 346 public boolean validateStrictly() { 347 return strictValidation; 348 } 349 350 354 public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() { 355 return breakIndentInheritanceOnReferenceAreaBoundary; 356 } 357 358 371 public void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value) { 372 this.breakIndentInheritanceOnReferenceAreaBoundary = value; 373 } 374 375 376 public boolean isBase14KerningEnabled() { 377 return this.enableBase14Kerning; 378 } 379 380 384 public void setBase14KerningEnabled(boolean value) { 385 this.enableBase14Kerning = value; 386 } 387 388 389 public float getSourceResolution() { 390 return this.sourceResolution; 391 } 392 393 399 public float getSourcePixelUnitToMillimeter() { 400 return 25.4f / getSourceResolution(); 401 } 402 403 408 public void setSourceResolution(int dpi) { 409 this.sourceResolution = dpi; 410 } 411 412 418 public String getPageHeight() { 419 return this.pageHeight; 420 } 421 422 428 public void setPageHeight(String pageHeight) { 429 this.pageHeight = pageHeight; 430 } 431 432 438 public String getPageWidth() { 439 return this.pageWidth; 440 } 441 442 448 public void setPageWidth(String pageWidth) { 449 this.pageWidth = pageWidth; 450 } 451 452 458 public void ignoreNamespace(String namespaceURI) { 459 this.ignoredNamespaces.add(namespaceURI); 460 } 461 462 468 public void ignoreNamespaces(Collection namespaceURIs) { 469 this.ignoredNamespaces.addAll(namespaceURIs); 470 } 471 472 477 public boolean isNamespaceIgnored(String namespaceURI) { 478 return this.ignoredNamespaces.contains(namespaceURI); 479 } 480 481 482 public Set getIgnoredNamespace() { 483 return Collections.unmodifiableSet(this.ignoredNamespaces); 484 } 485 486 488 494 public void setUserConfig(File userConfigFile) 495 throws SAXException , IOException { 496 try { 497 DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); 498 setUserConfig(cfgBuilder.buildFromFile(userConfigFile)); 499 } catch (ConfigurationException cfge) { 500 log.error("Error loading configuration: " 501 + cfge.getMessage()); 502 } 503 } 504 505 511 public void setUserConfig(String uri) 512 throws SAXException , IOException { 513 try { 514 DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); 515 setUserConfig(cfgBuilder.build(uri)); 516 } catch (ConfigurationException cfge) { 517 log.error("Error loading configuration: " 518 + cfge.getMessage()); 519 } 520 } 521 522 526 public void setUserConfig(Configuration userConfig) { 527 this.userConfig = userConfig; 528 try { 529 initUserConfig(); 530 } catch (ConfigurationException cfge) { 531 log.error("Error initializing factory configuration: " 532 + cfge.getMessage()); 533 } 534 } 535 536 540 public Configuration getUserConfig() { 541 return userConfig; 542 } 543 544 551 public void initUserConfig() throws ConfigurationException { 552 log.debug("Initializing User Agent Configuration"); 553 setFontBaseURL(getBaseURLfromConfig(userConfig, "font-base")); 554 final String hyphBase = getBaseURLfromConfig(userConfig, "hyphenation-base"); 555 if (hyphBase != null) { 556 this.hyphResolver = new HyphenationTreeResolver() { 557 public Source resolve(String href) { 558 return resolveURI(href, hyphBase); 559 } 560 }; 561 } 562 if (userConfig.getChild("source-resolution", false) != null) { 563 this.sourceResolution 564 = userConfig.getChild("source-resolution").getValueAsFloat( 565 DEFAULT_SOURCE_RESOLUTION); 566 log.info("Source resolution set to: " + sourceResolution 567 + "dpi (px2mm=" + getSourcePixelUnitToMillimeter() + ")"); 568 } 569 if (userConfig.getChild("strict-validation", false) != null) { 570 this.strictValidation = userConfig.getChild("strict-validation").getValueAsBoolean(); 571 } 572 if (userConfig.getChild("break-indent-inheritance", false) != null) { 573 this.breakIndentInheritanceOnReferenceAreaBoundary 574 = userConfig.getChild("break-indent-inheritance").getValueAsBoolean(); 575 } 576 Configuration pageConfig = userConfig.getChild("default-page-settings"); 577 if (pageConfig.getAttribute("height", null) != null) { 578 setPageHeight(pageConfig.getAttribute("height")); 579 log.info("Default page-height set to: " + pageHeight); 580 } 581 if (pageConfig.getAttribute("width", null) != null) { 582 setPageWidth(pageConfig.getAttribute("width")); 583 log.info("Default page-width set to: " + pageWidth); 584 } 585 } 586 587 593 public static String getBaseURLfromConfig(Configuration cfg, String name) { 594 if (cfg.getChild(name, false) != null) { 595 try { 596 String cfgBaseDir = cfg.getChild(name).getValue(null); 597 if (cfgBaseDir != null) { 598 File dir = new File (cfgBaseDir); 599 if (dir.isDirectory()) { 600 cfgBaseDir = dir.toURL().toExternalForm(); 601 } 602 } 603 log.info(name + " set to: " + cfgBaseDir); 604 return cfgBaseDir; 605 } catch (MalformedURLException mue) { 606 log.error("Base URL in user config is malformed!"); 607 } 608 } 609 return null; 610 } 611 612 614 624 public Source resolveURI(String uri, String base) { 625 Source source = null; 626 boolean bypassURIResolution = uri.startsWith("data:"); 628 if (!bypassURIResolution && uriResolver != null) { 629 try { 630 source = uriResolver.resolve(uri, base); 631 } catch (TransformerException te) { 632 log.error("Attempt to resolve URI '" + uri + "' failed: ", te); 633 } 634 } 635 if (source == null) { 636 try { 638 source = foURIResolver.resolve(uri, base); 639 } catch (TransformerException te) { 640 log.error("Attempt to resolve URI '" + uri + "' failed: ", te); 641 } 642 } 643 return source; 644 } 645 646 659 public ColorSpace getColorSpace(String base, String iccProfileSrc) { 660 ColorSpace colorSpace = null; 661 if (!this.colorSpaceMap.containsKey(base + iccProfileSrc)) { 662 try { 663 ICC_Profile iccProfile = null; 664 Source src = this.resolveURI(iccProfileSrc, base); 667 if (src != null && src instanceof StreamSource ) { 668 iccProfile = ICC_Profile.getInstance(((StreamSource ) src) 671 .getInputStream()); 672 } else { 673 } 681 if (iccProfile != null) { 682 colorSpace = new ICC_ColorSpace (iccProfile); 683 } 684 } catch (IOException e) { 685 } 688 689 if (colorSpace != null) { 690 this.colorSpaceMap.put(base + iccProfileSrc, colorSpace); 692 } else { 693 log.warn("Color profile '" + iccProfileSrc + "' not found."); 696 } 697 } else { 698 colorSpace = (ColorSpace ) this.colorSpaceMap.get(base 699 + iccProfileSrc); 700 } 701 return colorSpace; 702 } 703 704 } 705 | Popular Tags |