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.

FopFactoryBuilder.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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.net.URI;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.HashSet;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import javax.xml.transform.URIResolver;
  26. import org.apache.avalon.framework.configuration.Configuration;
  27. import org.apache.xmlgraphics.image.loader.ImageContext;
  28. import org.apache.xmlgraphics.image.loader.ImageManager;
  29. import org.apache.fop.apps.io.ResourceResolver;
  30. import org.apache.fop.apps.io.ResourceResolverFactory;
  31. import org.apache.fop.fonts.FontManager;
  32. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  33. /**
  34. * This is the builder class for {@link FopFactory}. Setters can be chained to
  35. * make building a {@link FopFactory} object more concise and intuitive e.g.
  36. *
  37. * <pre>
  38. * {@code
  39. * FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(<URI>)
  40. * .setURIResolver(<URIResolver>)
  41. * .setPageHeight(<String>)
  42. * .setPageWidth(<String>)
  43. * .setStrictUserConfigValidation(<boolean>)
  44. * ... etc ...
  45. * FopFactory fopFactory = fopFactoryBuilder.build();
  46. * }
  47. * </pre>
  48. */
  49. public final class FopFactoryBuilder {
  50. private final FopFactoryConfig config;
  51. private FopFactoryConfigBuilder fopFactoryConfigBuilder;
  52. /**
  53. * A builder class for {@link FopFactory} which can be used for setting configuration. This is
  54. * a helper constructor that uses the default URI resolver implementation that FOP packages
  55. * provide ({@link DefaultResourceResolver}).
  56. *
  57. * @param defaultBaseURI the default base URI for resolving URIs against
  58. */
  59. public FopFactoryBuilder(URI defaultBaseURI) {
  60. this(defaultBaseURI, ResourceResolverFactory.createDefaultResourceResolver());
  61. }
  62. /**
  63. * A builder class for {@link FopFactory} which can be used for setting configuration.
  64. *
  65. * @param defaultBaseURI the default base URI for resolving URIs against
  66. * @param uriResolver the URI resolver
  67. */
  68. public FopFactoryBuilder(URI defaultBaseURI, ResourceResolver uriResolver) {
  69. this(EnvironmentalProfileFactory.createDefault(defaultBaseURI, uriResolver));
  70. }
  71. /**
  72. * A builder class for {@link FopFactory} which can be used for setting configuration.
  73. *
  74. * @param enviro the profile of the FOP deployment environment
  75. */
  76. public FopFactoryBuilder(EnvironmentProfile enviro) {
  77. config = new FopFactoryConfigImpl(enviro);
  78. fopFactoryConfigBuilder = new ActiveFopFactoryConfigBuilder((FopFactoryConfigImpl) config);
  79. }
  80. /**
  81. * Returns the {@link FopFactoryConfig} which is needed to get an instance of
  82. * {@link FopFactory}.
  83. *
  84. * @return build the {@link FopFactoryConfig}
  85. * @deprecated Exposing the {@link FopFactoryConfig} is only to maintain backwards compatibility
  86. */
  87. public FopFactoryConfig buildConfig() {
  88. fopFactoryConfigBuilder = CompletedFopFactoryConfigBuilder.INSTANCE;
  89. return config;
  90. }
  91. /**
  92. * Builds an instance of the the {@link FopFactory}.
  93. *
  94. * @return the FopFactory instance
  95. */
  96. public FopFactory build() {
  97. return FopFactory.newInstance(buildConfig());
  98. }
  99. /**
  100. * Gets the base URI used to resolve all URIs within FOP.
  101. *
  102. * @return the base URI
  103. */
  104. URI getBaseUri() {
  105. return config.getBaseURI();
  106. }
  107. /**
  108. * Returns the {@link FontManager} used for managing the fonts within FOP.
  109. *
  110. * @return the font managing object
  111. */
  112. public FontManager getFontManager() {
  113. return config.getFontManager();
  114. }
  115. /**
  116. * Return the {@link ImageManager} used for handling images through out FOP.
  117. *
  118. * @return the image manager
  119. */
  120. public ImageManager getImageManager() {
  121. return config.getImageManager();
  122. }
  123. /**
  124. * Sets whether to include accessibility features in document creation.
  125. *
  126. * @param enableAccessibility true to set accessibility on
  127. * @return <code>this</code>
  128. */
  129. public FopFactoryBuilder setAccessibility(boolean enableAccessibility) {
  130. fopFactoryConfigBuilder.setAccessibility(enableAccessibility);
  131. return this;
  132. }
  133. /**
  134. * Sets the {@link LayoutManagerMaker} so that users can configure how FOP creates
  135. * {@link LayoutManager}s.
  136. *
  137. * @param lmMaker he layout manager maker
  138. * @return <code>this</code>
  139. */
  140. public FopFactoryBuilder setLayoutManagerMakerOverride(
  141. LayoutManagerMaker lmMaker) {
  142. fopFactoryConfigBuilder.setLayoutManagerMakerOverride(lmMaker);
  143. return this;
  144. }
  145. /**
  146. * Sets the URI resolver to be used for controlling FOP's file access.
  147. *
  148. * @param resolver the URI resolver
  149. * @return <code>this</code>
  150. * @deprecated this URIResolver will be phased out in favour of a unified URI resolution
  151. * mechanism
  152. */
  153. public FopFactoryBuilder setURIResolver(URIResolver resolver) {
  154. fopFactoryConfigBuilder.setURIResolver(resolver);
  155. return this;
  156. }
  157. /**
  158. * Sets the base URI, this will be used for resolving all URIs given to FOP.
  159. *
  160. * @param baseURI the base URI
  161. * @return <code>this</code>
  162. */
  163. public FopFactoryBuilder setBaseURI(URI baseURI) {
  164. fopFactoryConfigBuilder.setBaseURI(baseURI);
  165. return this;
  166. }
  167. /**
  168. * Sets whether to perform strict validation on the FO used.
  169. *
  170. * @param validateStrictly true if the FO is to be strictly validated
  171. * @return <code>this</code>
  172. */
  173. public FopFactoryBuilder setStrictFOValidation(boolean validateStrictly) {
  174. fopFactoryConfigBuilder.setStrictFOValidation(validateStrictly);
  175. return this;
  176. }
  177. /**
  178. * Sets whether to perform strict alidation on the user-configuration.
  179. *
  180. * @param validateStrictly true if the fop conf is to be strictly validated
  181. * @return <code>this</code>
  182. */
  183. public FopFactoryBuilder setStrictUserConfigValidation(
  184. boolean validateStrictly) {
  185. fopFactoryConfigBuilder.setStrictUserConfigValidation(validateStrictly);
  186. return this;
  187. }
  188. /**
  189. * Sets whether the indent inheritance should be broken when crossing reference area boundaries.
  190. *
  191. * @param value true to break inheritance when crossing reference area boundaries
  192. * @return <code>this</code>
  193. */
  194. public FopFactoryBuilder setBreakIndentInheritanceOnReferenceAreaBoundary(
  195. boolean value) {
  196. fopFactoryConfigBuilder.setBreakIndentInheritanceOnReferenceAreaBoundary(value);
  197. return this;
  198. }
  199. /**
  200. * Sets the resolution of resolution-dependent input.
  201. *
  202. * @param dpi the source resolution
  203. * @return <code>this</code>
  204. */
  205. public FopFactoryBuilder setSourceResolution(float dpi) {
  206. fopFactoryConfigBuilder.setSourceResolution(dpi);
  207. return this;
  208. }
  209. /**
  210. * Sets the resolution of resolution-dependent output.
  211. *
  212. * @param dpi the target resolution
  213. * @return <code>this</code>
  214. */
  215. public FopFactoryBuilder setTargetResolution(float dpi) {
  216. fopFactoryConfigBuilder.setTargetResolution(dpi);
  217. return this;
  218. }
  219. /**
  220. * Sets the page height of the paginated output.
  221. *
  222. * @param pageHeight the page height
  223. * @return <code>this</code>
  224. */
  225. public FopFactoryBuilder setPageHeight(String pageHeight) {
  226. fopFactoryConfigBuilder.setPageHeight(pageHeight);
  227. return this;
  228. }
  229. /**
  230. * Sets the page width of the paginated output.
  231. *
  232. * @param pageWidth the page width
  233. * @return <code>this</code>
  234. */
  235. public FopFactoryBuilder setPageWidth(String pageWidth) {
  236. fopFactoryConfigBuilder.setPageWidth(pageWidth);
  237. return this;
  238. }
  239. /**
  240. * FOP will ignore the specified XML element namespace.
  241. *
  242. * @param namespaceURI the namespace URI to ignore
  243. * @return <code>this</code>
  244. */
  245. public FopFactoryBuilder ignoreNamespace(String namespaceURI) {
  246. fopFactoryConfigBuilder.ignoreNamespace(namespaceURI);
  247. return this;
  248. }
  249. /**
  250. * FOP will ignore the colletion of XML element namespaces.
  251. *
  252. * @param namespaceURIs a collection of namespace URIs to ignore
  253. * @return <code>this</code>
  254. */
  255. public FopFactoryBuilder ignoreNamespaces(Collection<String> namespaceURIs) {
  256. fopFactoryConfigBuilder.ignoreNamespaces(namespaceURIs);
  257. return this;
  258. }
  259. /**
  260. * Sets the Avalon configuration if a FOP conf is used.
  261. *
  262. * @param cfg the fop conf configuration
  263. * @return <code>this</code>
  264. */
  265. public FopFactoryBuilder setConfiguration(Configuration cfg) {
  266. fopFactoryConfigBuilder.setConfiguration(cfg);
  267. return this;
  268. }
  269. /**
  270. * Sets whether to chose a {@link Renderer} in preference to an
  271. * {@link org.apache.fop.render.intermediate.IFDocumentHandler}.
  272. *
  273. * @see {@link RendererFactory}
  274. * @param preferRenderer true to prefer {@link Renderer}
  275. * @return <code>this</code>
  276. */
  277. public FopFactoryBuilder setPreferRenderer(boolean preferRenderer) {
  278. fopFactoryConfigBuilder.setPreferRenderer(preferRenderer);
  279. return this;
  280. }
  281. public FopFactoryBuilder setComplexScriptFeatures(boolean csf) {
  282. fopFactoryConfigBuilder.setComplexScriptFeaturesEnabled(csf);
  283. return this;
  284. }
  285. public FopFactoryBuilder setHyphPatNames(Map<String, String> hyphPatNames) {
  286. fopFactoryConfigBuilder.setHyphPatNames(hyphPatNames);
  287. return this;
  288. }
  289. public static class FopFactoryConfigImpl implements FopFactoryConfig {
  290. private final EnvironmentProfile enviro;
  291. private final ImageManager imageManager;
  292. private boolean accessibility;
  293. private LayoutManagerMaker layoutManagerMaker;
  294. private URI baseURI;
  295. private boolean hasStrictFOValidation = true;
  296. private boolean hasStrictUserValidation = FopFactoryConfig.DEFAULT_STRICT_USERCONFIG_VALIDATION;
  297. private boolean breakIndentInheritanceOnReferenceBoundary
  298. = FopFactoryConfig.DEFAULT_BREAK_INDENT_INHERITANCE;
  299. private float sourceResolution = FopFactoryConfig.DEFAULT_SOURCE_RESOLUTION;
  300. private float targetResolution = FopFactoryConfig.DEFAULT_TARGET_RESOLUTION;
  301. private String pageHeight = FopFactoryConfig.DEFAULT_PAGE_HEIGHT;
  302. private String pageWidth = FopFactoryConfig.DEFAULT_PAGE_WIDTH;
  303. private Set<String> ignoredNamespaces = new HashSet<String>();
  304. private URIResolver resolver;
  305. private Configuration cfg;
  306. private boolean preferRenderer;
  307. private boolean isComplexScript = true;
  308. private Map<String, String> hyphPatNames;
  309. private static final class ImageContextImpl implements ImageContext {
  310. private final FopFactoryConfig config;
  311. ImageContextImpl(FopFactoryConfig config) {
  312. this.config = config;
  313. }
  314. public float getSourceResolution() {
  315. return config.getSourceResolution();
  316. }
  317. }
  318. FopFactoryConfigImpl(EnvironmentProfile enviro) {
  319. this.enviro = enviro;
  320. this.baseURI = enviro.getDefaultBaseURI();
  321. this.imageManager = new ImageManager(new ImageContextImpl(this));
  322. }
  323. /** {@inheritDoc} */
  324. public boolean isAccessibilityEnabled() {
  325. return accessibility;
  326. }
  327. /** {@inheritDoc} */
  328. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  329. return layoutManagerMaker;
  330. }
  331. /** {@inheritDoc} */
  332. public ResourceResolver getNewURIResolver() {
  333. return enviro.getResourceResolver();
  334. }
  335. /** {@inheritDoc} */
  336. public URIResolver getURIResolver() {
  337. return resolver;
  338. }
  339. /** {@inheritDoc} */
  340. public URI getBaseURI() {
  341. return baseURI;
  342. }
  343. /** {@inheritDoc} */
  344. public boolean validateStrictly() {
  345. return hasStrictFOValidation;
  346. }
  347. /** {@inheritDoc} */
  348. public boolean validateUserConfigStrictly() {
  349. return hasStrictUserValidation;
  350. }
  351. /** {@inheritDoc} */
  352. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  353. return breakIndentInheritanceOnReferenceBoundary;
  354. }
  355. /** {@inheritDoc} */
  356. public float getSourceResolution() {
  357. return sourceResolution;
  358. }
  359. /** {@inheritDoc} */
  360. public float getTargetResolution() {
  361. return targetResolution;
  362. }
  363. /** {@inheritDoc} */
  364. public String getPageHeight() {
  365. return pageHeight;
  366. }
  367. /** {@inheritDoc} */
  368. public String getPageWidth() {
  369. return pageWidth;
  370. }
  371. /** {@inheritDoc} */
  372. public Set<String> getIgnoredNamespaces() {
  373. return Collections.unmodifiableSet(ignoredNamespaces);
  374. }
  375. /** {@inheritDoc} */
  376. public boolean isNamespaceIgnored(String namespace) {
  377. return ignoredNamespaces.contains(namespace);
  378. }
  379. /** {@inheritDoc} */
  380. public Configuration getUserConfig() {
  381. return cfg;
  382. }
  383. /** {@inheritDoc} */
  384. public boolean preferRenderer() {
  385. return preferRenderer;
  386. }
  387. /** {@inheritDoc} */
  388. public FontManager getFontManager() {
  389. return enviro.getFontManager();
  390. }
  391. /** {@inheritDoc} */
  392. public ImageManager getImageManager() {
  393. return imageManager;
  394. }
  395. public boolean isComplexScriptFeaturesEnabled() {
  396. return isComplexScript;
  397. }
  398. public Map<String, String> getHyphPatNames() {
  399. return hyphPatNames;
  400. }
  401. }
  402. private interface FopFactoryConfigBuilder {
  403. void setAccessibility(boolean enableAccessibility);
  404. void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker);
  405. void setURIResolver(URIResolver resolver);
  406. void setBaseURI(URI baseURI);
  407. void setStrictFOValidation(boolean validateStrictly);
  408. void setStrictUserConfigValidation(boolean validateStrictly);
  409. void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value);
  410. void setSourceResolution(float dpi);
  411. void setTargetResolution(float dpi);
  412. void setPageHeight(String pageHeight);
  413. void setPageWidth(String pageWidth);
  414. void ignoreNamespace(String namespaceURI);
  415. void ignoreNamespaces(Collection<String> namespaceURIs);
  416. void setConfiguration(Configuration cfg);
  417. void setPreferRenderer(boolean preferRenderer);
  418. void setComplexScriptFeaturesEnabled(boolean csf);
  419. void setHyphPatNames(Map<String, String> hyphPatNames);
  420. }
  421. private static final class CompletedFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
  422. private static final CompletedFopFactoryConfigBuilder INSTANCE
  423. = new CompletedFopFactoryConfigBuilder();
  424. private void throwIllegalStateException() {
  425. throw new IllegalStateException("The final FOP Factory configuration has already been built");
  426. }
  427. public void setAccessibility(boolean enableAccessibility) {
  428. throwIllegalStateException();
  429. }
  430. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  431. throwIllegalStateException();
  432. }
  433. public void setURIResolver(URIResolver resolver) {
  434. throwIllegalStateException();
  435. }
  436. public void setBaseURI(URI baseURI) {
  437. throwIllegalStateException();
  438. }
  439. public void setHyphenationBaseURI(URI hyphenationBase) {
  440. throwIllegalStateException();
  441. }
  442. public void setStrictFOValidation(boolean validateStrictly) {
  443. throwIllegalStateException();
  444. }
  445. public void setStrictUserConfigValidation(boolean validateStrictly) {
  446. throwIllegalStateException();
  447. }
  448. public void setBreakIndentInheritanceOnReferenceAreaBoundary(
  449. boolean value) {
  450. throwIllegalStateException();
  451. }
  452. public void setSourceResolution(float dpi) {
  453. throwIllegalStateException();
  454. }
  455. public void setTargetResolution(float dpi) {
  456. throwIllegalStateException();
  457. }
  458. public void setPageHeight(String pageHeight) {
  459. throwIllegalStateException();
  460. }
  461. public void setPageWidth(String pageWidth) {
  462. throwIllegalStateException();
  463. }
  464. public void ignoreNamespace(String namespaceURI) {
  465. throwIllegalStateException();
  466. }
  467. public void ignoreNamespaces(Collection<String> namespaceURIs) {
  468. throwIllegalStateException();
  469. }
  470. public void setConfiguration(Configuration cfg) {
  471. throwIllegalStateException();
  472. }
  473. public void setPreferRenderer(boolean preferRenderer) {
  474. throwIllegalStateException();
  475. }
  476. public void setComplexScriptFeaturesEnabled(boolean csf) {
  477. throwIllegalStateException();
  478. }
  479. public void setHyphPatNames(Map<String, String> hyphPatNames) {
  480. throwIllegalStateException();
  481. }
  482. }
  483. private static final class ActiveFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
  484. private final FopFactoryConfigImpl config;
  485. private ActiveFopFactoryConfigBuilder(FopFactoryConfigImpl config) {
  486. this.config = config;
  487. }
  488. public void setAccessibility(boolean enableAccessibility) {
  489. config.accessibility = enableAccessibility;
  490. }
  491. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  492. config.layoutManagerMaker = lmMaker;
  493. }
  494. public void setURIResolver(URIResolver resolver) {
  495. config.resolver = resolver;
  496. }
  497. public void setBaseURI(URI baseURI) {
  498. config.baseURI = baseURI;
  499. }
  500. public void setStrictFOValidation(boolean validateStrictly) {
  501. config.hasStrictFOValidation = validateStrictly;
  502. }
  503. public void setStrictUserConfigValidation(
  504. boolean validateStrictly) {
  505. config.hasStrictUserValidation = validateStrictly;
  506. }
  507. public void setBreakIndentInheritanceOnReferenceAreaBoundary(
  508. boolean value) {
  509. config.breakIndentInheritanceOnReferenceBoundary = value;
  510. }
  511. public void setSourceResolution(float dpi) {
  512. config.sourceResolution = dpi;
  513. }
  514. public void setTargetResolution(float dpi) {
  515. config.targetResolution = dpi;
  516. }
  517. public void setPageHeight(String pageHeight) {
  518. config.pageHeight = pageHeight;
  519. }
  520. public void setPageWidth(String pageWidth) {
  521. config.pageWidth = pageWidth;
  522. }
  523. public void ignoreNamespace(String namespaceURI) {
  524. config.ignoredNamespaces.add(namespaceURI);
  525. }
  526. public void ignoreNamespaces(
  527. Collection<String> namespaceURIs) {
  528. config.ignoredNamespaces.addAll(namespaceURIs);
  529. }
  530. public void setConfiguration(Configuration cfg) {
  531. config.cfg = cfg;
  532. }
  533. public void setPreferRenderer(boolean preferRenderer) {
  534. config.preferRenderer = preferRenderer;
  535. }
  536. public void setComplexScriptFeaturesEnabled(boolean csf) {
  537. config.isComplexScript = csf;
  538. }
  539. public void setHyphPatNames(Map<String, String> hyphPatNames) {
  540. config.hyphPatNames = hyphPatNames;
  541. }
  542. }
  543. }