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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 java.util.HashMap;
  23. import java.util.Map;
  24. import org.xml.sax.SAXException;
  25. import org.apache.avalon.framework.configuration.Configuration;
  26. import org.apache.avalon.framework.configuration.ConfigurationException;
  27. import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.xmlgraphics.image.GraphicsConstants;
  31. import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry;
  32. import org.apache.xmlgraphics.image.loader.util.Penalty;
  33. import org.apache.fop.fonts.FontManagerConfigurator;
  34. import org.apache.fop.hyphenation.HyphenationTreeCache;
  35. import org.apache.fop.util.LogUtil;
  36. /**
  37. * FopFactory configurator
  38. */
  39. public class FopFactoryConfigurator {
  40. /** Defines if FOP should use an alternative rule to determine text indents */
  41. public static final boolean DEFAULT_BREAK_INDENT_INHERITANCE = false;
  42. /** Defines if FOP should validate the user config strictly */
  43. public static final boolean DEFAULT_STRICT_USERCONFIG_VALIDATION = true;
  44. /** Defines if FOP should use strict validation for FO and user config */
  45. public static final boolean DEFAULT_STRICT_FO_VALIDATION = true;
  46. /** Defines the default page-width */
  47. public static final String DEFAULT_PAGE_WIDTH = "8.26in";
  48. /** Defines the default page-height */
  49. public static final String DEFAULT_PAGE_HEIGHT = "11in";
  50. /** Defines the default source resolution (72dpi) for FOP */
  51. public static final float DEFAULT_SOURCE_RESOLUTION = GraphicsConstants.DEFAULT_DPI; //dpi
  52. /** Defines the default target resolution (72dpi) for FOP */
  53. public static final float DEFAULT_TARGET_RESOLUTION = GraphicsConstants.DEFAULT_DPI; //dpi
  54. private static final String PREFER_RENDERER = "prefer-renderer";
  55. /** logger instance */
  56. private final Log log = LogFactory.getLog(FopFactoryConfigurator.class);
  57. /** Fop factory */
  58. private FopFactory factory = null;
  59. /** Fop factory configuration */
  60. private Configuration cfg = null;
  61. /**
  62. * Default constructor
  63. * @param factory fop factory
  64. */
  65. public FopFactoryConfigurator(FopFactory factory) {
  66. super();
  67. this.factory = factory;
  68. }
  69. /**
  70. * Initializes user agent settings from the user configuration
  71. * file, if present: baseURL, resolution, default page size,...
  72. * @param factory fop factory
  73. * @throws FOPException fop exception
  74. */
  75. public void configure(FopFactory factory) throws FOPException { // CSOK: MethodLength
  76. // strict configuration
  77. if (cfg.getChild("strict-configuration", false) != null) {
  78. try {
  79. factory.setStrictUserConfigValidation(
  80. cfg.getChild("strict-configuration").getValueAsBoolean());
  81. } catch (ConfigurationException e) {
  82. LogUtil.handleException(log, e, false);
  83. }
  84. }
  85. boolean strict = factory.validateUserConfigStrictly();
  86. if (log.isDebugEnabled()) {
  87. log.debug("Initializing FopFactory Configuration"
  88. + "with " + (strict ? "strict" : "permissive") + " validation");
  89. }
  90. if (cfg.getChild("accessibility", false) != null) {
  91. try {
  92. this.factory.setAccessibility(
  93. cfg.getChild("accessibility").getValueAsBoolean());
  94. } catch (ConfigurationException e) {
  95. LogUtil.handleException(log, e, strict);
  96. }
  97. }
  98. // strict fo validation
  99. if (cfg.getChild("strict-validation", false) != null) {
  100. try {
  101. factory.setStrictValidation(
  102. cfg.getChild("strict-validation").getValueAsBoolean());
  103. } catch (ConfigurationException e) {
  104. LogUtil.handleException(log, e, strict);
  105. }
  106. }
  107. // base definitions for relative path resolution
  108. if (cfg.getChild("base", false) != null) {
  109. try {
  110. factory.setBaseURL(
  111. cfg.getChild("base").getValue(null));
  112. } catch (MalformedURLException mfue) {
  113. LogUtil.handleException(log, mfue, strict);
  114. }
  115. }
  116. if (cfg.getChild("hyphenation-base", false) != null) {
  117. try {
  118. factory.setHyphenBaseURL(
  119. cfg.getChild("hyphenation-base").getValue(null));
  120. } catch (MalformedURLException mfue) {
  121. LogUtil.handleException(log, mfue, strict);
  122. }
  123. }
  124. /**
  125. * Read configuration elements hyphenation-pattern,
  126. * construct a map ll_CC => filename, and set it on the factory
  127. */
  128. Configuration[] hyphPatConfig = cfg.getChildren("hyphenation-pattern");
  129. if (hyphPatConfig.length != 0) {
  130. Map/*<String,String>*/ hyphPatNames = new HashMap/*<String,String>*/();
  131. for (int i = 0; i < hyphPatConfig.length; ++i) {
  132. String lang, country, filename;
  133. StringBuffer error = new StringBuffer();
  134. String location = hyphPatConfig[i].getLocation();
  135. lang = hyphPatConfig[i].getAttribute("lang", null);
  136. if (lang == null) {
  137. addError("The lang attribute of a hyphenation-pattern configuration"
  138. + " element must exist (" + location + ")", error);
  139. } else if (!lang.matches("[a-zA-Z]{2}")) {
  140. addError("The lang attribute of a hyphenation-pattern configuration"
  141. + " element must consist of exactly two letters ("
  142. + location + ")", error);
  143. }
  144. lang = lang.toLowerCase();
  145. country = hyphPatConfig[i].getAttribute("country", null);
  146. if ("".equals(country)) {
  147. country = null;
  148. }
  149. if (country != null) {
  150. if (!country.matches("[a-zA-Z]{2}")) {
  151. addError("The country attribute of a hyphenation-pattern configuration"
  152. + " element must consist of exactly two letters ("
  153. + location + ")", error);
  154. }
  155. country = country.toUpperCase();
  156. }
  157. filename = hyphPatConfig[i].getValue(null);
  158. if (filename == null) {
  159. addError("The value of a hyphenation-pattern configuration"
  160. + " element may not be empty (" + location + ")", error);
  161. }
  162. if (error.length() != 0) {
  163. LogUtil.handleError(log, error.toString(), strict);
  164. continue;
  165. }
  166. String llccKey = HyphenationTreeCache.constructLlccKey(lang, country);
  167. hyphPatNames.put(llccKey, filename);
  168. if (log.isDebugEnabled()) {
  169. log.debug("Using hyphenation pattern filename " + filename
  170. + " for lang=\"" + lang + "\""
  171. + (country != null ? ", country=\"" + country + "\"" : ""));
  172. }
  173. }
  174. factory.setHyphPatNames(hyphPatNames);
  175. }
  176. // renderer options
  177. if (cfg.getChild("source-resolution", false) != null) {
  178. factory.setSourceResolution(
  179. cfg.getChild("source-resolution").getValueAsFloat(
  180. FopFactoryConfigurator.DEFAULT_SOURCE_RESOLUTION));
  181. if (log.isDebugEnabled()) {
  182. log.debug("source-resolution set to: " + factory.getSourceResolution()
  183. + "dpi (px2mm=" + factory.getSourcePixelUnitToMillimeter() + ")");
  184. }
  185. }
  186. if (cfg.getChild("target-resolution", false) != null) {
  187. factory.setTargetResolution(
  188. cfg.getChild("target-resolution").getValueAsFloat(
  189. FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION));
  190. if (log.isDebugEnabled()) {
  191. log.debug("target-resolution set to: " + factory.getTargetResolution()
  192. + "dpi (px2mm=" + factory.getTargetPixelUnitToMillimeter()
  193. + ")");
  194. }
  195. }
  196. if (cfg.getChild("break-indent-inheritance", false) != null) {
  197. try {
  198. factory.setBreakIndentInheritanceOnReferenceAreaBoundary(
  199. cfg.getChild("break-indent-inheritance").getValueAsBoolean());
  200. } catch (ConfigurationException e) {
  201. LogUtil.handleException(log, e, strict);
  202. }
  203. }
  204. Configuration pageConfig = cfg.getChild("default-page-settings");
  205. if (pageConfig.getAttribute("height", null) != null) {
  206. factory.setPageHeight(
  207. pageConfig.getAttribute("height", FopFactoryConfigurator.DEFAULT_PAGE_HEIGHT));
  208. if (log.isInfoEnabled()) {
  209. log.info("Default page-height set to: " + factory.getPageHeight());
  210. }
  211. }
  212. if (pageConfig.getAttribute("width", null) != null) {
  213. factory.setPageWidth(
  214. pageConfig.getAttribute("width", FopFactoryConfigurator.DEFAULT_PAGE_WIDTH));
  215. if (log.isInfoEnabled()) {
  216. log.info("Default page-width set to: " + factory.getPageWidth());
  217. }
  218. }
  219. // prefer Renderer over IFDocumentHandler
  220. if (cfg.getChild(PREFER_RENDERER, false) != null) {
  221. try {
  222. factory.getRendererFactory().setRendererPreferred(
  223. cfg.getChild(PREFER_RENDERER).getValueAsBoolean());
  224. } catch (ConfigurationException e) {
  225. LogUtil.handleException(log, e, strict);
  226. }
  227. }
  228. // configure font manager
  229. new FontManagerConfigurator(cfg).configure(factory.getFontManager(), strict);
  230. // configure image loader framework
  231. configureImageLoading(cfg.getChild("image-loading", false), strict);
  232. }
  233. private static void addError(String message, StringBuffer error) {
  234. if (error.length() != 0) {
  235. error.append(". ");
  236. }
  237. error.append(message);
  238. }
  239. private void configureImageLoading(Configuration parent, boolean strict) throws FOPException {
  240. if (parent == null) {
  241. return;
  242. }
  243. ImageImplRegistry registry = factory.getImageManager().getRegistry();
  244. Configuration[] penalties = parent.getChildren("penalty");
  245. try {
  246. for (int i = 0, c = penalties.length; i < c; i++) {
  247. Configuration penaltyCfg = penalties[i];
  248. String className = penaltyCfg.getAttribute("class");
  249. String value = penaltyCfg.getAttribute("value");
  250. Penalty p = null;
  251. if (value.toUpperCase().startsWith("INF")) {
  252. p = Penalty.INFINITE_PENALTY;
  253. } else {
  254. try {
  255. p = Penalty.toPenalty(Integer.parseInt(value));
  256. } catch (NumberFormatException nfe) {
  257. LogUtil.handleException(log, nfe, strict);
  258. }
  259. }
  260. if (p != null) {
  261. registry.setAdditionalPenalty(className, p);
  262. }
  263. }
  264. } catch (ConfigurationException e) {
  265. LogUtil.handleException(log, e, strict);
  266. }
  267. }
  268. /**
  269. * Set the user configuration.
  270. * @param userConfigFile the configuration file
  271. * @throws IOException if an I/O error occurs
  272. * @throws SAXException if a parsing error occurs
  273. */
  274. public void setUserConfig(File userConfigFile) throws SAXException, IOException {
  275. try {
  276. DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  277. setUserConfig(cfgBuilder.buildFromFile(userConfigFile));
  278. } catch (ConfigurationException e) {
  279. throw new FOPException(e);
  280. }
  281. }
  282. /**
  283. * Set the user configuration from an URI.
  284. * @param uri the URI to the configuration file
  285. * @throws IOException if an I/O error occurs
  286. * @throws SAXException if a parsing error occurs
  287. */
  288. public void setUserConfig(String uri) throws SAXException, IOException {
  289. try {
  290. DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  291. setUserConfig(cfgBuilder.build(uri));
  292. } catch (ConfigurationException e) {
  293. throw new FOPException(e);
  294. }
  295. }
  296. /**
  297. * Set the user configuration.
  298. * @param cfg avalon configuration
  299. * @throws FOPException if a configuration problem occurs
  300. */
  301. public void setUserConfig(Configuration cfg) throws FOPException {
  302. this.cfg = cfg;
  303. configure(this.factory);
  304. }
  305. /**
  306. * Get the avalon user configuration.
  307. * @return the user configuration
  308. */
  309. public Configuration getUserConfig() {
  310. return this.cfg;
  311. }
  312. }