001// 002// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-fcs 003// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 004// Any modifications to this file will be lost upon recompilation of the source schema. 005// Generated on: 2012.10.03 at 04:27:47 AM CEST 006// 007 008package org.jdtaus.mojo.resource.model.impl.runtime; 009 010import java.io.IOException; 011 012import javax.xml.bind.DatatypeConverter; 013import javax.xml.bind.JAXBException; 014import javax.xml.bind.UnmarshallerHandler; 015import javax.xml.bind.helpers.AbstractUnmarshallerImpl; 016 017import org.w3c.dom.Document; 018import org.w3c.dom.Element; 019import org.w3c.dom.Node; 020import org.xml.sax.InputSource; 021import org.xml.sax.SAXException; 022import org.xml.sax.XMLReader; 023import org.xml.sax.helpers.DefaultHandler; 024 025import com.sun.xml.bind.DatatypeConverterImpl; 026import com.sun.xml.bind.unmarshaller.DOMScanner; 027import com.sun.xml.bind.unmarshaller.InterningXMLReader; 028import com.sun.xml.bind.validator.DOMLocator; 029import com.sun.xml.bind.validator.Locator; 030import com.sun.xml.bind.validator.SAXLocator; 031 032/** 033 * Default Unmarshall implementation. 034 * 035 * <p> 036 * This class can be extended by the generated code to provide 037 * type-safe unmarshall methods. 038 * 039 * @author 040 * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a> 041 */ 042public class UnmarshallerImpl extends AbstractUnmarshallerImpl 043{ 044 /** parent JAXBContext object that created this unmarshaller */ 045 private DefaultJAXBContextImpl context = null; 046 047 private final GrammarInfo grammarInfo; 048 049 public UnmarshallerImpl( DefaultJAXBContextImpl context, GrammarInfo gi ) { 050 051 this.context = context; 052 this.grammarInfo = gi; 053 054 // initialize datatype converter with ours 055 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance); 056 } 057 058 public void setValidating(boolean validating) throws JAXBException { 059 super.setValidating(validating); 060 if(validating==true) 061 // make sure that we can actually load the grammar. 062 // this could be a lengthy operation if your schema is big. 063 context.getGrammar(); 064 } 065 066 public UnmarshallerHandler getUnmarshallerHandler() { 067 068 // use InterningUnmarshallerHandler since we don't know 069 // if the caller will intern Strings before firing SAX events. 070 071 // we don't know the Locator to be used, 072 // but SAXLocator would always be a good default, 073 // as the source of SAX2 events can always set org.xml.sax.Locator. 074 return new InterningUnmarshallerHandler( 075 createUnmarshallerHandler(new SAXLocator())); 076 } 077 078 079 080 /** 081 * Creates and configures a new unmarshalling pipe line. 082 * Depending on the setting, we put a validator as a filter. 083 * 084 * @return 085 * A component that implements both UnmarshallerHandler 086 * and ValidationEventHandler. All the parsing errors 087 * should be reported to this error handler for the unmarshalling 088 * process to work correctly. 089 * 090 * @param locator 091 * The object that is responsible to obtain the source 092 * location information for {@link ValidationEvent}s. 093 */ 094 private SAXUnmarshallerHandler createUnmarshallerHandler( Locator locator ) { 095 096 SAXUnmarshallerHandler unmarshaller = 097 new SAXUnmarshallerHandlerImpl( this, grammarInfo ); 098 099 try { 100 101 // use the simple check to determine if validation is on 102 if( isValidating() ) { 103 // if the validation is turned on, insert another 104 // component into the event pipe line. 105 unmarshaller = ValidatingUnmarshaller.create( 106 context.getGrammar(), unmarshaller, locator ); 107 } 108 } catch( JAXBException e ) { 109 // impossible since we've already made sure that a grammar is accessible. 110 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 // saxErrorHandler will be set by the createUnmarshallerHandler method. 126 // configure XMLReader so that the error will be sent to it. 127 // This is essential for the UnmarshallerHandler to be able to abort 128 // unmarshalling when an error is found. 129 // 130 // Note that when this XMLReader is provided by the client code, 131 // it might be already configured to call a client error handler. 132 // This will clobber such handler, if any. 133 // 134 // Ryan noted that we might want to report errors to such a client 135 // error handler as well. 136 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 // avoid keeping unnecessary references too long to let the GC 150 // reclaim more memory. 151 // setting null upsets some parsers, so use a dummy instance instead. 152 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 // no other type of input is supported 171 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}