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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. */
  38. public class DelegatingContentHandler
  39. implements EntityResolver, DTDHandler, ContentHandler, LexicalHandler, ErrorHandler {
  40. private ContentHandler delegate;
  41. private EntityResolver entityResolver;
  42. private DTDHandler dtdHandler;
  43. private LexicalHandler lexicalHandler;
  44. private ErrorHandler errorHandler;
  45. /**
  46. * Main constructor.
  47. */
  48. public DelegatingContentHandler() {
  49. //nop
  50. }
  51. /**
  52. * @return the delegate that all ContentHandler events are forwarded to
  53. */
  54. public ContentHandler getDelegateContentHandler() {
  55. return this.delegate;
  56. }
  57. /**
  58. * Sets the delegate ContentHandler that all events are forwarded to.
  59. * @param handler the delegate instance
  60. */
  61. public void setDelegateContentHandler(ContentHandler handler) {
  62. this.delegate = handler;
  63. }
  64. /**
  65. * Sets the delegate EntityResolver.
  66. * @param resolver the delegate instance
  67. */
  68. public void setDelegateEntityResolver(EntityResolver resolver) {
  69. this.entityResolver = resolver;
  70. }
  71. /**
  72. * Sets the delegate DTDHandler.
  73. * @param handler the delegate instance
  74. */
  75. public void setDelegateDTDHandler(DTDHandler handler) {
  76. this.dtdHandler = handler;
  77. }
  78. /**
  79. * Sets the delegate LexicalHandler.
  80. * @param handler the delegate instance
  81. */
  82. public void setDelegateLexicalHandler(LexicalHandler handler) {
  83. this.lexicalHandler = handler;
  84. }
  85. /**
  86. * Sets the delegate ErrorHandler.
  87. * @param handler the delegate instance
  88. */
  89. public void setDelegateErrorHandler(ErrorHandler handler) {
  90. this.errorHandler = handler;
  91. }
  92. // ==== EntityResolver
  93. /**
  94. * @inheritDoc org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
  95. */
  96. public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  97. if (entityResolver != null) {
  98. return entityResolver.resolveEntity(publicId, systemId);
  99. } else {
  100. return null;
  101. }
  102. }
  103. // ==== DTDHandler
  104. /**
  105. * @inheritDoc org.xml.sax.DTDHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String)
  106. */
  107. public void notationDecl(String name, String publicId, String systemId) throws SAXException {
  108. if (dtdHandler != null) {
  109. dtdHandler.notationDecl(name, publicId, systemId);
  110. }
  111. }
  112. /**
  113. * @inheritDoc org.xml.sax.DTDHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
  114. */
  115. public void unparsedEntityDecl(String name, String publicId, String systemId,
  116. String notationName) throws SAXException {
  117. if (dtdHandler != null) {
  118. dtdHandler.unparsedEntityDecl(name, publicId, systemId, notationName);
  119. }
  120. }
  121. // ==== ContentHandler
  122. /**
  123. * @inheritDoc org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
  124. */
  125. public void setDocumentLocator(Locator locator) {
  126. delegate.setDocumentLocator(locator);
  127. }
  128. /**
  129. * @inheritDoc org.xml.sax.ContentHandler#startDocument()
  130. */
  131. public void startDocument() throws SAXException {
  132. delegate.startDocument();
  133. }
  134. /**
  135. * @inheritDoc org.xml.sax.ContentHandler#endDocument()
  136. */
  137. public void endDocument() throws SAXException {
  138. delegate.endDocument();
  139. }
  140. /**
  141. * @inheritDoc org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
  142. */
  143. public void startPrefixMapping(String prefix, String uri) throws SAXException {
  144. delegate.startPrefixMapping(prefix, uri);
  145. }
  146. /**
  147. * @inheritDoc org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
  148. */
  149. public void endPrefixMapping(String prefix) throws SAXException {
  150. delegate.endPrefixMapping(prefix);
  151. }
  152. /**
  153. * @inheritDoc org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
  154. */
  155. public void startElement(String uri, String localName, String qName,
  156. Attributes atts) throws SAXException {
  157. delegate.startElement(uri, localName, qName, atts);
  158. }
  159. /**
  160. * @inheritDoc org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
  161. */
  162. public void endElement(String uri, String localName, String qName) throws SAXException {
  163. delegate.endElement(uri, localName, qName);
  164. }
  165. /**
  166. * @inheritDoc org.xml.sax.ContentHandler#characters(char[], int, int)
  167. */
  168. public void characters(char[] ch, int start, int length) throws SAXException {
  169. delegate.characters(ch, start, length);
  170. }
  171. /**
  172. * @inheritDoc org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
  173. */
  174. public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
  175. delegate.ignorableWhitespace(ch, start, length);
  176. }
  177. /**
  178. * @inheritDoc org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
  179. */
  180. public void processingInstruction(String target, String data) throws SAXException {
  181. delegate.processingInstruction(target, data);
  182. }
  183. /**
  184. * @inheritDoc org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
  185. */
  186. public void skippedEntity(String name) throws SAXException {
  187. delegate.skippedEntity(name);
  188. }
  189. // ==== LexicalHandler
  190. /**
  191. * @inheritDoc org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String, java.lang.String, java.lang.String)
  192. */
  193. public void startDTD(String name, String publicId, String systemId) throws SAXException {
  194. if (lexicalHandler != null) {
  195. lexicalHandler.startDTD(name, publicId, systemId);
  196. }
  197. }
  198. /**
  199. * @inheritDoc org.xml.sax.ext.LexicalHandler#endDTD()
  200. */
  201. public void endDTD() throws SAXException {
  202. if (lexicalHandler != null) {
  203. lexicalHandler.endDTD();
  204. }
  205. }
  206. /**
  207. * @inheritDoc org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
  208. */
  209. public void startEntity(String name) throws SAXException {
  210. if (lexicalHandler != null) {
  211. lexicalHandler.startEntity(name);
  212. }
  213. }
  214. /**
  215. * @inheritDoc org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
  216. */
  217. public void endEntity(String name) throws SAXException {
  218. if (lexicalHandler != null) {
  219. lexicalHandler.endEntity(name);
  220. }
  221. }
  222. /**
  223. * @inheritDoc org.xml.sax.ext.LexicalHandler#startCDATA()
  224. */
  225. public void startCDATA() throws SAXException {
  226. if (lexicalHandler != null) {
  227. lexicalHandler.startCDATA();
  228. }
  229. }
  230. /**
  231. * @inheritDoc org.xml.sax.ext.LexicalHandler#endCDATA()
  232. */
  233. public void endCDATA() throws SAXException {
  234. if (lexicalHandler != null) {
  235. lexicalHandler.endCDATA();
  236. }
  237. }
  238. /**
  239. * @inheritDoc org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
  240. */
  241. public void comment(char[] ch, int start, int length) throws SAXException {
  242. if (lexicalHandler != null) {
  243. lexicalHandler.comment(ch, start, length);
  244. }
  245. }
  246. // ==== ErrorHandler
  247. /**
  248. * @inheritDoc org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
  249. */
  250. public void warning(SAXParseException exception) throws SAXException {
  251. if (errorHandler != null) {
  252. errorHandler.warning(exception);
  253. }
  254. }
  255. /**
  256. * @inheritDoc org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
  257. */
  258. public void error(SAXParseException exception) throws SAXException {
  259. if (errorHandler != null) {
  260. errorHandler.error(exception);
  261. }
  262. }
  263. /**
  264. * @inheritDoc org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
  265. */
  266. public void fatalError(SAXParseException exception) throws SAXException {
  267. if (errorHandler != null) {
  268. errorHandler.fatalError(exception);
  269. }
  270. }
  271. }