Search in sources :

Example 1 with DAOImpl

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

the class DOMJavaClassIntrospector method getNodes.

public static String[] getNodes(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) {
        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 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 = getNodes(extraData);
        String[] fields = getAccessibleFields(node, false);
        if (extraFields == null)
            return fields;
        // concat and remove duplicates
        LinkedHashSet<String> set = new LinkedHashSet<String>(Arrays.asList(fields));
        for (String ef : extraFields) if (!set.add(ef))
            if (log != null)
                log.warning("Duplicate node '" + nodeName + "/" + ef + "'.");
        return set.toArray(new String[0]);
    } else
        return getAccessibleFields(node, false);
}
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 2 with DAOImpl

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

the class CDBAccess method internalConnect.

/**
	 * Performs the connect of the specified DAO.
	 * 
	 * @param	proxy	the proxy to connect, non-<code>null</code>
	 */
private void internalConnect(DAOProxy proxy) {
    String curl = null;
    try {
        checkDALConnection();
    } catch (Throwable th) {
        // TODO @todo replace
        RuntimeException re = new RuntimeException("Failed to obtain DAO for proxy '" + proxy + "'.", th);
        throw re;
    }
    DAOOperations dao = null;
    try {
        curl = proxy.getCURL();
        if (remoteDAO) {
            dao = dalReference.get_DAO_Servant(curl);
        } else {
            String xml = dalReference.get_DAO(curl);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            // use CDB XML handler which does not creates strings...
            XMLHandler xmlSolver = new XMLHandler(false, logger);
            saxParser.parse(new InputSource(new StringReader(xml)), xmlSolver);
            if (xmlSolver.m_errorString != null) {
                AcsJCDBXMLErrorEx e = new AcsJCDBXMLErrorEx();
                e.setErrorString("XML parser error: " + xmlSolver.m_errorString);
                throw e;
            //throw new XMLerror("XML parser error: " + xmlSolver.m_errorString);
            }
            // create non-CORBA related, silent DAO
            dao = new DAOImpl(curl, xmlSolver.m_rootNode, null, logger, true);
            proxy.setElementName(xmlSolver.m_rootNode.getName());
        }
        // register listener, if not already registered
        if (changeListener != null) {
            if (!changeListener.isRegistered(curl))
                changeListener.handle(dalReference, curl, proxy);
        }
    } catch (Throwable th) {
        // TODO @todo replace
        RuntimeException re = new RuntimeException("Failed to obtain DAO object for proxy '" + proxy + "'.", th);
        throw re;
    }
    try {
        proxy.initialize(dao);
    } catch (Throwable th) {
        // TODO @todo replace
        RuntimeException re = new RuntimeException("The proxy '" + proxy + "' rejects the DAO.", th);
        throw re;
    }
    logger.config("Connected to DAO '" + proxy.getCURL() + "'.");
}
Also used : XMLHandler(com.cosylab.cdb.jdal.XMLHandler) InputSource(org.xml.sax.InputSource) DAOOperations(com.cosylab.CDB.DAOOperations) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) AcsJCDBXMLErrorEx(alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx) DAOImpl(com.cosylab.cdb.jdal.DAOImpl) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with DAOImpl

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

the class DOMJavaClassIntrospector method getNodesObjects.

public static NamedObject[] getNodesObjects(Object node, String nodeName, Logger log) {
    if (node instanceof DAOImpl) {
        return getNodesObjects(((DAOImpl) node).getRootNode().getNodesMap(), null, null);
    } else if (node instanceof XMLTreeNode) {
        return getNodesObjects(((XMLTreeNode) node).getNodesMap(), null, null);
    } else if (node instanceof Map) {
        ArrayList<NamedObject> subnodes = new ArrayList<NamedObject>();
        Map nodeMap = (Map) node;
        Set keySet = nodeMap.keySet();
        for (Object key : keySet) subnodes.add(new NamedObject(key.toString(), nodeMap.get(key)));
        return subnodes.toArray(new NamedObject[subnodes.size()]);
    } else if (node instanceof Element) {
        ArrayList<NamedObject> list = new ArrayList<NamedObject>();
        for (Node childNode = ((Element) node).getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
            if (childNode.getNodeType() == Element.ELEMENT_NODE)
                list.add(new NamedObject(childNode.getNodeName(), (Element) childNode));
        }
        return list.toArray(new NamedObject[list.size()]);
    } else {
        ArrayList<NamedObject> list = new ArrayList<NamedObject>();
        if (node instanceof ExtraDataFeature) {
            NamedObject[] extraFields = null;
            Element extraData = ((ExtraDataFeature) node).getExtraData();
            if (extraData != null)
                extraFields = getNodesObjects(extraData, null, null);
            getAccessibleFieldsObjects(node, false, list);
            if (extraFields != null) {
                for (NamedObject no : extraFields) list.add(no);
            }
        } else
            getAccessibleFieldsObjects(node, false, list);
        return list.toArray(new NamedObject[list.size()]);
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Element(org.w3c.dom.Element) XMLTreeNode(com.cosylab.cdb.jdal.XMLTreeNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XMLTreeNode(com.cosylab.cdb.jdal.XMLTreeNode) DAOImpl(com.cosylab.cdb.jdal.DAOImpl) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 4 with DAOImpl

use of com.cosylab.cdb.jdal.DAOImpl 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 5 with DAOImpl

use of com.cosylab.cdb.jdal.DAOImpl 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)

Aggregations

DAOImpl (com.cosylab.cdb.jdal.DAOImpl)5 XMLTreeNode (com.cosylab.cdb.jdal.XMLTreeNode)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 List (java.util.List)2 AcsJCDBXMLErrorEx (alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx)1 DAOOperations (com.cosylab.CDB.DAOOperations)1 XMLHandler (com.cosylab.cdb.jdal.XMLHandler)1 StringReader (java.io.StringReader)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 InputSource (org.xml.sax.InputSource)1