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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 java.net.URI;
  20. import java.net.URISyntaxException;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.SAXException;
  23. import org.xml.sax.helpers.AttributesImpl;
  24. import org.xml.sax.helpers.DefaultHandler;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.fop.render.afp.extensions.AFPPageSegmentElement.AFPPageSegmentSetup;
  28. import org.apache.fop.util.ContentHandlerFactory;
  29. import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
  30. /**
  31. * ContentHandler (parser) for restoring AFPExtension objects from XML.
  32. */
  33. public class AFPExtensionHandler extends DefaultHandler
  34. implements ContentHandlerFactory.ObjectSource {
  35. /** Logger instance */
  36. protected static final Log log = LogFactory.getLog(AFPExtensionHandler.class);
  37. private StringBuffer content = new StringBuffer();
  38. private Attributes lastAttributes;
  39. private AFPExtensionAttachment returnedObject;
  40. private ObjectBuiltListener listener;
  41. /** {@inheritDoc} */
  42. public void startElement(String uri, String localName, String qName, Attributes attributes)
  43. throws SAXException {
  44. boolean handled = false;
  45. if (AFPExtensionAttachment.CATEGORY.equals(uri)) {
  46. lastAttributes = new AttributesImpl(attributes);
  47. handled = true;
  48. if (localName.equals(AFPElementMapping.NO_OPERATION)
  49. || localName.equals(AFPElementMapping.TAG_LOGICAL_ELEMENT)
  50. || localName.equals(AFPElementMapping.INCLUDE_PAGE_OVERLAY)
  51. || localName.equals(AFPElementMapping.INCLUDE_PAGE_SEGMENT)
  52. || localName.equals(AFPElementMapping.INCLUDE_FORM_MAP)
  53. || localName.equals(AFPElementMapping.INVOKE_MEDIUM_MAP)) {
  54. //handled in endElement
  55. } else {
  56. handled = false;
  57. }
  58. }
  59. if (!handled) {
  60. if (AFPExtensionAttachment.CATEGORY.equals(uri)) {
  61. throw new SAXException("Unhandled element " + localName
  62. + " in namespace: " + uri);
  63. } else {
  64. log.warn("Unhandled element " + localName
  65. + " in namespace: " + uri);
  66. }
  67. }
  68. }
  69. /** {@inheritDoc} */
  70. public void endElement(String uri, String localName, String qName) throws SAXException {
  71. if (AFPExtensionAttachment.CATEGORY.equals(uri)) {
  72. if (AFPElementMapping.INCLUDE_FORM_MAP.equals(localName)) {
  73. AFPIncludeFormMap formMap = new AFPIncludeFormMap();
  74. String name = lastAttributes.getValue("name");
  75. formMap.setName(name);
  76. String src = lastAttributes.getValue("src");
  77. try {
  78. formMap.setSrc(new URI(src));
  79. } catch (URISyntaxException e) {
  80. throw new SAXException("Invalid URI: " + src, e);
  81. }
  82. this.returnedObject = formMap;
  83. } else if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(localName)) {
  84. this.returnedObject = new AFPPageOverlay();
  85. String name = lastAttributes.getValue("name");
  86. if (name != null) {
  87. returnedObject.setName(name);
  88. }
  89. } else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(localName)) {
  90. AFPPageSegmentSetup pageSetupExtn = null;
  91. pageSetupExtn = new AFPPageSegmentSetup(localName);
  92. this.returnedObject = pageSetupExtn;
  93. String name = lastAttributes.getValue("name");
  94. if (name != null) {
  95. returnedObject.setName(name);
  96. }
  97. String value = lastAttributes.getValue("value");
  98. if (value != null && pageSetupExtn != null) {
  99. pageSetupExtn.setValue(value);
  100. }
  101. String resourceSrc = lastAttributes.getValue("resource-file");
  102. if (resourceSrc != null && pageSetupExtn != null) {
  103. pageSetupExtn.setResourceSrc(resourceSrc);
  104. }
  105. if (content.length() > 0 && pageSetupExtn != null) {
  106. pageSetupExtn.setContent(content.toString());
  107. content.setLength(0); //Reset text buffer (see characters())
  108. }
  109. } else {
  110. AFPPageSetup pageSetupExtn = null;
  111. if (AFPElementMapping.INVOKE_MEDIUM_MAP.equals(localName)) {
  112. this.returnedObject = new AFPInvokeMediumMap();
  113. } else {
  114. pageSetupExtn = new AFPPageSetup(localName);
  115. this.returnedObject = pageSetupExtn;
  116. }
  117. String name = lastAttributes.getValue("name");
  118. if (name != null) {
  119. returnedObject.setName(name);
  120. }
  121. String value = lastAttributes.getValue("value");
  122. if (value != null && pageSetupExtn != null) {
  123. pageSetupExtn.setValue(value);
  124. }
  125. if (content.length() > 0 && pageSetupExtn != null) {
  126. pageSetupExtn.setContent(content.toString());
  127. content.setLength(0); //Reset text buffer (see characters())
  128. }
  129. }
  130. }
  131. }
  132. /** {@inheritDoc} */
  133. public void characters(char[] ch, int start, int length) throws SAXException {
  134. content.append(ch, start, length);
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public void endDocument() throws SAXException {
  140. if (listener != null) {
  141. listener.notifyObjectBuilt(getObject());
  142. }
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public Object getObject() {
  148. return returnedObject;
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. public void setObjectBuiltListener(ObjectBuiltListener listen) {
  154. this.listener = listen;
  155. }
  156. }