001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.jaxp.validation;
019:
020: import java.io.IOException;
021: import java.util.Locale;
022:
023: import javax.xml.transform.Result;
024: import javax.xml.transform.Source;
025: import javax.xml.transform.dom.DOMSource;
026: import javax.xml.transform.sax.SAXSource;
027: import javax.xml.transform.stream.StreamSource;
028: import javax.xml.validation.Validator;
029:
030: import org.apache.xerces.impl.Constants;
031: import org.apache.xerces.util.SAXMessageFormatter;
032: import org.apache.xerces.xni.parser.XMLConfigurationException;
033: import org.apache.xerces.xs.AttributePSVI;
034: import org.apache.xerces.xs.ElementPSVI;
035: import org.apache.xerces.xs.PSVIProvider;
036: import org.w3c.dom.ls.LSResourceResolver;
037: import org.xml.sax.ErrorHandler;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.SAXNotRecognizedException;
040: import org.xml.sax.SAXNotSupportedException;
041:
042: /**
043: * <p>Implementation of Validator for W3C XML Schemas.</p>
044: *
045: * @author <a href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
046: * @author Michael Glavassevich, IBM
047: *
048: * @version $Id: ValidatorImpl.java 447235 2006-09-18 05:01:44Z mrglavas $
049: */
050: final class ValidatorImpl extends Validator implements PSVIProvider {
051:
052: // property identifiers
053:
054: /** Property identifier: Current element node. */
055: private static final String CURRENT_ELEMENT_NODE = Constants.XERCES_PROPERTY_PREFIX
056: + Constants.CURRENT_ELEMENT_NODE_PROPERTY;
057:
058: //
059: // Data
060: //
061:
062: /** Component manager. **/
063: private XMLSchemaValidatorComponentManager fComponentManager;
064:
065: /** SAX validator helper. **/
066: private ValidatorHandlerImpl fSAXValidatorHelper;
067:
068: /** DOM validator helper. **/
069: private DOMValidatorHelper fDOMValidatorHelper;
070:
071: /** Stream validator helper. **/
072: private StreamValidatorHelper fStreamValidatorHelper;
073:
074: /** Flag for tracking whether features/properties changed since last reset. */
075: private boolean fConfigurationChanged = false;
076:
077: /** Flag for tracking whether the error handler changed since last reset. */
078: private boolean fErrorHandlerChanged = false;
079:
080: /** Flag for tracking whether the resource resolver changed since last reset. */
081: private boolean fResourceResolverChanged = false;
082:
083: public ValidatorImpl(XSGrammarPoolContainer grammarContainer) {
084: fComponentManager = new XMLSchemaValidatorComponentManager(
085: grammarContainer);
086: setErrorHandler(null);
087: setResourceResolver(null);
088: }
089:
090: public void validate(Source source, Result result)
091: throws SAXException, IOException {
092: if (source instanceof SAXSource) {
093: // Hand off to SAX validator helper.
094: if (fSAXValidatorHelper == null) {
095: fSAXValidatorHelper = new ValidatorHandlerImpl(
096: fComponentManager);
097: }
098: fSAXValidatorHelper.validate(source, result);
099: } else if (source instanceof DOMSource) {
100: // Hand off to DOM validator helper.
101: if (fDOMValidatorHelper == null) {
102: fDOMValidatorHelper = new DOMValidatorHelper(
103: fComponentManager);
104: }
105: fDOMValidatorHelper.validate(source, result);
106: } else if (source instanceof StreamSource) {
107: // Hand off to stream validator helper.
108: if (fStreamValidatorHelper == null) {
109: fStreamValidatorHelper = new StreamValidatorHelper(
110: fComponentManager);
111: }
112: fStreamValidatorHelper.validate(source, result);
113: }
114: // Source parameter cannot be null.
115: else if (source == null) {
116: throw new NullPointerException(
117: JAXPValidationMessageFormatter.formatMessage(Locale
118: .getDefault(), "SourceParameterNull", null));
119: }
120: // Source parameter must be a SAXSource, DOMSource or StreamSource
121: else {
122: throw new IllegalArgumentException(
123: JAXPValidationMessageFormatter
124: .formatMessage(Locale.getDefault(),
125: "SourceNotAccepted",
126: new Object[] { source.getClass()
127: .getName() }));
128: }
129: }
130:
131: public void setErrorHandler(ErrorHandler errorHandler) {
132: fErrorHandlerChanged = (errorHandler != null);
133: fComponentManager.setErrorHandler(errorHandler);
134: }
135:
136: public ErrorHandler getErrorHandler() {
137: return fComponentManager.getErrorHandler();
138: }
139:
140: public void setResourceResolver(LSResourceResolver resourceResolver) {
141: fResourceResolverChanged = (resourceResolver != null);
142: fComponentManager.setResourceResolver(resourceResolver);
143: }
144:
145: public LSResourceResolver getResourceResolver() {
146: return fComponentManager.getResourceResolver();
147: }
148:
149: public boolean getFeature(String name)
150: throws SAXNotRecognizedException, SAXNotSupportedException {
151: if (name == null) {
152: throw new NullPointerException();
153: }
154: try {
155: return fComponentManager.getFeature(name);
156: } catch (XMLConfigurationException e) {
157: final String identifier = e.getIdentifier();
158: final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? "feature-not-recognized"
159: : "feature-not-supported";
160: throw new SAXNotRecognizedException(SAXMessageFormatter
161: .formatMessage(Locale.getDefault(), key,
162: new Object[] { identifier }));
163: }
164: }
165:
166: public void setFeature(String name, boolean value)
167: throws SAXNotRecognizedException, SAXNotSupportedException {
168: if (name == null) {
169: throw new NullPointerException();
170: }
171: try {
172: fComponentManager.setFeature(name, value);
173: } catch (XMLConfigurationException e) {
174: final String identifier = e.getIdentifier();
175: final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? "feature-not-recognized"
176: : "feature-not-supported";
177: throw new SAXNotRecognizedException(SAXMessageFormatter
178: .formatMessage(Locale.getDefault(), key,
179: new Object[] { identifier }));
180: }
181: fConfigurationChanged = true;
182: }
183:
184: public Object getProperty(String name)
185: throws SAXNotRecognizedException, SAXNotSupportedException {
186: if (name == null) {
187: throw new NullPointerException();
188: }
189: if (CURRENT_ELEMENT_NODE.equals(name)) {
190: return (fDOMValidatorHelper != null) ? fDOMValidatorHelper
191: .getCurrentElement() : null;
192: }
193: try {
194: return fComponentManager.getProperty(name);
195: } catch (XMLConfigurationException e) {
196: final String identifier = e.getIdentifier();
197: final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? "property-not-recognized"
198: : "property-not-supported";
199: throw new SAXNotRecognizedException(SAXMessageFormatter
200: .formatMessage(Locale.getDefault(), key,
201: new Object[] { identifier }));
202: }
203: }
204:
205: public void setProperty(String name, Object object)
206: throws SAXNotRecognizedException, SAXNotSupportedException {
207: if (name == null) {
208: throw new NullPointerException();
209: }
210: if (CURRENT_ELEMENT_NODE.equals(name)) {
211: throw new SAXNotSupportedException(
212: SAXMessageFormatter.formatMessage(Locale
213: .getDefault(), "property-read-only",
214: new Object[] { name }));
215: }
216: try {
217: fComponentManager.setProperty(name, object);
218: } catch (XMLConfigurationException e) {
219: final String identifier = e.getIdentifier();
220: final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? "property-not-recognized"
221: : "property-not-supported";
222: throw new SAXNotRecognizedException(SAXMessageFormatter
223: .formatMessage(Locale.getDefault(), key,
224: new Object[] { identifier }));
225: }
226: fConfigurationChanged = true;
227: }
228:
229: public void reset() {
230: // avoid resetting features and properties if the state the validator
231: // is currently in, is the same as it will be after reset.
232: if (fConfigurationChanged) {
233: fComponentManager.restoreInitialState();
234: setErrorHandler(null);
235: setResourceResolver(null);
236: fConfigurationChanged = false;
237: fErrorHandlerChanged = false;
238: fResourceResolverChanged = false;
239: } else {
240: if (fErrorHandlerChanged) {
241: setErrorHandler(null);
242: fErrorHandlerChanged = false;
243: }
244: if (fResourceResolverChanged) {
245: setResourceResolver(null);
246: fResourceResolverChanged = false;
247: }
248: }
249: }
250:
251: /*
252: * PSVIProvider methods
253: */
254:
255: public ElementPSVI getElementPSVI() {
256: return (fSAXValidatorHelper != null) ? fSAXValidatorHelper
257: .getElementPSVI() : null;
258: }
259:
260: public AttributePSVI getAttributePSVI(int index) {
261: return (fSAXValidatorHelper != null) ? fSAXValidatorHelper
262: .getAttributePSVI(index) : null;
263: }
264:
265: public AttributePSVI getAttributePSVIByName(String uri,
266: String localname) {
267: return (fSAXValidatorHelper != null) ? fSAXValidatorHelper
268: .getAttributePSVIByName(uri, localname) : null;
269: }
270:
271: } // ValidatorImpl
|