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

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