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.

PDFEmbeddedFileElement.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 java.net.URI;
  20. import java.net.URISyntaxException;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.Locator;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.datatypes.URISpecification;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.FONode;
  27. import org.apache.fop.fo.PropertyList;
  28. import org.apache.fop.fo.extensions.ExtensionAttachment;
  29. /**
  30. * Extension element for pdf:embedded-file.
  31. */
  32. public class PDFEmbeddedFileElement extends AbstractPDFExtensionElement {
  33. /** name of element */
  34. protected static final String ELEMENT = "embedded-file";
  35. /**
  36. * Main constructor
  37. * @param parent parent FO node
  38. */
  39. PDFEmbeddedFileElement(FONode parent) {
  40. super(parent);
  41. }
  42. @Override
  43. public void startOfNode() throws FOPException {
  44. super.startOfNode();
  45. if (parent.getNameId() != Constants.FO_DECLARATIONS) {
  46. invalidChildError(getLocator(), parent.getName(), getNamespaceURI(), getName(),
  47. "rule.childOfDeclarations");
  48. }
  49. }
  50. @Override
  51. public void processNode(String elementName, Locator locator,
  52. Attributes attlist, PropertyList propertyList)
  53. throws FOPException {
  54. PDFEmbeddedFileAttachment embeddedFile
  55. = (PDFEmbeddedFileAttachment)getExtensionAttachment();
  56. String desc = attlist.getValue("description");
  57. if (desc != null && desc.length() > 0) {
  58. embeddedFile.setDesc(desc);
  59. }
  60. String src = attlist.getValue("src");
  61. src = URISpecification.getURL(src);
  62. if (src != null && src.length() > 0) {
  63. embeddedFile.setSrc(src);
  64. } else {
  65. missingPropertyError("src");
  66. }
  67. String filename = attlist.getValue("filename");
  68. if (filename == null || filename.length() == 0) {
  69. try {
  70. URI uri = new URI(src);
  71. String path = uri.getPath();
  72. int idx = path.lastIndexOf('/');
  73. if (idx > 0) {
  74. filename = path.substring(idx + 1);
  75. } else {
  76. filename = path;
  77. }
  78. embeddedFile.setFilename(filename);
  79. } catch (URISyntaxException e) {
  80. //Filename could not be deduced from URI
  81. missingPropertyError("name");
  82. }
  83. }
  84. embeddedFile.setFilename(filename);
  85. }
  86. @Override
  87. public String getLocalName() {
  88. return ELEMENT;
  89. }
  90. @Override
  91. protected ExtensionAttachment instantiateExtensionAttachment() {
  92. return new PDFEmbeddedFileAttachment();
  93. }
  94. }