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   * Content handler proxy that forwards the received SAX events to zero or
27   * more underlying content handlers.
28   */
29  public class TeeContentHandler extends DefaultHandler {
30  
31      private final ContentHandler[] handlers;
32  
33      public TeeContentHandler(ContentHandler... handlers) {
34          this.handlers = handlers;
35      }
36  
37      @Override
38      public void startPrefixMapping(String prefix, String uri)
39              throws SAXException {
40          for (ContentHandler handler : handlers) {
41              handler.startPrefixMapping(prefix, uri);
42          }
43      }
44  
45      @Override
46      public void endPrefixMapping(String prefix) throws SAXException {
47          for (ContentHandler handler : handlers) {
48              handler.endPrefixMapping(prefix);
49          }
50      }
51  
52      @Override
53      public void processingInstruction(String target, String data)
54              throws SAXException {
55          for (ContentHandler handler : handlers) {
56              handler.processingInstruction(target, data);
57          }
58      }
59  
60      @Override
61      public void setDocumentLocator(Locator locator) {
62          for (ContentHandler handler : handlers) {
63              handler.setDocumentLocator(locator);
64          }
65      }
66  
67      @Override
68      public void startDocument() throws SAXException {
69          for (ContentHandler handler : handlers) {
70              handler.startDocument();
71          }
72      }
73  
74      @Override
75      public void endDocument() throws SAXException {
76          for (ContentHandler handler : handlers) {
77              handler.endDocument();
78          }
79      }
80  
81      @Override
82      public void startElement(
83              String uri, String localName, String name, Attributes atts)
84              throws SAXException {
85          for (ContentHandler handler : handlers) {
86              handler.startElement(uri, localName, name, atts);
87          }
88      }
89  
90      @Override
91      public void endElement(String uri, String localName, String name)
92              throws SAXException {
93          for (ContentHandler handler : handlers) {
94              handler.endElement(uri, localName, name);
95          }
96      }
97  
98      @Override
99      public void characters(char[] ch, int start, int length)
100             throws SAXException {
101         for (ContentHandler handler : handlers) {
102             handler.characters(ch, start, length);
103         }
104     }
105 
106     @Override
107     public void ignorableWhitespace(char[] ch, int start, int length)
108             throws SAXException {
109         for (ContentHandler handler : handlers) {
110             handler.ignorableWhitespace(ch, start, length);
111         }
112     }
113 
114     @Override
115     public void skippedEntity(String name) throws SAXException {
116         for (ContentHandler handler : handlers) {
117             handler.skippedEntity(name);
118         }
119     }
120 
121 }