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.

PDFEmbeddedFileAttachment.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.fop.pdf.PDFText;
  23. /**
  24. * This is the pass-through value object for the PDF extension.
  25. */
  26. public class PDFEmbeddedFileAttachment extends PDFExtensionAttachment {
  27. private static final long serialVersionUID = -1L;
  28. /** element name */
  29. protected static final String ELEMENT = "embedded-file";
  30. /** name of file to be embedded */
  31. private static final String ATT_NAME = "filename";
  32. /** source of file to be embedded (URI) */
  33. private static final String ATT_SRC = "src";
  34. /** a description of the file to be embedded */
  35. private static final String ATT_DESC = "description";
  36. /** filename attribute */
  37. private String filename;
  38. /** unicode filename attribute */
  39. private String unicodeFilename;
  40. /** description attribute (optional) */
  41. private String desc;
  42. /** source name attribute */
  43. private String src;
  44. /**
  45. * No-argument contructor.
  46. */
  47. public PDFEmbeddedFileAttachment() {
  48. super();
  49. }
  50. /**
  51. * Default constructor.
  52. * @param filename the name of the file
  53. * @param src the location of the file
  54. * @param desc the description of the file
  55. */
  56. public PDFEmbeddedFileAttachment(String filename, String src, String desc) {
  57. super();
  58. this.setFilename(filename);
  59. this.src = src;
  60. this.desc = desc;
  61. }
  62. /**
  63. * Returns the file name.
  64. * @return the file name
  65. */
  66. public String getFilename() {
  67. return filename;
  68. }
  69. /**
  70. * Returns the unicode file name.
  71. * @return the file name
  72. */
  73. public String getUnicodeFilename() {
  74. return unicodeFilename;
  75. }
  76. /**
  77. * Sets the file name.
  78. * @param name The file name to set.
  79. */
  80. public void setFilename(String name) {
  81. if (!PDFText.toPDFString(name).equals(name)) {
  82. // replace with auto generated filename, because non-ascii chars are used.
  83. this.filename = "att" + name.hashCode();
  84. } else {
  85. this.filename = name;
  86. }
  87. this.unicodeFilename = name;
  88. }
  89. /**
  90. * Returns the file description.
  91. * @return the description
  92. */
  93. public String getDesc() {
  94. return desc;
  95. }
  96. /**
  97. * Sets the description of the file.
  98. * @param desc the description to set
  99. */
  100. public void setDesc(String desc) {
  101. this.desc = desc;
  102. }
  103. /**
  104. * Returns the source URI of the file.
  105. * @return the source URI
  106. */
  107. public String getSrc() {
  108. return src;
  109. }
  110. /**
  111. * Sets the source URI of the file.
  112. * @param src the source URI
  113. */
  114. public void setSrc(String src) {
  115. this.src = src;
  116. }
  117. /** {@inheritDoc} */
  118. public String getCategory() {
  119. return CATEGORY;
  120. }
  121. /** {@inheritDoc} */
  122. public String toString() {
  123. return "PDFEmbeddedFile(name=" + getFilename() + ", " + getSrc() + ")";
  124. }
  125. /**
  126. * @return the element name
  127. */
  128. protected String getElement() {
  129. return ELEMENT;
  130. }
  131. /** {@inheritDoc} */
  132. public void toSAX(ContentHandler handler) throws SAXException {
  133. AttributesImpl atts = new AttributesImpl();
  134. if (filename != null && filename.length() > 0) {
  135. atts.addAttribute("", ATT_NAME, ATT_NAME, "CDATA", filename);
  136. }
  137. if (src != null && src.length() > 0) {
  138. atts.addAttribute("", ATT_SRC, ATT_SRC, "CDATA", src);
  139. }
  140. if (desc != null && desc.length() > 0) {
  141. atts.addAttribute("", ATT_DESC, ATT_DESC, "CDATA", desc);
  142. }
  143. String element = getElement();
  144. handler.startElement(CATEGORY, element, element, atts);
  145. handler.endElement(CATEGORY, element, element);
  146. }
  147. }