1 17 18 19 20 package org.apache.fop.apps; 21 22 import java.io.File ; 24 import java.util.Date ; 25 import java.util.Map ; 26 import javax.xml.transform.Source ; 27 import javax.xml.transform.TransformerException ; 28 import javax.xml.transform.URIResolver ; 29 30 import org.apache.avalon.framework.configuration.Configuration; 32 import org.apache.avalon.framework.configuration.ConfigurationException; 33 34 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import org.apache.fop.Version; 40 import org.apache.fop.fo.FOEventHandler; 41 import org.apache.fop.pdf.PDFEncryptionParams; 42 import org.apache.fop.render.Renderer; 43 import org.apache.fop.render.RendererFactory; 44 import org.apache.fop.render.XMLHandlerRegistry; 45 import org.apache.fop.render.pdf.PDFRenderer; 46 47 67 public class FOUserAgent { 68 69 70 public static final float DEFAULT_TARGET_RESOLUTION = 72.0f; 72 private static Log log = LogFactory.getLog("FOP"); 73 74 private FopFactory factory; 75 76 77 private String baseURL; 78 79 80 private URIResolver uriResolver = null; 81 82 private float targetResolution = DEFAULT_TARGET_RESOLUTION; 83 private Map rendererOptions = new java.util.HashMap (); 84 private File outputFile = null; 85 private Renderer rendererOverride = null; 86 private FOEventHandler foEventHandlerOverride = null; 87 88 91 protected String producer = "Apache FOP Version " + Version.getVersion(); 92 93 96 protected String creator = null; 97 98 101 protected Date creationDate = null; 102 103 104 protected String author = null; 105 106 protected String title = null; 107 108 protected String keywords = null; 109 110 116 public FOUserAgent() { 117 this(FopFactory.newInstance()); 118 } 119 120 126 public FOUserAgent(FopFactory factory) { 127 if (factory == null) { 128 throw new NullPointerException ("The factory parameter must not be null"); 129 } 130 this.factory = factory; 131 if (factory.getUserConfig() != null) { 132 configure(factory.getUserConfig()); 133 } 134 } 135 136 137 public FopFactory getFactory() { 138 return this.factory; 139 } 140 141 143 148 public void setRendererOverride(Renderer renderer) { 149 this.rendererOverride = renderer; 150 } 151 152 156 public Renderer getRendererOverride() { 157 return rendererOverride; 158 } 159 160 165 public void setFOEventHandlerOverride(FOEventHandler handler) { 166 this.foEventHandlerOverride = handler; 167 } 168 169 173 public FOEventHandler getFOEventHandlerOverride() { 174 return this.foEventHandlerOverride; 175 } 176 177 181 public void setProducer(String producer) { 182 this.producer = producer; 183 } 184 185 189 public String getProducer() { 190 return producer; 191 } 192 193 197 public void setCreator(String creator) { 198 this.creator = creator; 199 } 200 201 205 public String getCreator() { 206 return creator; 207 } 208 209 213 public void setCreationDate(Date creationDate) { 214 this.creationDate = creationDate; 215 } 216 217 221 public Date getCreationDate() { 222 return creationDate; 223 } 224 225 229 public void setAuthor(String author) { 230 this.author = author; 231 } 232 233 237 public String getAuthor() { 238 return author; 239 } 240 241 246 public void setTitle(String title) { 247 this.title = title; 248 } 249 250 254 public String getTitle() { 255 return title; 256 } 257 258 262 public void setKeywords(String keywords) { 263 this.keywords = keywords; 264 } 265 266 270 public String getKeywords() { 271 return keywords; 272 } 273 274 278 public Map getRendererOptions() { 279 return rendererOptions; 280 } 281 282 287 protected void configure(Configuration cfg) { 288 setBaseURL(FopFactory.getBaseURLfromConfig(cfg, "base")); 289 if (cfg.getChild("target-resolution", false) != null) { 290 this.targetResolution 291 = cfg.getChild("target-resolution").getValueAsFloat( 292 DEFAULT_TARGET_RESOLUTION); 293 log.info("Target resolution set to: " + targetResolution 294 + "dpi (px2mm=" + getTargetPixelUnitToMillimeter() + ")"); 295 } 296 } 297 298 303 public Configuration getUserRendererConfig(String mimeType) { 304 305 Configuration cfg = getFactory().getUserConfig(); 306 if (cfg == null || mimeType == null) { 307 return null; 308 } 309 310 Configuration userRendererConfig = null; 311 312 Configuration[] cfgs 313 = cfg.getChild("renderers").getChildren("renderer"); 314 for (int i = 0; i < cfgs.length; ++i) { 315 Configuration child = cfgs[i]; 316 try { 317 if (child.getAttribute("mime").equals(mimeType)) { 318 userRendererConfig = child; 319 break; 320 } 321 } catch (ConfigurationException e) { 322 } 324 } 325 log.debug((userRendererConfig == null ? "No u" : "U") 326 + "ser configuration found for MIME type " + mimeType); 327 return userRendererConfig; 328 } 329 330 334 public void setBaseURL(String baseURL) { 335 this.baseURL = baseURL; 336 } 337 338 342 public String getBaseURL() { 343 return this.baseURL; 344 } 345 346 350 public void setURIResolver(URIResolver resolver) { 351 this.uriResolver = resolver; 352 } 353 354 358 public URIResolver getURIResolver() { 359 return this.uriResolver; 360 } 361 362 368 public PDFEncryptionParams getPDFEncryptionParams() { 369 return (PDFEncryptionParams)getRendererOptions().get(PDFRenderer.ENCRYPTION_PARAMS); 370 } 371 372 380 public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) { 381 getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, pdfEncryptionParams); 382 } 383 384 385 394 public Source resolveURI(String uri) { 395 return resolveURI(uri, getBaseURL()); 396 } 397 398 408 public Source resolveURI(String uri, String base) { 409 Source source = null; 410 boolean bypassURIResolution = uri.startsWith("data:"); 412 if (!bypassURIResolution && uriResolver != null) { 413 try { 414 source = uriResolver.resolve(uri, base); 415 } catch (TransformerException te) { 416 log.error("Attempt to resolve URI '" + uri + "' failed: ", te); 417 } 418 } 419 if (source == null) { 420 source = getFactory().resolveURI(uri, base); 422 } 423 return source; 424 } 425 426 430 public void setOutputFile(File f) { 431 this.outputFile = f; 432 } 433 434 438 public File getOutputFile() { 439 return outputFile; 440 } 441 442 448 public float getTargetPixelUnitToMillimeter() { 449 return 25.4f / this.targetResolution; 450 } 451 452 453 public float getTargetResolution() { 454 return this.targetResolution; 455 } 456 457 463 public void setTargetResolution(int dpi) { 464 this.targetResolution = dpi; 465 } 466 467 470 471 public String getFontBaseURL() { 472 String fontBaseURL = getFactory().getFontBaseURL(); 473 return fontBaseURL != null ? fontBaseURL : this.baseURL; 474 } 475 476 482 public float getSourcePixelUnitToMillimeter() { 483 return getFactory().getSourcePixelUnitToMillimeter(); 484 } 485 486 487 public float getSourceResolution() { 488 return getFactory().getSourceResolution(); 489 } 490 491 498 public String getPageHeight() { 499 return getFactory().getPageHeight(); 500 } 501 502 509 public String getPageWidth() { 510 return getFactory().getPageWidth(); 511 } 512 513 518 public boolean validateStrictly() { 519 return getFactory().validateStrictly(); 520 } 521 522 527 public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() { 528 return getFactory().isBreakIndentInheritanceOnReferenceAreaBoundary(); 529 } 530 531 534 public RendererFactory getRendererFactory() { 535 return getFactory().getRendererFactory(); 536 } 537 538 541 public XMLHandlerRegistry getXMLHandlerRegistry() { 542 return getFactory().getXMLHandlerRegistry(); 543 } 544 545 } 546 547 | Popular Tags |