Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FopConfParser.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.URI;
  24. import java.net.URISyntaxException;
  25. import java.util.HashMap;
  26. import java.util.Locale;
  27. import java.util.Map;
  28. import org.xml.sax.SAXException;
  29. import org.apache.avalon.framework.configuration.Configuration;
  30. import org.apache.avalon.framework.configuration.ConfigurationException;
  31. import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry;
  35. import org.apache.xmlgraphics.image.loader.util.Penalty;
  36. import org.apache.xmlgraphics.io.ResourceResolver;
  37. import org.apache.fop.apps.io.InternalResourceResolver;
  38. import org.apache.fop.apps.io.ResourceResolverFactory;
  39. import org.apache.fop.fonts.FontManagerConfigurator;
  40. import org.apache.fop.hyphenation.HyphenationTreeCache;
  41. import org.apache.fop.util.LogUtil;
  42. /**
  43. * Parses the FOP configuration file and returns a {@link FopFactoryBuilder} which builds a
  44. * {@link FopFactory}.
  45. */
  46. public class FopConfParser {
  47. private static final String PREFER_RENDERER = "prefer-renderer";
  48. private final Log log = LogFactory.getLog(FopConfParser.class);
  49. private final FopFactoryBuilder fopFactoryBuilder;
  50. /**
  51. * Constructor that takes the FOP conf in the form of an {@link InputStream}. A default base URI
  52. * must be given as a fall-back mechanism for URI resolution.
  53. *
  54. * @param fopConfStream the fop conf input stream
  55. * @param enviro the profile of the FOP deployment environment
  56. * @throws SAXException if a SAX error was thrown parsing the FOP conf
  57. * @throws IOException if an I/O error is thrown while parsing the FOP conf
  58. */
  59. public FopConfParser(InputStream fopConfStream, EnvironmentProfile enviro)
  60. throws SAXException, IOException {
  61. DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  62. Configuration cfg;
  63. try {
  64. cfg = cfgBuilder.build(fopConfStream);
  65. } catch (ConfigurationException e) {
  66. throw new FOPException(e);
  67. }
  68. // The default base URI is taken from the directory in which the fopConf resides
  69. fopFactoryBuilder = new FopFactoryBuilder(enviro).setConfiguration(cfg);
  70. configure(enviro.getDefaultBaseURI(), enviro.getResourceResolver(), cfg);
  71. }
  72. /**
  73. * Constructor that takes the FOP conf in the form of an {@link InputStream}. A default base URI
  74. * must be given as a fall-back mechanism for URI resolution.
  75. *
  76. * @param fopConfStream the fop conf input stream
  77. * @param defaultBaseURI the default base URI
  78. * @param resourceResolver the URI resolver
  79. * @throws SAXException if a SAX error was thrown parsing the FOP conf
  80. * @throws IOException if an I/O error is thrown while parsing the FOP conf
  81. */
  82. public FopConfParser(InputStream fopConfStream, URI defaultBaseURI,
  83. ResourceResolver resourceResolver) throws SAXException, IOException {
  84. this(fopConfStream, EnvironmentalProfileFactory.createDefault(defaultBaseURI, resourceResolver));
  85. }
  86. /**
  87. * Constructor that takes the FOP conf in the form of an {@link InputStream}. A default base URI
  88. * must be given as a fall-back mechanism for URI resolution. The default URI resolvers is used.
  89. *
  90. * @param fopConfStream the fop conf input stream
  91. * @param defaultBaseURI the default base URI
  92. * @throws SAXException if a SAX error was thrown parsing the FOP conf
  93. * @throws IOException if an I/O error is thrown while parsing the FOP conf
  94. */
  95. public FopConfParser(InputStream fopConfStream, URI defaultBaseURI) throws SAXException,
  96. IOException {
  97. this(fopConfStream, defaultBaseURI, ResourceResolverFactory.createDefaultResourceResolver());
  98. }
  99. /**
  100. * Constructor that takes the FOP conf and uses the default URI resolver.
  101. *
  102. * @param fopConfFile the FOP conf file
  103. * @throws SAXException if a SAX error was thrown parsing the FOP conf
  104. * @throws IOException if an I/O error is thrown while parsing the FOP conf
  105. */
  106. public FopConfParser(File fopConfFile) throws SAXException, IOException {
  107. this(fopConfFile, ResourceResolverFactory.createDefaultResourceResolver());
  108. }
  109. /**
  110. * Constructor that parses the FOP conf and uses the URI resolver given.
  111. *
  112. * @param fopConfFile the FOP conf file
  113. * @param resourceResolver the URI resolver
  114. * @throws SAXException if a SAX error was thrown parsing the FOP conf
  115. * @throws IOException if an I/O error is thrown while parsing the FOP conf
  116. */
  117. public FopConfParser(File fopConfFile, ResourceResolver resourceResolver)
  118. throws SAXException, IOException {
  119. this(new FileInputStream(fopConfFile),
  120. fopConfFile.getAbsoluteFile().getParentFile().toURI(), resourceResolver);
  121. }
  122. private void configure(final URI defaultBaseURI, final ResourceResolver resourceResolver,
  123. Configuration cfg) throws FOPException {
  124. if (log.isDebugEnabled()) {
  125. log.debug("Initializing FopFactory Configuration");
  126. }
  127. // strict fo validation
  128. if (cfg.getChild("strict-validation", false) != null) {
  129. try {
  130. boolean strict = cfg.getChild("strict-validation").getValueAsBoolean();
  131. fopFactoryBuilder.setStrictFOValidation(strict);
  132. } catch (ConfigurationException e) {
  133. LogUtil.handleException(log, e, false);
  134. }
  135. }
  136. boolean strict = false;
  137. if (cfg.getChild("strict-configuration", false) != null) {
  138. try {
  139. strict = cfg.getChild("strict-configuration").getValueAsBoolean();
  140. fopFactoryBuilder.setStrictUserConfigValidation(strict);
  141. } catch (ConfigurationException e) {
  142. LogUtil.handleException(log, e, false);
  143. }
  144. }
  145. if (cfg.getChild("accessibility", false) != null) {
  146. try {
  147. fopFactoryBuilder.setAccessibility(cfg.getChild("accessibility").getValueAsBoolean());
  148. } catch (ConfigurationException e) {
  149. LogUtil.handleException(log, e, false);
  150. }
  151. }
  152. // base definitions for relative path resolution
  153. if (cfg.getChild("base", false) != null) {
  154. try {
  155. URI confUri = InternalResourceResolver.getBaseURI(cfg.getChild("base").getValue(null));
  156. fopFactoryBuilder.setBaseURI(defaultBaseURI.resolve(confUri));
  157. } catch (URISyntaxException use) {
  158. LogUtil.handleException(log, use, strict);
  159. }
  160. }
  161. // renderer options
  162. if (cfg.getChild("source-resolution", false) != null) {
  163. float srcRes = cfg.getChild("source-resolution").getValueAsFloat(
  164. FopFactoryConfig.DEFAULT_SOURCE_RESOLUTION);
  165. fopFactoryBuilder.setSourceResolution(srcRes);
  166. if (log.isDebugEnabled()) {
  167. log.debug("source-resolution set to: " + srcRes + "dpi");
  168. }
  169. }
  170. if (cfg.getChild("target-resolution", false) != null) {
  171. float targetRes = cfg.getChild("target-resolution").getValueAsFloat(
  172. FopFactoryConfig.DEFAULT_TARGET_RESOLUTION);
  173. fopFactoryBuilder.setTargetResolution(targetRes);
  174. if (log.isDebugEnabled()) {
  175. log.debug("target-resolution set to: " + targetRes + "dpi");
  176. }
  177. }
  178. if (cfg.getChild("break-indent-inheritance", false) != null) {
  179. try {
  180. fopFactoryBuilder.setBreakIndentInheritanceOnReferenceAreaBoundary(
  181. cfg.getChild("break-indent-inheritance").getValueAsBoolean());
  182. } catch (ConfigurationException e) {
  183. LogUtil.handleException(log, e, strict);
  184. }
  185. }
  186. Configuration pageConfig = cfg.getChild("default-page-settings");
  187. if (pageConfig.getAttribute("height", null) != null) {
  188. String pageHeight = pageConfig.getAttribute("height",
  189. FopFactoryConfig.DEFAULT_PAGE_HEIGHT);
  190. fopFactoryBuilder.setPageHeight(pageHeight);
  191. if (log.isInfoEnabled()) {
  192. log.info("Default page-height set to: " + pageHeight);
  193. }
  194. }
  195. if (pageConfig.getAttribute("width", null) != null) {
  196. String pageWidth = pageConfig.getAttribute("width",
  197. FopFactoryConfig.DEFAULT_PAGE_WIDTH);
  198. fopFactoryBuilder.setPageWidth(pageWidth);
  199. if (log.isInfoEnabled()) {
  200. log.info("Default page-width set to: " + pageWidth);
  201. }
  202. }
  203. if (cfg.getChild("complex-scripts") != null) {
  204. Configuration csConfig = cfg.getChild("complex-scripts");
  205. fopFactoryBuilder.setComplexScriptFeatures(!csConfig.getAttributeAsBoolean("disabled",
  206. false));
  207. }
  208. setHyphPatNames(cfg, fopFactoryBuilder, strict);
  209. // prefer Renderer over IFDocumentHandler
  210. if (cfg.getChild(PREFER_RENDERER, false) != null) {
  211. try {
  212. fopFactoryBuilder.setPreferRenderer(
  213. cfg.getChild(PREFER_RENDERER).getValueAsBoolean());
  214. } catch (ConfigurationException e) {
  215. LogUtil.handleException(log, e, strict);
  216. }
  217. }
  218. // configure font manager
  219. new FontManagerConfigurator(cfg, fopFactoryBuilder.getBaseURI(), resourceResolver).configure(
  220. fopFactoryBuilder.getFontManager(), strict);
  221. // configure image loader framework
  222. configureImageLoading(cfg.getChild("image-loading", false), strict);
  223. }
  224. private void setHyphPatNames(Configuration cfg, FopFactoryBuilder builder, boolean strict)
  225. throws FOPException {
  226. Configuration[] hyphPatConfig = cfg.getChildren("hyphenation-pattern");
  227. if (hyphPatConfig.length != 0) {
  228. Map<String, String> hyphPatNames = new HashMap<String, String>();
  229. for (int i = 0; i < hyphPatConfig.length; ++i) {
  230. String lang;
  231. String country;
  232. String filename;
  233. StringBuffer error = new StringBuffer();
  234. String location = hyphPatConfig[i].getLocation();
  235. lang = hyphPatConfig[i].getAttribute("lang", null);
  236. if (lang == null) {
  237. addError("The lang attribute of a hyphenation-pattern configuration"
  238. + " element must exist (" + location + ")", error);
  239. } else if (!lang.matches("[a-zA-Z]{2}")) {
  240. addError("The lang attribute of a hyphenation-pattern configuration"
  241. + " element must consist of exactly two letters ("
  242. + location + ")", error);
  243. }
  244. lang = lang.toLowerCase(Locale.getDefault());
  245. country = hyphPatConfig[i].getAttribute("country", null);
  246. if ("".equals(country)) {
  247. country = null;
  248. }
  249. if (country != null) {
  250. if (!country.matches("[a-zA-Z]{2}")) {
  251. addError("The country attribute of a hyphenation-pattern configuration"
  252. + " element must consist of exactly two letters ("
  253. + location + ")", error);
  254. }
  255. country = country.toUpperCase(Locale.getDefault());
  256. }
  257. filename = hyphPatConfig[i].getValue(null);
  258. if (filename == null) {
  259. addError("The value of a hyphenation-pattern configuration"
  260. + " element may not be empty (" + location + ")", error);
  261. }
  262. if (error.length() != 0) {
  263. LogUtil.handleError(log, error.toString(), strict);
  264. continue;
  265. }
  266. String llccKey = HyphenationTreeCache.constructLlccKey(lang, country);
  267. hyphPatNames.put(llccKey, filename);
  268. if (log.isDebugEnabled()) {
  269. log.debug("Using hyphenation pattern filename " + filename
  270. + " for lang=\"" + lang + "\""
  271. + (country != null ? ", country=\"" + country + "\"" : ""));
  272. }
  273. }
  274. builder.setHyphPatNames(hyphPatNames);
  275. }
  276. }
  277. private static void addError(String message, StringBuffer error) {
  278. if (error.length() != 0) {
  279. error.append(". ");
  280. }
  281. error.append(message);
  282. }
  283. private void configureImageLoading(Configuration parent, boolean strict) throws FOPException {
  284. if (parent == null) {
  285. return;
  286. }
  287. ImageImplRegistry registry = fopFactoryBuilder.getImageManager().getRegistry();
  288. Configuration[] penalties = parent.getChildren("penalty");
  289. try {
  290. for (int i = 0, c = penalties.length; i < c; i++) {
  291. Configuration penaltyCfg = penalties[i];
  292. String className = penaltyCfg.getAttribute("class");
  293. String value = penaltyCfg.getAttribute("value");
  294. Penalty p = null;
  295. if (value.toUpperCase(Locale.getDefault()).startsWith("INF")) {
  296. p = Penalty.INFINITE_PENALTY;
  297. } else {
  298. try {
  299. p = Penalty.toPenalty(Integer.parseInt(value));
  300. } catch (NumberFormatException nfe) {
  301. LogUtil.handleException(log, nfe, strict);
  302. }
  303. }
  304. if (p != null) {
  305. registry.setAdditionalPenalty(className, p);
  306. }
  307. }
  308. } catch (ConfigurationException e) {
  309. LogUtil.handleException(log, e, strict);
  310. }
  311. }
  312. /**
  313. * Returns the {@link FopFactoryBuilder}.
  314. *
  315. * @return the object for configuring the {@link FopFactory}
  316. */
  317. public FopFactoryBuilder getFopFactoryBuilder() {
  318. return fopFactoryBuilder;
  319. }
  320. }