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

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