Search in sources :

Example 6 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project cxf by apache.

the class JAXBSchemaInitializer method buildGenericSeq.

private void buildGenericSeq(XmlSchema schema, XmlSchemaComplexType generics, Class<?> genericsClass) {
    XmlSchemaSequence genericsSeq = new XmlSchemaSequence();
    generics.setParticle(genericsSeq);
    XmlAccessType accessType = Utils.getXmlAccessType(genericsClass);
    for (Field f : Utils.getFields(genericsClass, accessType)) {
        if (f.getGenericType() instanceof TypeVariable) {
            String genericName = Character.toLowerCase(f.getName().charAt(0)) + f.getName().substring(1);
            XmlSchemaElement genericEle = new XmlSchemaElement(schema, false);
            genericEle.setName(genericName);
            genericEle.setMinOccurs(0);
            JAXBBeanInfo anyBean = getBeanInfo(context, f.getType());
            Iterator<QName> itr = anyBean.getTypeNames().iterator();
            if (!itr.hasNext()) {
                return;
            }
            QName typeName = itr.next();
            genericEle.setSchemaTypeName(typeName);
            genericsSeq.getItems().add(genericEle);
        }
    }
    for (Method genericMethod : Utils.getGetters(genericsClass, accessType)) {
        if (genericMethod.getGenericReturnType() instanceof TypeVariable) {
            int idx = genericMethod.getName().startsWith("get") ? 3 : 2;
            String genericName = genericMethod.getName().substring(idx);
            genericName = Character.toLowerCase(genericName.charAt(0)) + genericName.substring(1);
            XmlSchemaElement genericEle = new XmlSchemaElement(schema, false);
            genericEle.setName(genericName);
            genericEle.setMinOccurs(0);
            JAXBBeanInfo anyBean = getBeanInfo(context, genericMethod.getReturnType());
            Iterator<QName> itr = anyBean.getTypeNames().iterator();
            if (!itr.hasNext()) {
                return;
            }
            QName typeName = itr.next();
            genericEle.setSchemaTypeName(typeName);
            genericsSeq.getItems().add(genericEle);
        }
    }
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) Field(java.lang.reflect.Field) TypeVariable(java.lang.reflect.TypeVariable) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) Method(java.lang.reflect.Method) XmlAccessType(javax.xml.bind.annotation.XmlAccessType)

Example 7 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project cxf by apache.

the class JAXBSchemaInitializer method end.

public void end(FaultInfo fault) {
    MessagePartInfo part = fault.getFirstMessagePart();
    Class<?> cls = part.getTypeClass();
    Class<?> cl2 = (Class<?>) fault.getProperty(Class.class.getName());
    if (cls != cl2) {
        QName name = (QName) fault.getProperty("elementName");
        part.setElementQName(name);
        JAXBBeanInfo beanInfo = getBeanInfo(cls);
        if (beanInfo == null) {
            throw new Fault(new Message("NO_BEAN_INFO", LOG, cls.getName()));
        }
        SchemaInfo schemaInfo = serviceInfo.getSchema(part.getElementQName().getNamespaceURI());
        if (schemaInfo != null && !isExistSchemaElement(schemaInfo.getSchema(), part.getElementQName())) {
            XmlSchemaElement el = new XmlSchemaElement(schemaInfo.getSchema(), true);
            el.setName(part.getElementQName().getLocalPart());
            el.setNillable(true);
            schemaInfo.setElement(null);
            Iterator<QName> itr = beanInfo.getTypeNames().iterator();
            if (!itr.hasNext()) {
                return;
            }
            QName typeName = itr.next();
            el.setSchemaTypeName(typeName);
        }
    } else if (part.getXmlSchema() == null) {
        try {
            cls.getConstructor(new Class[] { String.class });
        } catch (Exception e) {
            try {
                cls.getConstructor(new Class[0]);
            } catch (Exception e2) {
                // no String or default constructor, we cannot use it
                return;
            }
        }
        // not mappable in JAXBContext directly, we'll have to do it manually :-(
        SchemaInfo schemaInfo = serviceInfo.getSchema(part.getElementQName().getNamespaceURI());
        if (schemaInfo == null || isExistSchemaElement(schemaInfo.getSchema(), part.getElementQName())) {
            return;
        }
        XmlSchemaElement el = new XmlSchemaElement(schemaInfo.getSchema(), true);
        el.setName(part.getElementQName().getLocalPart());
        schemaInfo.setElement(null);
        part.setXmlSchema(el);
        XmlSchemaComplexType ct = new XmlSchemaComplexType(schemaInfo.getSchema(), false);
        el.setSchemaType(ct);
        XmlSchemaSequence seq = new XmlSchemaSequence();
        ct.setParticle(seq);
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            if (m.getName().startsWith("get") || m.getName().startsWith("is")) {
                int beginIdx = m.getName().startsWith("get") ? 3 : 2;
                try {
                    m.getDeclaringClass().getMethod("set" + m.getName().substring(beginIdx), m.getReturnType());
                    JAXBBeanInfo beanInfo = getBeanInfo(m.getReturnType());
                    if (beanInfo != null) {
                        el = new XmlSchemaElement(schemaInfo.getSchema(), false);
                        el.setName(m.getName().substring(beginIdx));
                        Iterator<QName> itr = beanInfo.getTypeNames().iterator();
                        if (!itr.hasNext()) {
                            return;
                        }
                        QName typeName = itr.next();
                        el.setSchemaTypeName(typeName);
                    }
                    seq.getItems().add(el);
                } catch (Exception e) {
                // not mappable
                }
            }
        }
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) Iterator(java.util.Iterator) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 8 with JAXBBeanInfo

use of org.apache.cxf.common.jaxb.JAXBBeanInfo in project cxf by apache.

the class JAXBSchemaInitializer method begin.

@Override
public void begin(MessagePartInfo part) {
    // Check to see if the WSDL information has been filled in for us.
    if (part.getTypeQName() != null || part.getElementQName() != null) {
        checkForExistence(part);
        return;
    }
    Class<?> clazz = part.getTypeClass();
    if (clazz == null) {
        return;
    }
    boolean isFromWrapper = part.getMessageInfo().getOperation().isUnwrapped();
    boolean isList = false;
    if (clazz.isArray()) {
        if (isFromWrapper && !Byte.TYPE.equals(clazz.getComponentType())) {
            clazz = clazz.getComponentType();
        } else if (!isFromWrapper) {
            Annotation[] anns = (Annotation[]) part.getProperty("parameter.annotations");
            for (Annotation a : anns) {
                if (a instanceof XmlList) {
                    part.setProperty("honor.jaxb.annotations", Boolean.TRUE);
                    clazz = clazz.getComponentType();
                    isList = true;
                }
            }
        }
    }
    Annotation[] anns = (Annotation[]) part.getProperty("parameter.annotations");
    XmlJavaTypeAdapter jta = findFromTypeAdapter(context, clazz, anns);
    JAXBBeanInfo jtaBeanInfo = null;
    if (jta != null) {
        jtaBeanInfo = findFromTypeAdapter(context, jta.value());
    }
    JAXBBeanInfo beanInfo = getBeanInfo(clazz);
    if (jtaBeanInfo != beanInfo && jta != null) {
        beanInfo = jtaBeanInfo;
        if (anns == null) {
            anns = new Annotation[] { jta };
        } else {
            boolean found = false;
            for (Annotation t : anns) {
                if (t == jta) {
                    found = true;
                }
            }
            if (!found) {
                Annotation[] tmp = new Annotation[anns.length + 1];
                System.arraycopy(anns, 0, tmp, 0, anns.length);
                tmp[anns.length] = jta;
                anns = tmp;
            }
        }
        part.setProperty("parameter.annotations", anns);
        part.setProperty("honor.jaxb.annotations", Boolean.TRUE);
    }
    if (beanInfo == null) {
        if (Exception.class.isAssignableFrom(clazz)) {
            QName name = (QName) part.getMessageInfo().getProperty("elementName");
            part.setElementQName(name);
            buildExceptionType(part, clazz);
        }
        return;
    }
    boolean isElement = beanInfo.isElement() && !Boolean.TRUE.equals(part.getMessageInfo().getOperation().getProperty("operation.force.types"));
    boolean hasType = !beanInfo.getTypeNames().isEmpty();
    if (isElement && isFromWrapper && hasType) {
        // if there is both a Global element and a global type, AND we are in a wrapper,
        // make sure we use the type instead of a ref to the element to
        // match the rules for wrapped/unwrapped
        isElement = false;
    }
    part.setElement(isElement);
    if (isElement) {
        QName name = new QName(beanInfo.getElementNamespaceURI(null), beanInfo.getElementLocalName(null));
        XmlSchemaElement el = schemas.getElementByQName(name);
        if (el != null && el.getRef().getTarget() != null) {
            part.setTypeQName(el.getRef().getTargetQName());
        } else {
            part.setElementQName(name);
        }
        part.setXmlSchema(el);
    } else {
        QName typeName = getTypeName(beanInfo);
        if (typeName != null) {
            XmlSchemaType type = schemas.getTypeByQName(typeName);
            if (isList && type instanceof XmlSchemaSimpleType) {
                XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(type.getParent(), false);
                XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType stype = (XmlSchemaSimpleType) type;
                list.setItemTypeName(stype.getQName());
                simpleType.setContent(list);
                part.setXmlSchema(simpleType);
                if (part.getConcreteName() == null) {
                    part.setConcreteName(new QName(null, part.getName().getLocalPart()));
                }
            } else {
                part.setTypeQName(typeName);
                part.setXmlSchema(type);
            }
        }
    }
}
Also used : XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) Annotation(java.lang.annotation.Annotation) XmlList(javax.xml.bind.annotation.XmlList)

Aggregations

JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)8 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)6 QName (javax.xml.namespace.QName)5 Method (java.lang.reflect.Method)4 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)2 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)2 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)2 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)2 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)2 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)2 IOException (java.io.IOException)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 Iterator (java.util.Iterator)1 XmlAccessorOrder (javax.xml.bind.annotation.XmlAccessorOrder)1