1 57 package com.sun.org.apache.xerces.internal.jaxp.validation; 58 59 import java.io.IOException ; 60 import javax.xml.parsers.FactoryConfigurationError ; 61 import javax.xml.parsers.SAXParserFactory ; 62 import javax.xml.transform.Result ; 63 import javax.xml.transform.Source ; 64 import javax.xml.transform.Transformer ; 65 import javax.xml.transform.TransformerConfigurationException ; 66 import javax.xml.transform.TransformerException ; 67 import javax.xml.transform.TransformerFactoryConfigurationError ; 68 import javax.xml.transform.dom.DOMResult ; 69 import javax.xml.transform.dom.DOMSource ; 70 import javax.xml.transform.sax.SAXResult ; 71 import javax.xml.transform.sax.SAXSource ; 72 import javax.xml.transform.sax.SAXTransformerFactory ; 73 import javax.xml.transform.sax.TransformerHandler ; 74 import javax.xml.transform.stream.StreamSource ; 75 import javax.xml.validation.Schema ; 76 import javax.xml.validation.Validator ; 77 import javax.xml.validation.ValidatorHandler ; 78 import org.w3c.dom.ls.LSInput ; 79 import org.w3c.dom.ls.LSResourceResolver ; 80 import org.xml.sax.EntityResolver ; 81 import org.xml.sax.ErrorHandler ; 82 import org.xml.sax.InputSource ; 83 import org.xml.sax.SAXException ; 84 import org.xml.sax.SAXParseException ; 85 import org.xml.sax.XMLReader ; 86 87 103 class ValidatorImpl extends Validator { 104 105 108 private final ValidatorHandlerImpl handler; 109 110 113 private Transformer identityTransformer1 = null; 114 private TransformerHandler identityTransformer2 = null; 115 116 ValidatorImpl( ValidatorHandlerImpl _handler ) { 117 this.handler = _handler; 118 } 119 120 public LSResourceResolver getResourceResolver() { 121 return handler.getResourceResolver(); 122 } 123 124 125 public ErrorHandler getErrorHandler() { 126 return handler.getErrorHandler(); 127 } 128 129 public void setResourceResolver(LSResourceResolver resolver) { 130 handler.setResourceResolver(resolver); 131 } 132 133 public void setErrorHandler(ErrorHandler errorHandler) { 134 handler.setErrorHandler(errorHandler); 135 } 136 137 public void validate(Source source, Result result) throws SAXException , IOException { 138 if( source instanceof DOMSource ) { 139 if( result!=null && !(result instanceof DOMResult ) ) 140 throw new IllegalArgumentException (result.getClass().getName()); 141 process( (DOMSource )source, (DOMResult )result ); 142 return; 143 } 144 if( source instanceof SAXSource ) { 145 if( result!=null && !(result instanceof SAXResult ) ) 146 throw new IllegalArgumentException (result.getClass().getName()); 147 process( (SAXSource )source, (SAXResult )result ); 148 return; 149 } 150 if( source instanceof StreamSource ) { 151 if( result!=null ) 152 throw new IllegalArgumentException (result.getClass().getName()); 153 StreamSource ss = (StreamSource )source; 154 InputSource is = new InputSource (); 155 is.setByteStream(ss.getInputStream()); 156 is.setCharacterStream(ss.getReader()); 157 is.setPublicId(ss.getPublicId()); 158 is.setSystemId(ss.getSystemId()); 159 process( new SAXSource (is), null ); 160 return; 161 } 162 throw new IllegalArgumentException (source.getClass().getName()); 163 } 164 165 168 private void process(SAXSource source, SAXResult result) throws IOException , SAXException { 169 if( result!=null ) { 170 handler.setContentHandler(result.getHandler()); 171 } 172 173 try { 174 XMLReader reader = source.getXMLReader(); 175 if( reader==null ) { 176 SAXParserFactory spf = SAXParserFactory.newInstance(); 178 spf.setNamespaceAware(true); 179 try { 180 reader = spf.newSAXParser().getXMLReader(); 181 } catch( Exception e ) { 182 throw new FactoryConfigurationError (e); 184 } 185 } 186 187 reader.setErrorHandler(errorForwarder); 188 reader.setEntityResolver(resolutionForwarder); 189 reader.setContentHandler(handler); 190 191 InputSource is = source.getInputSource(); 192 reader.parse(is); 193 } finally { 194 handler.setContentHandler(null); 196 } 197 } 198 199 202 private void process( DOMSource source, DOMResult result ) throws SAXException { 203 if( identityTransformer1==null ) { 204 try { 205 SAXTransformerFactory tf = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 206 identityTransformer1 = tf.newTransformer(); 207 identityTransformer2 = tf.newTransformerHandler(); 208 } catch (TransformerConfigurationException e) { 209 throw new TransformerFactoryConfigurationError (e); 211 } 212 } 213 214 if( result!=null ) { 215 handler.setContentHandler(identityTransformer2); 216 identityTransformer2.setResult(result); 217 } 218 219 try { 220 identityTransformer1.transform( source, new SAXResult (handler) ); 221 } catch (TransformerException e) { 222 if( e.getException() instanceof SAXException ) 223 throw (SAXException )e.getException(); 224 throw new SAXException (e); 225 } finally { 226 handler.setContentHandler(null); 227 } 228 } 229 230 235 private final ErrorHandler errorForwarder = new ErrorHandler () { 236 public void warning(SAXParseException exception) throws SAXException { 237 ErrorHandler realHandler = handler.getErrorHandler(); 238 if( realHandler!=null ) 239 realHandler.warning(exception); 240 } 241 242 public void error(SAXParseException exception) throws SAXException { 243 ErrorHandler realHandler = handler.getErrorHandler(); 244 if( realHandler!=null ) 245 realHandler.error(exception); 246 else 247 throw exception; 248 } 249 250 public void fatalError(SAXParseException exception) throws SAXException { 251 ErrorHandler realHandler = handler.getErrorHandler(); 252 if( realHandler!=null ) 253 realHandler.fatalError(exception); 254 else 255 throw exception; 256 } 257 }; 258 259 264 private final EntityResolver resolutionForwarder = new EntityResolver () { 265 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 266 LSResourceResolver resolver = handler.getResourceResolver(); 267 if( resolver==null ) return null; 268 269 LSInput di = resolver.resolveResource(null,null,publicId,systemId,null); 270 if(di==null) return null; 271 272 InputSource r = new InputSource (); 273 r.setByteStream(di.getByteStream()); 274 r.setCharacterStream(di.getCharacterStream()); 275 r.setEncoding(di.getEncoding()); 276 r.setPublicId(di.getPublicId()); 277 r.setSystemId(di.getSystemId()); 278 return r; 279 } 280 }; 281 282 public void reset() { 283 handler.reset(); 284 285 if(identityTransformer1!=null) { 288 identityTransformer1.reset(); 289 } 290 } 291 } 292 | Popular Tags |