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

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