Search in sources :

Example 26 with DatabindingException

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

the class AbstractTypeCreator method createUserType.

protected AegisType createUserType(TypeClassInfo info) {
    try {
        AegisType type = info.getAegisTypeClass().newInstance();
        QName name = info.getTypeName();
        if (name == null) {
            // We do not want to use the java.lang.whatever schema type.
            // If the @ annotation or XML file didn't specify a schema type,
            // but the natural type has a schema type mapping, we use that rather
            // than create nonsense.
            Class<?> typeClass = TypeUtil.getTypeRelatedClass(info.getType());
            if (typeClass.getPackage().getName().startsWith("java")) {
                name = tm.getTypeQName(typeClass);
            }
            // an invalid schema.
            if (name == null) {
                name = createQName(typeClass);
            }
        }
        type.setSchemaType(name);
        type.setTypeClass(info.getType());
        type.setTypeMapping(getTypeMapping());
        return type;
    } catch (InstantiationException e) {
        throw new DatabindingException("Couldn't instantiate type classs " + info.getAegisTypeClass().getName(), e);
    } catch (IllegalAccessException e) {
        throw new DatabindingException("Couldn't access type classs " + info.getAegisTypeClass().getName(), e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) QName(javax.xml.namespace.QName)

Example 27 with DatabindingException

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

the class XMLTypeCreator method getGenericParameterFromSpec.

// This cannot do List<List<x>>.
private Type getGenericParameterFromSpec(Element mapping, String componentType) {
    if (componentType.startsWith("#")) {
        String name = componentType.substring(1);
        Element propertyEl = getMatch(mapping, "./component[@name='" + name + "']");
        if (propertyEl == null) {
            throw new DatabindingException("Could not find <component> element in mapping named '" + name + "'");
        }
        String className = DOMUtils.getAttributeValueEmptyNull(propertyEl, "class");
        if (className == null) {
            throw new DatabindingException("A 'class' attribute must be specified for <component> " + name);
        }
        return loadComponentClass(className);
    }
    return loadComponentClass(componentType);
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) Element(org.w3c.dom.Element)

Example 28 with DatabindingException

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

the class BeanType method readObject.

/**
 * {@inheritDoc}
 */
@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    BeanTypeInfo inf = getTypeInfo();
    try {
        Class<?> clazz = getTypeClass();
        Object object;
        // the target for properties; either the object or the proxy handler
        Object target;
        if (isInterface) {
            String impl = context.getGlobalContext().getBeanImplementationMap().get(clazz);
            if (impl == null) {
                InvocationHandler handler = new InterfaceInvocationHandler();
                object = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler);
                target = handler;
            } else {
                try {
                    clazz = ClassLoaderUtils.loadClass(impl, getClass());
                    object = clazz.newInstance();
                    target = object;
                } catch (ClassNotFoundException e) {
                    throw new DatabindingException("Could not find implementation class " + impl + " for class " + clazz.getName());
                }
            }
        } else if (isException) {
            object = createFromFault(context);
            target = object;
        } else {
            object = clazz.newInstance();
            target = object;
        }
        // Read attributes
        while (reader.hasMoreAttributeReaders()) {
            MessageReader childReader = reader.getNextAttributeReader();
            QName name = childReader.getName();
            AegisType type = inf.getType(name);
            if (type != null) {
                Object writeObj = type.readObject(childReader, context);
                writeProperty(name, target, writeObj, clazz, inf);
            }
        }
        // Read child elements
        while (reader.hasMoreElementReaders()) {
            MessageReader childReader = reader.getNextElementReader();
            QName name = childReader.getName();
            // Find the BeanTypeInfo that contains a property for the element name
            BeanTypeInfo propertyTypeInfo = getBeanTypeInfoWithProperty(name);
            // Get the AegisType for the property
            AegisType type = getElementType(name, propertyTypeInfo, childReader, context);
            if (type != null) {
                if (!childReader.isXsiNil()) {
                    Object writeObj;
                    if (type.isFlatArray()) {
                        ArrayType aType = (ArrayType) type;
                        PropertyDescriptor desc = inf.getPropertyDescriptorFromMappedName(name);
                        boolean isList = List.class.isAssignableFrom(desc.getPropertyType());
                        writeObj = aType.readObject(childReader, name, context, !isList);
                    } else {
                        writeObj = type.readObject(childReader, context);
                    }
                    writeProperty(name, target, writeObj, clazz, propertyTypeInfo);
                } else {
                    if (!alwaysAllowNillables() && !propertyTypeInfo.isNillable(name)) {
                        throw new DatabindingException(name.getLocalPart() + " is nil, but not nillable.");
                    }
                    childReader.readToEnd();
                }
            } else {
                childReader.readToEnd();
            }
        }
        return object;
    } catch (IllegalAccessException e) {
        throw new DatabindingException("Illegal access. " + e.getMessage(), e);
    } catch (InstantiationException e) {
        throw new DatabindingException("Couldn't instantiate class. " + e.getMessage(), e);
    } catch (SecurityException e) {
        throw new DatabindingException("Illegal access. " + e.getMessage(), e);
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument. " + e.getMessage(), e);
    } catch (InvocationTargetException e) {
        throw new DatabindingException("Could not create class: " + e.getMessage(), e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) MessageReader(org.apache.cxf.aegis.xml.MessageReader) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) DatabindingException(org.apache.cxf.aegis.DatabindingException)

Example 29 with DatabindingException

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

the class BeanTypeInfo method initializeProperties.

private void initializeProperties() {
    BeanInfo beanInfo = null;
    try {
        if (beanClass.isInterface() || beanClass.isPrimitive()) {
            descriptors = getInterfacePropertyDescriptors(beanClass);
        } else if (beanClass.isEnum()) {
        // do nothing
        } else if (beanClass == Object.class || beanClass == Throwable.class) {
        // do nothing
        } else if (RuntimeException.class.isAssignableFrom(beanClass)) {
            beanInfo = Introspector.getBeanInfo(beanClass, RuntimeException.class);
        } else if (Exception.class.isAssignableFrom(beanClass) && !Exception.class.equals(beanClass)) {
            beanInfo = Introspector.getBeanInfo(beanClass, Exception.class);
        } else if (Throwable.class.isAssignableFrom(beanClass)) {
            beanInfo = Introspector.getBeanInfo(beanClass, Throwable.class);
        } else {
            beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
        }
    } catch (IntrospectionException e) {
        throw new DatabindingException("Couldn't introspect interface.", e);
    }
    if (beanInfo != null) {
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        if (propertyDescriptors != null) {
            // see comments on this function.
            descriptors = ReflectionUtil.getPropertyDescriptorsAvoidSunBug(getClass(), beanInfo, beanClass, propertyDescriptors);
        }
    }
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    Arrays.sort(descriptors, new Comparator<PropertyDescriptor>() {

        public int compare(PropertyDescriptor o1, PropertyDescriptor o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) IntrospectionException(java.beans.IntrospectionException) DatabindingException(org.apache.cxf.aegis.DatabindingException)

Example 30 with DatabindingException

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

the class ObjectType method readObject.

@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    if (isNil(reader.getAttributeReader(XSI_NIL))) {
        while (reader.hasMoreElementReaders()) {
            reader.getNextElementReader();
        }
        return null;
    }
    MessageReader typeReader = reader.getAttributeReader(XSI_TYPE);
    if (null == typeReader && !readToDocument) {
        throw new DatabindingException("Missing 'xsi:type' attribute");
    }
    String typeName = null;
    if (typeReader != null) {
        typeName = typeReader.getValue();
    }
    if (null == typeName && !readToDocument) {
        throw new DatabindingException("Missing 'xsi:type' attribute value");
    }
    AegisType type = null;
    QName typeQName = null;
    if (typeName != null) {
        typeName = typeName.trim();
        typeQName = extractQName(reader, typeName);
    } else {
        typeQName = reader.getName();
    }
    TypeMapping tm = context.getTypeMapping();
    if (tm == null) {
        tm = getTypeMapping();
    }
    type = tm.getType(typeQName);
    if (type == null) {
        type = tm.getType(getSchemaType());
    }
    if (type == this) {
        throw new DatabindingException("Could not determine how to read type: " + typeQName);
    }
    if (type == null && readToDocument) {
        type = getTypeMapping().getType(Document.class);
    }
    if (null == type) {
        throw new DatabindingException("No mapped type for '" + typeName + "' (" + typeQName + ")");
    }
    return type.readObject(reader, context);
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) TypeMapping(org.apache.cxf.aegis.type.TypeMapping) MessageReader(org.apache.cxf.aegis.xml.MessageReader) Document(org.w3c.dom.Document)

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