1 8 package com.sun.japex.testsuite.impl.runtime; 9 10 import java.io.IOException ; 11 12 import javax.xml.bind.DatatypeConverter; 13 import javax.xml.bind.JAXBException; 14 import javax.xml.bind.UnmarshallerHandler; 15 import javax.xml.bind.helpers.AbstractUnmarshallerImpl; 16 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Node ; 20 import org.xml.sax.InputSource ; 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.XMLReader ; 23 import org.xml.sax.helpers.DefaultHandler ; 24 25 import com.sun.xml.bind.DatatypeConverterImpl; 26 import com.sun.xml.bind.unmarshaller.DOMScanner; 27 import com.sun.xml.bind.unmarshaller.InterningXMLReader; 28 import com.sun.xml.bind.validator.DOMLocator; 29 import com.sun.xml.bind.validator.Locator; 30 import com.sun.xml.bind.validator.SAXLocator; 31 32 42 public class UnmarshallerImpl extends AbstractUnmarshallerImpl 43 { 44 45 private DefaultJAXBContextImpl context = null; 46 47 private final GrammarInfo grammarInfo; 48 49 public UnmarshallerImpl( DefaultJAXBContextImpl context, GrammarInfo gi ) { 50 51 this.context = context; 52 this.grammarInfo = gi; 53 54 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance); 56 } 57 58 public void setValidating(boolean validating) throws JAXBException { 59 super.setValidating(validating); 60 if(validating==true) 61 context.getGrammar(); 64 } 65 66 public UnmarshallerHandler getUnmarshallerHandler() { 67 68 71 return new InterningUnmarshallerHandler( 75 createUnmarshallerHandler(new SAXLocator())); 76 } 77 78 79 80 94 private SAXUnmarshallerHandler createUnmarshallerHandler( Locator locator ) { 95 96 SAXUnmarshallerHandler unmarshaller = 97 new SAXUnmarshallerHandlerImpl( this, grammarInfo ); 98 99 try { 100 101 if( isValidating() ) { 103 unmarshaller = ValidatingUnmarshaller.create( 106 context.getGrammar(), unmarshaller, locator ); 107 } 108 } catch( JAXBException e ) { 109 e.printStackTrace(); 111 } 112 113 return unmarshaller; 114 } 115 116 117 protected Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException { 118 119 SAXLocator locator = new SAXLocator(); 120 SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator); 121 122 reader = InterningXMLReader.adapt(reader); 123 124 reader.setContentHandler(handler); 125 reader.setErrorHandler( 137 new ErrorHandlerAdaptor(handler,locator)); 138 139 try { 140 reader.parse(source); 141 } catch( IOException e ) { 142 throw new JAXBException(e); 143 } catch( SAXException e ) { 144 throw createUnmarshalException(e); 145 } 146 147 Object result = handler.getResult(); 148 149 reader.setContentHandler(dummyHandler); 153 reader.setErrorHandler(dummyHandler); 154 155 return result; 156 } 157 158 public final Object unmarshal( Node node ) throws JAXBException { 159 try { 160 DOMScanner scanner = new DOMScanner(); 161 UnmarshallerHandler handler = new InterningUnmarshallerHandler( 162 createUnmarshallerHandler(new DOMLocator(scanner))); 163 164 if(node instanceof Element ) 165 scanner.parse((Element )node,handler); 166 else 167 if(node instanceof Document ) 168 scanner.parse(((Document )node).getDocumentElement(),handler); 169 else 170 throw new IllegalArgumentException (); 172 173 return handler.getResult(); 174 } catch( SAXException e ) { 175 throw createUnmarshalException(e); 176 } 177 } 178 179 private static final DefaultHandler dummyHandler = new DefaultHandler (); 180 } 181 | Popular Tags |