1 20 21 27 28 package com.sun.org.apache.xerces.internal.jaxp.validation; 29 30 import java.io.IOException ; 31 import java.util.Locale ; 32 33 import javax.xml.transform.Result ; 34 import javax.xml.transform.Source ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactoryConfigurationError ; 39 import javax.xml.transform.sax.SAXResult ; 40 import javax.xml.transform.sax.SAXTransformerFactory ; 41 import javax.xml.transform.sax.TransformerHandler ; 42 import javax.xml.transform.stax.StAXResult; 43 import javax.xml.transform.stax.StAXSource; 44 45 import org.xml.sax.SAXException ; 46 47 52 public final class StAXValidatorHelper implements ValidatorHelper { 53 54 55 private XMLSchemaValidatorComponentManager fComponentManager; 56 57 private Transformer identityTransformer1 = null; 58 private TransformerHandler identityTransformer2 = null; 59 private ValidatorHandlerImpl handler = null; 60 61 62 public StAXValidatorHelper(XMLSchemaValidatorComponentManager componentManager) { 63 fComponentManager = componentManager; 64 } 65 66 public void validate(Source source, Result result) 67 throws SAXException , IOException { 68 69 if (result == null || result instanceof StAXResult) { 70 71 if( identityTransformer1==null ) { 72 try { 73 SAXTransformerFactory tf = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 74 identityTransformer1 = tf.newTransformer(); 75 identityTransformer2 = tf.newTransformerHandler(); 76 } catch (TransformerConfigurationException e) { 77 throw new TransformerFactoryConfigurationError (e); 79 } 80 } 81 82 if( result!=null ) { 83 handler = new ValidatorHandlerImpl(fComponentManager); 84 handler.setContentHandler(identityTransformer2); 85 identityTransformer2.setResult(result); 86 } 87 88 try { 89 identityTransformer1.transform( source, new SAXResult (handler) ); 90 } catch (TransformerException e) { 91 if( e.getException() instanceof SAXException ) 92 throw (SAXException )e.getException(); 93 throw new SAXException (e); 94 } finally { 95 handler.setContentHandler(null); 96 } 97 return; 98 } 99 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 100 "SourceResultMismatch", 101 new Object [] {source.getClass().getName(), result.getClass().getName()})); 102 } 103 } 104 | Popular Tags |