Search in sources :

Example 11 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class BeanType method getDependencies.

/**
 * {@inheritDoc}
 */
@Override
public Set<AegisType> getDependencies() {
    Set<AegisType> deps = new HashSet<>();
    BeanTypeInfo inf = getTypeInfo();
    for (QName name : inf.getAttributes()) {
        if (isInheritedProperty(inf, name)) {
            continue;
        }
        deps.add(inf.getType(name));
    }
    for (QName name : inf.getElements()) {
        if (isInheritedProperty(inf, name)) {
            continue;
        }
        deps.add(inf.getType(name));
    }
    /*
         * Automagically add chain of superclasses if this is an an extension.
         */
    AegisType sooperType = getSuperType();
    if (sooperType != null) {
        deps.add(sooperType);
    }
    return deps;
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) HashSet(java.util.HashSet)

Example 12 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class BeanType method writeSchema.

@Override
public void writeSchema(XmlSchema root) {
    BeanTypeInfo inf = getTypeInfo();
    XmlSchemaComplexType complex = new XmlSchemaComplexType(root, true);
    complex.setName(getSchemaType().getLocalPart());
    AegisType sooperType = getSuperType();
    /*
         * See Java Virtual Machine specification:
         * http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734
         */
    if (((inf.getTypeClass().getModifiers() & Modifier.ABSTRACT) != 0) && !inf.getTypeClass().isInterface()) {
        complex.setAbstract(true);
    }
    XmlSchemaSequence sequence = new XmlSchemaSequence();
    /*
         * Decide if we're going to extend another type. If we are going to defer, then make sure that we
         * extend the type for our superclass.
         */
    boolean isExtension = inf.isExtension();
    if (isExtension && sooperType != null) {
        // if sooperType is null, things are confused.
        XmlSchemaComplexContent content = new XmlSchemaComplexContent();
        complex.setContentModel(content);
        XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
        content.setContent(extension);
        extension.setBaseTypeName(sooperType.getSchemaType());
        extension.setParticle(sequence);
    } else {
        complex.setParticle(sequence);
    }
    boolean needXmime = false;
    boolean needUtilityTypes = false;
    // Write out schema for elements
    for (QName name : inf.getElements()) {
        if (isInheritedProperty(inf, name)) {
            continue;
        }
        XmlSchemaElement element = new XmlSchemaElement(root, false);
        element.setName(name.getLocalPart());
        sequence.getItems().add(element);
        AegisType type = getType(inf, name);
        if (type.isFlatArray()) {
            // ok, we need some tricks here
            element.setMinOccurs(type.getMinOccurs());
            element.setMaxOccurs(type.getMaxOccurs());
            // for now, assume ArrayType. Look at lists or more general solutions later.
            ArrayType aType = (ArrayType) type;
            type = aType.getComponentType();
            element.setNillable(type.isNillable());
        } else {
            if (AbstractTypeCreator.HTTP_CXF_APACHE_ORG_ARRAYS.equals(type.getSchemaType().getNamespaceURI())) {
                XmlSchemaUtils.addImportIfNeeded(root, AbstractTypeCreator.HTTP_CXF_APACHE_ORG_ARRAYS);
            }
        }
        writeTypeReference(name, element, type, root);
        needXmime |= type.usesXmime();
        needUtilityTypes |= type.usesUtilityTypes();
    }
    if (needXmime) {
        addXmimeToSchema(root);
    }
    if (needUtilityTypes) {
        AegisContext.addUtilityTypesToSchema(root);
    }
    /**
     * if future proof then add <xsd:any/> element
     */
    if (inf.isExtensibleElements()) {
        XmlSchemaAny any = new XmlSchemaAny();
        any.setMinOccurs(0);
        any.setMaxOccurs(Long.MAX_VALUE);
        sequence.getItems().add(any);
    }
    // Write out schema for attributes
    for (QName name : inf.getAttributes()) {
        if (isInheritedProperty(inf, name)) {
            continue;
        }
        XmlSchemaAttribute attribute = new XmlSchemaAttribute(root, false);
        complex.getAttributes().add(attribute);
        attribute.setName(name.getLocalPart());
        AegisType type = getType(inf, name);
        attribute.setSchemaTypeName(type.getSchemaType());
        String ns = name.getNamespaceURI();
        if (!ns.equals(root.getTargetNamespace())) {
            XmlSchemaUtils.addImportIfNeeded(root, ns);
        }
    }
    /**
     * If extensible attributes then add <xsd:anyAttribute/>
     */
    if (inf.isExtensibleAttributes()) {
        complex.setAnyAttribute(new XmlSchemaAnyAttribute());
    }
}
Also used : XmlSchemaComplexContentExtension(org.apache.ws.commons.schema.XmlSchemaComplexContentExtension) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaAny(org.apache.ws.commons.schema.XmlSchemaAny) XmlSchemaComplexContent(org.apache.ws.commons.schema.XmlSchemaComplexContent) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaAnyAttribute(org.apache.ws.commons.schema.XmlSchemaAnyAttribute)

Example 13 with AegisType

use of org.apache.cxf.aegis.type.AegisType 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 14 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class BeanType method getElementType.

protected AegisType getElementType(QName name, BeanTypeInfo beanTypeInfo, MessageReader reader, Context context) {
    AegisType type = beanTypeInfo.getType(name);
    // AegisType can be overriden with a xsi:type attribute
    type = TypeUtil.getReadType(reader.getXMLStreamReader(), context.getGlobalContext(), type);
    return type;
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType)

Example 15 with AegisType

use of org.apache.cxf.aegis.type.AegisType 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 (DatabindingException e) {
            throw e;
        } catch (Exception 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)

Aggregations

AegisType (org.apache.cxf.aegis.type.AegisType)97 QName (javax.xml.namespace.QName)43 Test (org.junit.Test)40 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)35 DatabindingException (org.apache.cxf.aegis.DatabindingException)18 AegisContext (org.apache.cxf.aegis.AegisContext)16 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)11 Element (org.w3c.dom.Element)11 Context (org.apache.cxf.aegis.Context)10 BeanType (org.apache.cxf.aegis.type.basic.BeanType)10 Method (java.lang.reflect.Method)9 MessageReader (org.apache.cxf.aegis.xml.MessageReader)9 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)8 BeanTypeInfo (org.apache.cxf.aegis.type.basic.BeanTypeInfo)8 CollectionType (org.apache.cxf.aegis.type.collection.CollectionType)8 HashSet (java.util.HashSet)7 MapType (org.apache.cxf.aegis.type.collection.MapType)7 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)7 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5