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.

DelegatingContentHandler.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.util;
  19. import java.io.IOException;
  20. import org.xml.sax.Attributes;
  21. import org.xml.sax.ContentHandler;
  22. import org.xml.sax.DTDHandler;
  23. import org.xml.sax.EntityResolver;
  24. import org.xml.sax.ErrorHandler;
  25. import org.xml.sax.InputSource;
  26. import org.xml.sax.Locator;
  27. import org.xml.sax.SAXException;
  28. import org.xml.sax.SAXParseException;
  29. import org.xml.sax.ext.LexicalHandler;
  30. /**
  31. * SAX 2 Event Handler which simply delegates all calls to another ContentHandler. Subclasses can
  32. * do additional processing. This class is the passive counterpart to XMLFilterImpl.
  33. * <p>
  34. * The ContentHandler is the only instance that is required. All others (DTDHandler,
  35. * EntityResolver, LexicalHandler and ErrorHandler) may be ignored.
  36. */
  37. public class DelegatingContentHandler
  38. implements EntityResolver, DTDHandler, ContentHandler, LexicalHandler, ErrorHandler {
  39. private ContentHandler delegate;
  40. private EntityResolver entityResolver;
  41. private DTDHandler dtdHandler;
  42. private LexicalHandler lexicalHandler;
  43. private ErrorHandler errorHandler;
  44. /**
  45. * Main constructor.
  46. */
  47. public DelegatingContentHandler() {
  48. //nop
  49. }
  50. /**
  51. * Convenience constructor. If the given handler also implements any of the EntityResolver,
  52. * DTDHandler, LexicalHandler or ErrorHandler interfaces, these are set automatically.
  53. * @param handler the content handler to delegate to
  54. */
  55. public DelegatingContentHandler(ContentHandler handler) {
  56. setDelegateContentHandler(handler);
  57. if (handler instanceof EntityResolver) {
  58. setDelegateEntityResolver((EntityResolver)handler);
  59. }
  60. if (handler instanceof DTDHandler) {
  61. setDelegateDTDHandler((DTDHandler)handler);
  62. }
  63. if (handler instanceof LexicalHandler) {
  64. setDelegateLexicalHandler((LexicalHandler)handler);
  65. }
  66. if (handler instanceof ErrorHandler) {
  67. setDelegateErrorHandler((ErrorHandler)handler);
  68. }
  69. }
  70. /**
  71. * @return the delegate that all ContentHandler events are forwarded to
  72. */
  73. public ContentHandler getDelegateContentHandler() {
  74. return this.delegate;
  75. }
  76. /**
  77. * Sets the delegate ContentHandler that all events are forwarded to.
  78. * @param handler the delegate instance
  79. */
  80. public void setDelegateContentHandler(ContentHandler handler) {
  81. this.delegate = handler;
  82. }
  83. /**
  84. * Sets the delegate EntityResolver.
  85. * @param resolver the delegate instance
  86. */
  87. public void setDelegateEntityResolver(EntityResolver resolver) {
  88. this.entityResolver = resolver;
  89. }
  90. /**
  91. * Sets the delegate DTDHandler.
  92. * @param handler the delegate instance
  93. */
  94. public void setDelegateDTDHandler(DTDHandler handler) {
  95. this.dtdHandler = handler;
  96. }
  97. /**
  98. * Sets the delegate LexicalHandler.
  99. * @param handler the delegate instance
  100. */
  101. public void setDelegateLexicalHandler(LexicalHandler handler) {
  102. this.lexicalHandler = handler;
  103. }
  104. /**
  105. * Sets the delegate ErrorHandler.
  106. * @param handler the delegate instance
  107. */
  108. public void setDelegateErrorHandler(ErrorHandler handler) {
  109. this.errorHandler = handler;
  110. }
  111. // ==== EntityResolver
  112. /** {@inheritDoc} */
  113. public InputSource resolveEntity(String publicId, String systemId)
  114. throws SAXException, IOException {
  115. if (entityResolver != null) {
  116. return entityResolver.resolveEntity(publicId, systemId);
  117. } else {
  118. return null;
  119. }
  120. }
  121. // ==== DTDHandler
  122. /** {@inheritDoc} */
  123. public void notationDecl(String name, String publicId, String systemId) throws SAXException {
  124. if (dtdHandler != null) {
  125. dtdHandler.notationDecl(name, publicId, systemId);
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. public void unparsedEntityDecl(String name, String publicId, String systemId,
  130. String notationName) throws SAXException {
  131. if (dtdHandler != null) {
  132. dtdHandler.unparsedEntityDecl(name, publicId, systemId, notationName);
  133. }
  134. }
  135. // ==== ContentHandler
  136. /** {@inheritDoc} */
  137. public void setDocumentLocator(Locator locator) {
  138. delegate.setDocumentLocator(locator);
  139. }
  140. /** {@inheritDoc} */
  141. public void startDocument() throws SAXException {
  142. delegate.startDocument();
  143. }
  144. /** {@inheritDoc} */
  145. public void endDocument() throws SAXException {
  146. delegate.endDocument();
  147. }
  148. /** {@inheritDoc} */
  149. public void startPrefixMapping(String prefix, String uri) throws SAXException {
  150. delegate.startPrefixMapping(prefix, uri);
  151. }
  152. /** {@inheritDoc} */
  153. public void endPrefixMapping(String prefix) throws SAXException {
  154. delegate.endPrefixMapping(prefix);
  155. }
  156. /** {@inheritDoc} */
  157. public void startElement(String uri, String localName, String qName,
  158. Attributes atts) throws SAXException {
  159. delegate.startElement(uri, localName, qName, atts);
  160. }
  161. /** {@inheritDoc} */
  162. public void endElement(String uri, String localName, String qName) throws SAXException {
  163. delegate.endElement(uri, localName, qName);
  164. }
  165. /** {@inheritDoc} */
  166. public void characters(char[] ch, int start, int length) throws SAXException {
  167. delegate.characters(ch, start, length);
  168. }
  169. /** {@inheritDoc} */
  170. public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
  171. delegate.ignorableWhitespace(ch, start, length);
  172. }
  173. /** {@inheritDoc} */
  174. public void processingInstruction(String target, String data) throws SAXException {
  175. delegate.processingInstruction(target, data);
  176. }
  177. /** {@inheritDoc} */
  178. public void skippedEntity(String name) throws SAXException {
  179. delegate.skippedEntity(name);
  180. }
  181. // ==== LexicalHandler
  182. /** {@inheritDoc} */
  183. public void startDTD(String name, String publicId, String systemId) throws SAXException {
  184. if (lexicalHandler != null) {
  185. lexicalHandler.startDTD(name, publicId, systemId);
  186. }
  187. }
  188. /** {@inheritDoc} */
  189. public void endDTD() throws SAXException {
  190. if (lexicalHandler != null) {
  191. lexicalHandler.endDTD();
  192. }
  193. }
  194. /** {@inheritDoc} */
  195. public void startEntity(String name) throws SAXException {
  196. if (lexicalHandler != null) {
  197. lexicalHandler.startEntity(name);
  198. }
  199. }
  200. /** {@inheritDoc} */
  201. public void endEntity(String name) throws SAXException {
  202. if (lexicalHandler != null) {
  203. lexicalHandler.endEntity(name);
  204. }
  205. }
  206. /** {@inheritDoc} */
  207. public void startCDATA() throws SAXException {
  208. if (lexicalHandler != null) {
  209. lexicalHandler.startCDATA();
  210. }
  211. }
  212. /** {@inheritDoc} */
  213. public void endCDATA() throws SAXException {
  214. if (lexicalHandler != null) {
  215. lexicalHandler.endCDATA();
  216. }
  217. }
  218. /** {@inheritDoc} */
  219. public void comment(char[] ch, int start, int length) throws SAXException {
  220. if (lexicalHandler != null) {
  221. lexicalHandler.comment(ch, start, length);
  222. }
  223. }
  224. // ==== ErrorHandler
  225. /** {@inheritDoc} */
  226. public void warning(SAXParseException exception) throws SAXException {
  227. if (errorHandler != null) {
  228. errorHandler.warning(exception);
  229. }
  230. }
  231. /** {@inheritDoc} */
  232. public void error(SAXParseException exception) throws SAXException {
  233. if (errorHandler != null) {
  234. errorHandler.error(exception);
  235. }
  236. }
  237. /** {@inheritDoc} */
  238. public void fatalError(SAXParseException exception) throws SAXException {
  239. if (errorHandler != null) {
  240. errorHandler.fatalError(exception);
  241. }
  242. }
  243. }