001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml;
018:
019: import org.xml.sax.Attributes;
020: import org.xml.sax.ContentHandler;
021: import org.xml.sax.Locator;
022: import org.xml.sax.SAXException;
023: import org.xml.sax.ext.LexicalHandler;
024:
025: /**
026: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
027: * @version CVS $Id: XMLMulticaster.java 433543 2006-08-22 06:22:54Z crossley $
028: */
029:
030: public final class XMLMulticaster implements XMLConsumer {
031:
032: /**
033: * The XMLMulticaster forwards incomming sax events to a list of
034: * receiving objects.
035: */
036: private ContentHandler[] contentHandlerList;
037: private LexicalHandler[] lexicalHandlerList;
038:
039: /**
040: * Create a new XMLMulticaster with two consumers
041: */
042: public XMLMulticaster(XMLConsumer firstConsumer,
043: XMLConsumer secondConsumer) {
044: this .contentHandlerList = new ContentHandler[] { firstConsumer,
045: secondConsumer };
046: this .lexicalHandlerList = new LexicalHandler[] { firstConsumer,
047: secondConsumer };
048: }
049:
050: /**
051: * Create a new XMLMulticaster from two contentHandler/lexicalHandler pairs
052: */
053: public XMLMulticaster(ContentHandler firstContentHandler,
054: LexicalHandler firstLexicalHandler,
055: ContentHandler secondContentHandler,
056: LexicalHandler secondLexicalHandler) {
057: this .contentHandlerList = new ContentHandler[] {
058: firstContentHandler, secondContentHandler };
059: this .lexicalHandlerList = new LexicalHandler[] {
060: firstLexicalHandler, secondLexicalHandler };
061: }
062:
063: public XMLMulticaster(ContentHandler[] chList,
064: LexicalHandler[] lhList) {
065: this .contentHandlerList = chList;
066: this .lexicalHandlerList = lhList;
067: }
068:
069: public void startDocument() throws SAXException {
070: for (int i = 0; i < this .contentHandlerList.length; i++) {
071: this .contentHandlerList[i].startDocument();
072: }
073: }
074:
075: public void endDocument() throws SAXException {
076: for (int i = 0; i < this .contentHandlerList.length; i++) {
077: this .contentHandlerList[i].endDocument();
078: }
079: }
080:
081: public void startPrefixMapping(java.lang.String prefix,
082: java.lang.String uri) throws SAXException {
083: for (int i = 0; i < this .contentHandlerList.length; i++)
084: this .contentHandlerList[i].startPrefixMapping(prefix, uri);
085: }
086:
087: public void endPrefixMapping(java.lang.String prefix)
088: throws SAXException {
089: for (int i = 0; i < this .contentHandlerList.length; i++)
090: this .contentHandlerList[i].endPrefixMapping(prefix);
091: }
092:
093: public void startElement(java.lang.String namespaceURI,
094: java.lang.String localName, java.lang.String qName,
095: Attributes atts) throws SAXException {
096: for (int i = 0; i < this .contentHandlerList.length; i++)
097: this .contentHandlerList[i].startElement(namespaceURI,
098: localName, qName, atts);
099: }
100:
101: public void endElement(java.lang.String namespaceURI,
102: java.lang.String localName, java.lang.String qName)
103: throws SAXException {
104: for (int i = 0; i < this .contentHandlerList.length; i++)
105: this .contentHandlerList[i].endElement(namespaceURI,
106: localName, qName);
107: }
108:
109: public void characters(char[] ch, int start, int length)
110: throws SAXException {
111: for (int i = 0; i < this .contentHandlerList.length; i++)
112: this .contentHandlerList[i].characters(ch, start, length);
113: }
114:
115: public void ignorableWhitespace(char[] ch, int start, int length)
116: throws SAXException {
117: for (int i = 0; i < this .contentHandlerList.length; i++)
118: this .contentHandlerList[i].ignorableWhitespace(ch, start,
119: length);
120: }
121:
122: public void processingInstruction(java.lang.String target,
123: java.lang.String data) throws SAXException {
124: for (int i = 0; i < this .contentHandlerList.length; i++)
125: this .contentHandlerList[i].processingInstruction(target,
126: data);
127: }
128:
129: public void setDocumentLocator(Locator locator) {
130: for (int i = 0; i < this .contentHandlerList.length; i++)
131: this .contentHandlerList[i].setDocumentLocator(locator);
132: }
133:
134: public void skippedEntity(java.lang.String name)
135: throws SAXException {
136: for (int i = 0; i < this .contentHandlerList.length; i++)
137: this .contentHandlerList[i].skippedEntity(name);
138: }
139:
140: public void startDTD(String name, String public_id, String system_id)
141: throws SAXException {
142: for (int i = 0; i < this .lexicalHandlerList.length; i++)
143: if (this .lexicalHandlerList[i] != null)
144: this .lexicalHandlerList[i].startDTD(name, public_id,
145: system_id);
146: }
147:
148: public void endDTD() throws SAXException {
149: for (int i = 0; i < this .lexicalHandlerList.length; i++)
150: if (this .lexicalHandlerList[i] != null)
151: this .lexicalHandlerList[i].endDTD();
152: }
153:
154: public void startEntity(String name) throws SAXException {
155: for (int i = 0; i < this .lexicalHandlerList.length; i++)
156: if (this .lexicalHandlerList[i] != null)
157: this .lexicalHandlerList[i].startEntity(name);
158: }
159:
160: public void endEntity(String name) throws SAXException {
161: for (int i = 0; i < this .lexicalHandlerList.length; i++)
162: if (this .lexicalHandlerList[i] != null)
163: this .lexicalHandlerList[i].endEntity(name);
164: }
165:
166: public void startCDATA() throws SAXException {
167: for (int i = 0; i < this .lexicalHandlerList.length; i++)
168: if (this .lexicalHandlerList[i] != null)
169: this .lexicalHandlerList[i].startCDATA();
170: }
171:
172: public void endCDATA() throws SAXException {
173: for (int i = 0; i < this .lexicalHandlerList.length; i++)
174: if (this .lexicalHandlerList[i] != null)
175: this .lexicalHandlerList[i].endCDATA();
176: }
177:
178: public void comment(char ary[], int start, int length)
179: throws SAXException {
180: for (int i = 0; i < this.lexicalHandlerList.length; i++)
181: if (this.lexicalHandlerList[i] != null)
182: this.lexicalHandlerList[i].comment(ary, start, length);
183: }
184: }
|