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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. /**
  145. * Sets the {@link LayoutManagerMaker} so that users can configure how FOP creates
  146. * {@link org.apache.fop.layoutmgr.LayoutManager}s.
  147. *
  148. * @param lmMaker he layout manager maker
  149. * @return <code>this</code>
  150. */
  151. public FopFactoryBuilder setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  152. fopFactoryConfigBuilder.setLayoutManagerMakerOverride(lmMaker);
  153. return this;
  154. }
  155. /**
  156. * Sets the base URI, this will be used for resolving all URIs given to FOP.
  157. *
  158. * @param baseURI the base URI
  159. * @return <code>this</code>
  160. */
  161. public FopFactoryBuilder setBaseURI(URI baseURI) {
  162. fopFactoryConfigBuilder.setBaseURI(baseURI);
  163. return this;
  164. }
  165. public FopFactoryBuilder setHyphenBaseResourceResolver(InternalResourceResolver hyphenationResourceResolver) {
  166. fopFactoryConfigBuilder.setHyphenationResourceResolver(hyphenationResourceResolver);
  167. return this;
  168. }
  169. /**
  170. * Sets whether to perform strict validation on the FO used.
  171. *
  172. * @param validateStrictly true if the FO is to be strictly validated
  173. * @return <code>this</code>
  174. */
  175. public FopFactoryBuilder setStrictFOValidation(boolean validateStrictly) {
  176. fopFactoryConfigBuilder.setStrictFOValidation(validateStrictly);
  177. return this;
  178. }
  179. /**
  180. * Sets whether to perform strict alidation on the user-configuration.
  181. *
  182. * @param validateStrictly true if the fop conf is to be strictly validated
  183. * @return <code>this</code>
  184. */
  185. public FopFactoryBuilder setStrictUserConfigValidation(
  186. boolean validateStrictly) {
  187. fopFactoryConfigBuilder.setStrictUserConfigValidation(validateStrictly);
  188. return this;
  189. }
  190. /**
  191. * Sets whether the indent inheritance should be broken when crossing reference area boundaries.
  192. *
  193. * @param value true to break inheritance when crossing reference area boundaries
  194. * @return <code>this</code>
  195. */
  196. public FopFactoryBuilder setBreakIndentInheritanceOnReferenceAreaBoundary(
  197. boolean value) {
  198. fopFactoryConfigBuilder.setBreakIndentInheritanceOnReferenceAreaBoundary(value);
  199. return this;
  200. }
  201. /**
  202. * Sets the resolution of resolution-dependent input.
  203. *
  204. * @param dpi the source resolution
  205. * @return <code>this</code>
  206. */
  207. public FopFactoryBuilder setSourceResolution(float dpi) {
  208. fopFactoryConfigBuilder.setSourceResolution(dpi);
  209. return this;
  210. }
  211. /**
  212. * Sets the resolution of resolution-dependent output.
  213. *
  214. * @param dpi the target resolution
  215. * @return <code>this</code>
  216. */
  217. public FopFactoryBuilder setTargetResolution(float dpi) {
  218. fopFactoryConfigBuilder.setTargetResolution(dpi);
  219. return this;
  220. }
  221. /**
  222. * Sets the page height of the paginated output.
  223. *
  224. * @param pageHeight the page height
  225. * @return <code>this</code>
  226. */
  227. public FopFactoryBuilder setPageHeight(String pageHeight) {
  228. fopFactoryConfigBuilder.setPageHeight(pageHeight);
  229. return this;
  230. }
  231. /**
  232. * Sets the page width of the paginated output.
  233. *
  234. * @param pageWidth the page width
  235. * @return <code>this</code>
  236. */
  237. public FopFactoryBuilder setPageWidth(String pageWidth) {
  238. fopFactoryConfigBuilder.setPageWidth(pageWidth);
  239. return this;
  240. }
  241. /**
  242. * FOP will ignore the specified XML element namespace.
  243. *
  244. * @param namespaceURI the namespace URI to ignore
  245. * @return <code>this</code>
  246. */
  247. public FopFactoryBuilder ignoreNamespace(String namespaceURI) {
  248. fopFactoryConfigBuilder.ignoreNamespace(namespaceURI);
  249. return this;
  250. }
  251. /**
  252. * FOP will ignore the colletion of XML element namespaces.
  253. *
  254. * @param namespaceURIs a collection of namespace URIs to ignore
  255. * @return <code>this</code>
  256. */
  257. public FopFactoryBuilder ignoreNamespaces(Collection<String> namespaceURIs) {
  258. fopFactoryConfigBuilder.ignoreNamespaces(namespaceURIs);
  259. return this;
  260. }
  261. /**
  262. * Sets the Avalon configuration if a FOP conf is used.
  263. *
  264. * @param cfg the fop conf configuration
  265. * @return <code>this</code>
  266. */
  267. public FopFactoryBuilder setConfiguration(Configuration cfg) {
  268. fopFactoryConfigBuilder.setConfiguration(cfg);
  269. return this;
  270. }
  271. /**
  272. * Sets whether to chose a {@link org.apache.fop.render.Renderer} in preference to an
  273. * {@link org.apache.fop.render.intermediate.IFDocumentHandler}.
  274. *
  275. * @param preferRenderer true to prefer {@link org.apache.fop.render.Renderer}
  276. * @return <code>this</code>
  277. */
  278. public FopFactoryBuilder setPreferRenderer(boolean preferRenderer) {
  279. fopFactoryConfigBuilder.setPreferRenderer(preferRenderer);
  280. return this;
  281. }
  282. public FopFactoryBuilder setComplexScriptFeatures(boolean csf) {
  283. fopFactoryConfigBuilder.setComplexScriptFeaturesEnabled(csf);
  284. return this;
  285. }
  286. public FopFactoryBuilder setHyphPatNames(Map<String, String> hyphPatNames) {
  287. fopFactoryConfigBuilder.setHyphPatNames(hyphPatNames);
  288. return this;
  289. }
  290. public static class FopFactoryConfigImpl implements FopFactoryConfig {
  291. private final EnvironmentProfile enviro;
  292. private final ImageManager imageManager;
  293. private boolean accessibility;
  294. private LayoutManagerMaker layoutManagerMaker;
  295. private URI baseURI;
  296. private InternalResourceResolver hyphenationResourceResolver;
  297. private boolean hasStrictFOValidation = true;
  298. private boolean hasStrictUserValidation = FopFactoryConfig.DEFAULT_STRICT_USERCONFIG_VALIDATION;
  299. private boolean breakIndentInheritanceOnReferenceBoundary
  300. = FopFactoryConfig.DEFAULT_BREAK_INDENT_INHERITANCE;
  301. private float sourceResolution = FopFactoryConfig.DEFAULT_SOURCE_RESOLUTION;
  302. private float targetResolution = FopFactoryConfig.DEFAULT_TARGET_RESOLUTION;
  303. private String pageHeight = FopFactoryConfig.DEFAULT_PAGE_HEIGHT;
  304. private String pageWidth = FopFactoryConfig.DEFAULT_PAGE_WIDTH;
  305. private Set<String> ignoredNamespaces = new HashSet<String>();
  306. private Configuration cfg;
  307. private boolean preferRenderer;
  308. private boolean isComplexScript = true;
  309. private Map<String, String> hyphPatNames;
  310. private static final class ImageContextImpl implements ImageContext {
  311. private final FopFactoryConfig config;
  312. ImageContextImpl(FopFactoryConfig config) {
  313. this.config = config;
  314. }
  315. public float getSourceResolution() {
  316. return config.getSourceResolution();
  317. }
  318. }
  319. FopFactoryConfigImpl(EnvironmentProfile enviro) {
  320. this.enviro = enviro;
  321. this.baseURI = enviro.getDefaultBaseURI();
  322. this.imageManager = new ImageManager(new ImageContextImpl(this));
  323. }
  324. /** {@inheritDoc} */
  325. public boolean isAccessibilityEnabled() {
  326. return accessibility;
  327. }
  328. /** {@inheritDoc} */
  329. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  330. return layoutManagerMaker;
  331. }
  332. /** {@inheritDoc} */
  333. public ResourceResolver getResourceResolver() {
  334. return enviro.getResourceResolver();
  335. }
  336. /** {@inheritDoc} */
  337. public URI getBaseURI() {
  338. return baseURI;
  339. }
  340. public InternalResourceResolver getHyphenationResourceResolver() {
  341. return hyphenationResourceResolver;
  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> getHyphenationPatternNames() {
  399. return hyphPatNames;
  400. }
  401. public FallbackResolver getFallbackResolver() {
  402. return enviro.getFallbackResolver();
  403. }
  404. }
  405. private interface FopFactoryConfigBuilder {
  406. void setAccessibility(boolean enableAccessibility);
  407. void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker);
  408. void setBaseURI(URI baseURI);
  409. void setHyphenationResourceResolver(InternalResourceResolver hyphenationResourceResolver);
  410. void setStrictFOValidation(boolean validateStrictly);
  411. void setStrictUserConfigValidation(boolean validateStrictly);
  412. void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value);
  413. void setSourceResolution(float dpi);
  414. void setTargetResolution(float dpi);
  415. void setPageHeight(String pageHeight);
  416. void setPageWidth(String pageWidth);
  417. void ignoreNamespace(String namespaceURI);
  418. void ignoreNamespaces(Collection<String> namespaceURIs);
  419. void setConfiguration(Configuration cfg);
  420. void setPreferRenderer(boolean preferRenderer);
  421. void setComplexScriptFeaturesEnabled(boolean csf);
  422. void setHyphPatNames(Map<String, String> hyphPatNames);
  423. }
  424. private static final class CompletedFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
  425. private static final CompletedFopFactoryConfigBuilder INSTANCE
  426. = new CompletedFopFactoryConfigBuilder();
  427. private void throwIllegalStateException() {
  428. throw new IllegalStateException("The final FOP Factory configuration has already been built");
  429. }
  430. public void setAccessibility(boolean enableAccessibility) {
  431. throwIllegalStateException();
  432. }
  433. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  434. throwIllegalStateException();
  435. }
  436. public void setBaseURI(URI baseURI) {
  437. throwIllegalStateException();
  438. }
  439. public void setHyphenationResourceResolver(InternalResourceResolver hyphenationResourceResolver) {
  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 setBaseURI(URI baseURI) {
  495. config.baseURI = baseURI;
  496. }
  497. public void setHyphenationResourceResolver(InternalResourceResolver hyphenationResourceResolver) {
  498. config.hyphenationResourceResolver = hyphenationResourceResolver;
  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. }