Search in sources :

Example 16 with XmlType

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

the class SpringAnnotationProcessor method processModelClass.

protected void processModelClass(final ProcessingEnvironment processingEnv, final RoundEnvironment roundEnv, final TypeElement classElement) {
    final String javaTypeName = canonicalClassName(classElement.getQualifiedName().toString());
    String packageName = javaTypeName.substring(0, javaTypeName.lastIndexOf("."));
    // skip abstract classes
    if (classElement.getModifiers().contains(Modifier.ABSTRACT)) {
        return;
    }
    final XmlRootElement rootElement = classElement.getAnnotation(XmlRootElement.class);
    if (rootElement == null) {
        return;
    }
    String aName = rootElement.name();
    if (isNullOrEmpty(aName) || "##default".equals(aName)) {
        XmlType typeElement = classElement.getAnnotation(XmlType.class);
        aName = typeElement.name();
    }
    final String name = aName;
    // lets use the xsd name as the file name
    String fileName;
    if (isNullOrEmpty(name) || "##default".equals(name)) {
        fileName = classElement.getSimpleName().toString() + ".json";
    } else {
        fileName = name + ".json";
    }
    // write json schema
    Func1<PrintWriter, Void> handler = new Func1<PrintWriter, Void>() {

        @Override
        public Void call(PrintWriter writer) {
            writeJSonSchemeDocumentation(processingEnv, writer, roundEnv, classElement, rootElement, javaTypeName, name);
            return null;
        }
    };
    processFile(processingEnv, packageName, fileName, handler);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlType(javax.xml.bind.annotation.XmlType) PrintWriter(java.io.PrintWriter)

Example 17 with XmlType

use of javax.xml.bind.annotation.XmlType in project midpoint by Evolveum.

the class PrismBeanInspector method getPropOrderUncached.

private List<String> getPropOrderUncached(Class<? extends Object> beanClass) {
    List<String> propOrder;
    // Superclass first!
    Class superclass = beanClass.getSuperclass();
    if (superclass == null || superclass.equals(Object.class) || superclass.getAnnotation(XmlType.class) == null) {
        propOrder = new ArrayList<>();
    } else {
        propOrder = new ArrayList<>(getPropOrder(superclass));
    }
    XmlType xmlType = beanClass.getAnnotation(XmlType.class);
    if (xmlType == null) {
        throw new IllegalArgumentException("Cannot marshall " + beanClass + " it does not have @XmlType annotation");
    }
    String[] myPropOrder = xmlType.propOrder();
    for (String myProp : myPropOrder) {
        if (StringUtils.isNotBlank(myProp)) {
            // some properties starts with underscore..we don't want to serialize them with underscore, so remove it..
            if (myProp.startsWith("_")) {
                myProp = myProp.replace("_", "");
            }
            propOrder.add(myProp);
        }
    }
    Field[] fields = beanClass.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(XmlAttribute.class)) {
            propOrder.add(field.getName());
        }
    }
    Method[] methods = beanClass.getDeclaredMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(XmlAttribute.class)) {
            propOrder.add(getPropertyNameFromGetter(method.getName()));
        }
    }
    return propOrder;
}
Also used : XmlType(javax.xml.bind.annotation.XmlType)

Example 18 with XmlType

use of javax.xml.bind.annotation.XmlType in project midpoint by Evolveum.

the class BeanMarshaller method visit.

public void visit(Object bean, Handler<Object> handler) {
    if (bean == null) {
        return;
    }
    Class<? extends Object> beanClass = bean.getClass();
    handler.handle(bean);
    if (beanClass.isEnum() || beanClass.isPrimitive()) {
        //nothing more to do
        return;
    }
    // TODO: implement special handling for RawType, if necessary (it has no XmlType annotation any more)
    XmlType xmlType = beanClass.getAnnotation(XmlType.class);
    if (xmlType == null) {
        // no @XmlType annotation, we are not interested to go any deeper
        return;
    }
    List<String> propOrder = inspector.getPropOrder(beanClass);
    for (String fieldName : propOrder) {
        Method getter = inspector.findPropertyGetter(beanClass, fieldName);
        if (getter == null) {
            throw new IllegalStateException("No getter for field " + fieldName + " in " + beanClass);
        }
        Object getterResult = getValue(bean, getter, fieldName);
        if (getterResult == null) {
            continue;
        }
        if (getterResult instanceof Collection<?>) {
            Collection col = (Collection) getterResult;
            if (col.isEmpty()) {
                continue;
            }
            for (Object element : col) {
                visitValue(element, handler);
            }
        } else {
            visitValue(getterResult, handler);
        }
    }
}
Also used : PolyString(com.evolveum.midpoint.prism.polystring.PolyString) XmlType(javax.xml.bind.annotation.XmlType)

Example 19 with XmlType

use of javax.xml.bind.annotation.XmlType in project jaffa-framework by jaffa-projects.

the class JAXBHelper method unmarshalPayload.

/**
 * Obtains the contents of the input message, and unmarshals it into the original POJO.
 * JAXB is used for unmarshalling, if the dataBeanClass carries the 'XmlRootElement' JAXB annotation.
 * Else the XMLDecoder will be used for unmarshalling.
 * @param xml the XML to be unmarshalled.
 * @param dataBeanClassName the className of the source dataBean that was used to generate the Message.
 * @return the contents of the input message, unmarshalled into the original POJO.
 * @throws ClassNotFoundException if the dataBean class is not found.
 * @throws JAXBException if any error occurs during the unmarshal process.
 */
public static Object unmarshalPayload(String xml, String dataBeanClassName) throws JAXBException, ClassNotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Unmarshalling into an instance of " + dataBeanClassName + " the XML\n" + xml);
    }
    Object payload = null;
    Class dataBeanClass = Class.forName(dataBeanClassName);
    if (dataBeanClass.isAnnotationPresent(XmlRootElement.class)) {
        if (log.isDebugEnabled()) {
            log.debug(dataBeanClassName + " has the 'XmlRootElement' JAXB annotation, and hence will be unmarshalled using JAXB");
        }
        JAXBContext jc = obtainJAXBContext(dataBeanClass);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        payload = unmarshaller.unmarshal(new BufferedReader(new StringReader(xml)));
    } else if (dataBeanClass.isAnnotationPresent(XmlType.class)) {
        if (log.isDebugEnabled())
            log.debug(dataBeanClassName + " has the 'XmlType' JAXB annotation, and hence will be unmarshalled using JAXB");
        JAXBContext jc = JAXBHelper.obtainJAXBContext(dataBeanClass);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement rootElement = unmarshaller.unmarshal(new StreamSource(new BufferedReader(new StringReader(xml))), dataBeanClass);
        payload = rootElement.getValue();
    } else {
        if (log.isDebugEnabled()) {
            log.debug(dataBeanClassName + " does not have the 'XmlRootElement' JAXB annotation, and hence will be unmarshalled using XMLDecoder");
        }
        XMLDecoder d = new XMLDecoder(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())));
        payload = d.readObject();
        d.close();
    }
    if (log.isDebugEnabled()) {
        log.debug("Unmarshalled Payload: " + payload);
    }
    return payload;
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) XMLDecoder(java.beans.XMLDecoder) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) XmlType(javax.xml.bind.annotation.XmlType)

Example 20 with XmlType

use of javax.xml.bind.annotation.XmlType in project jaffa-framework by jaffa-projects.

the class JAXBHelper method marshalPayload.

/**
 * Marshals the payload into XML.
 * JAXB is used for marshalling, if the payload carries the 'XmlRootElement' JAXB annotation.
 * Else the XMLEncoder will be used for marshalling.
 * @param payload Any serializable object.
 * @return the XML representation of the payload.
 * @throws JAXBException if any JAXB error occurs.
 */
public static String marshalPayload(Object payload) throws JAXBException {
    String output = null;
    if (payload.getClass().isAnnotationPresent(XmlRootElement.class)) {
        if (log.isDebugEnabled()) {
            log.debug(payload.getClass().getName() + " has the 'XmlRootElement' JAXB annotation, and hence will be marshalled using JAXB");
        }
        StringWriter tempWriter = new StringWriter();
        JAXBContext jc = obtainJAXBContext(payload.getClass());
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(payload, new BufferedWriter(tempWriter));
        output = tempWriter.toString();
    } else if (payload.getClass().isAnnotationPresent(XmlType.class)) {
        if (log.isDebugEnabled())
            log.debug(payload.getClass().getName() + " has the 'XmlType' JAXB annotation, and hence will be marshalled using JAXB");
        StringWriter tempWriter = new StringWriter();
        JAXBContext jc = obtainJAXBContext(payload.getClass());
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        XmlType xmlType = payload.getClass().getAnnotation(XmlType.class);
        QName qName = new QName(xmlType.namespace() != null ? xmlType.namespace() : XMLConstants.NULL_NS_URI, xmlType.name() != null ? xmlType.name() : StringHelper.getJavaBeanStyle(payload.getClass().getSimpleName()));
        JAXBElement rootElement = new JAXBElement(qName, payload.getClass(), payload);
        marshaller.marshal(rootElement, new BufferedWriter(tempWriter));
        output = tempWriter.toString();
    } else {
        if (log.isDebugEnabled()) {
            log.debug(payload.getClass().getName() + " does not have the 'XmlRootElement' JAXB annotation, and hence will be marshalled using XMLEncoder");
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(os));
        e.writeObject(payload);
        e.close();
        output = os.toString();
    }
    if (log.isDebugEnabled()) {
        log.debug("Marshalled Payload is\n" + output);
    }
    return output;
}
Also used : Marshaller(javax.xml.bind.Marshaller) XMLEncoder(java.beans.XMLEncoder) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) 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