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

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