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.ContentHandler;
020: import org.xml.sax.SAXException;
021: import org.xml.sax.ext.LexicalHandler;
022:
023: /**
024: * This class implements a ContentHandler for embedding a full SAX
025: * event stream into an existing stream of SAX events. An instance of
026: * this class will pass unmodified all the SAX events to the linked
027: * ContentHandler, but it will ignore the startDocument/endDocument
028: * and startDTD/endDTD events, as well as all comment events within
029: * the DTD.
030: *
031: * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
032: * @version CVS $Id: EmbeddedXMLPipe.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class EmbeddedXMLPipe extends AbstractXMLPipe {
035:
036: private boolean inDTD;
037:
038: /**
039: * Creates an EmbeddedXMLPipe that writes into the given XMLConsumer.
040: */
041: public EmbeddedXMLPipe(XMLConsumer consumer) {
042: setConsumer(consumer);
043: }
044:
045: /**
046: * Creates an EmbeddedXMLPipe that writes into the given ContentHandler.
047: */
048: public EmbeddedXMLPipe(ContentHandler handler) {
049: setContentHandler(handler);
050: if (handler instanceof LexicalHandler) {
051: setLexicalHandler((LexicalHandler) handler);
052: }
053: }
054:
055: /**
056: * Creates an EmbeddedXMLPipe that writes into the given ContentHandler.
057: */
058: public EmbeddedXMLPipe(ContentHandler contentHandler,
059: LexicalHandler lexicalHandler) {
060: setContentHandler(contentHandler);
061: setLexicalHandler(lexicalHandler);
062: }
063:
064: /**
065: * Ignore the <code>startDocument</code> event: this method does nothing.
066: *
067: * @exception SAXException if an error occurs
068: */
069: public void startDocument() throws SAXException {
070: }
071:
072: /**
073: * Ignore the <code>endDocument</code> event: this method does nothing.
074: *
075: * @exception SAXException if an error occurs
076: */
077: public void endDocument() throws SAXException {
078: }
079:
080: /**
081: * Ignore the <code>startDTD</code> event: this method does nothing.
082: *
083: * @exception SAXException if an error occurs
084: */
085: public void startDTD(String name, String publicId, String systemId)
086: throws SAXException {
087: // Ignored
088: this .inDTD = true;
089: }
090:
091: /**
092: * Ignore the <code>endDTD</code> event: this method does nothing.
093: *
094: * @exception SAXException if an error occurs
095: */
096: public void endDTD() throws SAXException {
097: // Ignored
098: this .inDTD = false;
099: }
100:
101: /**
102: * Ignore all <code>comment</code> events if between
103: * startDTD/endDTD events.
104: *
105: * @exception SAXException if an error occurs
106: */
107: public void comment(char ch[], int start, int len)
108: throws SAXException {
109: if (!inDTD) {
110: super.comment(ch, start, len);
111: }
112: }
113: }
|