1 package com.sun.tools.xjc.runtime; 2 3 import java.io.IOException ; 4 5 import javax.xml.bind.DatatypeConverter; 6 import javax.xml.bind.JAXBException; 7 import javax.xml.bind.UnmarshallerHandler; 8 import javax.xml.bind.helpers.AbstractUnmarshallerImpl; 9 10 import org.w3c.dom.Document ; 11 import org.w3c.dom.Element ; 12 import org.w3c.dom.Node ; 13 import org.xml.sax.InputSource ; 14 import org.xml.sax.SAXException ; 15 import org.xml.sax.XMLReader ; 16 import org.xml.sax.helpers.DefaultHandler ; 17 18 import com.sun.xml.bind.DatatypeConverterImpl; 19 import com.sun.xml.bind.unmarshaller.DOMScanner; 20 import com.sun.xml.bind.unmarshaller.InterningXMLReader; 21 import com.sun.xml.bind.validator.DOMLocator; 22 import com.sun.xml.bind.validator.Locator; 23 import com.sun.xml.bind.validator.SAXLocator; 24 25 35 public class UnmarshallerImpl extends AbstractUnmarshallerImpl 36 { 37 38 private DefaultJAXBContextImpl context = null; 39 40 private final GrammarInfo grammarInfo; 41 42 public UnmarshallerImpl( DefaultJAXBContextImpl context, GrammarInfo gi ) { 43 44 this.context = context; 45 this.grammarInfo = gi; 46 47 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance); 49 } 50 51 public void setValidating(boolean validating) throws JAXBException { 52 if( MetaVariable.W ) { super.setValidating(validating); 54 if(validating==true) 55 context.getGrammar(); 58 } else { throw new UnsupportedOperationException ( 60 "When generating this code, the compiler option was specified not to generate code for the unmarshal-time validation"); 61 } } 63 64 public UnmarshallerHandler getUnmarshallerHandler() { 65 66 69 return new InterningUnmarshallerHandler( 73 createUnmarshallerHandler(new SAXLocator())); 74 } 75 76 77 78 92 private SAXUnmarshallerHandler createUnmarshallerHandler( Locator locator ) { 93 94 SAXUnmarshallerHandler unmarshaller = 95 new SAXUnmarshallerHandlerImpl( this, grammarInfo ); 96 97 try { 99 100 if( isValidating() ) { 102 unmarshaller = ValidatingUnmarshaller.create( 105 context.getGrammar(), unmarshaller, locator ); 106 } 107 } catch( JAXBException e ) { 108 e.printStackTrace(); 110 } 111 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 |