001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.util;
038:
039: import org.xml.sax.Attributes;
040: import org.xml.sax.ContentHandler;
041: import org.xml.sax.Locator;
042: import org.xml.sax.SAXException;
043:
044: /**
045: * ContentHandler that "forks" the incoming SAX2 events to
046: * two ContentHandlers.
047: *
048: *
049: * @version $Id: ForkContentHandler.java,v 1.2.12.1 2007/05/31 22:02:01 ofung Exp $
050: * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
051: */
052: public class ForkContentHandler implements ContentHandler {
053:
054: /**
055: * Creates a ForkContentHandler.
056: *
057: * @param first
058: * This handler will receive a SAX event first.
059: * @param second
060: * This handler will receive a SAX event after the first handler
061: * receives it.
062: */
063: public ForkContentHandler(ContentHandler first,
064: ContentHandler second) {
065: lhs = first;
066: rhs = second;
067: }
068:
069: /**
070: * Creates ForkContentHandlers so that the specified handlers
071: * will receive SAX events in the order of the array.
072: */
073: public static ContentHandler create(ContentHandler[] handlers) {
074: if (handlers.length == 0)
075: throw new IllegalArgumentException();
076:
077: ContentHandler result = handlers[0];
078: for (int i = 1; i < handlers.length; i++)
079: result = new ForkContentHandler(result, handlers[i]);
080: return result;
081: }
082:
083: private final ContentHandler lhs, rhs;
084:
085: public void setDocumentLocator(Locator locator) {
086: lhs.setDocumentLocator(locator);
087: rhs.setDocumentLocator(locator);
088: }
089:
090: public void startDocument() throws SAXException {
091: lhs.startDocument();
092: rhs.startDocument();
093: }
094:
095: public void endDocument() throws SAXException {
096: lhs.endDocument();
097: rhs.endDocument();
098: }
099:
100: public void startPrefixMapping(String prefix, String uri)
101: throws SAXException {
102: lhs.startPrefixMapping(prefix, uri);
103: rhs.startPrefixMapping(prefix, uri);
104: }
105:
106: public void endPrefixMapping(String prefix) throws SAXException {
107: lhs.endPrefixMapping(prefix);
108: rhs.endPrefixMapping(prefix);
109: }
110:
111: public void startElement(String uri, String localName,
112: String qName, Attributes attributes) throws SAXException {
113: lhs.startElement(uri, localName, qName, attributes);
114: rhs.startElement(uri, localName, qName, attributes);
115: }
116:
117: public void endElement(String uri, String localName, String qName)
118: throws SAXException {
119: lhs.endElement(uri, localName, qName);
120: rhs.endElement(uri, localName, qName);
121: }
122:
123: public void characters(char ch[], int start, int length)
124: throws SAXException {
125: lhs.characters(ch, start, length);
126: rhs.characters(ch, start, length);
127: }
128:
129: public void ignorableWhitespace(char ch[], int start, int length)
130: throws SAXException {
131: lhs.ignorableWhitespace(ch, start, length);
132: rhs.ignorableWhitespace(ch, start, length);
133: }
134:
135: public void processingInstruction(String target, String data)
136: throws SAXException {
137: lhs.processingInstruction(target, data);
138: rhs.processingInstruction(target, data);
139: }
140:
141: public void skippedEntity(String name) throws SAXException {
142: lhs.skippedEntity(name);
143: rhs.skippedEntity(name);
144: }
145:
146: }
|