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.

PSExtensionHandler.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.ps.extensions;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.util.ContentHandlerFactory;
  22. import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
  23. import org.xml.sax.Attributes;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.helpers.DefaultHandler;
  26. /**
  27. * ContentHandler (parser) for restoring PSExtension objects from XML.
  28. */
  29. public class PSExtensionHandler extends DefaultHandler
  30. implements ContentHandlerFactory.ObjectSource {
  31. /** Logger instance */
  32. protected static Log log = LogFactory.getLog(PSExtensionHandler.class);
  33. private StringBuffer content = new StringBuffer();
  34. private Attributes lastAttributes;
  35. private PSExtensionAttachment 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 (PSExtensionAttachment.CATEGORY.equals(uri)) {
  42. lastAttributes = attributes;
  43. handled = false;
  44. if (localName.equals(PSSetupCode.ELEMENT)
  45. || localName.equals(PSSetPageDevice.ELEMENT)
  46. || localName.equals(PSCommentBefore.ELEMENT)
  47. || localName.equals(PSCommentAfter.ELEMENT)) {
  48. //handled in endElement
  49. handled = true;
  50. }
  51. }
  52. if (!handled) {
  53. if (PSExtensionAttachment.CATEGORY.equals(uri)) {
  54. throw new SAXException("Unhandled element " + localName
  55. + " in namespace: " + uri);
  56. } else {
  57. log.warn("Unhandled element " + localName
  58. + " in namespace: " + uri);
  59. }
  60. }
  61. }
  62. /** {@inheritDoc} */
  63. public void endElement(String uri, String localName, String qName) throws SAXException {
  64. if (PSExtensionAttachment.CATEGORY.equals(uri)) {
  65. if (PSSetupCode.ELEMENT.equals(localName)) {
  66. String name = lastAttributes.getValue("name");
  67. this.returnedObject = new PSSetupCode(name, content.toString());
  68. } else if (PSSetPageDevice.ELEMENT.equals(localName)) {
  69. String name = lastAttributes.getValue("name");
  70. this.returnedObject = new PSSetPageDevice(name, content.toString());
  71. } else if (PSCommentBefore.ELEMENT.equals(localName)) {
  72. this.returnedObject = new PSCommentBefore(content.toString());
  73. } else if (PSCommentAfter.ELEMENT.equals(localName)) {
  74. this.returnedObject = new PSCommentAfter(content.toString());
  75. }
  76. }
  77. content.setLength(0); //Reset text buffer (see characters())
  78. }
  79. /** {@inheritDoc} */
  80. public void characters(char[] ch, int start, int length) throws SAXException {
  81. content.append(ch, start, length);
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public void endDocument() throws SAXException {
  87. if (listener != null) {
  88. listener.notifyObjectBuilt(getObject());
  89. }
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public Object getObject() {
  95. return returnedObject;
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public void setObjectBuiltListener(ObjectBuiltListener listener) {
  101. this.listener = listener;
  102. }
  103. }