Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FopFactory.java 27KB

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