1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.jaxp.validation;
  22 
  23 import java.io.IOException;
  24 
  25 import javax.xml.XMLConstants;
  26 import javax.xml.transform.Result;
  27 import javax.xml.transform.Source;
  28 import javax.xml.transform.dom.DOMSource;
  29 import javax.xml.transform.sax.SAXSource;
  30 import javax.xml.transform.stax.StAXResult;
  31 import javax.xml.transform.stax.StAXSource;
  32 import javax.xml.transform.stream.StreamSource;
  33 import javax.xml.validation.Validator;
  34 
  35 import com.sun.org.apache.xerces.internal.impl.Constants;
  36 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
  37 import com.sun.org.apache.xerces.internal.util.Status;
  38 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  39 import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
  40 import com.sun.org.apache.xerces.internal.xs.ElementPSVI;
  41 import com.sun.org.apache.xerces.internal.xs.PSVIProvider;
  42 import org.w3c.dom.ls.LSResourceResolver;
  43 import org.xml.sax.ErrorHandler;
  44 import org.xml.sax.SAXException;
  45 import org.xml.sax.SAXNotRecognizedException;
  46 import org.xml.sax.SAXNotSupportedException;
  47 
  48 /**
  49  * <p>Implementation of Validator for W3C XML Schemas.</p>
  50  *
  51  * @author <a href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
  52  * @author Michael Glavassevich, IBM
  53  * @author <a href="mailto:Sunitha.Reddy@Sun.com">Sunitha Reddy</a>
  54  */
  55 final class ValidatorImpl extends Validator implements PSVIProvider {
  56 
  57     //
  58     // Data
  59     //
  60 
  61     /** Component manager. **/
  62     private XMLSchemaValidatorComponentManager fComponentManager;
  63 
  64     /** SAX validator helper. **/
  65     private ValidatorHandlerImpl fSAXValidatorHelper;
  66 
  67     /** DOM validator helper. **/
  68     private DOMValidatorHelper fDOMValidatorHelper;
  69 
  70     /** Stream validator helper. **/
  71     private StreamValidatorHelper fStreamValidatorHelper;
  72 
  73     /** StAX validator helper. **/
  74     private StAXValidatorHelper fStaxValidatorHelper;
  75 
  76     /** Flag for tracking whether features/properties changed since last reset. */
  77     private boolean fConfigurationChanged = false;
  78 
  79     /** Flag for tracking whether the error handler changed since last reset. */
  80     private boolean fErrorHandlerChanged = false;
  81 
  82     /** Flag for tracking whether the resource resolver changed since last reset. */
  83     private boolean fResourceResolverChanged = false;
  84 
  85     /** Support current-element-node property */
  86     private static final String CURRENT_ELEMENT_NODE = Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY;
  87 
  88     public ValidatorImpl(XSGrammarPoolContainer grammarContainer) {
  89         fComponentManager = new XMLSchemaValidatorComponentManager(grammarContainer);
  90         setErrorHandler(null);
  91         setResourceResolver(null);
  92     }
  93 
  94     public void validate(Source source, Result result)
  95         throws SAXException, IOException {
  96         if (source instanceof SAXSource) {
  97             // Hand off to SAX validator helper.
  98             if (fSAXValidatorHelper == null) {
  99                 fSAXValidatorHelper = new ValidatorHandlerImpl(fComponentManager);
 100             }
 101             fSAXValidatorHelper.validate(source, result);
 102         }
 103         else if (source instanceof DOMSource) {
 104             // Hand off to DOM validator helper.
 105             if (fDOMValidatorHelper == null) {
 106                 fDOMValidatorHelper = new DOMValidatorHelper(fComponentManager);
 107             }
 108             fDOMValidatorHelper.validate(source, result);
 109         }
 110         else if (source instanceof StreamSource) {
 111             // Hand off to stream validator helper.
 112             if (fStreamValidatorHelper == null) {
 113                 fStreamValidatorHelper = new StreamValidatorHelper(fComponentManager);
 114             }
 115             fStreamValidatorHelper.validate(source, result);
 116         }
 117         else if (source instanceof StAXSource) {
 118             // Hand off to stax validator helper.
 119             if (fStaxValidatorHelper == null) {
 120                 fStaxValidatorHelper = new StAXValidatorHelper(fComponentManager);
 121             }
 122             fStaxValidatorHelper.validate(source, result);
 123         }
 124         // Source parameter cannot be null.
 125         else if (source == null) {
 126             throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(fComponentManager.getLocale(),
 127                     "SourceParameterNull", null));
 128         }
 129         // Source parameter must be a SAXSource, DOMSource or StreamSource
 130         else {
 131             throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(fComponentManager.getLocale(),
 132                     "SourceNotAccepted", new Object [] {source.getClass().getName()}));
 133         }
 134     }
 135 
 136     public void setErrorHandler(ErrorHandler errorHandler) {
 137         fErrorHandlerChanged = (errorHandler != null);
 138         fComponentManager.setErrorHandler(errorHandler);
 139     }
 140 
 141     public ErrorHandler getErrorHandler() {
 142         return fComponentManager.getErrorHandler();
 143     }
 144 
 145     public void setResourceResolver(LSResourceResolver resourceResolver) {
 146         fResourceResolverChanged = (resourceResolver != null);
 147         fComponentManager.setResourceResolver(resourceResolver);
 148     }
 149 
 150     public LSResourceResolver getResourceResolver() {
 151         return fComponentManager.getResourceResolver();
 152     }
 153 
 154     public boolean getFeature(String name)
 155         throws SAXNotRecognizedException, SAXNotSupportedException {
 156         if (name == null) {
 157             throw new NullPointerException();
 158         }
 159         try {
 160             return fComponentManager.getFeature(name);
 161         }
 162         catch (XMLConfigurationException e) {
 163             final String identifier = e.getIdentifier();
 164             final String key = e.getType() == Status.NOT_RECOGNIZED ?
 165                     "feature-not-recognized" : "feature-not-supported";
 166             throw new SAXNotRecognizedException(
 167                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 168                     key, new Object [] {identifier}));
 169         }
 170     }
 171 
 172     public void setFeature(String name, boolean value)
 173         throws SAXNotRecognizedException, SAXNotSupportedException {
 174         if (name == null) {
 175             throw new NullPointerException();
 176         }
 177         try {
 178             fComponentManager.setFeature(name, value);
 179         }
 180         catch (XMLConfigurationException e) {
 181             final String identifier = e.getIdentifier();
 182             final String key;
 183             if (e.getType() == Status.NOT_ALLOWED) {
 184                 //for now, the identifier can only be (XMLConstants.FEATURE_SECURE_PROCESSING)
 185                 throw new SAXNotSupportedException(
 186                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 187                     "jaxp-secureprocessing-feature", null));
 188             } else if (e.getType() == Status.NOT_RECOGNIZED) {
 189                 key = "feature-not-recognized";
 190             } else {
 191                 key = "feature-not-supported";
 192             }
 193             throw new SAXNotRecognizedException(
 194                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 195                     key, new Object [] {identifier}));
 196         }
 197         fConfigurationChanged = true;
 198     }
 199 
 200     public Object getProperty(String name)
 201         throws SAXNotRecognizedException, SAXNotSupportedException {
 202         if (name == null) {
 203             throw new NullPointerException();
 204         }
 205         //Support current-element-node; return current node if DOMSource is used.
 206         if (CURRENT_ELEMENT_NODE.equals(name)) {
 207             return (fDOMValidatorHelper != null) ? fDOMValidatorHelper.getCurrentElement() : null;
 208         }
 209         try {
 210             return fComponentManager.getProperty(name);
 211         }
 212         catch (XMLConfigurationException e) {
 213             final String identifier = e.getIdentifier();
 214             final String key = e.getType() == Status.NOT_RECOGNIZED ?
 215                     "property-not-recognized" : "property-not-supported";
 216             throw new SAXNotRecognizedException(
 217                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 218                     key, new Object [] {identifier}));
 219         }
 220     }
 221 
 222     public void setProperty(String name, Object object)
 223         throws SAXNotRecognizedException, SAXNotSupportedException {
 224         if (name == null) {
 225             throw new NullPointerException();
 226         }
 227         try {
 228             fComponentManager.setProperty(name, object);
 229         }
 230         catch (XMLConfigurationException e) {
 231             final String identifier = e.getIdentifier();
 232             final String key = e.getType() == Status.NOT_RECOGNIZED ?
 233                     "property-not-recognized" : "property-not-supported";
 234             throw new SAXNotRecognizedException(
 235                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 236                     key, new Object [] {identifier}));
 237         }
 238         fConfigurationChanged = true;
 239     }
 240 
 241     public void reset() {
 242         // avoid resetting features and properties if the state the validator
 243         // is currently in, is the same as it will be after reset.
 244         if (fConfigurationChanged) {
 245             fComponentManager.restoreInitialState();
 246             setErrorHandler(null);
 247             setResourceResolver(null);
 248             fConfigurationChanged = false;
 249             fErrorHandlerChanged = false;
 250             fResourceResolverChanged = false;
 251         }
 252         else {
 253             if (fErrorHandlerChanged) {
 254                 setErrorHandler(null);
 255                 fErrorHandlerChanged = false;
 256             }
 257             if (fResourceResolverChanged) {
 258                 setResourceResolver(null);
 259                 fResourceResolverChanged = false;
 260             }
 261         }
 262     }
 263 
 264     /*
 265      * PSVIProvider methods
 266      */
 267 
 268     public ElementPSVI getElementPSVI() {
 269         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getElementPSVI() : null;
 270     }
 271 
 272     public AttributePSVI getAttributePSVI(int index) {
 273         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVI(index) : null;
 274     }
 275 
 276     public AttributePSVI getAttributePSVIByName(String uri, String localname) {
 277         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVIByName(uri, localname) : null;
 278     }
 279 
 280 } // ValidatorImpl