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.

AFPExtensionHandler.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.afp.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 AFPExtension objects from XML.
  28. */
  29. public class AFPExtensionHandler extends DefaultHandler
  30. implements ContentHandlerFactory.ObjectSource {
  31. /** Logger instance */
  32. protected static Log log = LogFactory.getLog(AFPExtensionHandler.class);
  33. private StringBuffer content = new StringBuffer();
  34. private Attributes lastAttributes;
  35. private AFPPageSetup 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 (AFPPageSetup.CATEGORY.equals(uri)) {
  42. lastAttributes = attributes;
  43. handled = true;
  44. if (localName.equals(AFPElementMapping.NO_OPERATION)
  45. || localName.equals(AFPElementMapping.TAG_LOGICAL_ELEMENT)
  46. || localName.equals(AFPElementMapping.INCLUDE_PAGE_OVERLAY)
  47. || localName.equals(AFPElementMapping.INCLUDE_PAGE_SEGMENT)
  48. || localName.equals(AFPElementMapping.PAGE)
  49. || localName.equals(AFPElementMapping.PAGE_GROUP)) {
  50. //handled in endElement
  51. } else {
  52. handled = false;
  53. }
  54. }
  55. if (!handled) {
  56. if (AFPPageSetup.CATEGORY.equals(uri)) {
  57. throw new SAXException("Unhandled element " + localName
  58. + " in namespace: " + uri);
  59. } else {
  60. log.warn("Unhandled element " + localName
  61. + " in namespace: " + uri);
  62. }
  63. }
  64. }
  65. /** {@inheritDoc} */
  66. public void endElement(String uri, String localName, String qName) throws SAXException {
  67. if (AFPPageSetup.CATEGORY.equals(uri)) {
  68. this.returnedObject = new AFPPageSetup(localName);
  69. String name = lastAttributes.getValue("name");
  70. if (name != null) {
  71. returnedObject.setName(name);
  72. }
  73. String value = lastAttributes.getValue("value");
  74. if (value != null) {
  75. returnedObject.setValue(value);
  76. }
  77. if (content.length() > 0) {
  78. returnedObject.setContent(content.toString());
  79. content.setLength(0); //Reset text buffer (see characters())
  80. }
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. public void characters(char[] ch, int start, int length) throws SAXException {
  85. content.append(ch, start, length);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public void endDocument() throws SAXException {
  91. if (listener != null) {
  92. listener.notifyObjectBuilt(getObject());
  93. }
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public Object getObject() {
  99. return returnedObject;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public void setObjectBuiltListener(ObjectBuiltListener listen) {
  105. this.listener = listen;
  106. }
  107. }