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.

PDFExtensionHandler.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Attributes;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.helpers.AttributesImpl;
  22. import org.xml.sax.helpers.DefaultHandler;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.util.ContentHandlerFactory;
  26. import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
  27. /**
  28. * ContentHandler (parser) for restoring PDF extension objects from XML.
  29. */
  30. public class PDFExtensionHandler extends DefaultHandler
  31. implements ContentHandlerFactory.ObjectSource {
  32. /** Logger instance */
  33. protected static final Log log = LogFactory.getLog(PDFExtensionHandler.class);
  34. private Attributes lastAttributes;
  35. private PDFExtensionAttachment returnedObject;
  36. private ObjectBuiltListener listener;
  37. /** {@inheritDoc} */
  38. public void startElement(String uri, String localName, String qName, Attributes attributes)
  39. throws SAXException {
  40. boolean handled = false;
  41. if (PDFExtensionAttachment.CATEGORY.equals(uri)) {
  42. lastAttributes = new AttributesImpl(attributes);
  43. handled = false;
  44. if (localName.equals(PDFEmbeddedFileExtensionAttachment.ELEMENT)) {
  45. //handled in endElement
  46. handled = true;
  47. }
  48. }
  49. if (!handled) {
  50. if (PDFExtensionAttachment.CATEGORY.equals(uri)) {
  51. throw new SAXException("Unhandled element " + localName
  52. + " in namespace: " + uri);
  53. } else {
  54. log.warn("Unhandled element " + localName
  55. + " in namespace: " + uri);
  56. }
  57. }
  58. }
  59. /** {@inheritDoc} */
  60. public void endElement(String uri, String localName, String qName) throws SAXException {
  61. if (PDFExtensionAttachment.CATEGORY.equals(uri)) {
  62. if (PDFEmbeddedFileExtensionAttachment.ELEMENT.equals(localName)) {
  63. String name = lastAttributes.getValue("name");
  64. String src = lastAttributes.getValue("src");
  65. String desc = lastAttributes.getValue("description");
  66. this.returnedObject = new PDFEmbeddedFileExtensionAttachment(name, src, desc);
  67. }
  68. }
  69. }
  70. /** {@inheritDoc} */
  71. public void endDocument() throws SAXException {
  72. if (listener != null) {
  73. listener.notifyObjectBuilt(getObject());
  74. }
  75. }
  76. /** {@inheritDoc} */
  77. public Object getObject() {
  78. return returnedObject;
  79. }
  80. /** {@inheritDoc} */
  81. public void setObjectBuiltListener(ObjectBuiltListener listener) {
  82. this.listener = listener;
  83. }
  84. }