You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FopFactory.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.apps;
  19. import java.awt.color.ColorSpace;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.net.MalformedURLException;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import javax.xml.transform.Source;
  30. import javax.xml.transform.TransformerException;
  31. import javax.xml.transform.URIResolver;
  32. import org.xml.sax.SAXException;
  33. import org.apache.avalon.framework.configuration.Configuration;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. import org.apache.xmlgraphics.image.loader.ImageContext;
  37. import org.apache.xmlgraphics.image.loader.ImageManager;
  38. import org.apache.xmlgraphics.util.UnitConv;
  39. import org.apache.fop.fo.ElementMapping;
  40. import org.apache.fop.fo.ElementMappingRegistry;
  41. import org.apache.fop.fonts.FontCache;
  42. import org.apache.fop.fonts.FontManager;
  43. import org.apache.fop.hyphenation.HyphenationTreeResolver;
  44. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  45. import org.apache.fop.render.ImageHandlerRegistry;
  46. import org.apache.fop.render.RendererFactory;
  47. import org.apache.fop.render.XMLHandlerRegistry;
  48. import org.apache.fop.util.ColorSpaceCache;
  49. import org.apache.fop.util.ContentHandlerFactoryRegistry;
  50. /**
  51. * Factory class which instantiates new Fop and FOUserAgent instances. This
  52. * class also holds environmental information and configuration used by FOP.
  53. * Information that may potentially be different for each rendering run can be
  54. * found and managed in the FOUserAgent.
  55. */
  56. public class FopFactory implements ImageContext {
  57. /** logger instance */
  58. private static Log log = LogFactory.getLog(FopFactory.class);
  59. /** Factory for Renderers and FOEventHandlers */
  60. private RendererFactory rendererFactory;
  61. /** Registry for XML handlers */
  62. private XMLHandlerRegistry xmlHandlers;
  63. /** Registry for image handlers */
  64. private ImageHandlerRegistry imageHandlers;
  65. /** The registry for ElementMapping instances */
  66. private ElementMappingRegistry elementMappingRegistry;
  67. /** The registry for ContentHandlerFactory instance */
  68. private ContentHandlerFactoryRegistry contentHandlerFactoryRegistry
  69. = new ContentHandlerFactoryRegistry();
  70. /** The resolver for user-supplied hyphenation patterns */
  71. private HyphenationTreeResolver hyphResolver = null;
  72. private ColorSpaceCache colorSpaceCache = null;
  73. /** Image manager for loading and caching image objects */
  74. private ImageManager imageManager;
  75. /** Font manager for font substitution, autodetection and caching **/
  76. private FontManager fontManager;
  77. /** Configuration layer used to configure fop */
  78. private FopFactoryConfigurator config = null;
  79. /**
  80. * The base URL for all URL resolutions, especially for
  81. * external-graphics.
  82. */
  83. private String base = null;
  84. /**
  85. * Controls if accessibility is turned on or off
  86. */
  87. private boolean accessibility = false;
  88. /** The base URL for all hyphen URL resolutions. */
  89. private String hyphenBase = null;
  90. /**
  91. * Map of configured names of hyphenation pattern file names: ll_CC => name
  92. */
  93. private Map/*<String,String>*/ hyphPatNames = null;
  94. /**
  95. * FOP has the ability, for some FO's, to continue processing even if the
  96. * input XSL violates that FO's content model. This is the default
  97. * behavior for FOP. However, this flag, if set, provides the user the
  98. * ability for FOP to halt on all content model violations if desired.
  99. */
  100. private boolean strictFOValidation = FopFactoryConfigurator.DEFAULT_STRICT_FO_VALIDATION;
  101. /**
  102. * FOP will validate the contents of the user configuration strictly
  103. * (e.g. base-urls and font urls/paths).
  104. */
  105. private boolean strictUserConfigValidation
  106. = FopFactoryConfigurator.DEFAULT_STRICT_USERCONFIG_VALIDATION;
  107. /** Source resolution in dpi */
  108. private float sourceResolution = FopFactoryConfigurator.DEFAULT_SOURCE_RESOLUTION;
  109. /** Target resolution in dpi */
  110. private float targetResolution = FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION;
  111. /** Page height */
  112. private String pageHeight = FopFactoryConfigurator.DEFAULT_PAGE_HEIGHT;
  113. /** Page width */
  114. private String pageWidth = FopFactoryConfigurator.DEFAULT_PAGE_WIDTH;
  115. /** @see #setBreakIndentInheritanceOnReferenceAreaBoundary(boolean) */
  116. private boolean breakIndentInheritanceOnReferenceAreaBoundary
  117. = FopFactoryConfigurator.DEFAULT_BREAK_INDENT_INHERITANCE;
  118. /** Optional overriding LayoutManagerMaker */
  119. private LayoutManagerMaker lmMakerOverride = null;
  120. private Set ignoredNamespaces;
  121. private FOURIResolver foURIResolver;
  122. /**
  123. * Main constructor.
  124. */
  125. protected FopFactory() {
  126. this.config = new FopFactoryConfigurator(this);
  127. this.elementMappingRegistry = new ElementMappingRegistry(this);
  128. this.foURIResolver = new FOURIResolver(validateUserConfigStrictly());
  129. this.fontManager = new FontManager() {
  130. /** {@inheritDoc} */
  131. public void setFontBaseURL(String fontBase) throws MalformedURLException {
  132. super.setFontBaseURL(getFOURIResolver().checkBaseURL(fontBase));
  133. }
  134. };
  135. this.colorSpaceCache = new ColorSpaceCache(foURIResolver);
  136. this.imageManager = new ImageManager(this);
  137. this.rendererFactory = new RendererFactory();
  138. this.xmlHandlers = new XMLHandlerRegistry();
  139. this.imageHandlers = new ImageHandlerRegistry();
  140. this.ignoredNamespaces = new java.util.HashSet();
  141. }
  142. /**
  143. * Returns a new FopFactory instance.
  144. * @return the requested FopFactory instance.
  145. */
  146. public static FopFactory newInstance() {
  147. return new FopFactory();
  148. }
  149. /**
  150. * Returns a new FOUserAgent instance. Use the FOUserAgent to configure special values that
  151. * are particular to a rendering run. Don't reuse instances over multiple rendering runs but
  152. * instead create a new one each time and reuse the FopFactory.
  153. * @return the newly created FOUserAgent instance initialized with default values
  154. * @throws FOPException
  155. */
  156. public FOUserAgent newFOUserAgent() {
  157. FOUserAgent userAgent = new FOUserAgent(this);
  158. return userAgent;
  159. }
  160. /**
  161. * Sets accessibility support.
  162. *
  163. * @param value <code>true</code> to enable accessibility, <code>false</code> otherwise
  164. */
  165. void setAccessibility(boolean value) {
  166. this.accessibility = value;
  167. }
  168. boolean isAccessibilityEnabled() {
  169. return accessibility;
  170. }
  171. /**
  172. * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
  173. * instance.
  174. * <p>
  175. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  176. * use the constants defined in {@link MimeConstants}.
  177. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  178. * @return the new Fop instance
  179. * @throws FOPException when the constructor fails
  180. */
  181. public Fop newFop(String outputFormat) throws FOPException {
  182. return newFop(outputFormat, newFOUserAgent());
  183. }
  184. /**
  185. * Returns a new {@link Fop} instance. Use this factory method if you want to configure this
  186. * very rendering run, i.e. if you want to set some metadata like the title and author of the
  187. * document you want to render. In that case, create a new {@link FOUserAgent}
  188. * instance using {@link #newFOUserAgent()}.
  189. * <p>
  190. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  191. * use the constants defined in {@link MimeConstants}.
  192. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  193. * @param userAgent the user agent that will be used to control the rendering run
  194. * @return the new Fop instance
  195. * @throws FOPException when the constructor fails
  196. */
  197. public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException {
  198. return newFop(outputFormat, userAgent, null);
  199. }
  200. /**
  201. * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
  202. * instance. Use this factory method if your output type requires an output stream.
  203. * <p>
  204. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  205. * use the constants defined in {@link MimeConstants}.
  206. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  207. * @param stream the output stream
  208. * @return the new Fop instance
  209. * @throws FOPException when the constructor fails
  210. */
  211. public Fop newFop(String outputFormat, OutputStream stream) throws FOPException {
  212. return newFop(outputFormat, newFOUserAgent(), stream);
  213. }
  214. /**
  215. * Returns a new {@link Fop} instance. Use this factory method if your output type
  216. * requires an output stream and you want to configure this very rendering run,
  217. * i.e. if you want to set some metadata like the title and author of the document
  218. * you want to render. In that case, create a new {@link FOUserAgent} instance
  219. * using {@link #newFOUserAgent()}.
  220. * <p>
  221. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  222. * use the constants defined in {@link MimeConstants}.
  223. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  224. * @param userAgent the user agent that will be used to control the rendering run
  225. * @param stream the output stream
  226. * @return the new Fop instance
  227. * @throws FOPException when the constructor fails
  228. */
  229. public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream)
  230. throws FOPException {
  231. if (userAgent == null) {
  232. throw new NullPointerException("The userAgent parameter must not be null!");
  233. }
  234. return new Fop(outputFormat, userAgent, stream);
  235. }
  236. /**
  237. * Returns a new {@link Fop} instance. Use this factory method if you want to supply your
  238. * own {@link org.apache.fop.render.Renderer Renderer} or
  239. * {@link org.apache.fop.fo.FOEventHandler FOEventHandler}
  240. * instance instead of the default ones created internally by FOP.
  241. * @param userAgent the user agent that will be used to control the rendering run
  242. * @return the new Fop instance
  243. * @throws FOPException when the constructor fails
  244. */
  245. public Fop newFop(FOUserAgent userAgent) throws FOPException {
  246. if (userAgent.getRendererOverride() == null
  247. && userAgent.getFOEventHandlerOverride() == null
  248. && userAgent.getDocumentHandlerOverride() == null) {
  249. throw new IllegalStateException("An overriding renderer,"
  250. + " FOEventHandler or IFDocumentHandler must be set on the user agent"
  251. + " when this factory method is used!");
  252. }
  253. return newFop(null, userAgent);
  254. }
  255. /** @return the RendererFactory */
  256. public RendererFactory getRendererFactory() {
  257. return this.rendererFactory;
  258. }
  259. /** @return the XML handler registry */
  260. public XMLHandlerRegistry getXMLHandlerRegistry() {
  261. return this.xmlHandlers;
  262. }
  263. /** @return the image handler registry */
  264. public ImageHandlerRegistry getImageHandlerRegistry() {
  265. return this.imageHandlers;
  266. }
  267. /** @return the element mapping registry */
  268. public ElementMappingRegistry getElementMappingRegistry() {
  269. return this.elementMappingRegistry;
  270. }
  271. /** @return the content handler factory registry */
  272. public ContentHandlerFactoryRegistry getContentHandlerFactoryRegistry() {
  273. return this.contentHandlerFactoryRegistry;
  274. }
  275. /**
  276. * Returns the image manager.
  277. * @return the image manager
  278. */
  279. public ImageManager getImageManager() {
  280. return this.imageManager;
  281. }
  282. /**
  283. * Add the element mapping with the given class name.
  284. * @param elementMapping the class name representing the element mapping.
  285. */
  286. public void addElementMapping(ElementMapping elementMapping) {
  287. this.elementMappingRegistry.addElementMapping(elementMapping);
  288. }
  289. /**
  290. * Sets an explicit LayoutManagerMaker instance which overrides the one
  291. * defined by the AreaTreeHandler.
  292. * @param lmMaker the LayoutManagerMaker instance
  293. */
  294. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  295. this.lmMakerOverride = lmMaker;
  296. }
  297. /**
  298. * Returns the overriding LayoutManagerMaker instance, if any.
  299. * @return the overriding LayoutManagerMaker or null
  300. */
  301. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  302. return this.lmMakerOverride;
  303. }
  304. /**
  305. * Sets the base URL.
  306. * @param base the base URL
  307. * @throws MalformedURLException if there's a problem with a file URL
  308. */
  309. public void setBaseURL(String base) throws MalformedURLException {
  310. this.base = foURIResolver.checkBaseURL(base);
  311. }
  312. /**
  313. * Returns the base URL.
  314. * @return the base URL
  315. */
  316. public String getBaseURL() {
  317. return this.base;
  318. }
  319. /**
  320. * Sets the font base URL.
  321. * @param fontBase font base URL
  322. * @throws MalformedURLException if there's a problem with a file URL
  323. * @deprecated use getFontManager().setFontBaseURL(fontBase) instead
  324. */
  325. public void setFontBaseURL(String fontBase) throws MalformedURLException {
  326. getFontManager().setFontBaseURL(fontBase);
  327. }
  328. /**
  329. * @return the font base URL
  330. * @deprecated use getFontManager().setFontBaseURL(fontBase) instead
  331. */
  332. public String getFontBaseURL() {
  333. return getFontManager().getFontBaseURL();
  334. }
  335. /** @return the hyphen base URL */
  336. public String getHyphenBaseURL() {
  337. return this.hyphenBase;
  338. }
  339. /**
  340. * Sets the hyphen base URL.
  341. * @param hyphenBase hythen base URL
  342. * @throws MalformedURLException if there's a problem with a file URL
  343. * */
  344. public void setHyphenBaseURL(final String hyphenBase) throws MalformedURLException {
  345. if (hyphenBase != null) {
  346. setHyphenationTreeResolver(
  347. new HyphenationTreeResolver() {
  348. public Source resolve(String href) {
  349. return resolveURI(href, hyphenBase);
  350. }
  351. });
  352. }
  353. this.hyphenBase = foURIResolver.checkBaseURL(hyphenBase);
  354. }
  355. /**
  356. * @return the hyphPatNames
  357. */
  358. public Map getHyphPatNames() {
  359. return hyphPatNames;
  360. }
  361. /**
  362. * @param hyphPatNames the hyphPatNames to set
  363. */
  364. public void setHyphPatNames(Map hyphPatNames) {
  365. if (hyphPatNames == null) {
  366. hyphPatNames = new HashMap();
  367. }
  368. this.hyphPatNames = hyphPatNames;
  369. }
  370. /**
  371. * Sets the URI Resolver. It is used for resolving factory-level URIs like hyphenation
  372. * patterns and as backup for URI resolution performed during a rendering run.
  373. * @param uriResolver the new URI resolver
  374. */
  375. public void setURIResolver(URIResolver uriResolver) {
  376. foURIResolver.setCustomURIResolver(uriResolver);
  377. }
  378. /**
  379. * Returns the URI Resolver.
  380. * @return the URI Resolver
  381. */
  382. public URIResolver getURIResolver() {
  383. return foURIResolver;
  384. }
  385. /**
  386. * Returns the FO URI Resolver.
  387. * @return the FO URI Resolver
  388. */
  389. public FOURIResolver getFOURIResolver() {
  390. return foURIResolver;
  391. }
  392. /** @return the HyphenationTreeResolver for resolving user-supplied hyphenation patterns. */
  393. public HyphenationTreeResolver getHyphenationTreeResolver() {
  394. return this.hyphResolver;
  395. }
  396. /**
  397. * Sets the HyphenationTreeResolver to be used for resolving user-supplied hyphenation files.
  398. * @param hyphResolver the HyphenationTreeResolver instance
  399. */
  400. public void setHyphenationTreeResolver(HyphenationTreeResolver hyphResolver) {
  401. this.hyphResolver = hyphResolver;
  402. }
  403. /**
  404. * Activates strict XSL content model validation for FOP
  405. * Default is false (FOP will continue processing where it can)
  406. * @param validateStrictly true to turn on strict validation
  407. */
  408. public void setStrictValidation(boolean validateStrictly) {
  409. this.strictFOValidation = validateStrictly;
  410. }
  411. /**
  412. * Returns whether FOP is strictly validating input XSL
  413. * @return true of strict validation turned on, false otherwise
  414. */
  415. public boolean validateStrictly() {
  416. return strictFOValidation;
  417. }
  418. /**
  419. * @return true if the indent inheritance should be broken when crossing reference area
  420. * boundaries (for more info, see the javadoc for the relative member variable)
  421. */
  422. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  423. return breakIndentInheritanceOnReferenceAreaBoundary;
  424. }
  425. /**
  426. * Controls whether to enable a feature that breaks indent inheritance when crossing
  427. * reference area boundaries.
  428. * <p>
  429. * This flag controls whether FOP will enable special code that breaks property
  430. * inheritance for start-indent and end-indent when the evaluation of the inherited
  431. * value would cross a reference area. This is described under
  432. * http://wiki.apache.org/xmlgraphics-fop/IndentInheritance as is intended to
  433. * improve interoperability with commercial FO implementations and to produce
  434. * results that are more in line with the expectation of unexperienced FO users.
  435. * Note: Enabling this features violates the XSL specification!
  436. * @param value true to enable the feature
  437. */
  438. public void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value) {
  439. this.breakIndentInheritanceOnReferenceAreaBoundary = value;
  440. }
  441. /**
  442. * @return true if kerning on base 14 fonts is enabled
  443. * @deprecated use getFontManager().isBase14KerningEnabled() instead
  444. */
  445. public boolean isBase14KerningEnabled() {
  446. return getFontManager().isBase14KerningEnabled();
  447. }
  448. /**
  449. * Controls whether kerning is activated on base 14 fonts.
  450. * @param value true if kerning should be activated
  451. * @deprecated use getFontManager().setBase14KerningEnabled(boolean) instead
  452. */
  453. public void setBase14KerningEnabled(boolean value) {
  454. getFontManager().setBase14KerningEnabled(value);
  455. }
  456. /** @return the resolution for resolution-dependant input */
  457. public float getSourceResolution() {
  458. return this.sourceResolution;
  459. }
  460. /**
  461. * Returns the conversion factor from pixel units to millimeters. This
  462. * depends on the desired source resolution.
  463. * @return float conversion factor
  464. * @see #getSourceResolution()
  465. */
  466. public float getSourcePixelUnitToMillimeter() {
  467. return UnitConv.IN2MM / getSourceResolution();
  468. }
  469. /**
  470. * Sets the source resolution in dpi. This value is used to interpret the pixel size
  471. * of source documents like SVG images and bitmap images without resolution information.
  472. * @param dpi resolution in dpi
  473. */
  474. public void setSourceResolution(float dpi) {
  475. this.sourceResolution = dpi;
  476. if (log.isDebugEnabled()) {
  477. log.debug("source-resolution set to: " + sourceResolution
  478. + "dpi (px2mm=" + getSourcePixelUnitToMillimeter() + ")");
  479. }
  480. }
  481. /** @return the resolution for resolution-dependant output */
  482. public float getTargetResolution() {
  483. return this.targetResolution;
  484. }
  485. /**
  486. * Returns the conversion factor from pixel units to millimeters. This
  487. * depends on the desired target resolution.
  488. * @return float conversion factor
  489. * @see #getTargetResolution()
  490. */
  491. public float getTargetPixelUnitToMillimeter() {
  492. return UnitConv.IN2MM / this.targetResolution;
  493. }
  494. /**
  495. * Sets the source resolution in dpi. This value is used to interpret the pixel size
  496. * of source documents like SVG images and bitmap images without resolution information.
  497. * @param dpi resolution in dpi
  498. */
  499. public void setTargetResolution(float dpi) {
  500. this.targetResolution = dpi;
  501. }
  502. /**
  503. * Sets the source resolution in dpi. This value is used to interpret the pixel size
  504. * of source documents like SVG images and bitmap images without resolution information.
  505. * @param dpi resolution in dpi
  506. */
  507. public void setSourceResolution(int dpi) {
  508. setSourceResolution((float)dpi);
  509. }
  510. /**
  511. * Gets the default page-height to use as fallback,
  512. * in case page-height="auto"
  513. *
  514. * @return the page-height, as a String
  515. */
  516. public String getPageHeight() {
  517. return this.pageHeight;
  518. }
  519. /**
  520. * Sets the page-height to use as fallback, in case
  521. * page-height="auto"
  522. *
  523. * @param pageHeight page-height as a String
  524. */
  525. public void setPageHeight(String pageHeight) {
  526. this.pageHeight = pageHeight;
  527. if (log.isDebugEnabled()) {
  528. log.debug("Default page-height set to: " + pageHeight);
  529. }
  530. }
  531. /**
  532. * Gets the default page-width to use as fallback,
  533. * in case page-width="auto"
  534. *
  535. * @return the page-width, as a String
  536. */
  537. public String getPageWidth() {
  538. return this.pageWidth;
  539. }
  540. /**
  541. * Sets the page-width to use as fallback, in case
  542. * page-width="auto"
  543. *
  544. * @param pageWidth page-width as a String
  545. */
  546. public void setPageWidth(String pageWidth) {
  547. this.pageWidth = pageWidth;
  548. if (log.isDebugEnabled()) {
  549. log.debug("Default page-width set to: " + pageWidth);
  550. }
  551. }
  552. /**
  553. * Adds a namespace to the set of ignored namespaces.
  554. * If FOP encounters a namespace which it cannot handle, it issues a warning except if this
  555. * namespace is in the ignored set.
  556. * @param namespaceURI the namespace URI
  557. */
  558. public void ignoreNamespace(String namespaceURI) {
  559. this.ignoredNamespaces.add(namespaceURI);
  560. }
  561. /**
  562. * Adds a collection of namespaces to the set of ignored namespaces.
  563. * If FOP encounters a namespace which it cannot handle, it issues a warning except if this
  564. * namespace is in the ignored set.
  565. * @param namespaceURIs the namespace URIs
  566. */
  567. public void ignoreNamespaces(Collection namespaceURIs) {
  568. this.ignoredNamespaces.addAll(namespaceURIs);
  569. }
  570. /**
  571. * Indicates whether a namespace URI is on the ignored list.
  572. * @param namespaceURI the namespace URI
  573. * @return true if the namespace is ignored by FOP
  574. */
  575. public boolean isNamespaceIgnored(String namespaceURI) {
  576. return this.ignoredNamespaces.contains(namespaceURI);
  577. }
  578. /** @return the set of namespaces that are ignored by FOP */
  579. public Set getIgnoredNamespace() {
  580. return Collections.unmodifiableSet(this.ignoredNamespaces);
  581. }
  582. //------------------------------------------- Configuration stuff
  583. /**
  584. * Set the user configuration.
  585. * @param userConfigFile the configuration file
  586. * @throws IOException if an I/O error occurs
  587. * @throws SAXException if a parsing error occurs
  588. */
  589. public void setUserConfig(File userConfigFile) throws SAXException, IOException {
  590. config.setUserConfig(userConfigFile);
  591. }
  592. /**
  593. * Set the user configuration from an URI.
  594. * @param uri the URI to the configuration file
  595. * @throws IOException if an I/O error occurs
  596. * @throws SAXException if a parsing error occurs
  597. */
  598. public void setUserConfig(String uri) throws SAXException, IOException {
  599. config.setUserConfig(uri);
  600. }
  601. /**
  602. * Set the user configuration.
  603. * @param userConfig configuration
  604. * @throws FOPException if a configuration problem occurs
  605. */
  606. public void setUserConfig(Configuration userConfig) throws FOPException {
  607. config.setUserConfig(userConfig);
  608. }
  609. /**
  610. * Get the user configuration.
  611. * @return the user configuration
  612. */
  613. public Configuration getUserConfig() {
  614. return config.getUserConfig();
  615. }
  616. /**
  617. * Is the user configuration to be validated?
  618. * @param strictUserConfigValidation strict user config validation
  619. */
  620. public void setStrictUserConfigValidation(boolean strictUserConfigValidation) {
  621. this.strictUserConfigValidation = strictUserConfigValidation;
  622. this.foURIResolver.setThrowExceptions(strictUserConfigValidation);
  623. }
  624. /**
  625. * Is the user configuration to be validated?
  626. * @return if the user configuration should be validated
  627. */
  628. public boolean validateUserConfigStrictly() {
  629. return this.strictUserConfigValidation;
  630. }
  631. //------------------------------------------- Font related stuff
  632. /**
  633. * Whether or not to cache results of font triplet detection/auto-config
  634. * @param useCache use cache or not
  635. * @deprecated use getFontManager().setUseCache(boolean) instead
  636. */
  637. public void setUseCache(boolean useCache) {
  638. getFontManager().setUseCache(useCache);
  639. }
  640. /**
  641. * Cache results of font triplet detection/auto-config?
  642. * @return whether this factory is uses the cache
  643. * @deprecated use getFontManager().useCache() instead
  644. */
  645. public boolean useCache() {
  646. return getFontManager().useCache();
  647. }
  648. /**
  649. * Returns the font cache instance used by this factory.
  650. * @return the font cache
  651. * @deprecated use getFontManager().getFontCache() instead
  652. */
  653. public FontCache getFontCache() {
  654. return getFontManager().getFontCache();
  655. }
  656. /**
  657. * Returns the font manager.
  658. * @return the font manager
  659. */
  660. public FontManager getFontManager() {
  661. return this.fontManager;
  662. }
  663. /**
  664. * Attempts to resolve the given URI.
  665. * Will use the configured resolver and if not successful fall back
  666. * to the default resolver.
  667. * @param href URI to access
  668. * @param baseUri the base URI to resolve against
  669. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  670. * cannot be resolved.
  671. * @see org.apache.fop.apps.FOURIResolver
  672. */
  673. public Source resolveURI(String href, String baseUri) {
  674. Source source = null;
  675. try {
  676. source = foURIResolver.resolve(href, baseUri);
  677. } catch (TransformerException e) {
  678. log.error("Attempt to resolve URI '" + href + "' failed: ", e);
  679. }
  680. return source;
  681. }
  682. /**
  683. * Create (if needed) and return an ICC ColorSpace instance.
  684. *
  685. * The ICC profile source is taken from the src attribute of the color-profile FO element.
  686. * If the ICC ColorSpace is not yet in the cache a new one is created and stored in the cache.
  687. *
  688. * The FOP URI resolver is used to try and locate the ICC file.
  689. * If that fails null is returned.
  690. *
  691. * @param baseUri a base URI to resolve relative URIs
  692. * @param iccProfileSrc ICC Profile source to return a ColorSpace for
  693. * @return ICC ColorSpace object or null if ColorSpace could not be created
  694. */
  695. public ColorSpace getColorSpace(String baseUri, String iccProfileSrc) {
  696. return colorSpaceCache.get(baseUri, iccProfileSrc);
  697. }
  698. }