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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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.fop.apps.io.ResourceResolver;
  29. import org.apache.fop.apps.io.ResourceResolverFactory;
  30. import org.apache.fop.fonts.FontManager;
  31. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  32. /**
  33. * This is the builder class for {@link FopFactory}. Setters can be chained to
  34. * make building a {@link FopFactory} object more concise and intuitive e.g.
  35. *
  36. * <pre>
  37. * {@code
  38. * FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(<URI>)
  39. * .setURIResolver(<URIResolver>)
  40. * .setPageHeight(<String>)
  41. * .setPageWidth(<String>)
  42. * .setStrictUserConfigValidation(<boolean>)
  43. * ... etc ...
  44. * FopFactory fopFactory = fopFactoryBuilder.build();
  45. * }
  46. * </pre>
  47. */
  48. public final class FopFactoryBuilder {
  49. private final FopFactoryConfig config;
  50. private FopFactoryConfigBuilder fopFactoryConfigBuilder;
  51. /**
  52. * A builder class for {@link FopFactory} which can be used for setting configuration. This is
  53. * a helper constructor that uses the default URI resolver implementation that FOP packages
  54. * provide ({@link DefaultResourceResolver}).
  55. *
  56. * @param defaultBaseURI the default base URI for resolving URIs against
  57. */
  58. public FopFactoryBuilder(URI defaultBaseURI) {
  59. this(defaultBaseURI, ResourceResolverFactory.createDefaultResourceResolver());
  60. }
  61. /**
  62. * A builder class for {@link FopFactory} which can be used for setting configuration.
  63. *
  64. * @param defaultBaseURI the default base URI for resolving URIs against
  65. * @param resourceResolver the URI resolver
  66. */
  67. public FopFactoryBuilder(URI defaultBaseURI, ResourceResolver resourceResolver) {
  68. this(EnvironmentalProfileFactory.createDefault(defaultBaseURI, resourceResolver));
  69. }
  70. /**
  71. * A builder class for {@link FopFactory} which can be used for setting configuration.
  72. *
  73. * @param enviro the profile of the FOP deployment environment
  74. */
  75. public FopFactoryBuilder(EnvironmentProfile enviro) {
  76. config = new FopFactoryConfigImpl(enviro);
  77. fopFactoryConfigBuilder = new ActiveFopFactoryConfigBuilder((FopFactoryConfigImpl) config);
  78. }
  79. /**
  80. * Returns the {@link FopFactoryConfig} which is needed to get an instance of
  81. * {@link FopFactory}.
  82. *
  83. * @return build the {@link FopFactoryConfig}
  84. * @deprecated Exposing the {@link FopFactoryConfig} is only to maintain backwards compatibility
  85. */
  86. public FopFactoryConfig buildConfig() {
  87. return buildConfiguration();
  88. }
  89. /**
  90. * Builds the configuration object used by the FopFactory.
  91. *
  92. * @return the config for the {@link FopFactory}
  93. */
  94. // The {@link FopFactoryConfig} doesn't need to be exposed in the "public" API, this method
  95. // should remain package private.
  96. FopFactoryConfig buildConfiguration() {
  97. fopFactoryConfigBuilder = CompletedFopFactoryConfigBuilder.INSTANCE;
  98. return config;
  99. }
  100. /**
  101. * Builds an instance of the the {@link FopFactory}.
  102. *
  103. * @return the FopFactory instance
  104. */
  105. public FopFactory build() {
  106. return FopFactory.newInstance(buildConfiguration());
  107. }
  108. /**
  109. * Gets the base URI used to resolve all URIs within FOP.
  110. *
  111. * @return the base URI
  112. */
  113. URI getBaseURI() {
  114. return config.getBaseURI();
  115. }
  116. /**
  117. * Returns the {@link FontManager} used for managing the fonts within FOP.
  118. *
  119. * @return the font managing object
  120. */
  121. public FontManager getFontManager() {
  122. return config.getFontManager();
  123. }
  124. /**
  125. * Return the {@link ImageManager} used for handling images through out FOP.
  126. *
  127. * @return the image manager
  128. */
  129. public ImageManager getImageManager() {
  130. return config.getImageManager();
  131. }
  132. /**
  133. * Sets whether to include accessibility features in document creation.
  134. *
  135. * @param enableAccessibility true to set accessibility on
  136. * @return <code>this</code>
  137. */
  138. public FopFactoryBuilder setAccessibility(boolean enableAccessibility) {
  139. fopFactoryConfigBuilder.setAccessibility(enableAccessibility);
  140. return this;
  141. }
  142. /**
  143. * Sets the {@link LayoutManagerMaker} so that users can configure how FOP creates
  144. * {@link LayoutManager}s.
  145. *
  146. * @param lmMaker he layout manager maker
  147. * @return <code>this</code>
  148. */
  149. public FopFactoryBuilder setLayoutManagerMakerOverride(
  150. 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 Renderer} in preference to an
  268. * {@link org.apache.fop.render.intermediate.IFDocumentHandler}.
  269. *
  270. * @see {@link RendererFactory}
  271. * @param preferRenderer true to prefer {@link Renderer}
  272. * @return <code>this</code>
  273. */
  274. public FopFactoryBuilder setPreferRenderer(boolean preferRenderer) {
  275. fopFactoryConfigBuilder.setPreferRenderer(preferRenderer);
  276. return this;
  277. }
  278. public FopFactoryBuilder setComplexScriptFeatures(boolean csf) {
  279. fopFactoryConfigBuilder.setComplexScriptFeaturesEnabled(csf);
  280. return this;
  281. }
  282. public FopFactoryBuilder setHyphPatNames(Map<String, String> hyphPatNames) {
  283. fopFactoryConfigBuilder.setHyphPatNames(hyphPatNames);
  284. return this;
  285. }
  286. public static class FopFactoryConfigImpl implements FopFactoryConfig {
  287. private final EnvironmentProfile enviro;
  288. private final ImageManager imageManager;
  289. private boolean accessibility;
  290. private LayoutManagerMaker layoutManagerMaker;
  291. private URI baseURI;
  292. private boolean hasStrictFOValidation = true;
  293. private boolean hasStrictUserValidation = FopFactoryConfig.DEFAULT_STRICT_USERCONFIG_VALIDATION;
  294. private boolean breakIndentInheritanceOnReferenceBoundary
  295. = FopFactoryConfig.DEFAULT_BREAK_INDENT_INHERITANCE;
  296. private float sourceResolution = FopFactoryConfig.DEFAULT_SOURCE_RESOLUTION;
  297. private float targetResolution = FopFactoryConfig.DEFAULT_TARGET_RESOLUTION;
  298. private String pageHeight = FopFactoryConfig.DEFAULT_PAGE_HEIGHT;
  299. private String pageWidth = FopFactoryConfig.DEFAULT_PAGE_WIDTH;
  300. private Set<String> ignoredNamespaces = new HashSet<String>();
  301. private Configuration cfg;
  302. private boolean preferRenderer;
  303. private boolean isComplexScript = true;
  304. private Map<String, String> hyphPatNames;
  305. private static final class ImageContextImpl implements ImageContext {
  306. private final FopFactoryConfig config;
  307. ImageContextImpl(FopFactoryConfig config) {
  308. this.config = config;
  309. }
  310. public float getSourceResolution() {
  311. return config.getSourceResolution();
  312. }
  313. }
  314. FopFactoryConfigImpl(EnvironmentProfile enviro) {
  315. this.enviro = enviro;
  316. this.baseURI = enviro.getDefaultBaseURI();
  317. this.imageManager = new ImageManager(new ImageContextImpl(this));
  318. }
  319. /** {@inheritDoc} */
  320. public boolean isAccessibilityEnabled() {
  321. return accessibility;
  322. }
  323. /** {@inheritDoc} */
  324. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  325. return layoutManagerMaker;
  326. }
  327. /** {@inheritDoc} */
  328. public ResourceResolver getResourceResolver() {
  329. return enviro.getResourceResolver();
  330. }
  331. /** {@inheritDoc} */
  332. public URI getBaseURI() {
  333. return baseURI;
  334. }
  335. /** {@inheritDoc} */
  336. public boolean validateStrictly() {
  337. return hasStrictFOValidation;
  338. }
  339. /** {@inheritDoc} */
  340. public boolean validateUserConfigStrictly() {
  341. return hasStrictUserValidation;
  342. }
  343. /** {@inheritDoc} */
  344. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  345. return breakIndentInheritanceOnReferenceBoundary;
  346. }
  347. /** {@inheritDoc} */
  348. public float getSourceResolution() {
  349. return sourceResolution;
  350. }
  351. /** {@inheritDoc} */
  352. public float getTargetResolution() {
  353. return targetResolution;
  354. }
  355. /** {@inheritDoc} */
  356. public String getPageHeight() {
  357. return pageHeight;
  358. }
  359. /** {@inheritDoc} */
  360. public String getPageWidth() {
  361. return pageWidth;
  362. }
  363. /** {@inheritDoc} */
  364. public Set<String> getIgnoredNamespaces() {
  365. return Collections.unmodifiableSet(ignoredNamespaces);
  366. }
  367. /** {@inheritDoc} */
  368. public boolean isNamespaceIgnored(String namespace) {
  369. return ignoredNamespaces.contains(namespace);
  370. }
  371. /** {@inheritDoc} */
  372. public Configuration getUserConfig() {
  373. return cfg;
  374. }
  375. /** {@inheritDoc} */
  376. public boolean preferRenderer() {
  377. return preferRenderer;
  378. }
  379. /** {@inheritDoc} */
  380. public FontManager getFontManager() {
  381. return enviro.getFontManager();
  382. }
  383. /** {@inheritDoc} */
  384. public ImageManager getImageManager() {
  385. return imageManager;
  386. }
  387. public boolean isComplexScriptFeaturesEnabled() {
  388. return isComplexScript;
  389. }
  390. public Map<String, String> getHyphenationPatternNames() {
  391. return hyphPatNames;
  392. }
  393. }
  394. private interface FopFactoryConfigBuilder {
  395. void setAccessibility(boolean enableAccessibility);
  396. void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker);
  397. void setBaseURI(URI baseURI);
  398. void setStrictFOValidation(boolean validateStrictly);
  399. void setStrictUserConfigValidation(boolean validateStrictly);
  400. void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value);
  401. void setSourceResolution(float dpi);
  402. void setTargetResolution(float dpi);
  403. void setPageHeight(String pageHeight);
  404. void setPageWidth(String pageWidth);
  405. void ignoreNamespace(String namespaceURI);
  406. void ignoreNamespaces(Collection<String> namespaceURIs);
  407. void setConfiguration(Configuration cfg);
  408. void setPreferRenderer(boolean preferRenderer);
  409. void setComplexScriptFeaturesEnabled(boolean csf);
  410. void setHyphPatNames(Map<String, String> hyphPatNames);
  411. }
  412. private static final class CompletedFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
  413. private static final CompletedFopFactoryConfigBuilder INSTANCE
  414. = new CompletedFopFactoryConfigBuilder();
  415. private void throwIllegalStateException() {
  416. throw new IllegalStateException("The final FOP Factory configuration has already been built");
  417. }
  418. public void setAccessibility(boolean enableAccessibility) {
  419. throwIllegalStateException();
  420. }
  421. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  422. throwIllegalStateException();
  423. }
  424. public void setBaseURI(URI baseURI) {
  425. throwIllegalStateException();
  426. }
  427. public void setStrictFOValidation(boolean validateStrictly) {
  428. throwIllegalStateException();
  429. }
  430. public void setStrictUserConfigValidation(boolean validateStrictly) {
  431. throwIllegalStateException();
  432. }
  433. public void setBreakIndentInheritanceOnReferenceAreaBoundary(
  434. boolean value) {
  435. throwIllegalStateException();
  436. }
  437. public void setSourceResolution(float dpi) {
  438. throwIllegalStateException();
  439. }
  440. public void setTargetResolution(float dpi) {
  441. throwIllegalStateException();
  442. }
  443. public void setPageHeight(String pageHeight) {
  444. throwIllegalStateException();
  445. }
  446. public void setPageWidth(String pageWidth) {
  447. throwIllegalStateException();
  448. }
  449. public void ignoreNamespace(String namespaceURI) {
  450. throwIllegalStateException();
  451. }
  452. public void ignoreNamespaces(Collection<String> namespaceURIs) {
  453. throwIllegalStateException();
  454. }
  455. public void setConfiguration(Configuration cfg) {
  456. throwIllegalStateException();
  457. }
  458. public void setPreferRenderer(boolean preferRenderer) {
  459. throwIllegalStateException();
  460. }
  461. public void setComplexScriptFeaturesEnabled(boolean csf) {
  462. throwIllegalStateException();
  463. }
  464. public void setHyphPatNames(Map<String, String> hyphPatNames) {
  465. throwIllegalStateException();
  466. }
  467. }
  468. private static final class ActiveFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
  469. private final FopFactoryConfigImpl config;
  470. private ActiveFopFactoryConfigBuilder(FopFactoryConfigImpl config) {
  471. this.config = config;
  472. }
  473. public void setAccessibility(boolean enableAccessibility) {
  474. config.accessibility = enableAccessibility;
  475. }
  476. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  477. config.layoutManagerMaker = lmMaker;
  478. }
  479. public void setBaseURI(URI baseURI) {
  480. config.baseURI = baseURI;
  481. }
  482. public void setStrictFOValidation(boolean validateStrictly) {
  483. config.hasStrictFOValidation = validateStrictly;
  484. }
  485. public void setStrictUserConfigValidation(
  486. boolean validateStrictly) {
  487. config.hasStrictUserValidation = validateStrictly;
  488. }
  489. public void setBreakIndentInheritanceOnReferenceAreaBoundary(
  490. boolean value) {
  491. config.breakIndentInheritanceOnReferenceBoundary = value;
  492. }
  493. public void setSourceResolution(float dpi) {
  494. config.sourceResolution = dpi;
  495. }
  496. public void setTargetResolution(float dpi) {
  497. config.targetResolution = dpi;
  498. }
  499. public void setPageHeight(String pageHeight) {
  500. config.pageHeight = pageHeight;
  501. }
  502. public void setPageWidth(String pageWidth) {
  503. config.pageWidth = pageWidth;
  504. }
  505. public void ignoreNamespace(String namespaceURI) {
  506. config.ignoredNamespaces.add(namespaceURI);
  507. }
  508. public void ignoreNamespaces(
  509. Collection<String> namespaceURIs) {
  510. config.ignoredNamespaces.addAll(namespaceURIs);
  511. }
  512. public void setConfiguration(Configuration cfg) {
  513. config.cfg = cfg;
  514. }
  515. public void setPreferRenderer(boolean preferRenderer) {
  516. config.preferRenderer = preferRenderer;
  517. }
  518. public void setComplexScriptFeaturesEnabled(boolean csf) {
  519. config.isComplexScript = csf;
  520. }
  521. public void setHyphPatNames(Map<String, String> hyphPatNames) {
  522. config.hyphPatNames = hyphPatNames;
  523. }
  524. }
  525. }