1 16 17 package org.apache.xerces.jaxp.validation; 18 19 import java.io.IOException ; 20 import java.util.Locale ; 21 22 import javax.xml.transform.Result ; 23 import javax.xml.transform.Source ; 24 import javax.xml.transform.dom.DOMSource ; 25 import javax.xml.transform.sax.SAXSource ; 26 import javax.xml.transform.stream.StreamSource ; 27 import javax.xml.validation.Validator ; 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 ; 35 import org.xml.sax.ErrorHandler ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXNotRecognizedException ; 38 import org.xml.sax.SAXNotSupportedException ; 39 40 48 final class ValidatorImpl extends Validator implements PSVIProvider { 49 50 54 55 private XMLSchemaValidatorComponentManager fComponentManager; 56 57 58 private ValidatorHandlerImpl fSAXValidatorHelper; 59 60 61 private DOMValidatorHelper fDOMValidatorHelper; 62 63 64 private StreamValidatorHelper fStreamValidatorHelper; 65 66 67 private boolean fConfigurationChanged = false; 68 69 70 private boolean fErrorHandlerChanged = false; 71 72 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 source, Result result) 82 throws SAXException , IOException { 83 if (source instanceof SAXSource ) { 84 if (fSAXValidatorHelper == null) { 86 fSAXValidatorHelper = new ValidatorHandlerImpl(fComponentManager); 87 } 88 fSAXValidatorHelper.validate(source, result); 89 } 90 else if (source instanceof DOMSource ) { 91 if (fDOMValidatorHelper == null) { 93 fDOMValidatorHelper = new DOMValidatorHelper(fComponentManager); 94 } 95 fDOMValidatorHelper.validate(source, result); 96 } 97 else if (source instanceof StreamSource ) { 98 if (fStreamValidatorHelper == null) { 100 fStreamValidatorHelper = new StreamValidatorHelper(fComponentManager); 101 } 102 fStreamValidatorHelper.validate(source, result); 103 } 104 else if (source == null) { 106 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 107 "SourceParameterNull", null)); 108 } 109 else { 111 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 112 "SourceNotAccepted", new Object [] {source.getClass().getName()})); 113 } 114 } 115 116 public void setErrorHandler(ErrorHandler errorHandler) { 117 fErrorHandlerChanged = (errorHandler != null); 118 fComponentManager.setErrorHandler(errorHandler); 119 } 120 121 public ErrorHandler getErrorHandler() { 122 return fComponentManager.getErrorHandler(); 123 } 124 125 public void setResourceResolver(LSResourceResolver resourceResolver) { 126 fResourceResolverChanged = (resourceResolver != null); 127 fComponentManager.setResourceResolver(resourceResolver); 128 } 129 130 public LSResourceResolver getResourceResolver() { 131 return fComponentManager.getResourceResolver(); 132 } 133 134 public boolean getFeature(String name) 135 throws SAXNotRecognizedException , SAXNotSupportedException { 136 if (name == null) { 137 throw new NullPointerException (); 138 } 139 try { 140 return fComponentManager.getFeature(name); 141 } 142 catch (XMLConfigurationException e) { 143 final String identifier = e.getIdentifier(); 144 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 145 "feature-not-recognized" : "feature-not-supported"; 146 throw new SAXNotRecognizedException ( 147 SAXMessageFormatter.formatMessage(Locale.getDefault(), 148 key, new Object [] {identifier})); 149 } 150 } 151 152 public void setFeature(String name, boolean value) 153 throws SAXNotRecognizedException , SAXNotSupportedException { 154 if (name == null) { 155 throw new NullPointerException (); 156 } 157 try { 158 fComponentManager.setFeature(name, value); 159 } 160 catch (XMLConfigurationException e) { 161 final String identifier = e.getIdentifier(); 162 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 163 "feature-not-recognized" : "feature-not-supported"; 164 throw new SAXNotRecognizedException ( 165 SAXMessageFormatter.formatMessage(Locale.getDefault(), 166 key, new Object [] {identifier})); 167 } 168 fConfigurationChanged = true; 169 } 170 171 public Object getProperty(String name) 172 throws SAXNotRecognizedException , SAXNotSupportedException { 173 if (name == null) { 174 throw new NullPointerException (); 175 } 176 try { 177 return fComponentManager.getProperty(name); 178 } 179 catch (XMLConfigurationException e) { 180 final String identifier = e.getIdentifier(); 181 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 182 "property-not-recognized" : "property-not-supported"; 183 throw new SAXNotRecognizedException ( 184 SAXMessageFormatter.formatMessage(Locale.getDefault(), 185 key, new Object [] {identifier})); 186 } 187 } 188 189 public void setProperty(String name, Object object) 190 throws SAXNotRecognizedException , SAXNotSupportedException { 191 if (name == null) { 192 throw new NullPointerException (); 193 } 194 try { 195 fComponentManager.setProperty(name, object); 196 } 197 catch (XMLConfigurationException e) { 198 final String identifier = e.getIdentifier(); 199 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 200 "property-not-recognized" : "property-not-supported"; 201 throw new SAXNotRecognizedException ( 202 SAXMessageFormatter.formatMessage(Locale.getDefault(), 203 key, new Object [] {identifier})); 204 } 205 fConfigurationChanged = true; 206 } 207 208 public void reset() { 209 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 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 uri, String localname) { 244 return (fSAXValidatorHelper != null) ? fSAXValidatorHelper.getAttributePSVIByName(uri, localname) : null; 245 } 246 247 } | Popular Tags |