Search in sources :

Example 6 with XMLTreeNode

use of com.cosylab.cdb.jdal.XMLTreeNode in project ACS by ACS-Community.

the class DOMJavaClassIntrospector method getChild.

public static Object getChild(String name, Object node) {
    synchronized (node) {
        if (node instanceof DAOImpl) {
            return (XMLTreeNode) (((DAOImpl) node).getRootNode().getNodesMap().get(name));
        }
        if (node instanceof XMLTreeNode) {
            Object n = (XMLTreeNode) (((XMLTreeNode) node).getNodesMap().get(name));
            if (n != null)
                return n;
            else
                return ((XMLTreeNode) node).getFieldMap().get(name);
        } else if (node instanceof Map) {
            Map map = (Map) node;
            if (map.containsKey(name))
                return map.get(name);
            // not found
            return null;
        } else if (node instanceof Element) {
            // check attribute
            Element element = (Element) node;
            if (element.hasAttribute(name))
                return element.getAttribute(name);
            // check element
            NodeList nodeList = element.getElementsByTagName(name);
            if (nodeList.getLength() > 0)
                return (Element) nodeList.item(0);
            // only digit is not allowed
            if (name.length() <= 1)
                return null;
            // "<name><index>" support for XML sequences, where index > 0
            // ends with digit?
            int pos = name.length() - 1;
            if (Character.isDigit(name.charAt(pos))) {
                pos--;
                // extract digit
                while (Character.isDigit(name.charAt(pos))) pos--;
                // point to the first digit
                pos++;
                String dig = name.substring(pos);
                name = name.substring(0, pos);
                try {
                    int ix = Integer.parseInt(dig);
                    if (ix > 0) {
                        nodeList = element.getElementsByTagName(name);
                        if (nodeList.getLength() > ix) {
                            return (Element) nodeList.item(ix);
                        } else
                            // index out of range or no such elements
                            return null;
                    } else
                        // ix <= 0
                        return null;
                } catch (Throwable th) {
                    // bad index
                    return null;
                }
            } else
                // does not end with digit
                return null;
        } else {
            Field field = null;
            final Class nodeType = node.getClass();
            Class type = nodeType;
            while (field == null && type != null) {
                try {
                    field = type.getDeclaredField(name);
                } catch (NoSuchFieldException e) {
                /* noop */
                }
                type = type.getSuperclass();
            }
            if (field != null) {
                if (name.equals(SUBNODES_MAP_NAME) || name.equals(SUBNODES_MAP_NAME_ALTERNATIVE)) {
                    // this will work only on public fields
                    try {
                        return field.get(node);
                    } catch (IllegalAccessException e) {
                        // failed to access the field
                        return null;
                    }
                } else {
                    // using accessor
                    try {
                        Method accessorMethod = getAccessorMethod(nodeType, name);
                        Object retVal = accessorMethod.invoke(node, (Object[]) null);
                        // @todo TODO now we consider null retVal as non-existant field
                        return retVal;
                    } catch (Throwable th) {
                        // failed to access the field
                        return null;
                    }
                }
            } else if (!name.equals(SUBNODES_MAP_NAME) && !name.equals(SUBNODES_MAP_NAME_ALTERNATIVE)) {
                // check extra data
                if (node instanceof ExtraDataFeature) {
                    Element extraData = ((ExtraDataFeature) node).getExtraData();
                    if (extraData != null) {
                        Object viaExtra = getChild(name, extraData);
                        if (viaExtra != null)
                            return viaExtra;
                    }
                }
                // check for subnodesMap map
                Object subnodesMap = getChild(SUBNODES_MAP_NAME, node);
                if (subnodesMap instanceof Map) {
                    Object obj = ((Map) subnodesMap).get(name);
                    if (obj != null)
                        return obj;
                }
                subnodesMap = getChild(SUBNODES_MAP_NAME_ALTERNATIVE, node);
                if (subnodesMap instanceof Map) {
                    Object obj = ((Map) subnodesMap).get(name);
                    if (obj != null)
                        return obj;
                }
                // not in map
                return null;
            }
            // not found
            return null;
        }
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) XMLTreeNode(com.cosylab.cdb.jdal.XMLTreeNode) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) DAOImpl(com.cosylab.cdb.jdal.DAOImpl) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 7 with XMLTreeNode

use of com.cosylab.cdb.jdal.XMLTreeNode in project ACS by ACS-Community.

the class DOMJavaClassIntrospector method getElements.

// element = internal node (excluding hierarchy)
public static String[] getElements(Object node, String nodeName, Logger log) {
    if (node instanceof DAOImpl) {
        return getNodes(((DAOImpl) node).getRootNode().getNodesMap());
    } else if (node instanceof XMLTreeNode) {
        return getNodes(((XMLTreeNode) node).getNodesMap());
    } else if (node instanceof Map) {
        if (node instanceof InternalElementsMap) {
            Set<String> subnodes = new LinkedHashSet<String>();
            Set keySet = ((Map) node).keySet();
            for (Object key : keySet) subnodes.add(key.toString());
            return subnodes.toArray(new String[subnodes.size()]);
        } else
            return new String[0];
    } else if (node instanceof Element) {
        List<String> list = new ArrayList<String>();
        for (Node childNode = ((Element) node).getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
            if (childNode.getNodeType() == Element.ELEMENT_NODE)
                list.add(childNode.getNodeName());
        }
        return list.toArray(new String[list.size()]);
    } else if (node instanceof ExtraDataFeature) {
        String[] extraFields = null;
        Element extraData = ((ExtraDataFeature) node).getExtraData();
        if (extraData != null)
            extraFields = getElements(extraData);
        String[] fields = getElementFields(node);
        if (extraFields == null)
            return fields;
        // concat and remove duplicates
        Set<String> set = new LinkedHashSet<String>(Arrays.asList(fields));
        for (String ef : extraFields) if (!set.add(ef))
            if (log != null)
                log.warning("Duplicate element '" + nodeName + "/" + ef + "'.");
        return set.toArray(new String[0]);
    } else
        return getElementFields(node);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Element(org.w3c.dom.Element) XMLTreeNode(com.cosylab.cdb.jdal.XMLTreeNode) Node(org.w3c.dom.Node) XMLTreeNode(com.cosylab.cdb.jdal.XMLTreeNode) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) DAOImpl(com.cosylab.cdb.jdal.DAOImpl) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 8 with XMLTreeNode

use of com.cosylab.cdb.jdal.XMLTreeNode in project ACS by ACS-Community.

the class DOMJavaClassIntrospector method getMutatorMethod.

public static NodeAndMutator getMutatorMethod(String name, Object node, XMLSaver parentSaver) {
    if (node instanceof XMLTreeNode) {
        XMLTreeNode treeNode = (XMLTreeNode) node;
        if (!treeNode.getFieldMap().containsKey(name))
            return null;
        XMLTreeNodeSetter setterObject = new XMLTreeNodeSetter(name, treeNode.getFieldMap(), parentSaver);
        return new NodeAndMutator(setterObject, getMutatorMethod(setterObject.getClass(), "field"));
    } else {
        Field field = null;
        final Class nodeType = node.getClass();
        Class type = nodeType;
        while (field == null && type != null) {
            try {
                field = type.getDeclaredField(name);
            } catch (NoSuchFieldException e) {
            /* noop */
            }
            type = type.getSuperclass();
        }
        if (field != null) {
            // using mutator
            return new NodeAndMutator(node, getMutatorMethod(nodeType, name));
        }
        // not found
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) XMLTreeNode(com.cosylab.cdb.jdal.XMLTreeNode)

Aggregations

XMLTreeNode (com.cosylab.cdb.jdal.XMLTreeNode)8 DAOImpl (com.cosylab.cdb.jdal.DAOImpl)4 Map (java.util.Map)4 Element (org.w3c.dom.Element)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)4 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 Field (java.lang.reflect.Field)2 List (java.util.List)2 CDBXMLErrorEx (alma.cdbErrType.CDBXMLErrorEx)1 AcsJCDBXMLErrorEx (alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx)1 WDAL (com.cosylab.CDB.WDAL)1 WDAO (com.cosylab.CDB.WDAO)1 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 NamingException (javax.naming.NamingException)1