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.

FopFactoryConfigurator.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.io.File;
  20. import java.io.IOException;
  21. import java.net.MalformedURLException;
  22. import org.xml.sax.SAXException;
  23. import org.apache.avalon.framework.configuration.Configuration;
  24. import org.apache.avalon.framework.configuration.ConfigurationException;
  25. import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.fonts.FontManager;
  29. import org.apache.fop.fonts.FontManagerConfigurator;
  30. import org.apache.fop.util.LogUtil;
  31. /**
  32. * FopFactory configurator
  33. */
  34. public class FopFactoryConfigurator {
  35. /** Defines if FOP should use an alternative rule to determine text indents */
  36. public static final boolean DEFAULT_BREAK_INDENT_INHERITANCE = false;
  37. /** Defines if FOP should validate the user config strictly */
  38. public static final boolean DEFAULT_STRICT_USERCONFIG_VALIDATION = true;
  39. /** Defines if FOP should use strict validation for FO and user config */
  40. public static final boolean DEFAULT_STRICT_FO_VALIDATION = true;
  41. /** Defines the default page-width */
  42. public static final String DEFAULT_PAGE_WIDTH = "8.26in";
  43. /** Defines the default page-height */
  44. public static final String DEFAULT_PAGE_HEIGHT = "11in";
  45. /** Defines the default source resolution (72dpi) for FOP */
  46. public static final float DEFAULT_SOURCE_RESOLUTION = 72.0f; //dpi
  47. /** Defines the default target resolution (72dpi) for FOP */
  48. public static final float DEFAULT_TARGET_RESOLUTION = 72.0f; //dpi
  49. private static final String PREFER_RENDERER = "prefer-renderer";
  50. /** logger instance */
  51. private final Log log = LogFactory.getLog(FopFactoryConfigurator.class);
  52. /** Fop factory */
  53. private FopFactory factory = null;
  54. /** Fop factory configuration */
  55. private Configuration cfg = null;
  56. /**
  57. * Default constructor
  58. * @param factory fop factory
  59. */
  60. public FopFactoryConfigurator(FopFactory factory) {
  61. super();
  62. this.factory = factory;
  63. }
  64. /**
  65. * Initializes user agent settings from the user configuration
  66. * file, if present: baseURL, resolution, default page size,...
  67. * @param factory fop factory
  68. * @throws FOPException fop exception
  69. */
  70. public void configure(FopFactory factory) throws FOPException {
  71. if (log.isDebugEnabled()) {
  72. log.debug("Initializing FopFactory Configuration");
  73. }
  74. if (cfg.getChild("accessibility", false) != null) {
  75. try {
  76. this.factory.setAccessibility(
  77. cfg.getChild("accessibility").getValueAsBoolean());
  78. } catch (ConfigurationException e) {
  79. throw new FOPException(e);
  80. }
  81. }
  82. // strict configuration
  83. if (cfg.getChild("strict-configuration", false) != null) {
  84. try {
  85. factory.setStrictUserConfigValidation(
  86. cfg.getChild("strict-configuration").getValueAsBoolean());
  87. } catch (ConfigurationException e) {
  88. LogUtil.handleException(log, e, false);
  89. }
  90. }
  91. boolean strict = factory.validateUserConfigStrictly();
  92. // strict fo validation
  93. if (cfg.getChild("strict-validation", false) != null) {
  94. try {
  95. factory.setStrictValidation(
  96. cfg.getChild("strict-validation").getValueAsBoolean());
  97. } catch (ConfigurationException e) {
  98. LogUtil.handleException(log, e, strict);
  99. }
  100. }
  101. // base definitions for relative path resolution
  102. if (cfg.getChild("base", false) != null) {
  103. try {
  104. factory.setBaseURL(
  105. cfg.getChild("base").getValue(null));
  106. } catch (MalformedURLException mfue) {
  107. LogUtil.handleException(log, mfue, strict);
  108. }
  109. }
  110. if (cfg.getChild("hyphenation-base", false) != null) {
  111. try {
  112. factory.setHyphenBaseURL(
  113. cfg.getChild("hyphenation-base").getValue(null));
  114. } catch (MalformedURLException mfue) {
  115. LogUtil.handleException(log, mfue, strict);
  116. }
  117. }
  118. // renderer options
  119. if (cfg.getChild("source-resolution", false) != null) {
  120. factory.setSourceResolution(
  121. cfg.getChild("source-resolution").getValueAsFloat(
  122. FopFactoryConfigurator.DEFAULT_SOURCE_RESOLUTION));
  123. if (log.isDebugEnabled()) {
  124. log.debug("source-resolution set to: " + factory.getSourceResolution()
  125. + "dpi (px2mm=" + factory.getSourcePixelUnitToMillimeter() + ")");
  126. }
  127. }
  128. if (cfg.getChild("target-resolution", false) != null) {
  129. factory.setTargetResolution(
  130. cfg.getChild("target-resolution").getValueAsFloat(
  131. FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION));
  132. if (log.isDebugEnabled()) {
  133. log.debug("target-resolution set to: " + factory.getTargetResolution()
  134. + "dpi (px2mm=" + factory.getTargetPixelUnitToMillimeter()
  135. + ")");
  136. }
  137. }
  138. if (cfg.getChild("break-indent-inheritance", false) != null) {
  139. try {
  140. factory.setBreakIndentInheritanceOnReferenceAreaBoundary(
  141. cfg.getChild("break-indent-inheritance").getValueAsBoolean());
  142. } catch (ConfigurationException e) {
  143. LogUtil.handleException(log, e, strict);
  144. }
  145. }
  146. Configuration pageConfig = cfg.getChild("default-page-settings");
  147. if (pageConfig.getAttribute("height", null) != null) {
  148. factory.setPageHeight(
  149. pageConfig.getAttribute("height", FopFactoryConfigurator.DEFAULT_PAGE_HEIGHT));
  150. if (log.isInfoEnabled()) {
  151. log.info("Default page-height set to: " + factory.getPageHeight());
  152. }
  153. }
  154. if (pageConfig.getAttribute("width", null) != null) {
  155. factory.setPageWidth(
  156. pageConfig.getAttribute("width", FopFactoryConfigurator.DEFAULT_PAGE_WIDTH));
  157. if (log.isInfoEnabled()) {
  158. log.info("Default page-width set to: " + factory.getPageWidth());
  159. }
  160. }
  161. // prefer Renderer over IFDocumentHandler
  162. if (cfg.getChild(PREFER_RENDERER, false) != null) {
  163. try {
  164. factory.getRendererFactory().setRendererPreferred(
  165. cfg.getChild(PREFER_RENDERER).getValueAsBoolean());
  166. } catch (ConfigurationException e) {
  167. LogUtil.handleException(log, e, strict);
  168. }
  169. }
  170. // configure font manager
  171. FontManager fontManager = factory.getFontManager();
  172. FontManagerConfigurator fontManagerConfigurator = new FontManagerConfigurator(cfg);
  173. fontManagerConfigurator.configure(fontManager, strict);
  174. }
  175. /**
  176. * Set the user configuration.
  177. * @param userConfigFile the configuration file
  178. * @throws IOException if an I/O error occurs
  179. * @throws SAXException if a parsing error occurs
  180. */
  181. public void setUserConfig(File userConfigFile) throws SAXException, IOException {
  182. try {
  183. DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  184. setUserConfig(cfgBuilder.buildFromFile(userConfigFile));
  185. } catch (ConfigurationException e) {
  186. throw new FOPException(e);
  187. }
  188. }
  189. /**
  190. * Set the user configuration from an URI.
  191. * @param uri the URI to the configuration file
  192. * @throws IOException if an I/O error occurs
  193. * @throws SAXException if a parsing error occurs
  194. */
  195. public void setUserConfig(String uri) throws SAXException, IOException {
  196. try {
  197. DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  198. setUserConfig(cfgBuilder.build(uri));
  199. } catch (ConfigurationException e) {
  200. throw new FOPException(e);
  201. }
  202. }
  203. /**
  204. * Set the user configuration.
  205. * @param cfg avalon configuration
  206. * @throws FOPException if a configuration problem occurs
  207. */
  208. public void setUserConfig(Configuration cfg) throws FOPException {
  209. this.cfg = cfg;
  210. configure(this.factory);
  211. }
  212. /**
  213. * Get the avalon user configuration.
  214. * @return the user configuration
  215. */
  216. public Configuration getUserConfig() {
  217. return this.cfg;
  218. }
  219. }