View Javadoc

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  package org.apache.tika.sax;
18  
19  import org.xml.sax.Attributes;
20  import org.xml.sax.ContentHandler;
21  import org.xml.sax.Locator;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.helpers.DefaultHandler;
24  
25  /**
26   * Decorator base class for the {@link ContentHandler} interface. This class
27   * simply delegates all SAX events calls to an underlying decorated handler
28   * instance. Subclasses can provide extra decoration by overriding one or more
29   * of the SAX event methods.
30   */
31  public class ContentHandlerDecorator extends DefaultHandler {
32  
33      /**
34       * Decorated SAX event handler.
35       */
36      private final ContentHandler handler;
37  
38      /**
39       * Creates a decorator for the given SAX event handler.
40       *
41       * @param handler SAX event handler to be decorated
42       */
43      public ContentHandlerDecorator(ContentHandler handler) {
44          this.handler = handler;
45      }
46  
47      @Override
48      public void startPrefixMapping(String prefix, String uri)
49              throws SAXException {
50          try {
51              handler.startPrefixMapping(prefix, uri);
52          } catch (SAXException e) {
53              handleException(e);
54          }
55      }
56  
57      @Override
58      public void endPrefixMapping(String prefix) throws SAXException {
59          try {
60              handler.endPrefixMapping(prefix);
61          } catch (SAXException e) {
62              handleException(e);
63          }
64      }
65  
66      @Override
67      public void processingInstruction(String target, String data)
68              throws SAXException {
69          try {
70              handler.processingInstruction(target, data);
71          } catch (SAXException e) {
72              handleException(e);
73          }
74      }
75  
76      @Override
77      public void setDocumentLocator(Locator locator) {
78          handler.setDocumentLocator(locator);
79      }
80  
81      @Override
82      public void startDocument() throws SAXException {
83          try {
84              handler.startDocument();
85          } catch (SAXException e) {
86              handleException(e);
87          }
88      }
89  
90      @Override
91      public void endDocument() throws SAXException {
92          try {
93              handler.endDocument();
94          } catch (SAXException e) {
95              handleException(e);
96          }
97      }
98  
99      @Override
100     public void startElement(
101             String uri, String localName, String name, Attributes atts)
102             throws SAXException {
103         try {
104             handler.startElement(uri, localName, name, atts);
105         } catch (SAXException e) {
106             handleException(e);
107         }
108     }
109 
110     @Override
111     public void endElement(String uri, String localName, String name)
112             throws SAXException {
113         try {
114             handler.endElement(uri, localName, name);
115         } catch (SAXException e) {
116             handleException(e);
117         }
118     }
119 
120     @Override
121     public void characters(char[] ch, int start, int length)
122             throws SAXException {
123         try {
124             handler.characters(ch, start, length);
125         } catch (SAXException e) {
126             handleException(e);
127         }
128     }
129 
130     @Override
131     public void ignorableWhitespace(char[] ch, int start, int length)
132             throws SAXException {
133         try {
134             handler.ignorableWhitespace(ch, start, length);
135         } catch (SAXException e) {
136             handleException(e);
137         }
138     }
139 
140     @Override
141     public void skippedEntity(String name) throws SAXException {
142         try {
143             handler.skippedEntity(name);
144         } catch (SAXException e) {
145             handleException(e);
146         }
147     }
148 
149     @Override
150     public String toString() {
151         return handler.toString();
152     }
153 
154     /**
155      * Handle any exceptions thrown by methods in this class. This method
156      * provides a single place to implement custom exception handling. The
157      * default behaviour is simply to re-throw the given exception, but
158      * subclasses can also provide alternative ways of handling the situation.
159      *
160      * @param exception the exception that was thrown
161      * @throws SAXException the exception (if any) thrown to the client
162      */
163     protected void handleException(SAXException exception) throws SAXException {
164         throw exception;
165     }
166 
167 }