Search in sources :

Example 6 with DatabindingException

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

the class ArrayType method writeObject.

/**
 * Write an array type, using the desired element name in the flattened case.
 * @param values values to write.
 * @param writer writer to sent it to.
 * @param context the aegis context.
 * @param flatElementName name to use for the element if flat.
 * @throws DatabindingException
 */
public void writeObject(Object values, MessageWriter writer, Context context, QName flatElementName) throws DatabindingException {
    boolean forceXsiWrite = false;
    if (values == null) {
        return;
    }
    if (values instanceof Collection) {
        Collection<?> c = (Collection<?>) values;
        values = c.toArray();
    }
    AegisType type = getComponentType();
    if (type == null) {
        throw new DatabindingException("Couldn't find type for array.");
    }
    if (Constants.XSD_ANYTYPE.equals(type.getSchemaType())) {
        forceXsiWrite = true;
    }
    String ns = null;
    if (type.isAbstract()) {
        ns = getSchemaType().getNamespaceURI();
    } else {
        ns = type.getSchemaType().getNamespaceURI();
    }
    /*
         * This is not the right name in the 'flat' case. In the flat case,
         * we need the element name that would have been attached
         * one level out.
         */
    String name;
    if (isFlat()) {
        name = flatElementName.getLocalPart();
        // override the namespace.
        ns = flatElementName.getNamespaceURI();
    } else {
        name = type.getSchemaType().getLocalPart();
    }
    Class<?> arrayType = type.getTypeClass();
    boolean oldXsiWrite = context.getGlobalContext().isWriteXsiTypes();
    try {
        if (forceXsiWrite) {
            context.getGlobalContext().setWriteXsiTypes(true);
        }
        int i;
        int n;
        if (Object.class.isAssignableFrom(arrayType)) {
            Object[] objects = (Object[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(objects[i], writer, context, type, name, ns);
            }
        } else if (Integer.TYPE.equals(arrayType)) {
            int[] objects = (int[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Integer.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Long.TYPE.equals(arrayType)) {
            long[] objects = (long[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Long.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Short.TYPE.equals(arrayType)) {
            short[] objects = (short[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Short.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Double.TYPE.equals(arrayType)) {
            double[] objects = (double[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Double.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Float.TYPE.equals(arrayType)) {
            float[] objects = (float[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Float.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Byte.TYPE.equals(arrayType)) {
            byte[] objects = (byte[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Byte.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Boolean.TYPE.equals(arrayType)) {
            boolean[] objects = (boolean[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Boolean.valueOf(objects[i]), writer, context, type, name, ns);
            }
        } else if (Character.TYPE.equals(arrayType)) {
            char[] objects = (char[]) values;
            for (i = 0, n = objects.length; i < n; i++) {
                writeValue(Character.valueOf(objects[i]), writer, context, type, name, ns);
            }
        }
    } finally {
        context.getGlobalContext().setWriteXsiTypes(oldXsiWrite);
    }
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) DatabindingException(org.apache.cxf.aegis.DatabindingException) Collection(java.util.Collection)

Example 7 with DatabindingException

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

the class Base64Type method readObject.

@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
    XMLStreamReader reader = mreader.getXMLStreamReader();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        reader.next();
        while (!reader.isCharacters() && !reader.isEndElement() && !reader.isStartElement()) {
            reader.next();
        }
        if (reader.isStartElement() && reader.getName().equals(AbstractXOPType.XOP_INCLUDE)) {
            return optimizedType.readMtoM(mreader, context);
        }
        if (reader.isEndElement()) {
            reader.next();
            return new byte[0];
        }
        CharArrayWriter writer = new CharArrayWriter(2048);
        while (reader.isCharacters()) {
            writer.write(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength());
            reader.next();
        }
        Base64Utility.decode(writer.toCharArray(), 0, writer.size(), bos);
        while (reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
            reader.next();
        }
        // Advance just past the end element
        reader.next();
        return bos.toByteArray();
    } catch (Base64Exception e) {
        throw new DatabindingException("Could not parse base64Binary data.", e);
    } catch (XMLStreamException e) {
        throw new DatabindingException("Could not parse base64Binary data.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) Base64Exception(org.apache.cxf.common.util.Base64Exception) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharArrayWriter(java.io.CharArrayWriter)

Example 8 with DatabindingException

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

the class BeanType method writeObjectInternal.

private void writeObjectInternal(Object object, MessageWriter writer, Context context, boolean wroteXsiType) throws DatabindingException {
    if (object == null) {
        return;
    }
    BeanTypeInfo inf = getTypeInfo();
    if (!wroteXsiType && object.getClass() == getTypeClass() && context.isWriteXsiTypes()) {
        writer.writeXsiType(getSchemaType());
    }
    for (QName name : inf.getAttributes()) {
        if (isInheritedProperty(inf, name)) {
            continue;
        }
        Object value = readProperty(object, name);
        if (value != null) {
            AegisType type = getType(inf, name);
            if (type == null) {
                throw new DatabindingException("Couldn't find type for " + value.getClass() + " for property " + name);
            }
            MessageWriter cwriter = writer.getAttributeWriter(name);
            type.writeObject(value, cwriter, context);
            cwriter.close();
        }
    }
    if (inf.isExtension()) {
        AegisType t = getSuperType();
        if (t != null) {
            t.writeObject(object, writer, context);
        }
    }
    for (QName name : inf.getElements()) {
        if (isInheritedProperty(inf, name)) {
            continue;
        }
        Object value = readProperty(object, name);
        AegisType defaultType = getType(inf, name);
        AegisType type = TypeUtil.getWriteType(context.getGlobalContext(), value, defaultType);
        // Write the value if it is not null.
        if (value != null) {
            if (type == null) {
                throw new DatabindingException("Couldn't find type for " + value.getClass() + " for property " + name);
            }
            writeElement(name, value, type, writer, context);
        } else if (inf.isNillable(name)) {
            MessageWriter cwriter = getWriter(writer, name, type);
            // Write the xsi:nil if it is null.
            cwriter.writeXsiNil();
            cwriter.close();
        }
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter)

Example 9 with DatabindingException

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

the class BeanType method readProperty.

protected Object readProperty(Object object, QName name) {
    try {
        PropertyDescriptor desc = getTypeInfo().getPropertyDescriptorFromMappedName(name);
        Method m = desc.getReadMethod();
        if (m == null) {
            throw new DatabindingException("No read method for property " + name + " in class " + object.getClass().getName());
        }
        return m.invoke(object, new Object[0]);
    } catch (Exception e) {
        throw new DatabindingException("Couldn't get property " + name + " from bean " + object, e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DatabindingException(org.apache.cxf.aegis.DatabindingException)

Example 10 with DatabindingException

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

the class BeanType method writeProperty.

/**
 * Write the specified property to a field.
 */
protected void writeProperty(QName name, Object object, Object property, Class<?> impl, BeanTypeInfo inf) throws DatabindingException {
    if (object instanceof InterfaceInvocationHandler) {
        InterfaceInvocationHandler delegate = (InterfaceInvocationHandler) object;
        delegate.writeProperty(name.getLocalPart(), property);
        return;
    }
    try {
        PropertyDescriptor desc = inf.getPropertyDescriptorFromMappedName(name);
        Method m = desc.getWriteMethod();
        if (m == null) {
            if (getTypeClass().isInterface()) {
                m = getWriteMethodFromImplClass(impl, desc);
            }
            if (m == null && property instanceof List) {
                m = desc.getReadMethod();
                List<Object> l = CastUtils.cast((List<?>) m.invoke(object));
                List<Object> p = CastUtils.cast((List<?>) property);
                l.addAll(p);
                return;
            }
            if (m == null) {
                throw new DatabindingException("No write method for property " + name + " in " + object.getClass());
            }
        }
        Class<?> propertyType = desc.getPropertyType();
        if ((property == null && !propertyType.isPrimitive()) || (property != null)) {
            m.invoke(object, new Object[] { property });
        }
    } catch (Exception e) {
        if (e instanceof DatabindingException) {
            throw (DatabindingException) e;
        }
        throw new DatabindingException("Couldn't set property " + name + " on " + object + ". " + e.getMessage(), e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) PropertyDescriptor(java.beans.PropertyDescriptor) List(java.util.List) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DatabindingException(org.apache.cxf.aegis.DatabindingException)

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