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.

PDFRendererConfigurator.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.List;
  20. import java.util.Map;
  21. import org.apache.avalon.framework.configuration.Configuration;
  22. import org.apache.avalon.framework.configuration.ConfigurationException;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.pdf.PDFAMode;
  26. import org.apache.fop.pdf.PDFFilterList;
  27. import org.apache.fop.pdf.PDFXMode;
  28. import org.apache.fop.render.PrintRendererConfigurator;
  29. import org.apache.fop.render.Renderer;
  30. import org.apache.fop.util.LogUtil;
  31. /**
  32. * PDF renderer configurator
  33. */
  34. public class PDFRendererConfigurator extends PrintRendererConfigurator {
  35. /**
  36. * Default constructor
  37. * @param userAgent user agent
  38. */
  39. public PDFRendererConfigurator(FOUserAgent userAgent) {
  40. super(userAgent);
  41. }
  42. /**
  43. * Configure the PDF renderer.
  44. * Get the configuration to be used for pdf stream filters,
  45. * fonts etc.
  46. * @param renderer pdf renderer
  47. * @throws FOPException fop exception
  48. */
  49. public void configure(Renderer renderer) throws FOPException {
  50. Configuration cfg = super.getRendererConfig(renderer);
  51. if (cfg != null) {
  52. PDFRenderer pdfRenderer = (PDFRenderer)renderer;
  53. //PDF filters
  54. try {
  55. Map filterMap = buildFilterMapFromConfiguration(cfg);
  56. if (filterMap != null) {
  57. pdfRenderer.setFilterMap(filterMap);
  58. }
  59. } catch (ConfigurationException e) {
  60. LogUtil.handleException(log, e, false);
  61. }
  62. super.configure(renderer);
  63. String s = cfg.getChild(PDFRenderer.PDF_A_MODE, true).getValue(null);
  64. if (s != null) {
  65. pdfRenderer.setAMode(PDFAMode.valueOf(s));
  66. }
  67. s = cfg.getChild(PDFRenderer.PDF_X_MODE, true).getValue(null);
  68. if (s != null) {
  69. pdfRenderer.setXMode(PDFXMode.valueOf(s));
  70. }
  71. s = cfg.getChild(PDFRenderer.KEY_OUTPUT_PROFILE, true).getValue(null);
  72. if (s != null) {
  73. pdfRenderer.setOutputProfileURI(s);
  74. }
  75. }
  76. }
  77. /**
  78. * Builds a filter map from an Avalon Configuration object.
  79. * @param cfg the Configuration object
  80. * @return Map the newly built filter map
  81. * @throws ConfigurationException if a filter list is defined twice
  82. */
  83. public static Map buildFilterMapFromConfiguration(Configuration cfg)
  84. throws ConfigurationException {
  85. Map filterMap = new java.util.HashMap();
  86. Configuration[] filterLists = cfg.getChildren("filterList");
  87. for (int i = 0; i < filterLists.length; i++) {
  88. Configuration filters = filterLists[i];
  89. String type = filters.getAttribute("type", null);
  90. Configuration[] filt = filters.getChildren("value");
  91. List filterList = new java.util.ArrayList();
  92. for (int j = 0; j < filt.length; j++) {
  93. String name = filt[j].getValue();
  94. filterList.add(name);
  95. }
  96. if (type == null) {
  97. type = PDFFilterList.DEFAULT_FILTER;
  98. }
  99. if (!filterList.isEmpty() && log.isDebugEnabled()) {
  100. StringBuffer debug = new StringBuffer("Adding PDF filter");
  101. if (filterList.size() != 1) {
  102. debug.append("s");
  103. }
  104. debug.append(" for type ").append(type).append(": ");
  105. for (int j = 0; j < filterList.size(); j++) {
  106. if (j != 0) {
  107. debug.append(", ");
  108. }
  109. debug.append(filterList.get(j));
  110. }
  111. log.debug(debug.toString());
  112. }
  113. if (filterMap.get(type) != null) {
  114. throw new ConfigurationException("A filterList of type '"
  115. + type + "' has already been defined");
  116. }
  117. filterMap.put(type, filterList);
  118. }
  119. return filterMap;
  120. }
  121. }