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

FopConfParser.java 16KB

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