Search in sources :

Example 21 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class ElementReader method getValue.

public String getValue() {
    if (value == null) {
        try {
            if (isXsiNil()) {
                readToEnd();
                return null;
            }
            value = root.getElementText();
            hasCheckedChildren = true;
            hasChildren = false;
            if (root.hasNext()) {
                root.next();
            }
        } catch (XMLStreamException e) {
            throw new DatabindingException("Could not read XML stream.", e);
        }
        if (value == null) {
            value = "";
        }
    }
    return value;
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 22 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class ElementWriter method getPrefixForNamespace.

public String getPrefixForNamespace(String ns, String hint) {
    try {
        String pfx = writer.getPrefix(ns);
        String contextPfx = writer.getNamespaceContext().getPrefix(ns);
        if (pfx == null) {
            String ns2 = writer.getNamespaceContext().getNamespaceURI(hint);
            // if the hint is "" (the default) and the context does
            if (ns2 == null && !"".equals(hint)) {
                pfx = hint;
            } else if (ns.equals(ns2)) {
                // just because it's in the context, doesn't mean it has been written.
                pfx = hint;
            } else if (contextPfx != null) {
                pfx = contextPfx;
            } else {
                pfx = StaxUtils.getUniquePrefix(writer);
            }
            writer.setPrefix(pfx, ns);
            writer.writeNamespace(pfx, ns);
        }
        return pfx;
    } catch (XMLStreamException e) {
        throw new DatabindingException("Error writing document.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 23 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class NoDefaultConstructorBeanType method readObject.

@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    BeanTypeInfo inf = getTypeInfo();
    try {
        String id = null;
        String name = null;
        // Read child elements
        while (reader.hasMoreElementReaders()) {
            MessageReader childReader = reader.getNextElementReader();
            if (childReader.isXsiNil()) {
                childReader.readToEnd();
                continue;
            }
            QName qName = childReader.getName();
            AegisType defaultType = inf.getType(qName);
            AegisType type = TypeUtil.getReadType(childReader.getXMLStreamReader(), context.getGlobalContext(), defaultType);
            if (type != null) {
                String value = (String) type.readObject(childReader, context);
                if ("id".equals(qName.getLocalPart())) {
                    id = value;
                } else if ("name".equals(qName.getLocalPart())) {
                    name = value;
                }
            } else {
                childReader.readToEnd();
            }
        }
        return new NoDefaultConstructorBeanImpl(id, name);
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument. " + e.getMessage(), e);
    }
}
Also used : BeanTypeInfo(org.apache.cxf.aegis.type.basic.BeanTypeInfo) DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) MessageReader(org.apache.cxf.aegis.xml.MessageReader) NoDefaultConstructorBeanImpl(org.apache.cxf.aegis.custom.service.NoDefaultConstructorBeanImpl)

Example 24 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class ElementDataWriter method write.

public void write(Object obj, MessagePartInfo part, Element output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    try {
        W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter(output);
        ElementWriter writer = new ElementWriter(domWriter);
        MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
        if (type.isNillable() && type.isWriteOuter() && obj == null) {
            w2.writeXsiNil();
            w2.close();
            return;
        }
        type.writeObject(obj, w2, context);
        w2.close();
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) DatabindingException(org.apache.cxf.aegis.DatabindingException) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Example 25 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class XMLStreamDataWriter method write.

public void write(Object obj, MessagePartInfo part, XMLStreamWriter output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        type = databinding.getTypeFromClass(obj.getClass());
    }
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG, part));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    /*
         * We arrive here with a 'type' of the inner type if isWriteOuter is null.
         * However, in that case, the original type is available.
         */
    AegisType outerType = null;
    if (part != null) {
        outerType = part.getProperty("org.apache.cxf.aegis.outerType", AegisType.class);
    }
    try {
        if (obj == null) {
            if (part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getMinOccurs() == 0) {
                // skip writing minOccurs=0 stuff if obj is null
                return;
            } else if (type.isNillable()) {
                ElementWriter writer = new ElementWriter(output);
                MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
                w2.writeXsiNil();
                w2.close();
                return;
            }
        }
        ElementWriter writer = new ElementWriter(output);
        // outerType is only != null for a flat array.
        if (outerType == null) {
            MessageWriter w2 = writer.getElementWriter(part != null ? part.getConcreteName() : type.getSchemaType());
            type.writeObject(obj, w2, context);
            w2.close();
        } else {
            // it has better be an array (!)
            ArrayType aType = (ArrayType) outerType;
            // the part has to have a name or we can't do this.
            aType.writeObject(obj, writer, context, part.getConcreteName());
        }
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) ArrayType(org.apache.cxf.aegis.type.basic.ArrayType) DatabindingException(org.apache.cxf.aegis.DatabindingException) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Aggregations

DatabindingException (org.apache.cxf.aegis.DatabindingException)38 AegisType (org.apache.cxf.aegis.type.AegisType)17 QName (javax.xml.namespace.QName)9 MessageReader (org.apache.cxf.aegis.xml.MessageReader)8 XMLStreamException (javax.xml.stream.XMLStreamException)6 PropertyDescriptor (java.beans.PropertyDescriptor)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 Element (org.w3c.dom.Element)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Context (org.apache.cxf.aegis.Context)3 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)3 BeanTypeInfo (org.apache.cxf.aegis.type.basic.BeanTypeInfo)3 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)3 Document (org.w3c.dom.Document)3 IntrospectionException (java.beans.IntrospectionException)2 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 AegisContext (org.apache.cxf.aegis.AegisContext)2 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)2