jaxbtype.java

来自「Xfire文件 用于开发web service 的一个开源工具 很好用的」· Java 代码 · 共 98 行

JAVA
98
字号
package org.codehaus.xfire.jaxb;import javanet.staxutils.StAXSource;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import javax.xml.namespace.QName;import javax.xml.stream.XMLStreamReader;import org.codehaus.xfire.MessageContext;import org.codehaus.xfire.XFireRuntimeException;import org.codehaus.xfire.aegis.MessageReader;import org.codehaus.xfire.aegis.MessageWriter;import org.codehaus.xfire.aegis.stax.ElementReader;import org.codehaus.xfire.aegis.stax.ElementWriter;import org.codehaus.xfire.aegis.type.Type;import org.codehaus.xfire.fault.XFireFault;public class JaxbType extends Type{    private QName mySchemaType;    public JaxbType(Class clazz, JAXBContext jaxbContext)    {        mySchemaType = new JaxbIntrospector(jaxbContext).introspect(clazz);        try        {            unmarshaller = jaxbContext.createUnmarshaller();            unmarshaller.setValidating(false);        } catch (JAXBException e)        {            throw new XFireRuntimeException("error creating unmarshaller", e);        }        try        {            marshaller = jaxbContext.createMarshaller();        } catch (JAXBException e)        {            throw new XFireRuntimeException("error creating marshaller", e);        }    }    Unmarshaller unmarshaller;    Marshaller marshaller;    public boolean isAbstract()    {        // right now we dont support abstract types        return false;    }    public boolean isComplex()    {        return true;    }    public Object readObject(MessageReader reader, MessageContext context) throws XFireFault    {        try        {            XMLStreamReader xmlStreamReader = ((ElementReader) reader).getXMLStreamReader();            StAXSource stAXSource = new StAXSource(xmlStreamReader);            return unmarshaller.unmarshal(stAXSource);        } catch (Exception e)        {            e.printStackTrace();            throw new XFireFault("Could not unmarshall type.", e, XFireFault.SENDER);        }    }    public void writeObject(Object object, MessageWriter writer, MessageContext context) throws XFireFault    {        try        {            marshaller.marshal(object, new FilteringStaxResult(((ElementWriter) writer).getXMLStreamWriter()));        } catch (JAXBException e)        {            throw new XFireFault("Could not marshall type.", e, XFireFault.RECEIVER);        }    }    public QName getSchemaType()    {        return mySchemaType;    }    public boolean isWriteOuter()    {        return false;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?