Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FopFactory.java 29KB

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