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.

PDFRendererConfig.java 10KB

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.render.pdf;
  19. import java.util.ArrayList;
  20. import java.util.EnumMap;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.avalon.framework.configuration.Configuration;
  25. import org.apache.avalon.framework.configuration.ConfigurationException;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.apps.FOPException;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.apps.MimeConstants;
  31. import org.apache.fop.fonts.DefaultFontConfig;
  32. import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser;
  33. import org.apache.fop.pdf.PDFEncryptionParams;
  34. import org.apache.fop.pdf.PDFFilterList;
  35. import org.apache.fop.render.RendererConfig;
  36. import org.apache.fop.render.RendererConfigOption;
  37. import org.apache.fop.util.LogUtil;
  38. import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_LENGTH;
  39. import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_PARAMS;
  40. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ACCESSCONTENT;
  41. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ANNOTATIONS;
  42. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ASSEMBLEDOC;
  43. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_COPY_CONTENT;
  44. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_EDIT_CONTENT;
  45. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_FILLINFORMS;
  46. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_PRINT;
  47. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_PRINTHQ;
  48. import static org.apache.fop.render.pdf.PDFEncryptionOption.OWNER_PASSWORD;
  49. import static org.apache.fop.render.pdf.PDFEncryptionOption.USER_PASSWORD;
  50. import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE;
  51. import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST;
  52. import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE;
  53. import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE;
  54. import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE;
  55. import static org.apache.fop.render.pdf.PDFRendererOption.VERSION;
  56. /**
  57. * The PDF renderer configuration data object.
  58. */
  59. public final class PDFRendererConfig implements RendererConfig {
  60. private static final Log LOG = LogFactory.getLog(PDFRendererConfig.class);
  61. private final PDFRendererOptionsConfig configOption;
  62. private final DefaultFontConfig fontConfig;
  63. private PDFRendererConfig(DefaultFontConfig fontConfig, PDFRendererOptionsConfig config) {
  64. this.fontConfig = fontConfig;
  65. this.configOption = config;
  66. }
  67. public PDFRendererOptionsConfig getConfigOptions() {
  68. return configOption;
  69. }
  70. public DefaultFontConfig getFontInfoConfig() {
  71. return fontConfig;
  72. }
  73. /**
  74. * The PDF renderer configuration data parser.
  75. */
  76. public static final class PDFRendererConfigParser implements RendererConfigParser {
  77. public PDFRendererConfig build(FOUserAgent userAgent, Configuration cfg) throws FOPException {
  78. boolean strict = userAgent != null ? userAgent.validateUserConfigStrictly() : false;
  79. return new ParserHelper(cfg, userAgent, strict).pdfConfig;
  80. }
  81. public String getMimeType() {
  82. return MimeConstants.MIME_PDF;
  83. }
  84. }
  85. private static final class ParserHelper {
  86. private final Map<PDFRendererOption, Object> configOptions
  87. = new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class);
  88. private PDFEncryptionParams encryptionConfig;
  89. private PDFRendererConfig pdfConfig;
  90. private ParserHelper(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException {
  91. if (cfg != null) {
  92. configure(cfg, userAgent, strict);
  93. }
  94. pdfConfig = new PDFRendererConfig(new DefaultFontConfigParser().parse(cfg, strict),
  95. new PDFRendererOptionsConfig(configOptions, encryptionConfig));
  96. }
  97. private void parseAndPut(PDFRendererOption option, Configuration cfg) {
  98. put(option, option.parse(parseConfig(cfg, option)));
  99. }
  100. private void put(PDFRendererOption option, Object value) {
  101. if (value != null && !value.equals(option.getDefaultValue())) {
  102. configOptions.put(option, value);
  103. }
  104. }
  105. private void configure(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException {
  106. try {
  107. buildFilterMapFromConfiguration(cfg);
  108. parseAndPut(PDF_A_MODE, cfg);
  109. parseAndPut(PDF_X_MODE, cfg);
  110. configureEncryptionParams(cfg, userAgent, strict);
  111. parseAndPut(OUTPUT_PROFILE, cfg);
  112. parseAndPut(DISABLE_SRGB_COLORSPACE, cfg);
  113. parseAndPut(VERSION, cfg);
  114. } catch (ConfigurationException e) {
  115. LogUtil.handleException(LOG, e, strict);
  116. }
  117. }
  118. private void configureEncryptionParams(Configuration cfg, FOUserAgent userAgent, boolean strict) {
  119. Configuration encryptCfg = cfg.getChild(ENCRYPTION_PARAMS, false);
  120. if (encryptCfg != null) {
  121. encryptionConfig = new PDFEncryptionParams();
  122. encryptionConfig.setOwnerPassword(parseConfig(encryptCfg, OWNER_PASSWORD));
  123. encryptionConfig.setUserPassword(parseConfig(encryptCfg, USER_PASSWORD));
  124. encryptionConfig.setAllowPrint(!doesValueExist(encryptCfg, NO_PRINT));
  125. encryptionConfig.setAllowCopyContent(!doesValueExist(encryptCfg, NO_COPY_CONTENT));
  126. encryptionConfig.setAllowEditContent(!doesValueExist(encryptCfg, NO_EDIT_CONTENT));
  127. encryptionConfig.setAllowEditAnnotations(!doesValueExist(encryptCfg, NO_ANNOTATIONS));
  128. encryptionConfig.setAllowFillInForms(!doesValueExist(encryptCfg, NO_FILLINFORMS));
  129. encryptionConfig.setAllowAccessContent(!doesValueExist(encryptCfg, NO_ACCESSCONTENT));
  130. encryptionConfig.setAllowAssembleDocument(!doesValueExist(encryptCfg, NO_ASSEMBLEDOC));
  131. encryptionConfig.setAllowPrintHq(!doesValueExist(encryptCfg, NO_PRINTHQ));
  132. String encryptionLength = parseConfig(encryptCfg, ENCRYPTION_LENGTH);
  133. if (encryptionLength != null) {
  134. int validatedLength = checkEncryptionLength(Integer.parseInt(encryptionLength), userAgent);
  135. encryptionConfig.setEncryptionLengthInBits(validatedLength);
  136. }
  137. }
  138. }
  139. private void buildFilterMapFromConfiguration(Configuration cfg) throws ConfigurationException,
  140. FOPException {
  141. Configuration[] filterLists = cfg.getChildren(FILTER_LIST.getName());
  142. Map<String, List<String>> filterMap = new HashMap<String, List<String>>();
  143. for (Configuration filters : filterLists) {
  144. String type = filters.getAttribute("type", PDFFilterList.DEFAULT_FILTER);
  145. List<String> filterList = new ArrayList<String>();
  146. for (Configuration nameCfg : filters.getChildren("value")) {
  147. filterList.add(nameCfg.getValue());
  148. }
  149. if (!filterList.isEmpty() && LOG.isDebugEnabled()) {
  150. StringBuffer debug = new StringBuffer("Adding PDF filter");
  151. if (filterList.size() != 1) {
  152. debug.append("s");
  153. }
  154. debug.append(" for type ").append(type).append(": ");
  155. for (int j = 0; j < filterList.size(); j++) {
  156. if (j != 0) {
  157. debug.append(", ");
  158. }
  159. debug.append(filterList.get(j));
  160. }
  161. LogUtil.handleError(LOG, debug.toString(), true);
  162. }
  163. if (filterMap.get(type) != null) {
  164. throw new ConfigurationException("A filterList of type '"
  165. + type + "' has already been defined");
  166. }
  167. filterMap.put(type, filterList);
  168. }
  169. put(FILTER_LIST, filterMap);
  170. }
  171. private String parseConfig(Configuration cfg, RendererConfigOption option) {
  172. Configuration child = cfg.getChild(option.getName());
  173. return child.getValue(null);
  174. }
  175. private boolean doesValueExist(Configuration cfg, RendererConfigOption option) {
  176. return cfg.getChild(option.getName(), false) != null;
  177. }
  178. private int checkEncryptionLength(int encryptionLength, FOUserAgent userAgent) {
  179. int correctEncryptionLength = encryptionLength;
  180. if (encryptionLength < 40) {
  181. correctEncryptionLength = 40;
  182. } else if (encryptionLength > 128) {
  183. correctEncryptionLength = 128;
  184. } else if (encryptionLength % 8 != 0) {
  185. correctEncryptionLength = Math.round(encryptionLength / 8.0f) * 8;
  186. }
  187. if (correctEncryptionLength != encryptionLength && userAgent != null) {
  188. PDFEventProducer.Provider.get(userAgent.getEventBroadcaster())
  189. .incorrectEncryptionLength(this, encryptionLength,
  190. correctEncryptionLength);
  191. }
  192. return correctEncryptionLength;
  193. }
  194. }
  195. }