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

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