View Javadoc
1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.tools.internal.xjc.util;
27  
28  import org.xml.sax.Attributes;
29  import org.xml.sax.ContentHandler;
30  import org.xml.sax.Locator;
31  import org.xml.sax.SAXException;
32  
33  /**
34   * ContentHandler that "forks" the incoming SAX2 events to
35   * two ContentHandlers.
36   *
37   *
38   * @author  <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
39   */
40  public class ForkContentHandler implements ContentHandler {
41  
42          /**
43           * Creates a ForkContentHandler.
44           *
45           * @param first
46           *     This handler will receive a SAX event first.
47           * @param second
48           *     This handler will receive a SAX event after the first handler
49           *     receives it.
50           */
51          public ForkContentHandler( ContentHandler first, ContentHandler second ) {
52                  lhs = first;
53                  rhs = second;
54          }
55  
56          /**
57           * Creates ForkContentHandlers so that the specified handlers
58           * will receive SAX events in the order of the array.
59           */
60          public static ContentHandler create( ContentHandler[] handlers ) {
61                  if(handlers.length==0)
62                          throw new IllegalArgumentException();
63  
64                  ContentHandler result = handlers[0];
65                  for( int i=1; i<handlers.length; i++ )
66                          result = new ForkContentHandler( result, handlers[i] );
67                  return result;
68          }
69  
70  
71          private final ContentHandler lhs,rhs;
72  
73          public void setDocumentLocator (Locator locator) {
74                  lhs.setDocumentLocator(locator);
75                  rhs.setDocumentLocator(locator);
76          }
77  
78          public void startDocument() throws SAXException {
79                  lhs.startDocument();
80                  rhs.startDocument();
81          }
82  
83          public void endDocument () throws SAXException {
84                  lhs.endDocument();
85                  rhs.endDocument();
86          }
87  
88          public void startPrefixMapping (String prefix, String uri) throws SAXException {
89                  lhs.startPrefixMapping(prefix,uri);
90                  rhs.startPrefixMapping(prefix,uri);
91          }
92  
93          public void endPrefixMapping (String prefix) throws SAXException {
94                  lhs.endPrefixMapping(prefix);
95                  rhs.endPrefixMapping(prefix);
96          }
97  
98          public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {
99                  lhs.startElement(uri,localName,qName,attributes);
100                 rhs.startElement(uri,localName,qName,attributes);
101         }
102 
103         public void endElement (String uri, String localName, String qName) throws SAXException {
104                 lhs.endElement(uri,localName,qName);
105                 rhs.endElement(uri,localName,qName);
106         }
107 
108         public void characters (char ch[], int start, int length) throws SAXException {
109                 lhs.characters(ch,start,length);
110                 rhs.characters(ch,start,length);
111         }
112 
113         public void ignorableWhitespace (char ch[], int start, int length) throws SAXException {
114                 lhs.ignorableWhitespace(ch,start,length);
115                 rhs.ignorableWhitespace(ch,start,length);
116         }
117 
118         public void processingInstruction (String target, String data) throws SAXException {
119                 lhs.processingInstruction(target,data);
120                 rhs.processingInstruction(target,data);
121         }
122 
123         public void skippedEntity (String name) throws SAXException {
124                 lhs.skippedEntity(name);
125                 rhs.skippedEntity(name);
126         }
127 
128 }