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.

PDFDictionaryAttachment.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.extensions;
  19. import org.xml.sax.ContentHandler;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.helpers.AttributesImpl;
  22. import org.apache.xmlgraphics.util.XMLizable;
  23. import org.apache.fop.render.intermediate.IFContext;
  24. import org.apache.fop.util.GenerationHelperContentHandler;
  25. // CSOFF: LineLengthCheck
  26. public class PDFDictionaryAttachment extends PDFExtensionAttachment {
  27. private static final long serialVersionUID = -5576832955238384505L;
  28. private PDFDictionaryExtension extension;
  29. public PDFDictionaryAttachment(PDFDictionaryExtension extension) {
  30. this.extension = extension;
  31. }
  32. public PDFDictionaryExtension getExtension() {
  33. return extension;
  34. }
  35. public void toSAX(ContentHandler handler) throws SAXException {
  36. int pageNumber = 0;
  37. if (extension instanceof PDFPageExtension) {
  38. if (handler instanceof GenerationHelperContentHandler) {
  39. Object context = ((GenerationHelperContentHandler) handler).getContentHandlerContext();
  40. if (context instanceof IFContext) {
  41. int pageIndex = ((IFContext) context).getPageIndex();
  42. if ((pageIndex >= 0) && ((PDFPageExtension) extension).matchesPageNumber(pageIndex + 1)) {
  43. pageNumber = pageIndex + 1;
  44. } else {
  45. pageNumber = -1;
  46. }
  47. }
  48. }
  49. }
  50. if (pageNumber >= 0) {
  51. toSAX(handler, extension);
  52. }
  53. }
  54. private void toSAX(ContentHandler handler, PDFDictionaryExtension dictionary) throws SAXException {
  55. AttributesImpl attributes = new AttributesImpl();
  56. String ln = dictionary.getElementName();
  57. String qn = PREFIX + ":" + ln;
  58. attributes = extractIFAttributes(attributes, dictionary);
  59. handler.startElement(CATEGORY, ln, qn, attributes);
  60. for (PDFCollectionEntryExtension entry : dictionary.getEntries()) {
  61. toSAX(handler, entry);
  62. }
  63. if (extension.getExtension() != null) {
  64. ((XMLizable) extension.getExtension()).toSAX(handler);
  65. }
  66. handler.endElement(CATEGORY, ln, qn);
  67. }
  68. private void toSAX(ContentHandler handler, PDFArrayExtension array) throws SAXException {
  69. AttributesImpl attributes = new AttributesImpl();
  70. String ln = array.getElementName();
  71. String qn = PREFIX + ":" + ln;
  72. attributes = extractIFAttributes(attributes, array);
  73. handler.startElement(CATEGORY, ln, qn, attributes);
  74. for (PDFCollectionEntryExtension entry : array.getEntries()) {
  75. toSAX(handler, entry);
  76. }
  77. handler.endElement(CATEGORY, ln, qn);
  78. }
  79. private void toSAX(ContentHandler handler, PDFCollectionEntryExtension entry) throws SAXException {
  80. if (entry instanceof PDFDictionaryExtension) {
  81. toSAX(handler, (PDFDictionaryExtension) entry);
  82. } else if (entry instanceof PDFArrayExtension) {
  83. toSAX(handler, (PDFArrayExtension) entry);
  84. } else {
  85. AttributesImpl attributes = new AttributesImpl();
  86. String ln = entry.getElementName();
  87. String qn = PREFIX + ":" + ln;
  88. attributes = extractIFAttributes(attributes, entry);
  89. handler.startElement(CATEGORY, ln, qn, attributes);
  90. if (!(entry instanceof PDFReferenceExtension)) {
  91. char[] characters = entry.getValueAsXMLEscapedString().toCharArray();
  92. if (characters.length > 0) {
  93. handler.characters(characters, 0, characters.length);
  94. }
  95. }
  96. handler.endElement(CATEGORY, ln, qn);
  97. }
  98. }
  99. private static AttributesImpl extractIFAttributes(AttributesImpl attributes, PDFDictionaryExtension dictionary) {
  100. PDFDictionaryType type = dictionary.getDictionaryType();
  101. if (dictionary.usesIDAttribute()) {
  102. String idName = PDFDictionaryElement.ATT_ID;
  103. String id = dictionary.getProperty(PDFDictionaryExtension.PROPERTY_ID);
  104. if (id != null) {
  105. attributes.addAttribute("", idName, idName, "ID", id);
  106. }
  107. }
  108. if (type == PDFDictionaryType.Action) {
  109. String actionTypeName = PDFActionElement.ATT_TYPE;
  110. String actionType = dictionary.getProperty(PDFActionExtension.PROPERTY_TYPE);
  111. if (actionType != null) {
  112. attributes.addAttribute("", actionTypeName, actionTypeName, "CDATA", actionType);
  113. }
  114. } else if (type == PDFDictionaryType.Page) {
  115. String pageNumbersName = PDFPageExtension.PROPERTY_PAGE_NUMBERS;
  116. String pageNumbers = dictionary.getProperty(pageNumbersName);
  117. if (pageNumbers != null) {
  118. attributes.addAttribute("", pageNumbersName, pageNumbersName, "CDATA", pageNumbers);
  119. }
  120. } else if (type == PDFDictionaryType.Dictionary) {
  121. String keyName = PDFCollectionEntryElement.ATT_KEY;
  122. String key = dictionary.getKey();
  123. if (key != null) {
  124. attributes.addAttribute("", keyName, keyName, "CDATA", key);
  125. }
  126. }
  127. return attributes;
  128. }
  129. private static AttributesImpl extractIFAttributes(AttributesImpl attributes, PDFArrayExtension array) {
  130. String keyName = PDFCollectionEntryExtension.PROPERTY_KEY;
  131. String key = array.getKey();
  132. if (key != null) {
  133. attributes.addAttribute("", keyName, keyName, "CDATA", key);
  134. }
  135. return attributes;
  136. }
  137. private static AttributesImpl extractIFAttributes(AttributesImpl attributes, PDFCollectionEntryExtension entry) {
  138. String keyName = PDFCollectionEntryElement.ATT_KEY;
  139. String key = entry.getKey();
  140. if (key != null) {
  141. attributes.addAttribute("", keyName, keyName, "CDATA", key);
  142. }
  143. if (entry instanceof PDFReferenceExtension) {
  144. String refid = ((PDFReferenceExtension) entry).getReferenceId();
  145. if (refid != null) {
  146. String refidName = PDFReferenceElement.ATT_REFID;
  147. attributes.addAttribute("", refidName, refidName, "IDREF", refid);
  148. }
  149. }
  150. return attributes;
  151. }
  152. }