package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.PropertyException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.attachment.AttachmentUnmarshaller;
import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import com.sun.xml.internal.bind.IDResolver;
import com.sun.xml.internal.bind.api.ClassResolver;
import com.sun.xml.internal.bind.unmarshaller.DOMScanner;
import com.sun.xml.internal.bind.unmarshaller.InfosetScanner;
import com.sun.xml.internal.bind.unmarshaller.Messages;
import com.sun.xml.internal.bind.v2.ClassFactory;
import com.sun.xml.internal.bind.v2.runtime.AssociationMap;
import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
import com.sun.xml.internal.bind.v2.util.XmlFactory;
import java.io.Closeable;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public final class UnmarshallerImpl extends AbstractUnmarshallerImpl implements ValidationEventHandler, Closeable
{
protected final JAXBContextImpl context;
private Schema schema;
public final UnmarshallingContext coordinator;
private Listener externalListener;
private AttachmentUnmarshaller attachmentUnmarshaller;
private IDResolver idResolver = new DefaultIDResolver();
public UnmarshallerImpl( JAXBContextImpl context, AssociationMap assoc ) {
this.context = context;
this.coordinator = new UnmarshallingContext( this, assoc );
try {
setEventHandler(this);
} catch (JAXBException e) {
throw new AssertionError(e);
}
}
public UnmarshallerHandler getUnmarshallerHandler() {
return getUnmarshallerHandler(true,null);
}
private XMLReader reader = null;
@Override
protected XMLReader getXMLReader() throws JAXBException {
if (reader == null) {
try {
SAXParserFactory parserFactory = XmlFactory.createParserFactory(context.disableSecurityProcessing);
parserFactory.setValidating(false);
reader = parserFactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException e) {
throw new JAXBException(e);
} catch (SAXException e) {
throw new JAXBException(e);
}
}
return reader;
}
private SAXConnector getUnmarshallerHandler( boolean intern, JaxBeanInfo expectedType ) {
XmlVisitor h = createUnmarshallerHandler(null, false, expectedType);
if (intern) {
h = new InterningXmlVisitor(h);
}
return new SAXConnector(h,null);
}
public final XmlVisitor createUnmarshallerHandler(InfosetScanner scanner, boolean inplace, JaxBeanInfo expectedType ) {
coordinator.reset(scanner,inplace,expectedType,idResolver);
XmlVisitor unmarshaller = coordinator;
if (schema != null) {
unmarshaller = new ValidatingUnmarshaller(schema,unmarshaller);
}
if(attachmentUnmarshaller!=null && attachmentUnmarshaller.isXOPPackage()) {
unmarshaller = new MTOMDecorator(this,unmarshaller,attachmentUnmarshaller);
}
return unmarshaller;
}
private static final DefaultHandler dummyHandler = new DefaultHandler();
public static boolean needsInterning( XMLReader reader ) {
try {
reader.setFeature("http://xml.org/sax/features/string-interning",true);
} catch (SAXException e) {
}
try {
if (reader.getFeature("http://xml.org/sax/features/string-interning")) {
return false;
}
} catch (SAXException e) {
}
return true;
}
protected Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException {
return unmarshal0(reader,source,null);
}
protected <T> JAXBElement<T> unmarshal( XMLReader reader, InputSource source, Class<T> expectedType ) throws JAXBException {
if(expectedType==null) {
throw new IllegalArgumentException();
}
return (JAXBElement)unmarshal0(reader,source,getBeanInfo(expectedType));
}
private Object unmarshal0( XMLReader reader, InputSource source, JaxBeanInfo expectedType ) throws JAXBException {
SAXConnector connector = getUnmarshallerHandler(needsInterning(reader),expectedType);
reader.setContentHandler(connector);
reader.setErrorHandler(coordinator);
try {
reader.parse(source);
} catch( IOException e ) {
coordinator.clearStates();
throw new UnmarshalException(e);
} catch( SAXException e ) {
coordinator.clearStates();
throw createUnmarshalException(e);
}
Object result = connector.getResult();
reader.setContentHandler(dummyHandler);
reader.setErrorHandler(dummyHandler);
return result;
}
@Override
public <T> JAXBElement<T> unmarshal( Source source, Class<T> expectedType ) throws JAXBException {
if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
XMLReader locReader = ss.getXMLReader();
if (locReader == null) {
locReader = getXMLReader();
}
return unmarshal(locReader, ss.getInputSource(), expectedType);
}
if (source instanceof StreamSource) {
return unmarshal(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType);
}
if (source instanceof DOMSource) {
return unmarshal(((DOMSource) source).getNode(), expectedType);
}
throw new IllegalArgumentException();
}
public Object unmarshal0( Source source, JaxBeanInfo expectedType ) throws JAXBException {
if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
XMLReader locReader = ss.getXMLReader();
if (locReader == null) {
locReader = getXMLReader();
}
return unmarshal0(locReader, ss.getInputSource(), expectedType);
}
if (source instanceof StreamSource) {
return unmarshal0(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType);
}
if (source instanceof DOMSource) {
return unmarshal0(((DOMSource) source).getNode(), expectedType);
}
throw new IllegalArgumentException();
}
@Override
public final ValidationEventHandler getEventHandler() {
try {
return super.getEventHandler();
} catch (JAXBException e) {
throw new AssertionError();
}
}
public final boolean hasEventHandler() {
return getEventHandler()!=this;
}
@Override
public <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException {
if (expectedType == null) {
throw new IllegalArgumentException();
}
return (JAXBElement)unmarshal0(node,getBeanInfo(expectedType));
}
public final Object unmarshal( Node node ) throws JAXBException {
return unmarshal0(node,null);
}
@Deprecated
public final Object unmarshal( SAXSource source ) throws JAXBException {
return super.unmarshal(source);
}
public final Object unmarshal0( Node node, JaxBeanInfo expectedType ) throws JAXBException {
try {
final DOMScanner scanner = new DOMScanner();
InterningXmlVisitor handler = new InterningXmlVisitor(createUnmarshallerHandler(null,false,expectedType));
scanner.setContentHandler(new SAXConnector(handler,scanner));
if(node.getNodeType() == Node.ELEMENT_NODE) {
scanner.scan((Element)node);
} else if(node.getNodeType() == Node.DOCUMENT_NODE) {
scanner.scan((Document)node);
} else {
throw new IllegalArgumentException("Unexpected node type: "+node);
}
Object retVal = handler.getContext().getResult();
handler.getContext().clearResult();
return retVal;
} catch( SAXException e ) {
throw createUnmarshalException(e);
}
}
@Override
public Object unmarshal(XMLStreamReader reader) throws JAXBException {
return unmarshal0(reader,null);
}
@Override
public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException {
if (expectedType==null) {
throw new IllegalArgumentException();
}
return (JAXBElement)unmarshal0(reader,getBeanInfo(expectedType));
}
public Object unmarshal0(XMLStreamReader reader, JaxBeanInfo expectedType) throws JAXBException {
if (reader == null) {
throw new IllegalArgumentException(
Messages.format(Messages.NULL_READER));
}
int eventType = reader.getEventType();
if (eventType != XMLStreamConstants.START_ELEMENT
&& eventType != XMLStreamConstants.START_DOCUMENT) {
throw new IllegalStateException(
Messages.format(Messages.ILLEGAL_READER_STATE,eventType));
}
XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
StAXConnector connector=StAXStreamConnector.create(reader,h);
try {
connector.bridge();
} catch (XMLStreamException e) {
throw handleStreamException(e);
}
Object retVal = h.getContext().getResult();
h.getContext().clearResult();
return retVal;
}
@Override
public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException {
if(expectedType==null) {
throw new IllegalArgumentException();
}
return (JAXBElement)unmarshal0(reader,getBeanInfo(expectedType));
}
@Override
public Object unmarshal(XMLEventReader reader) throws JAXBException {
return unmarshal0(reader,null);
}
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
if (reader == null) {
throw new IllegalArgumentException(
Messages.format(Messages.NULL_READER));
}
try {
XMLEvent event = reader.peek();
if (!event.isStartElement() && !event.isStartDocument()) {
throw new IllegalStateException(
Messages.format(
Messages.ILLEGAL_READER_STATE,event.getEventType()));
}
boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
if(!isZephyr) {
h = new InterningXmlVisitor(h);
}
new StAXEventConnector(reader,h).bridge();
return h.getContext().getResult();
} catch (XMLStreamException e) {
throw handleStreamException(e);
}
}
public Object unmarshal0( InputStream input, JaxBeanInfo expectedType ) throws JAXBException {
return unmarshal0(getXMLReader(),new InputSource(input),expectedType);
}
private static JAXBException handleStreamException(XMLStreamException e) {
Throwable ne = e.getNestedException();
if(ne instanceof JAXBException) {
return (JAXBException)ne;
}
if(ne instanceof SAXException) {
return new UnmarshalException(ne);
}
return new UnmarshalException(e);
}
@Override
public Object getProperty(String name) throws PropertyException {
if(name.equals(IDResolver.class.getName())) {
return idResolver;
}
return super.getProperty(name);
}
@Override
public void setProperty(String name, Object value) throws PropertyException {
if(name.equals(FACTORY)) {
coordinator.setFactories(value);
return;
}
if(name.equals(IDResolver.class.getName())) {
idResolver = (IDResolver)value;
return;
}
if(name.equals(ClassResolver.class.getName())) {
coordinator.classResolver = (ClassResolver)value;
return;
}
if(name.equals(ClassLoader.class.getName())) {
coordinator.classLoader = (ClassLoader)value;
return;
}
super.setProperty(name, value);
}
public static final String FACTORY = "com.sun.xml.internal.bind.ObjectFactory";
@Override
public void setSchema(Schema schema) {
this.schema = schema;
}
@Override
public Schema getSchema() {
return schema;
}
@Override
public AttachmentUnmarshaller getAttachmentUnmarshaller() {
return attachmentUnmarshaller;
}
@Override
public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) {
this.attachmentUnmarshaller = au;
}
@Override
public boolean isValidating() {
throw new UnsupportedOperationException();
}
@Override
public void setValidating(boolean validating) {
throw new UnsupportedOperationException();
}
@Override
public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
if (type==null) {
throw new IllegalArgumentException();
}
coordinator.putAdapter(type,adapter);
}
@Override
public <A extends XmlAdapter> A getAdapter(Class<A> type) {
if(type==null) {
throw new IllegalArgumentException();
}
if(coordinator.containsAdapter(type)) {
return coordinator.getAdapter(type);
} else {
return null;
}
}
@Override
public UnmarshalException createUnmarshalException( SAXException e ) {
return super.createUnmarshalException(e);
}
public boolean handleEvent(ValidationEvent event) {
return event.getSeverity()!=ValidationEvent.FATAL_ERROR;
}
private static InputSource streamSourceToInputSource( StreamSource ss ) {
InputSource is = new InputSource();
is.setSystemId( ss.getSystemId() );
is.setByteStream( ss.getInputStream() );
is.setCharacterStream( ss.getReader() );
return is;
}
public <T> JaxBeanInfo<T> getBeanInfo(Class<T> clazz) throws JAXBException {
return context.getBeanInfo(clazz,true);
}
@Override
public Listener getListener() {
return externalListener;
}
@Override
public void setListener(Listener listener) {
externalListener = listener;
}
public UnmarshallingContext getContext() {
return coordinator;
}
@Override
@SuppressWarnings("FinalizeDeclaration")
protected void finalize() throws Throwable {
try {
ClassFactory.cleanCache();
} finally {
super.finalize();
}
}
public void close() throws IOException {
ClassFactory.cleanCache();
}
}