Search in sources :

Example 1 with TypeCreator

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

the class AegisDatabinding method getParameterType.

private AegisType getParameterType(Service s, TypeMapping tm, MessagePartInfo param, int paramtype) {
    AegisType type = tm.getType(param.getTypeQName());
    if (type != null && type.getTypeClass() != param.getTypeClass()) {
        type = null;
    }
    int offset = 0;
    if (paramtype == OUT_PARAM) {
        offset = 1;
    }
    TypeCreator typeCreator = tm.getTypeCreator();
    if (type == null) {
        // Current author doesn't know how type can be non-null here.
        boolean usingComponentType = false;
        OperationInfo op = param.getMessageInfo().getOperation();
        Method m = getMethod(s, op);
        TypeClassInfo info;
        if (paramtype != FAULT_PARAM && m != null) {
            info = typeCreator.createClassInfo(m, param.getIndex() - offset);
        } else {
            info = typeCreator.createBasicClassInfo(param.getTypeClass());
        }
        Boolean nillable = info.getNillable();
        /*
             * Note that, for types from the mapping, the minOccurs, maxOccurs, and nillable from the 'info'
             * will be ignored by createTypeForClass below. So we need to override.
             */
        type = typeCreator.createTypeForClass(info);
        // if not writing outer, we don't need anything special.
        if (param.getMessageInfo().getOperation().isUnwrapped() && param.getTypeClass().isArray() && type.isWriteOuter()) {
            /*
                 * The service factory expects arrays going into the wrapper to be mapped to the array
                 * component type and will then add min=0/max=unbounded. That doesn't work for Aegis where we
                 * already created a wrapper ArrayType so we'll let it know we want the default.
                 */
            param.setProperty("minOccurs", "1");
            param.setProperty("maxOccurs", "1");
            if (nillable == null) {
                nillable = Boolean.TRUE;
            }
            param.setProperty("nillable", nillable);
        } else {
            if (nillable != null) {
                param.setProperty("nillable", nillable);
            }
            /*
                 * TypeClassInfo uses -1 to mean 'not specified'
                 */
            if (info.getMinOccurs() != -1) {
                param.setProperty("minOccurs", Long.toString(info.getMinOccurs()));
            }
            if (info.getMaxOccurs() != -1) {
                param.setProperty("maxOccurs", Long.toString(info.getMaxOccurs()));
            }
            if ((type instanceof ArrayType) && !type.isWriteOuter()) {
                param.setProperty("org.apache.cxf.aegis.outerType", type);
                ArrayType aType = (ArrayType) type;
                type = aType.getComponentType();
                usingComponentType = true;
            }
        }
        if (info.getMappedName() != null) {
            param.setConcreteName(info.getMappedName());
            param.setName(info.getMappedName());
        }
        if (!usingComponentType) {
            // param setting above?
            if (info.nonDefaultAttributes()) {
                tm.register(type);
            }
            type.setTypeMapping(tm);
        }
        part2Type.put(param, type);
    }
    return type;
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) ArrayType(org.apache.cxf.aegis.type.basic.ArrayType) AegisType(org.apache.cxf.aegis.type.AegisType) TypeClassInfo(org.apache.cxf.aegis.type.TypeClassInfo) Method(java.lang.reflect.Method) TypeCreator(org.apache.cxf.aegis.type.TypeCreator)

Example 2 with TypeCreator

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

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

the class NoNamespaceAegisElementProvider method getAegisContext.

@Override
protected AegisContext getAegisContext(Class<?> plainClass, Type genericType) {
    AegisContext context = new AegisContext();
    context.setWriteXsiTypes(writeXsiType);
    context.setReadXsiTypes(readXsiType);
    TypeCreationOptions tco = new TypeCreationOptions();
    tco.setQualifyElements(false);
    Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
    rootClasses.add(genericType);
    context.setTypeCreationOptions(tco);
    context.setRootClasses(rootClasses);
    TypeMapping baseMapping = DefaultTypeMapping.createSoap11TypeMapping(true, false, false);
    DefaultTypeMapping mapping = new DefaultTypeMapping(Constants.URI_2001_SCHEMA_XSD, baseMapping);
    TypeCreator stockTypeCreator = createTypeCreator(tco);
    mapping.setTypeCreator(stockTypeCreator);
    context.setTypeMapping(mapping);
    context.initialize();
    return context;
}
Also used : Type(java.lang.reflect.Type) DefaultTypeMapping(org.apache.cxf.aegis.type.DefaultTypeMapping) AegisContext(org.apache.cxf.aegis.AegisContext) DefaultTypeMapping(org.apache.cxf.aegis.type.DefaultTypeMapping) TypeMapping(org.apache.cxf.aegis.type.TypeMapping) TypeCreationOptions(org.apache.cxf.aegis.type.TypeCreationOptions) XMLTypeCreator(org.apache.cxf.aegis.type.XMLTypeCreator) TypeCreator(org.apache.cxf.aegis.type.TypeCreator) AbstractTypeCreator(org.apache.cxf.aegis.type.AbstractTypeCreator) Java5TypeCreator(org.apache.cxf.aegis.type.java5.Java5TypeCreator) HashSet(java.util.HashSet)

Example 4 with TypeCreator

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

the class AegisXMLStreamDataWriter method write.

/**
 * {@inheritDoc}
 */
public void write(Object obj, QName elementName, boolean optional, XMLStreamWriter output, java.lang.reflect.Type objectType) throws Exception {
    AegisType aegisType = null;
    if (objectType instanceof Class) {
        aegisType = getContext().getTypeMapping().getType(objectType);
    }
    if (aegisType == null) {
        TypeCreator creator = getContext().getTypeMapping().getTypeCreator();
        aegisType = creator.createType(objectType);
    }
    if (aegisType == null) {
        Message message = new Message("NO_MAPPING_FOR_TYPE", LOG, objectType);
        throw new DatabindingException(message);
    }
    write(obj, elementName, optional, output, aegisType);
}
Also used : Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) TypeCreator(org.apache.cxf.aegis.type.TypeCreator)

Example 5 with TypeCreator

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

the class AegisXMLStreamDataWriter method write.

/**
 * Write an object to the output. This method always writes xsi:type attributes.
 * @param obj The object to write.
 * @param elementName the QName of the XML Element.
 * @param optional set this for minOccurs = 0. It omits null elements.
 * @param output the output stream
 * @param aegisType the aegis type. This may be null if the object is non-null
 * and the type of the object is covered in the mapping. Warning, for collections
 * this will not do what you want, you must call the alternative version of
 * write that takes a {@link java.lang.reflect.Type}.
 * @throws Exception
 */
public void write(Object obj, QName elementName, boolean optional, XMLStreamWriter output, AegisType aegisType) throws Exception {
    if (obj == null && aegisType == null && !optional) {
        Message message = new Message("WRITE_NULL_NEEDS_TYPE", LOG);
        throw new DatabindingException(message);
    }
    if (obj != null && aegisType == null) {
        aegisType = getContext().getTypeMapping().getType(obj.getClass());
        if (aegisType == null) {
            TypeCreator creator = getContext().getTypeMapping().getTypeCreator();
            aegisType = creator.createType(obj.getClass());
        }
    }
    if (obj != null) {
        // look for overrides declared in the context.
        aegisType = TypeUtil.getWriteType(aegisContext, obj, aegisType);
    }
    if (aegisType == null) {
        Message message = new Message("WRITE_NEEDS_TYPE", LOG, obj);
        throw new DatabindingException(message);
    }
    if (obj == null) {
        if (optional) {
            // minOccurs = 0
            return;
        }
        if (aegisType.isNillable() && aegisType.isWriteOuter()) {
            ElementWriter writer = new ElementWriter(output);
            MessageWriter w2 = writer.getElementWriter(elementName);
            w2.writeXsiNil();
            w2.close();
            return;
        }
    }
    ElementWriter writer = new ElementWriter(output);
    MessageWriter w2 = writer.getElementWriter(elementName);
    if (getContext().isWriteXsiTypes() && aegisType.getSchemaType() != null) {
        // if we know the type, write it. We are standalone, and the reader needs it.
        w2.writeXsiType(aegisType.getSchemaType());
    }
    aegisType.writeObject(obj, w2, context);
    w2.close();
}
Also used : Message(org.apache.cxf.common.i18n.Message) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter) TypeCreator(org.apache.cxf.aegis.type.TypeCreator)

Aggregations

TypeCreator (org.apache.cxf.aegis.type.TypeCreator)5 AegisType (org.apache.cxf.aegis.type.AegisType)3 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)2 Message (org.apache.cxf.common.i18n.Message)2 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 QName (javax.xml.namespace.QName)1 AegisContext (org.apache.cxf.aegis.AegisContext)1 DatabindingException (org.apache.cxf.aegis.DatabindingException)1 AbstractTypeCreator (org.apache.cxf.aegis.type.AbstractTypeCreator)1 DefaultTypeMapping (org.apache.cxf.aegis.type.DefaultTypeMapping)1 TypeClassInfo (org.apache.cxf.aegis.type.TypeClassInfo)1 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)1 XMLTypeCreator (org.apache.cxf.aegis.type.XMLTypeCreator)1 ArrayType (org.apache.cxf.aegis.type.basic.ArrayType)1 Java5TypeCreator (org.apache.cxf.aegis.type.java5.Java5TypeCreator)1 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)1