KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > jaxp > validation > ValidatorImpl


1 /*
2  * Copyright 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
17 package org.apache.xerces.jaxp.validation;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.xml.transform.Result JavaDoc;
23 import javax.xml.transform.Source JavaDoc;
24 import javax.xml.transform.dom.DOMSource JavaDoc;
25 import javax.xml.transform.sax.SAXSource JavaDoc;
26 import javax.xml.transform.stream.StreamSource JavaDoc;
27 import javax.xml.validation.Validator JavaDoc;
28
29 import org.apache.xerces.util.SAXMessageFormatter;
30 import org.apache.xerces.xni.parser.XMLConfigurationException;
31 import org.apache.xerces.xs.AttributePSVI;
32 import org.apache.xerces.xs.ElementPSVI;
33 import org.apache.xerces.xs.PSVIProvider;
34 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.SAXNotRecognizedException JavaDoc;
38 import org.xml.sax.SAXNotSupportedException JavaDoc;
39
40 /**
41  * <p>Implementation of Validator for W3C XML Schemas.</p>
42  *
43  * @author <a HREF="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
44  * @author Michael Glavassevich, IBM
45  *
46  * @version $Id: ValidatorImpl.java,v 1.2 2005/06/13 21:28:55 mrglavas Exp $
47  */

48 final class ValidatorImpl extends Validator JavaDoc implements PSVIProvider {
49     
50     //
51
// Data
52
//
53

54     /** Component manager. **/
55     private XMLSchemaValidatorComponentManager fComponentManager;
56     
57     /** SAX validator helper. **/
58     private ValidatorHandlerImpl fSAXValidatorHelper;
59     
60     /** DOM validator helper. **/
61     private DOMValidatorHelper fDOMValidatorHelper;
62     
63     /** Stream validator helper. **/
64     private StreamValidatorHelper fStreamValidatorHelper;
65     
66     /** Flag for tracking whether features/properties changed since last reset. */
67     private boolean fConfigurationChanged = false;
68     
69     /** Flag for tracking whether the error handler changed since last reset. */
70     private boolean fErrorHandlerChanged = false;
71     
72     /** Flag for tracking whether the resource resolver changed since last reset. */
73     private boolean fResourceResolverChanged = false;
74     
75     public ValidatorImpl(XSGrammarPoolContainer grammarContainer) {
76         fComponentManager = new XMLSchemaValidatorComponentManager(grammarContainer);
77         setErrorHandler(null);
78         setResourceResolver(null);
79     }
80
81     public void validate(Source JavaDoc source, Result JavaDoc result)
82         throws SAXException JavaDoc, IOException JavaDoc {
83         if (source instanceof SAXSource JavaDoc) {
84             // Hand off to SAX validator helper.
85
if (fSAXValidatorHelper == null) {
86                 fSAXValidatorHelper = new ValidatorHandlerImpl(fComponentManager);
87             }
88             fSAXValidatorHelper.validate(source, result);
89         }
90         else if (source instanceof DOMSource JavaDoc) {
91             // Hand off to DOM validator helper.
92
if (fDOMValidatorHelper == null) {
93                 fDOMValidatorHelper = new DOMValidatorHelper(fComponentManager);
94             }
95             fDOMValidatorHelper.validate(source, result);
96         }
97         else if (source instanceof StreamSource JavaDoc) {
98             // Hand off to stream validator helper.
99
if (fStreamValidatorHelper == null) {
100                 fStreamValidatorHelper = new StreamValidatorHelper(fComponentManager);
101             }
102             fStreamValidatorHelper.validate(source, result);
103         }
104         // Source parameter cannot be null.
105
else if (source == null) {
106             throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
107                     "SourceParameterNull", null));
108         }
109         // Source parameter must be a SAXSource, DOMSource or StreamSource
110
else {
111             throw new IllegalArgumentException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
112                     "SourceNotAccepted", new Object JavaDoc [] {source.getClass().getName()}));
113         }
114     }
115
116     public void setErrorHandler(ErrorHandler JavaDoc errorHandler) {
117         fErrorHandlerChanged = (errorHandler != null);
118         fComponentManager.setErrorHandler(errorHandler);
119     }
120
121     public ErrorHandler JavaDoc getErrorHandler() {
122         return fComponentManager.getErrorHandler();
123     }
124
125     public void setResourceResolver(LSResourceResolver JavaDoc resourceResolver) {
126         fResourceResolverChanged = (resourceResolver != null);
127         fComponentManager.setResourceResolver(resourceResolver);
128     }
129
130     public LSResourceResolver JavaDoc getResourceResolver() {
131         return fComponentManager.getResourceResolver();
132     }
133     
134     public boolean getFeature(String JavaDoc name)
135         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
136         if (name == null) {
137             throw new NullPointerException JavaDoc();
138         }
139         try {
140             return fComponentManager.getFeature(name);
141         }
142         catch (XMLConfigurationException e) {
143             final String JavaDoc identifier = e.getIdentifier();
144             final String JavaDoc key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
145                     "feature-not-recognized" : "feature-not-supported";
146             throw new SAXNotRecognizedException JavaDoc(
147                     SAXMessageFormatter.formatMessage(Locale.getDefault(),
148                     key, new Object JavaDoc [] {identifier}));
149         }
150     }
151     
152     public void setFeature(String JavaDoc name, boolean value)
153         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
154         if (name == null) {
155             throw new NullPointerException JavaDoc();
156         }
157         try {
158             fComponentManager.setFeature(name, value);
159         }
160         catch (XMLConfigurationException e) {
161             final String JavaDoc identifier = e.getIdentifier();
162             final String JavaDoc key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
163                     "feature-not-recognized" : "feature-not-supported";
164             throw new SAXNotRecognizedException JavaDoc(
165                     SAXMessageFormatter.formatMessage(Locale.getDefault(),
166                     key, new Object JavaDoc [] {identifier}));
167         }
168         fConfigurationChanged = true;
169     }
170     
171     public Object JavaDoc getProperty(String JavaDoc name)
172         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
173         if (name == null) {
174             throw new NullPointerException JavaDoc();
175         }
176         try {
177             return fComponentManager.getProperty(name);
178         }
179         catch (XMLConfigurationException e) {
180             final String JavaDoc identifier = e.getIdentifier();
181             final String JavaDoc key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
182                     "property-not-recognized" : "property-not-supported";
183             throw new SAXNotRecognizedException JavaDoc(
184                     SAXMessageFormatter.formatMessage(Locale.getDefault(),
185                     key, new Object JavaDoc [] {identifier}));
186         }
187     }
188     
189     public void setProperty(String JavaDoc name, Object JavaDoc object)
190         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
191         if (name == null) {
192             throw new NullPointerException JavaDoc();
193         }
194         try {
195             fComponentManager.setProperty(name, object);
196         }
197         catch (XMLConfigurationException e) {
198             final String JavaDoc identifier = e.getIdentifier();
199             final String JavaDoc key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
200                     "property-not-recognized" : "property-not-supported";
201             throw new SAXNotRecognizedException JavaDoc(
202                     SAXMessageFormatter.formatMessage(Locale.getDefault(),
203                     key, new Object JavaDoc [] {identifier}));
204         }
205         fConfigurationChanged = true;
206     }
207     
208     public void reset() {
209         // avoid resetting features and properties if the state the validator
210
// is currently in, is the same as it will be after reset.
211
if (fConfigurationChanged) {
212             fComponentManager.restoreInitialState();
213             setErrorHandler(null);
214             setResourceResolver(null);
215             fConfigurationChanged = false;
216             fErrorHandlerChanged = false;
217             fResourceResolverChanged = false;
218         }
219         else {
220             if (fErrorHandlerChanged) {
221                 setErrorHandler(null);
222                 fErrorHandlerChanged = false;
223             }
224             if (fResourceResolverChanged) {
225                 setResourceResolver(null);
226                 fResourceResolverChanged = false;
227             }
228         }
229     }
230     
231     /*
232      * PSVIProvider methods
233      */

234     
235     public ElementPSVI getElementPSVI() {
236         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getElementPSVI() : null;
237     }
238     
239     public AttributePSVI getAttributePSVI(int index) {
240         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVI(index) : null;
241     }
242     
243     public AttributePSVI getAttributePSVIByName(String JavaDoc uri, String JavaDoc localname) {
244         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVIByName(uri, localname) : null;
245     }
246     
247 } // ValidatorImpl
248
Popular Tags