KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > validation > jing > JingReader


1 /*
2  * Copyright 1999-2005 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.cocoon.components.validation.jing;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Stack JavaDoc;
20
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22 import javax.xml.parsers.SAXParserFactory JavaDoc;
23
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.DTDHandler JavaDoc;
26 import org.xml.sax.ErrorHandler JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXNotRecognizedException JavaDoc;
30 import org.xml.sax.SAXNotSupportedException JavaDoc;
31 import org.xml.sax.XMLReader JavaDoc;
32
33 import com.thaiopensource.xml.sax.DraconianErrorHandler;
34
35 /**
36  * <p>A trivial {@link XMLReader} implementation populating and clearing the
37  * {@link Stack} of {@link InputSource}s for URI resolution kept inside
38  * a {@link JingResolver}.</p>
39  */

40 final class JingReader implements XMLReader JavaDoc {
41     
42     /** <p>The underlying {@link XMLReader} to use.</p> */
43     private final XMLReader JavaDoc reader;
44     /** <p>The {@link JingResolver} associated with this instance.</p> */
45     private final JingResolver context;
46
47     /**
48      * <p>Create a new {@link JingReader} instance associated with the specified
49      * {@link JingResolver}.</p>
50      */

51     protected JingReader(JingResolver context)
52     throws SAXException JavaDoc {
53         /*
54          * We have to look up the XMLReader using JAXP or SAX, as the SAXParser
55          * supplied by Avalon/Excalibur does not seem to work with JING.
56          */

57         try {
58             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
59             factory.setNamespaceAware(true);
60             factory.setValidating(false);
61             this.reader = factory.newSAXParser().getXMLReader();
62             this.setEntityResolver(context);
63             this.setErrorHandler(new DraconianErrorHandler());
64             this.context = context;
65         } catch (ParserConfigurationException JavaDoc exception) {
66             throw new SAXException JavaDoc("Can't create XML reader instance", exception);
67         }
68     }
69
70     public boolean getFeature(String JavaDoc feature)
71     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
72         return this.reader.getFeature(feature);
73     }
74
75     public void setFeature(String JavaDoc feature, boolean value)
76     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
77         this.reader.setFeature(feature, value);
78     }
79
80     public Object JavaDoc getProperty(String JavaDoc property)
81     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
82         return this.reader.getProperty(property);
83     }
84
85     public void setProperty(String JavaDoc property, Object JavaDoc value)
86     throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
87         this.reader.setProperty(property, value);
88     }
89
90     public void setEntityResolver(org.xml.sax.EntityResolver JavaDoc resolver) {
91         this.reader.setEntityResolver(resolver);
92     }
93
94     public org.xml.sax.EntityResolver JavaDoc getEntityResolver() {
95         return this.reader.getEntityResolver();
96     }
97
98     public void setDTDHandler(DTDHandler JavaDoc handler) {
99         this.reader.setDTDHandler(handler);
100     }
101
102     public DTDHandler JavaDoc getDTDHandler() {
103         return this.reader.getDTDHandler();
104     }
105
106     public void setContentHandler(ContentHandler JavaDoc handler) {
107         this.reader.setContentHandler(handler);
108     }
109
110     public ContentHandler JavaDoc getContentHandler() {
111         return this.reader.getContentHandler();
112     }
113
114     public void setErrorHandler(ErrorHandler JavaDoc handler) {
115         this.reader.setErrorHandler(handler);
116     }
117
118     public ErrorHandler JavaDoc getErrorHandler() {
119         return this.reader.getErrorHandler();
120     }
121
122     public void parse(InputSource JavaDoc source)
123     throws IOException JavaDoc, SAXException JavaDoc {
124         this.context.pushInputSource(source);
125         this.reader.parse(source);
126         this.context.popInputSource();
127     }
128
129     public void parse(String JavaDoc source)
130     throws IOException JavaDoc, SAXException JavaDoc {
131         this.parse(this.getEntityResolver().resolveEntity(null, source));
132     }
133 }
Popular Tags