Search in sources :

Example 1 with XmlList

use of jakarta.xml.bind.annotation.XmlList in project jOOQ by jOOQ.

the class MiniJAXB method unmarshal0.

private static void unmarshal0(Object result, Element element, Map<Class<?>, Map<String, Field>> fieldsByClass) throws Exception {
    if (result == null)
        return;
    Map<String, Field> map = fieldsByElementName(fieldsByClass, result.getClass());
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element childElement = (Element) item;
        Field child = map.get(childElement.getTagName());
        if (child == null)
            child = map.get(childElement.getLocalName());
        // skip unknown elements
        if (child == null)
            continue;
        XmlElementWrapper w = child.getAnnotation(XmlElementWrapper.class);
        XmlElement e = child.getAnnotation(XmlElement.class);
        XmlJavaTypeAdapter a = child.getAnnotation(XmlJavaTypeAdapter.class);
        XmlList l = child.getAnnotation(XmlList.class);
        String childName = child.getName();
        Class<?> childType = child.getType();
        if (List.class.isAssignableFrom(childType) && w != null && e != null) {
            List<Object> list = new ArrayList<Object>();
            unmarshalList0(list, childElement, e.name(), (Class<?>) ((ParameterizedType) child.getGenericType()).getActualTypeArguments()[0], fieldsByClass);
            Reflect.on(result).set(childName, list);
        } else if (List.class.isAssignableFrom(childType) && l != null) {
            List<Object> list = new ArrayList<Object>(asList(childElement.getTextContent().split(" +")));
            Reflect.on(result).set(childName, Convert.convert(list, (Class<?>) ((ParameterizedType) child.getGenericType()).getActualTypeArguments()[0]));
        } else if (childType.getAnnotation(XmlEnum.class) != null) {
            Reflect.on(result).set(childName, Reflect.onClass(childType).call("fromValue", childElement.getTextContent().trim()));
        } else if (childType.getAnnotation(XmlType.class) != null) {
            Object object = Reflect.on(childType).create().get();
            Reflect.on(result).set(childName, object);
            unmarshal0(object, childElement, fieldsByClass);
        } else if (a != null) {
            @SuppressWarnings("unchecked") XmlAdapter<Object, Object> adapter = a.value().getDeclaredConstructor().newInstance();
            Reflect.on(result).set(childName, adapter.unmarshal(childElement.getTextContent().trim()));
        } else {
            Reflect.on(result).set(childName, Convert.convert(childElement.getTextContent().trim(), childType));
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) XmlElement(jakarta.xml.bind.annotation.XmlElement) XmlRootElement(jakarta.xml.bind.annotation.XmlRootElement) Element(org.w3c.dom.Element) XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) ArrayList(java.util.ArrayList) XmlList(jakarta.xml.bind.annotation.XmlList) XmlEnum(jakarta.xml.bind.annotation.XmlEnum) Field(java.lang.reflect.Field) XmlElement(jakarta.xml.bind.annotation.XmlElement) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) XmlList(jakarta.xml.bind.annotation.XmlList) XmlElementWrapper(jakarta.xml.bind.annotation.XmlElementWrapper)

Aggregations

XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlElementWrapper (jakarta.xml.bind.annotation.XmlElementWrapper)1 XmlEnum (jakarta.xml.bind.annotation.XmlEnum)1 XmlList (jakarta.xml.bind.annotation.XmlList)1 XmlRootElement (jakarta.xml.bind.annotation.XmlRootElement)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1