選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FopConfParser.java 15KB

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