001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.util;
017:
018: import org.xml.sax.Attributes;
019: import org.xml.sax.ContentHandler;
020: import org.xml.sax.Locator;
021: import org.xml.sax.SAXException;
022:
023: /**
024: * Ensures that only one start and end document event is passed onto the underlying
025: * ContentHandler. This object can only be used once and then discarded.
026: *
027: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
028: */
029: public class SafeContentHandler implements ContentHandler {
030: private ContentHandler handler;
031: private boolean documentStarted;
032: private boolean documentEnded;
033:
034: public SafeContentHandler(ContentHandler handler) {
035: this .handler = handler;
036: }
037:
038: /**
039: * @throws org.xml.sax.SAXException
040: */
041: public void startDocument() throws SAXException {
042: if (!documentStarted) {
043: handler.startDocument();
044: documentStarted = true;
045: }
046: }
047:
048: /**
049: * @throws org.xml.sax.SAXException
050: */
051: public void endDocument() throws SAXException {
052: if (!documentEnded) {
053: handler.endDocument();
054: documentEnded = true;
055: }
056: }
057:
058: /**
059: * @param arg0
060: * @param arg1
061: * @param arg2
062: * @throws org.xml.sax.SAXException
063: */
064: public void characters(char[] arg0, int arg1, int arg2)
065: throws SAXException {
066: handler.characters(arg0, arg1, arg2);
067: }
068:
069: /**
070: * @param arg0
071: * @param arg1
072: * @param arg2
073: * @throws org.xml.sax.SAXException
074: */
075: public void endElement(String arg0, String arg1, String arg2)
076: throws SAXException {
077: handler.endElement(arg0, arg1, arg2);
078: }
079:
080: /**
081: * @param arg0
082: * @throws org.xml.sax.SAXException
083: */
084: public void endPrefixMapping(String arg0) throws SAXException {
085: handler.endPrefixMapping(arg0);
086: }
087:
088: /**
089: * @param arg0
090: * @param arg1
091: * @param arg2
092: * @throws org.xml.sax.SAXException
093: */
094: public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
095: throws SAXException {
096: handler.ignorableWhitespace(arg0, arg1, arg2);
097: }
098:
099: /**
100: * @param arg0
101: * @param arg1
102: * @throws org.xml.sax.SAXException
103: */
104: public void processingInstruction(String arg0, String arg1)
105: throws SAXException {
106: handler.processingInstruction(arg0, arg1);
107: }
108:
109: /**
110: * @param arg0
111: */
112: public void setDocumentLocator(Locator arg0) {
113: handler.setDocumentLocator(arg0);
114: }
115:
116: /**
117: * @param arg0
118: * @throws org.xml.sax.SAXException
119: */
120: public void skippedEntity(String arg0) throws SAXException {
121: handler.skippedEntity(arg0);
122: }
123:
124: /**
125: * @param arg0
126: * @param arg1
127: * @param arg2
128: * @param arg3
129: * @throws org.xml.sax.SAXException
130: */
131: public void startElement(String arg0, String arg1, String arg2,
132: Attributes arg3) throws SAXException {
133: handler.startElement(arg0, arg1, arg2, arg3);
134: }
135:
136: /**
137: * @param arg0
138: * @param arg1
139: * @throws org.xml.sax.SAXException
140: */
141: public void startPrefixMapping(String arg0, String arg1)
142: throws SAXException {
143: handler.startPrefixMapping(arg0, arg1);
144: }
145: }
|