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  * @version $Id: ValidatorImpl.java,v 1.10 2010-11-01 04:40:08 joehw Exp $
  55  */
  56 final class ValidatorImpl extends Validator implements PSVIProvider {
  57 
  58     //
  59     // Data
  60     //
  61 
  62     /** Component manager. **/
  63     private XMLSchemaValidatorComponentManager fComponentManager;
  64 
  65     /** SAX validator helper. **/
  66     private ValidatorHandlerImpl fSAXValidatorHelper;
  67 
  68     /** DOM validator helper. **/
  69     private DOMValidatorHelper fDOMValidatorHelper;
  70 
  71     /** Stream validator helper. **/
  72     private StreamValidatorHelper fStreamValidatorHelper;
  73 
  74     /** StAX validator helper. **/
  75     private StAXValidatorHelper fStaxValidatorHelper;
  76 
  77     /** Flag for tracking whether features/properties changed since last reset. */
  78     private boolean fConfigurationChanged = false;
  79 
  80     /** Flag for tracking whether the error handler changed since last reset. */
  81     private boolean fErrorHandlerChanged = false;
  82 
  83     /** Flag for tracking whether the resource resolver changed since last reset. */
  84     private boolean fResourceResolverChanged = false;
  85 
  86     /** Support current-element-node property */
  87     private static final String CURRENT_ELEMENT_NODE = Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY;
  88 
  89     public ValidatorImpl(XSGrammarPoolContainer grammarContainer) {
  90         fComponentManager = new XMLSchemaValidatorComponentManager(grammarContainer);
  91         setErrorHandler(null);
  92         setResourceResolver(null);
  93     }
  94 
  95     public void validate(Source source, Result result)
  96         throws SAXException, IOException {
  97         if (source instanceof SAXSource) {
  98             // Hand off to SAX validator helper.
  99             if (fSAXValidatorHelper == null) {
 100                 fSAXValidatorHelper = new ValidatorHandlerImpl(fComponentManager);
 101             }
 102             fSAXValidatorHelper.validate(source, result);
 103         }
 104         else if (source instanceof DOMSource) {
 105             // Hand off to DOM validator helper.
 106             if (fDOMValidatorHelper == null) {
 107                 fDOMValidatorHelper = new DOMValidatorHelper(fComponentManager);
 108             }
 109             fDOMValidatorHelper.validate(source, result);
 110         }
 111         else if (source instanceof StreamSource) {
 112             // Hand off to stream validator helper.
 113             if (fStreamValidatorHelper == null) {
 114                 fStreamValidatorHelper = new StreamValidatorHelper(fComponentManager);
 115             }
 116             fStreamValidatorHelper.validate(source, result);
 117         }
 118         else if (source instanceof StAXSource) {
 119             // Hand off to stax validator helper.
 120             if (fStaxValidatorHelper == null) {
 121                 fStaxValidatorHelper = new StAXValidatorHelper(fComponentManager);
 122             }
 123             fStaxValidatorHelper.validate(source, result);
 124         }
 125         // Source parameter cannot be null.
 126         else if (source == null) {
 127             throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(fComponentManager.getLocale(),
 128                     "SourceParameterNull", null));
 129         }
 130         // Source parameter must be a SAXSource, DOMSource or StreamSource
 131         else {
 132             throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(fComponentManager.getLocale(),
 133                     "SourceNotAccepted", new Object [] {source.getClass().getName()}));
 134         }
 135     }
 136 
 137     public void setErrorHandler(ErrorHandler errorHandler) {
 138         fErrorHandlerChanged = (errorHandler != null);
 139         fComponentManager.setErrorHandler(errorHandler);
 140     }
 141 
 142     public ErrorHandler getErrorHandler() {
 143         return fComponentManager.getErrorHandler();
 144     }
 145 
 146     public void setResourceResolver(LSResourceResolver resourceResolver) {
 147         fResourceResolverChanged = (resourceResolver != null);
 148         fComponentManager.setResourceResolver(resourceResolver);
 149     }
 150 
 151     public LSResourceResolver getResourceResolver() {
 152         return fComponentManager.getResourceResolver();
 153     }
 154 
 155     public boolean getFeature(String name)
 156         throws SAXNotRecognizedException, SAXNotSupportedException {
 157         if (name == null) {
 158             throw new NullPointerException();
 159         }
 160         try {
 161             return fComponentManager.getFeature(name);
 162         }
 163         catch (XMLConfigurationException e) {
 164             final String identifier = e.getIdentifier();
 165             final String key = e.getType() == Status.NOT_RECOGNIZED ?
 166                     "feature-not-recognized" : "feature-not-supported";
 167             throw new SAXNotRecognizedException(
 168                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 169                     key, new Object [] {identifier}));
 170         }
 171     }
 172 
 173     public void setFeature(String name, boolean value)
 174         throws SAXNotRecognizedException, SAXNotSupportedException {
 175         if (name == null) {
 176             throw new NullPointerException();
 177         }
 178         try {
 179             fComponentManager.setFeature(name, value);
 180         }
 181         catch (XMLConfigurationException e) {
 182             final String identifier = e.getIdentifier();
 183             final String key;
 184             if (e.getType() == Status.NOT_ALLOWED) {
 185                 //for now, the identifier can only be (XMLConstants.FEATURE_SECURE_PROCESSING)
 186                 throw new SAXNotSupportedException(
 187                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 188                     "jaxp-secureprocessing-feature", null));
 189             } else if (e.getType() == Status.NOT_RECOGNIZED) {
 190                 key = "feature-not-recognized";
 191             } else {
 192                 key = "feature-not-supported";
 193             }
 194             throw new SAXNotRecognizedException(
 195                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 196                     key, new Object [] {identifier}));
 197         }
 198         fConfigurationChanged = true;
 199     }
 200 
 201     public Object getProperty(String name)
 202         throws SAXNotRecognizedException, SAXNotSupportedException {
 203         if (name == null) {
 204             throw new NullPointerException();
 205         }
 206         //Support current-element-node; return current node if DOMSource is used.
 207         if (CURRENT_ELEMENT_NODE.equals(name)) {
 208             return (fDOMValidatorHelper != null) ? fDOMValidatorHelper.getCurrentElement() : null;
 209         }
 210         try {
 211             return fComponentManager.getProperty(name);
 212         }
 213         catch (XMLConfigurationException e) {
 214             final String identifier = e.getIdentifier();
 215             final String key = e.getType() == Status.NOT_RECOGNIZED ?
 216                     "property-not-recognized" : "property-not-supported";
 217             throw new SAXNotRecognizedException(
 218                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 219                     key, new Object [] {identifier}));
 220         }
 221     }
 222 
 223     public void setProperty(String name, Object object)
 224         throws SAXNotRecognizedException, SAXNotSupportedException {
 225         if (name == null) {
 226             throw new NullPointerException();
 227         }
 228         try {
 229             fComponentManager.setProperty(name, object);
 230         }
 231         catch (XMLConfigurationException e) {
 232             final String identifier = e.getIdentifier();
 233             final String key = e.getType() == Status.NOT_RECOGNIZED ?
 234                     "property-not-recognized" : "property-not-supported";
 235             throw new SAXNotRecognizedException(
 236                     SAXMessageFormatter.formatMessage(fComponentManager.getLocale(),
 237                     key, new Object [] {identifier}));
 238         }
 239         fConfigurationChanged = true;
 240     }
 241 
 242     public void reset() {
 243         // avoid resetting features and properties if the state the validator
 244         // is currently in, is the same as it will be after reset.
 245         if (fConfigurationChanged) {
 246             fComponentManager.restoreInitialState();
 247             setErrorHandler(null);
 248             setResourceResolver(null);
 249             fConfigurationChanged = false;
 250             fErrorHandlerChanged = false;
 251             fResourceResolverChanged = false;
 252         }
 253         else {
 254             if (fErrorHandlerChanged) {
 255                 setErrorHandler(null);
 256                 fErrorHandlerChanged = false;
 257             }
 258             if (fResourceResolverChanged) {
 259                 setResourceResolver(null);
 260                 fResourceResolverChanged = false;
 261             }
 262         }
 263     }
 264 
 265     /*
 266      * PSVIProvider methods
 267      */
 268 
 269     public ElementPSVI getElementPSVI() {
 270         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getElementPSVI() : null;
 271     }
 272 
 273     public AttributePSVI getAttributePSVI(int index) {
 274         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVI(index) : null;
 275     }
 276 
 277     public AttributePSVI getAttributePSVIByName(String uri, String localname) {
 278         return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVIByName(uri, localname) : null;
 279     }
 280 
 281 } // ValidatorImpl