View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.util;
17  
18  import org.xml.sax.Attributes;
19  import org.xml.sax.ContentHandler;
20  import org.xml.sax.Locator;
21  import org.xml.sax.SAXException;
22  
23  /***
24   * Ensures that only one start and end document event is passed onto the underlying
25   * ContentHandler. This object can only be used once and then discarded.
26   *
27   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28   */
29  public class SafeContentHandler implements ContentHandler {
30      private ContentHandler handler;
31      private boolean documentStarted;
32      private boolean documentEnded;
33  
34      public SafeContentHandler(ContentHandler handler) {
35          this.handler = handler;
36      }
37  
38      /***
39       * @throws org.xml.sax.SAXException
40       */
41      public void startDocument() throws SAXException {
42          if (! documentStarted) {
43              handler.startDocument();
44              documentStarted = true;
45          }
46      }
47  
48      /***
49       * @throws org.xml.sax.SAXException
50       */
51      public void endDocument() throws SAXException {
52          if (! documentEnded) {
53              handler.endDocument();
54              documentEnded = true;
55          }
56      }
57  
58      /***
59       * @param arg0
60       * @param arg1
61       * @param arg2
62       * @throws org.xml.sax.SAXException
63       */
64      public void characters(char[] arg0, int arg1, int arg2)
65          throws SAXException {
66          handler.characters(arg0, arg1, arg2);
67      }
68  
69      /***
70       * @param arg0
71       * @param arg1
72       * @param arg2
73       * @throws org.xml.sax.SAXException
74       */
75      public void endElement(String arg0, String arg1, String arg2)
76          throws SAXException {
77          handler.endElement(arg0, arg1, arg2);
78      }
79  
80      /***
81       * @param arg0
82       * @throws org.xml.sax.SAXException
83       */
84      public void endPrefixMapping(String arg0) throws SAXException {
85          handler.endPrefixMapping(arg0);
86      }
87  
88      /***
89       * @param arg0
90       * @param arg1
91       * @param arg2
92       * @throws org.xml.sax.SAXException
93       */
94      public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
95          throws SAXException {
96          handler.ignorableWhitespace(arg0, arg1, arg2);
97      }
98  
99      /***
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(
132         String arg0,
133         String arg1,
134         String arg2,
135         Attributes arg3)
136         throws SAXException {
137         handler.startElement(arg0, arg1, arg2, arg3);
138     }
139 
140     /***
141      * @param arg0
142      * @param arg1
143      * @throws org.xml.sax.SAXException
144      */
145     public void startPrefixMapping(String arg0, String arg1)
146         throws SAXException {
147         handler.startPrefixMapping(arg0, arg1);
148     }
149 }