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 27KB

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