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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. @Override
  43. public void startElement(String uri, String localName, String qName, Attributes attributes)
  44. throws SAXException {
  45. boolean handled = false;
  46. if (AFPExtensionAttachment.CATEGORY.equals(uri)) {
  47. lastAttributes = new AttributesImpl(attributes);
  48. handled = true;
  49. if (localName.equals(AFPElementMapping.NO_OPERATION)
  50. || localName.equals(AFPElementMapping.TAG_LOGICAL_ELEMENT)
  51. || localName.equals(AFPElementMapping.INCLUDE_PAGE_OVERLAY)
  52. || localName.equals(AFPElementMapping.INCLUDE_PAGE_SEGMENT)
  53. || localName.equals(AFPElementMapping.INCLUDE_FORM_MAP)
  54. || localName.equals(AFPElementMapping.INVOKE_MEDIUM_MAP)) {
  55. //handled in endElement
  56. } else {
  57. handled = false;
  58. }
  59. }
  60. if (!handled) {
  61. if (AFPExtensionAttachment.CATEGORY.equals(uri)) {
  62. throw new SAXException("Unhandled element " + localName
  63. + " in namespace: " + uri);
  64. } else {
  65. log.warn("Unhandled element " + localName
  66. + " in namespace: " + uri);
  67. }
  68. }
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public void endElement(String uri, String localName, String qName) throws SAXException {
  73. if (AFPExtensionAttachment.CATEGORY.equals(uri)) {
  74. if (AFPElementMapping.INCLUDE_FORM_MAP.equals(localName)) {
  75. AFPIncludeFormMap formMap = new AFPIncludeFormMap();
  76. String name = lastAttributes.getValue("name");
  77. formMap.setName(name);
  78. String src = lastAttributes.getValue("src");
  79. try {
  80. formMap.setSrc(new URI(src));
  81. } catch (URISyntaxException e) {
  82. throw new SAXException("Invalid URI: " + src, e);
  83. }
  84. this.returnedObject = formMap;
  85. } else if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(localName)) {
  86. this.returnedObject = new AFPPageOverlay();
  87. String name = lastAttributes.getValue("name");
  88. if (name != null) {
  89. returnedObject.setName(name);
  90. }
  91. } else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(localName)) {
  92. AFPPageSegmentSetup pageSetupExtn = null;
  93. pageSetupExtn = new AFPPageSegmentSetup(localName);
  94. this.returnedObject = pageSetupExtn;
  95. String name = lastAttributes.getValue("name");
  96. if (name != null) {
  97. returnedObject.setName(name);
  98. }
  99. String value = lastAttributes.getValue("value");
  100. if (value != null && pageSetupExtn != null) {
  101. pageSetupExtn.setValue(value);
  102. }
  103. String resourceSrc = lastAttributes.getValue("resource-file");
  104. if (resourceSrc != null && pageSetupExtn != null) {
  105. pageSetupExtn.setResourceSrc(resourceSrc);
  106. }
  107. if (content.length() > 0 && pageSetupExtn != null) {
  108. pageSetupExtn.setContent(content.toString());
  109. content.setLength(0); //Reset text buffer (see characters())
  110. }
  111. } else {
  112. AFPPageSetup pageSetupExtn = null;
  113. if (AFPElementMapping.INVOKE_MEDIUM_MAP.equals(localName)) {
  114. this.returnedObject = new AFPInvokeMediumMap();
  115. } else {
  116. pageSetupExtn = new AFPPageSetup(localName);
  117. this.returnedObject = pageSetupExtn;
  118. }
  119. String name = lastAttributes.getValue(AFPPageSetup.ATT_NAME);
  120. if (name != null) {
  121. returnedObject.setName(name);
  122. }
  123. String value = lastAttributes.getValue(AFPPageSetup.ATT_VALUE);
  124. if (value != null && pageSetupExtn != null) {
  125. pageSetupExtn.setValue(value);
  126. }
  127. String placement = lastAttributes.getValue(AFPPageSetup.ATT_PLACEMENT);
  128. if (placement != null && placement.length() > 0) {
  129. pageSetupExtn.setPlacement(ExtensionPlacement.fromXMLValue(placement));
  130. }
  131. String encoding = lastAttributes.getValue("encoding");
  132. if (encoding != null && pageSetupExtn != null) {
  133. pageSetupExtn.setEncoding(Integer.parseInt(encoding));
  134. }
  135. if (content.length() > 0 && pageSetupExtn != null) {
  136. pageSetupExtn.setContent(content.toString());
  137. content.setLength(0); //Reset text buffer (see characters())
  138. }
  139. }
  140. }
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public void characters(char[] ch, int start, int length) throws SAXException {
  145. content.append(ch, start, length);
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. @Override
  151. public void endDocument() throws SAXException {
  152. if (listener != null) {
  153. listener.notifyObjectBuilt(getObject());
  154. }
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public Object getObject() {
  160. return returnedObject;
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public void setObjectBuiltListener(ObjectBuiltListener listen) {
  166. this.listener = listen;
  167. }
  168. }