Search in sources :

Example 11 with DatabindingException

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

the class BeanTypeInfo method getType.

/**
 * Get the type class for the field with the specified QName.
 */
public AegisType getType(QName name) {
    // 1. Try a prexisting mapped type
    AegisType type = mappedName2type.get(name);
    // 2. Try to get the type by its name, if there is one
    if (type == null) {
        QName typeName = getMappedTypeName(name);
        if (typeName != null) {
            type = getTypeMapping().getType(typeName);
            if (type != null) {
                mapType(name, type);
            }
        }
    }
    // 3. Create the type from the property descriptor and map it
    if (type == null) {
        PropertyDescriptor desc;
        try {
            desc = getPropertyDescriptorFromMappedName(name);
        } catch (Exception e) {
            if (e instanceof DatabindingException) {
                throw (DatabindingException) e;
            }
            throw new DatabindingException("Couldn't get properties.", e);
        }
        if (desc == null) {
            return null;
        }
        try {
            TypeMapping tm = getTypeMapping();
            TypeCreator tc = tm.getTypeCreator();
            type = tc.createType(desc);
        } catch (DatabindingException e) {
            e.prepend("Couldn't create type for property " + desc.getName() + " on " + getTypeClass());
            throw e;
        }
        // second part is possible workaround for XFIRE-586
        if (registerType(desc)) {
            getTypeMapping().register(type);
        }
        mapType(name, type);
    }
    if (type == null) {
        throw new DatabindingException("Couldn't find type for property " + name);
    }
    return type;
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) PropertyDescriptor(java.beans.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) TypeMapping(org.apache.cxf.aegis.type.TypeMapping) TypeCreator(org.apache.cxf.aegis.type.TypeCreator) IntrospectionException(java.beans.IntrospectionException) DatabindingException(org.apache.cxf.aegis.DatabindingException)

Example 12 with DatabindingException

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

the class XMLBeanTypeInfo method mapProperty.

@Override
protected void mapProperty(PropertyDescriptor pd) {
    Element e = getPropertyElement(pd.getName());
    String style = null;
    QName mappedName = null;
    if (e != null) {
        String ignore = DOMUtils.getAttributeValueEmptyNull(e, "ignore");
        if ("true".equals(ignore)) {
            return;
        }
        LOG.finest("Found mapping for property " + pd.getName());
        style = DOMUtils.getAttributeValueEmptyNull(e, "style");
    }
    if (style == null) {
        style = "element";
    }
    boolean element = "element".equals(style);
    boolean qualify;
    if (element) {
        qualify = isQualifyElements();
    } else {
        qualify = isQualifyAttributes();
    }
    String namespace = null;
    if (qualify) {
        namespace = getDefaultNamespace();
    }
    if (e != null) {
        mappedName = NamespaceHelper.createQName(e, DOMUtils.getAttributeValueEmptyNull(e, "mappedName"), namespace);
    }
    if (mappedName == null) {
        mappedName = createMappedName(pd, qualify);
    }
    if (e != null) {
        QName mappedType = NamespaceHelper.createQName(e, DOMUtils.getAttributeValueEmptyNull(e, "typeName"), getDefaultNamespace());
        if (mappedType != null) {
            mapTypeName(mappedName, mappedType);
        }
        /*
             * Whenever we create a type object, it has to have a schema type. If we created a custom type
             * object out of thin air here, we've may have a problem. If "typeName" was specified, then then
             * we know the mapping. But if mappedName was not specified, then the typeName will come from the
             * type mapping, so we have to ask it. And if some other type creator has something to say about
             * it, we'll get it wrong.
             */
        String explicitTypeName = DOMUtils.getAttributeValueEmptyNull(e, "type");
        if (explicitTypeName != null) {
            try {
                Class<?> typeClass = ClassLoaderUtils.loadClass(explicitTypeName, XMLBeanTypeInfo.class);
                AegisType customTypeObject = (AegisType) typeClass.newInstance();
                mapType(mappedName, customTypeObject);
                QName schemaType = mappedType;
                if (schemaType == null) {
                    schemaType = getTypeMapping().getTypeQName(pd.getPropertyType());
                }
                customTypeObject.setTypeClass(typeClass);
                customTypeObject.setSchemaType(schemaType);
            } catch (ClassNotFoundException e1) {
            // 
            } catch (InstantiationException e2) {
            // 
            } catch (IllegalAccessException e3) {
            // 
            }
        }
        String nillableVal = DOMUtils.getAttributeValueEmptyNull(e, "nillable");
        if (nillableVal != null && nillableVal.length() > 0) {
            ensurePropertyInfo(mappedName).setNillable(Boolean.valueOf(nillableVal).booleanValue());
        }
        String minOccurs = DOMUtils.getAttributeValueEmptyNull(e, "minOccurs");
        if (minOccurs != null && minOccurs.length() > 0) {
            ensurePropertyInfo(mappedName).setMinOccurs(Integer.parseInt(minOccurs));
        }
        String maxOccurs = DOMUtils.getAttributeValueEmptyNull(e, "maxOccurs");
        if (maxOccurs != null && maxOccurs.length() > 0) {
            ensurePropertyInfo(mappedName).setMinOccurs(Integer.parseInt(maxOccurs));
        }
    }
    try {
        // name " + mappedName);
        if ("element".equals(style)) {
            mapElement(pd.getName(), mappedName);
        } else if ("attribute".equals(style)) {
            mapAttribute(pd.getName(), mappedName);
        } else {
            throw new DatabindingException("Invalid style: " + style);
        }
    } catch (DatabindingException ex) {
        ex.prepend("Couldn't create type for property " + pd.getName() + " on " + getTypeClass());
        throw ex;
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element)

Example 13 with DatabindingException

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

the class CollectionType method createCollection.

@Override
@SuppressWarnings("unchecked")
protected Collection<Object> createCollection() {
    Collection<Object> values = null;
    /*
         * getTypeClass returns the type of the object. These 'if's asked if the proposed
         * type can be assigned to the object, not the other way around. Thus List before
         * Vector and Set before SortedSet.
         */
    Class<?> userTypeClass = getTypeClass();
    if (userTypeClass.isAssignableFrom(List.class)) {
        values = new ArrayList<>();
    } else if (userTypeClass.isAssignableFrom(LinkedList.class)) {
        values = new LinkedList<Object>();
    } else if (userTypeClass.isAssignableFrom(Set.class)) {
        values = new HashSet<>();
    } else if (userTypeClass.isAssignableFrom(SortedSet.class)) {
        values = new TreeSet<Object>();
    } else if (userTypeClass.isAssignableFrom(Vector.class)) {
        values = new Vector<Object>();
    } else if (userTypeClass.isAssignableFrom(Stack.class)) {
        values = new Stack<Object>();
    } else if (userTypeClass.isInterface()) {
        values = new ArrayList<>();
    } else {
        try {
            values = (Collection<Object>) userTypeClass.newInstance();
        } catch (Exception e) {
            throw new DatabindingException("Could not create map implementation: " + userTypeClass.getName(), e);
        }
    }
    return values;
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) LinkedList(java.util.LinkedList) DatabindingException(org.apache.cxf.aegis.DatabindingException) Stack(java.util.Stack)

Example 14 with DatabindingException

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

the class CollectionType method writeObject.

@Override
public void writeObject(Object object, MessageWriter writer, Context context) throws DatabindingException {
    if (object == null) {
        return;
    }
    try {
        Collection<?> list = (Collection<?>) object;
        AegisType type = getComponentType();
        if (type == null) {
            throw new DatabindingException("Couldn't find component type for Collection.");
        }
        for (Iterator<?> itr = list.iterator(); itr.hasNext(); ) {
            String ns = null;
            if (type.isAbstract()) {
                ns = getSchemaType().getNamespaceURI();
            } else {
                ns = type.getSchemaType().getNamespaceURI();
            }
            writeValue(itr.next(), writer, context, type, type.getSchemaType().getLocalPart(), ns);
        }
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) Collection(java.util.Collection)

Example 15 with DatabindingException

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

the class BadXMLTest method testBadDescriptorNS.

@Test
public void testBadDescriptorNS() throws Exception {
    AegisContext context = new AegisContext();
    context.initialize();
    mapping = context.getTypeMapping();
    try {
        mapping.getTypeCreator().createType(BadBeanDescriptor.class);
        Assert.fail("No exception was thrown");
    } catch (DatabindingException e) {
    // this is supposed to happen
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisContext(org.apache.cxf.aegis.AegisContext) Test(org.junit.Test)

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