KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.cocoon.components.validation.impl.ValidationResolver;
22 import org.apache.excalibur.source.SourceResolver;
23 import org.xml.sax.EntityResolver JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.XMLReader JavaDoc;
27
28 import com.thaiopensource.xml.sax.XMLReaderCreator;
29
30 /**
31  * <p>A simple resolver used when parsing RELAX NG schemas through the use of
32  * <a HREF="http://www.thaiopensource.com/relaxng/jing.html">JING</a>.</p>
33  *
34  * <p>This is not thread safe and not recyclable. Once used, it <b>must</b> be
35  * garbage collected.</p>
36  *
37  * @author <a HREF="mailto:pier@betaversion.org">Pier Fumagalli</a>
38  */

39 public class JingResolver extends ValidationResolver implements XMLReaderCreator {
40
41     /** <p>The current {@link Stack} of {@link InputSource}s being parsed. </p> */
42     private final Stack JavaDoc parsedSourceStack = new Stack JavaDoc();
43
44     /**
45      * <p>Create a new {@link JingResolver} instance.</p>
46      */

47     public JingResolver(SourceResolver sourceResolver,
48                        EntityResolver JavaDoc entityResolver) {
49         super(sourceResolver, entityResolver);
50         this.parsedSourceStack.push(null);
51     }
52
53     /**
54      * <p>Push a new {@link InputSource} in the stack used for relative URIs
55      * resolution.</p>
56      */

57     public void pushInputSource(InputSource JavaDoc inputSource) {
58         this.parsedSourceStack.push(inputSource);
59     }
60
61     /**
62      * <p>Pop the last {@link InputSource} from the stack used for relative URIs
63      * resolution.</p>
64      */

65     public InputSource JavaDoc popInputSource() {
66         if (this.parsedSourceStack.empty()) return null;
67         return (InputSource JavaDoc) this.parsedSourceStack.pop();
68     }
69
70     /**
71      * <p>Return the {@link PropertyMap} associated with this instance and usable
72      * by <a HREF="http://www.thaiopensource.com/relaxng/jing.html">JING</a>.</p>
73      */

74     //public PropertyMap getProperties() {
75
// return this.validatorProperties;
76
//}
77

78     /* =========================================================================== */
79     /* SAX2 ENTITY RESOLVER INTERFACE IMPLEMENTATION */
80     /* =========================================================================== */
81
82     /**
83      * <p>Resolve an {@link InputSource} from a public ID and/or a system ID.</p>
84      *
85      * <p>This method can be called only while a schema is being parsed and will
86      * resolve URIs against a dynamic {@link Stack} of {@link InputSource}s.</p>
87      *
88      * <p>Since <a HREF="http://www.thaiopensource.com/relaxng/jing.html">JING</a>
89      * doesn't offer a complete URI resolution contract, a {@link Stack} is kept
90      * for all the sources parsed while reading a schema. Keeping in mind that the
91      * last {@link InputSource} pushed in the {@link Stack} can be considered to be
92      * the "base URI" for the current resolution, full relative resolution of system
93      * IDs can be achieved this way.<p>
94      *
95      * <p>Note that this method of resolving URIs by keeping a {@link Stack} of
96      * processed URIs is a <i>sort of a hack</i>, but it mimics the internal state
97      * of <a HREF="http://www.thaiopensource.com/relaxng/jing.html">JING</a> itself:
98      * if URI resolution fails, the {@link Stack} analysis is the first part to
99      * look at.</p>
100      *
101      * <p>Resolution will use the {@link EntityResolver} specified at construction
102      * to resolve public IDs, and then the {@link SourceResolver} again specified at
103      * construction to resolve the system IDs and to access the underlying byte
104      * streams of the entities to be parsed.</p>
105      *
106      * @param publicId the public ID of the entity to resolve.
107      * @param systemId the system ID of the entity to resolve.
108      * @return a <b>non-null</b> {@link InputSource} instance.
109      * @throws IOException if an I/O error occurred resolving the entity.
110      * @throws SAXException if an XML error occurred resolving the entity.
111      */

112     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
113     throws SAXException JavaDoc, IOException JavaDoc {
114         InputSource JavaDoc source = (InputSource JavaDoc) this.parsedSourceStack.peek();
115         if (source == null) {
116             return super.resolveEntity(publicId, systemId);
117         } else {
118             return super.resolveEntity(source.getSystemId(), publicId, systemId);
119         }
120     }
121
122
123     /* =========================================================================== */
124     /* CALL JING TO ACCESS A CACHED OR FRESHLY PARSED SCHEMA INSTANCE */
125     /* =========================================================================== */
126
127     /**
128      * <p>Create an {@link XMLReader} instance that can be used by
129      * <a HREF="http://www.thaiopensource.com/relaxng/jing.html">JING</a> for
130      * parsing schemas.</p>
131      *
132      * <p>The returned {@link XMLReader} will keep track of populating/clearing the
133      * {@link Stack} of {@link InputSource}s kept for URI resolution as explained
134      * in the description of the {@link #resolveEntity(String, String)} method.</p>
135      *
136      * @link JingReader
137      * @return a <b>non-null</b> {@link XMLReader} instance.
138      * @throws SAXException if an error occurrent creating the {@link XMLReader}.
139      */

140     public XMLReader JavaDoc createXMLReader()
141     throws SAXException JavaDoc {
142         return new JingReader(this);
143     }
144 }
145
Popular Tags