Search in sources :

Example 21 with XmlType

use of javax.xml.bind.annotation.XmlType in project cxf by apache.

the class SchemaCollectionContextProxy method getTypeQName.

private QName getTypeQName(Class<?> cls, String namespace) {
    QName qn = TYPE_MAP.get(cls);
    if (qn != null) {
        return qn;
    }
    XmlType xtype = cls.getAnnotation(XmlType.class);
    String tn = xtype == null ? "##default" : xtype.name();
    String tns = xtype == null ? "##default" : xtype.namespace();
    if ("##default".equals(tn)) {
        tn = java.beans.Introspector.decapitalize(cls.getSimpleName());
    }
    if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
        tns = JAXBUtils.getPackageNamespace(cls);
    }
    if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
        tns = namespace;
    }
    return new QName(tns, tn);
}
Also used : QName(javax.xml.namespace.QName) XmlType(javax.xml.bind.annotation.XmlType)

Example 22 with XmlType

use of javax.xml.bind.annotation.XmlType in project camel by apache.

the class TypeNameStrategy method findQNameForSoapActionOrType.

/**
     * @return determine element name by using the XmlType.name() of the type to
     *         be marshalled and the XmlSchema.namespace() of the package-info
     */
public QName findQNameForSoapActionOrType(String soapAction, Class<?> type) {
    XmlType xmlType = type.getAnnotation(XmlType.class);
    if (xmlType == null || xmlType.name() == null) {
        throw new RuntimeException("The type " + type.getName() + " needs to have an XmlType annotation with name");
    }
    String nameSpace = xmlType.namespace();
    if ("##default".equals(nameSpace)) {
        XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
        if (xmlSchema != null) {
            nameSpace = xmlSchema.namespace();
        }
    }
    // prefer name from the XmlType, and fallback to XmlRootElement
    String localName = xmlType.name();
    if (ObjectHelper.isEmpty(localName)) {
        XmlRootElement root = type.getAnnotation(XmlRootElement.class);
        if (root != null) {
            localName = root.name();
        }
    }
    return new QName(nameSpace, localName);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName) XmlType(javax.xml.bind.annotation.XmlType)

Example 23 with XmlType

use of javax.xml.bind.annotation.XmlType in project camel by apache.

the class XmlRootElementPreferringElementNameStrategy method findQNameForSoapActionOrType.

@Override
public QName findQNameForSoapActionOrType(String soapAction, Class<?> type) {
    XmlType xmlType = type.getAnnotation(XmlType.class);
    if (xmlType == null || xmlType.name() == null) {
        throw new RuntimeException("The type " + type.getName() + " needs to have an XmlType annotation with name");
    }
    // prefer name+ns from the XmlRootElement, and fallback to XmlType
    String localName = null;
    String nameSpace = null;
    XmlRootElement root = type.getAnnotation(XmlRootElement.class);
    if (root != null) {
        localName = ObjectHelper.isEmpty(localName) ? root.name() : localName;
        nameSpace = isInValidNamespace(nameSpace) ? root.namespace() : nameSpace;
    }
    if (ObjectHelper.isEmpty(localName)) {
        localName = xmlType.name();
    }
    if (isInValidNamespace(nameSpace)) {
        XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
        if (xmlSchema != null) {
            nameSpace = xmlSchema.namespace();
        }
    }
    if (isInValidNamespace(nameSpace)) {
        nameSpace = xmlType.namespace();
    }
    if (ObjectHelper.isEmpty(localName) || isInValidNamespace(nameSpace)) {
        throw new IllegalStateException("Unable to determine localName or namespace for type <" + type.getName() + ">");
    }
    return new QName(nameSpace, localName);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName) XmlType(javax.xml.bind.annotation.XmlType)

Example 24 with XmlType

use of javax.xml.bind.annotation.XmlType in project cxf by apache.

the class CorbaStreamFaultOutInterceptor method getRaisesType.

protected RaisesType getRaisesType(OperationType opType, String exClassName, Throwable ex) {
    RaisesType result = null;
    List<RaisesType> exList = opType.getRaises();
    result = findRaisesType(exList, exClassName);
    if (result == null) {
        // if doc-literal, the part element name should be used, but for RPC, the message part name
        try {
            Method faultMethod = ex.getClass().getMethod("getFaultInfo");
            if (faultMethod != null) {
                Class<?> faultClass = faultMethod.getReturnType();
                XmlType exType = faultClass.getAnnotation(XmlType.class);
                exClassName = exType.name();
                result = findRaisesType(exList, exClassName);
            }
        } catch (Exception exp) {
        // Ignore it
        }
    }
    return result;
}
Also used : RaisesType(org.apache.cxf.binding.corba.wsdl.RaisesType) Method(java.lang.reflect.Method) SystemException(org.omg.CORBA.SystemException) CorbaBindingException(org.apache.cxf.binding.corba.CorbaBindingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlType(javax.xml.bind.annotation.XmlType)

Example 25 with XmlType

use of javax.xml.bind.annotation.XmlType in project cxf by apache.

the class WrapperBeanAnnotator method annotate.

public void annotate(final JavaAnnotatable clz) {
    WrapperBeanClass beanClass = null;
    if (clz instanceof WrapperBeanClass) {
        beanClass = (WrapperBeanClass) clz;
    } else {
        throw new RuntimeException("WrapperBeanAnnotator expect JavaClass as input");
    }
    JAnnotation xmlRootElement = new JAnnotation(XmlRootElement.class);
    xmlRootElement.addElement(new JAnnotationElement("name", beanClass.getElementName().getLocalPart()));
    xmlRootElement.addElement(new JAnnotationElement("namespace", beanClass.getElementName().getNamespaceURI()));
    JAnnotation xmlAccessorType = new JAnnotation(XmlAccessorType.class);
    xmlAccessorType.addElement(new JAnnotationElement(null, XmlAccessType.FIELD));
    XmlType tp = null;
    if (sourceClass != null) {
        tp = sourceClass.getAnnotation(XmlType.class);
    }
    JAnnotation xmlType = new JAnnotation(XmlType.class);
    if (tp == null) {
        xmlType.addElement(new JAnnotationElement("name", beanClass.getElementName().getLocalPart()));
        xmlType.addElement(new JAnnotationElement("namespace", beanClass.getElementName().getNamespaceURI()));
    } else {
        if (!"##default".equals(tp.name())) {
            xmlType.addElement(new JAnnotationElement("name", tp.name()));
        }
        if (!"##default".equals(tp.namespace())) {
            xmlType.addElement(new JAnnotationElement("namespace", tp.namespace()));
        }
        if (!StringUtils.isEmpty(tp.factoryMethod())) {
            xmlType.addElement(new JAnnotationElement("factoryMethod", tp.factoryMethod()));
        }
        if (tp.propOrder().length != 1 || !StringUtils.isEmpty(tp.propOrder()[0])) {
            xmlType.addElement(new JAnnotationElement("propOrder", tp.propOrder()));
        }
    }
    List<String> props = new ArrayList<>();
    for (JavaField f : beanClass.getFields()) {
        props.add(f.getParaName());
    }
    if (props.size() > 1) {
        xmlType.addElement(new JAnnotationElement("propOrder", props));
    }
    // Revisit: why annotation is string?
    beanClass.addAnnotation(xmlRootElement);
    beanClass.addAnnotation(xmlAccessorType);
    beanClass.addAnnotation(xmlType);
}
Also used : JavaField(org.apache.cxf.tools.common.model.JavaField) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) ArrayList(java.util.ArrayList) WrapperBeanClass(org.apache.cxf.tools.java2wsdl.generator.wsdl11.model.WrapperBeanClass) XmlType(javax.xml.bind.annotation.XmlType)

Aggregations

XmlType (javax.xml.bind.annotation.XmlType)30 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)10 QName (javax.xml.namespace.QName)10 Method (java.lang.reflect.Method)8 XmlSchema (javax.xml.bind.annotation.XmlSchema)7 Field (java.lang.reflect.Field)6 ArrayList (java.util.ArrayList)5 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 XmlAccessorOrder (javax.xml.bind.annotation.XmlAccessorOrder)3 XmlElement (javax.xml.bind.annotation.XmlElement)3 PrintWriter (java.io.PrintWriter)2 Annotation (java.lang.annotation.Annotation)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)2