Example usage for org.w3c.dom NamedNodeMap getNamedItem

List of usage examples for org.w3c.dom NamedNodeMap getNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getNamedItem.

Prototype

public Node getNamedItem(String name);

Source Link

Document

Retrieves a node specified by name.

Usage

From source file:net.sourceforge.eclipsetrader.ats.Repository.java

TradingSystem loadTradingSystem(NodeList node) {
    TradingSystem obj = new TradingSystem(
            new Integer(Integer.parseInt(((Node) node).getAttributes().getNamedItem("id").getNodeValue())));

    for (int i = 0; i < node.getLength(); i++) {
        Node item = node.item(i);
        String nodeName = item.getNodeName();
        Node value = item.getFirstChild();
        if (value != null) {
            if (nodeName.equals("name")) //$NON-NLS-1$
                obj.setName(value.getNodeValue());
            else if (nodeName.equals("account")) //$NON-NLS-1$
            {// w ww. j  a v  a 2s.  c o m
                Integer id = new Integer(value.getNodeValue());
                Account account = (Account) CorePlugin.getRepository().load(Account.class, id);
                if (account == null) {
                    log.warn("Unknown account (id=" + id + ")");
                    account = new DefaultAccount(id);
                }
                obj.setAccount(account);
            } else if (nodeName.equals("tradingProvider")) //$NON-NLS-1$
                obj.setTradingProviderId(value.getNodeValue());
        }
        if (nodeName.equals("moneyManager")) //$NON-NLS-1$
            obj.setMoneyManager(loadComponent(item.getChildNodes()));
        else if (nodeName.equalsIgnoreCase("param") == true) {
            NamedNodeMap map = item.getAttributes();
            obj.getParams().put(map.getNamedItem("key").getNodeValue(),
                    map.getNamedItem("value").getNodeValue());
        }
        if (nodeName.equals("strategy")) //$NON-NLS-1$
            obj.addStrategy(loadStrategy(item.getChildNodes()));
    }

    obj.clearChanged();

    return obj;
}

From source file:net.sourceforge.eclipsetrader.ats.Repository.java

Component loadComponent(NodeList node) {
    NamedNodeMap map = ((Node) node).getAttributes();
    Component obj = new Component(new Integer(map.getNamedItem("id").getNodeValue()));
    obj.setPluginId(map.getNamedItem("pluginId").getNodeValue());

    for (int i = 0; i < node.getLength(); i++) {
        Node item = node.item(i);
        String nodeName = item.getNodeName();
        Node value = item.getFirstChild();
        if (value != null) {
        }//  w ww  .  j a  v  a  2 s.c o m
        if (nodeName.equalsIgnoreCase("param") == true) {
            map = item.getAttributes();
            obj.getPreferences().setValue(map.getNamedItem("key").getNodeValue(),
                    map.getNamedItem("value").getNodeValue());
        }
    }

    obj.clearChanged();

    return obj;
}

From source file:immf.growl.GrowlApiClient.java

private GrowlApiResult analyzeResponse(HttpResponse res) {

    GrowlApiResult result = GrowlApiResult.ERROR;

    if (res == null) {
        GrowlNotifier.log.info("HttpResponse is null");
        this.incrementInternalServerErrorCount();
        return GrowlApiResult.ERROR;
    } else {/* w w w .  j a v a 2 s  . c  om*/
        decrementCallsRemaining();
        int status = res.getStatusLine().getStatusCode();
        switch (status) {
        case 200:
            log.info("Api call is success.");
            result = GrowlApiResult.SUCCESS;
            break;
        case 400:
            log.error("The data supplied is in the wrong format, invalid length or null.");
            result = GrowlApiResult.INVALID;
            break;
        case 401:
            log.error("The 'apikey' provided is not valid.");
            result = GrowlApiResult.INVALID;
            break;
        case 402:
        case 406:
            log.error("Maximum number of API calls exceeded.");
            result = GrowlApiResult.EXCEEDED;
            break;
        case 500:
            log.error("Internal server error.");
            this.incrementInternalServerErrorCount();
            result = GrowlApiResult.ERROR;
            break;
        default:
            ;
        }

        if (status == 200 || status == 402 || status == 406) {
            HttpEntity entity = res.getEntity();
            try {
                final InputStream in = entity.getContent();
                final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                final Document resDoc = builder.parse(in);

                // ??
                Element rootNode = resDoc.getDocumentElement();
                if (rootNode != null && this.topLevelTag.equals(rootNode.getNodeName())) {
                    NodeList rootChildren = rootNode.getChildNodes();
                    for (int i = 0, max = rootChildren.getLength(); i < max; i++) {
                        Node node = rootChildren.item(i);
                        if (TAG_ERROR.equals(node.getNodeName()) || TAG_SUCCESS.equals(node.getNodeName())) {
                            NamedNodeMap attrs = node.getAttributes();
                            if (attrs != null) {
                                Node attrNode = attrs.getNamedItem(ATTR_RESETTIMER);
                                if (attrNode != null && attrNode.getNodeValue().length() > 0) {
                                    setResetTimer(Integer.parseInt(attrNode.getNodeValue()));
                                }
                                attrNode = attrs.getNamedItem(ATTR_RESETDATE);
                                if (attrNode != null && attrNode.getNodeValue().length() > 0) {
                                    setResetDate(Integer.parseInt(attrNode.getNodeValue()));
                                }
                                attrNode = attrs.getNamedItem(ATTR_REMAINING);
                                if (attrNode != null && attrNode.getNodeValue().length() > 0) {
                                    int remaining = Integer.parseInt(attrNode.getNodeValue());
                                    setCallsRemaining(remaining);
                                }
                            }
                        }
                    }
                }
            } catch (IllegalStateException e) {
                log.error(e.getMessage());
            } catch (IOException e) {
                log.error(e.getMessage());
            } catch (ParserConfigurationException e) {
                log.error(e.getMessage());
            } catch (SAXException e) {
                log.error(e.getMessage());
            }
        }

        return result;

    }
}

From source file:com.microsoft.alm.plugin.external.commands.Command.java

protected String getXPathAttributeValue(final NamedNodeMap attributeMap, final String attributeName) {
    String value = StringUtils.EMPTY;
    if (attributeMap != null) {
        final Node node = attributeMap.getNamedItem(attributeName);
        if (node != null) {
            value = node.getNodeValue();
        }//  w  w  w.ja va 2s. c  o  m
    }

    return value;
}

From source file:net.sourceforge.eclipsetrader.ats.Repository.java

Strategy loadStrategy(NodeList node) {
    NamedNodeMap map = ((Node) node).getAttributes();
    Strategy obj = new Strategy(new Integer(map.getNamedItem("id").getNodeValue()));
    if (map.getNamedItem("pluginId") != null)
        obj.setPluginId(map.getNamedItem("pluginId").getNodeValue());
    if (map.getNamedItem("autostart") != null)
        obj.setAutoStart(map.getNamedItem("autostart").getNodeValue().equals("true"));

    for (int i = 0; i < node.getLength(); i++) {
        Node item = node.item(i);
        String nodeName = item.getNodeName();
        Node value = item.getFirstChild();
        if (value != null) {
            if (nodeName.equals("name")) //$NON-NLS-1$
                obj.setName(value.getNodeValue());
        }/*  w ww  . j  a  va2  s  . com*/
        if (nodeName.equals("marketManager")) //$NON-NLS-1$
            obj.setMarketManager(loadComponent(item.getChildNodes()));
        else if (nodeName.equals("entry")) //$NON-NLS-1$
            obj.setEntry(loadComponent(item.getChildNodes()));
        else if (nodeName.equals("exit")) //$NON-NLS-1$
            obj.setExit(loadComponent(item.getChildNodes()));
        else if (nodeName.equals("moneyManager")) //$NON-NLS-1$
            obj.setMoneyManager(loadComponent(item.getChildNodes()));
        else if (nodeName.equalsIgnoreCase("security") == true) {
            map = item.getAttributes();
            Integer id = new Integer(map.getNamedItem("id").getNodeValue());
            Security security = (Security) CorePlugin.getRepository().load(Security.class, id);
            if (security == null)
                log.warn("Unknown security (id=" + id + ")");
            else
                obj.addSecurity(security);
        }
        if (nodeName.equalsIgnoreCase("param") == true) {
            map = item.getAttributes();
            obj.getParams().put(map.getNamedItem("key").getNodeValue(),
                    map.getNamedItem("value").getNodeValue());
        }
    }

    obj.clearChanged();

    return obj;
}

From source file:ambit.data.qmrf.QMRFConverter.java

protected String findAttributeValue(String name_attribute, Node source) throws Exception {

    NamedNodeMap objAttributes = source.getAttributes();
    Node attribute = objAttributes.getNamedItem(name_attribute);
    return org.apache.commons.lang.StringEscapeUtils.unescapeXml(replaceTags(attribute.getNodeValue()).trim());
}

From source file:com.hygenics.parser.ManualReplacement.java

private void transform() {
    log.info("Transforming");

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    for (String fpath : fpaths) {
        log.info("FILE: " + fpath);
        try {// w  ww.  j a v a  2  s .c om
            // TRANSFORM
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(fpath));
            Node root = doc.getFirstChild();
            XPathFactory xfactory = XPathFactory.newInstance();
            XPath xpath = xfactory.newXPath();
            String database = null;
            String path = "//transformation/connection";

            log.info("Removing");
            for (String dbname : remove) {
                log.info("XPATH:" + path + "[descendant::name[contains(text(),'" + dbname.trim() + "')]]");
                XPathExpression xepr = xpath
                        .compile(path + "[descendant::name[contains(text(),'" + dbname.trim() + "')]]");
                Node conn = (Node) xepr.evaluate(doc, XPathConstants.NODE);
                if (conn != null) {
                    root.removeChild(conn);
                }
            }

            log.info("Transforming");
            for (String key : databaseAttributes.keySet()) {
                database = key;
                log.info("XPATH:" + path + "[descendant::name[contains(text(),'" + database.trim() + "')]]");
                XPathExpression xepr = xpath
                        .compile(path + "[descendant::name[contains(text(),'" + database.trim() + "')]]");
                Node conn = (Node) xepr.evaluate(doc, XPathConstants.NODE);

                if (conn != null) {
                    if (remove.contains(key)) {
                        root.removeChild(conn);
                    } else {
                        Map<String, String> attrs = databaseAttributes.get(database);
                        NodeList nl = conn.getChildNodes();
                        Set<String> keys = databaseAttributes.get(key).keySet();

                        for (int i = 0; i < nl.getLength(); i++) {
                            org.w3c.dom.Node n = nl.item(i);

                            if (keys.contains(n.getNodeName().trim())) {
                                n.setNodeValue(attrs.get(n.getNodeName()));
                            }
                        }
                    }
                }

                if (!this.log_to_table || (this.log_to_table && this.loggingTables != null)) {
                    log.info("Logging Manipulation");
                    log.info("PERFORMING LOGGING MANIPULATION: " + (!this.log_to_table) != null
                            ? "Removing Logging Data"
                            : "Adding Logging data");
                    String[] sections = new String[] { "trans-log-table", "perf-log-table", "channel-log-table",
                            "step-log-table", "metrics-log-table" };
                    for (String section : sections) {
                        log.info("Changing Settings for " + section);
                        xepr = xpath.compile("//" + section + "/field/enabled");
                        NodeList nodes = (NodeList) xepr.evaluate(doc, XPathConstants.NODESET);
                        log.info("Nodes Found: " + Integer.toString(nodes.getLength()));
                        for (int i = 0; i < nodes.getLength(); i++) {
                            if (!this.log_to_table) {
                                nodes.item(i).setNodeValue("N");
                            } else {
                                nodes.item(i).setNodeValue("Y");
                            }
                        }

                        for (String nodeName : new String[] { "schema", "connection", "table",
                                "size_limit_lines", "interval", "timeout_days" }) {
                            if (!this.log_to_table) {
                                log.info("Changing Settings for Node: " + nodeName);
                                xepr = xpath.compile("//" + section + "/" + nodeName);
                                Node node = (Node) xepr.evaluate(doc, XPathConstants.NODE);
                                if (node != null) {
                                    if (!this.log_to_table) {
                                        node.setNodeValue(null);
                                    } else if (this.loggingTables.containsKey(section)
                                            && this.loggingTables.get(section).containsKey(nodeName)) {
                                        node.setNodeValue(this.loggingTables.get(section).get(nodeName));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // SET MAIN CONNECTION
            if (mainConnection != null) {
                XPathExpression xepr = xpath.compile(path);
                NodeList conns = (NodeList) xepr.evaluate(doc, XPathConstants.NODESET); // NodeSet is not a part of
                // org.w3c it is
                // actually a NodeList
                for (int i = 0; i < conns.getLength(); i++) {
                    if (!conns.item(i).hasChildNodes()) {// only connection
                        // elements
                        // without child
                        // nodes have
                        // text content
                        conns.item(i).setNodeValue(mainConnection);
                    }
                }
            }

            if (this.replacements != null) {
                for (String key : this.replacements.keySet()) {
                    XPathExpression xepr = xpath.compile(key);
                    Node node = (Node) xepr.evaluate(doc, XPathConstants.NODE);
                    if (node != null) {
                        for (String attrVal : this.replacements.get(key).keySet()) {
                            log.info("Replacing Information at " + key + "at " + attrVal);
                            log.info("Replacement Will Be: "
                                    + StringEscapeUtils.escapeXml11(this.replacements.get(key).get(attrVal)));

                            if (attrVal.toLowerCase().trim().equals("text")) {
                                node.setNodeValue(
                                        StringEscapeUtils.escapeXml11(this.replacements.get(key).get(attrVal)));
                                if (node.getNodeValue() == null) {
                                    node.setTextContent(StringEscapeUtils
                                            .escapeXml11(this.replacements.get(key).get(attrVal)));
                                }

                            } else {
                                NamedNodeMap nattrs = node.getAttributes();
                                Node n = nattrs.getNamedItem(attrVal);
                                if (n != null) {
                                    n.setNodeValue(StringEscapeUtils
                                            .escapeXml11(this.replacements.get(key).get(attrVal)));
                                } else {
                                    log.warn("Attribute Not Found " + attrVal);
                                }
                            }
                        }
                    } else {
                        log.warn("Node not found for " + key);
                    }
                }
            }

            // WRITE TO FILE
            log.info("Writing to File");
            TransformerFactory tfact = TransformerFactory.newInstance();
            Transformer transformer = tfact.newTransformer();
            DOMSource source = new DOMSource(doc);
            try (FileOutputStream stream = new FileOutputStream(new File(fpath))) {
                StreamResult result = new StreamResult(stream);
                transformer.transform(source, result);
                stream.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.cloud.hypervisor.kvm.resource.wrapper.LibvirtMigrateCommandWrapper.java

private String getSourceFileDevText(Node diskNode) {
    NodeList diskChildNodes = diskNode.getChildNodes();

    for (int i = 0; i < diskChildNodes.getLength(); i++) {
        Node diskChildNode = diskChildNodes.item(i);

        if ("source".equals(diskChildNode.getNodeName())) {
            NamedNodeMap diskNodeAttributes = diskChildNode.getAttributes();

            Node diskNodeAttribute = diskNodeAttributes.getNamedItem("file");

            if (diskNodeAttribute != null) {
                return diskNodeAttribute.getTextContent();
            }/*from  w w w  .  ja v a2s  .  c  o  m*/

            diskNodeAttribute = diskNodeAttributes.getNamedItem("dev");

            if (diskNodeAttribute != null) {
                return diskNodeAttribute.getTextContent();
            }
        }
    }

    return null;
}

From source file:com.amalto.workbench.widgets.SchematronExpressBuilder.java

private void parseFunxml() throws Exception {
    InputStream in = null;//from w w  w . j a  v a2 s. c o m
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document document;
        if (isSchematron) {
            in = SchematronExpressBuilder.class.getResourceAsStream("XPathFunc.xml");//$NON-NLS-1$
        } else {
            in = SchematronExpressBuilder.class.getResourceAsStream("StandardXPathFunc.xml");//$NON-NLS-1$
        }
        document = builder.parse(in);
        NodeList list = document.getElementsByTagName("category");//$NON-NLS-1$
        categories = new ArrayList<XPathFunc>();
        for (int i = 0; i < list.getLength(); i++) {
            XPathFunc xpathFunc = new XPathFunc();
            Node node = list.item(i);// get the number i node
            NamedNodeMap map = node.getAttributes();

            Node nameNode = map.getNamedItem("name");//$NON-NLS-1$
            xpathFunc.setCategory(nameNode.getTextContent());

            java.util.List<KeyValue> keylist = new ArrayList<KeyValue>();
            for (int j = 0; j < node.getChildNodes().getLength(); j++) {
                Node n = node.getChildNodes().item(j);
                NamedNodeMap fmap = n.getAttributes();
                if (fmap != null && fmap.getLength() > 0) {
                    Node n1 = fmap.getNamedItem("name");//$NON-NLS-1$
                    Node n2 = fmap.getNamedItem("help");//$NON-NLS-1$
                    String help = n2.getTextContent();
                    help = help.replaceAll("\\n", "\n");//$NON-NLS-1$//$NON-NLS-2$
                    KeyValue kv = new KeyValue(n1.getTextContent(), help);
                    keylist.add(kv);
                }
            }
            Collections.sort(keylist, new Comparator<KeyValue>() {

                public int compare(KeyValue o1, KeyValue o2) {
                    if (o1 != null && o2 != null) {
                        return o1.key.compareTo(o2.key);
                    }
                    return 0;
                }

            });
            xpathFunc.setFuncs(keylist);
            categories.add(xpathFunc);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.ironiacorp.persistence.datasource.HibernateConfigurationUtil.java

public void save() {
    NodeList propertiesNodes = config.getElementsByTagName("property");
    Node parentNode = config.getElementsByTagName("session-factory").item(0);

    for (int i = 0; i < propertiesNodes.getLength(); i++) {
        Node propertyNode = propertiesNodes.item(i);

        // Get the property name
        NamedNodeMap attributesNodes = propertyNode.getAttributes();
        Node attributeNode = attributesNodes.getNamedItem("name");
        String property = attributeNode.getNodeValue();

        if (property.equals("connection.driver_class") || property.equals("connection.url")
                || property.equals("connection.username") || property.equals("connection.password")
                || property.equals("dialect") || property.equals("hibernate.connection.datasource")
                || property.equals("hibernate.connection.username")
                || property.equals("hibernate.connection.password")) {
            parentNode.removeChild(propertyNode);
        }//from  w ww.j  a v a 2s . c o  m
    }

    for (Map.Entry<String, String> property : preferences.entrySet()) {
        if (property.getValue() != null) {
            Element propertyNode = config.createElement("property");
            propertyNode.setAttribute("name", (String) property.getKey());
            // TODO: propertyNode.setTextContent((String) property.getValue());
            parentNode.appendChild(propertyNode);
        }
    }

    try {
        File configFile = new File(contextPath + configFileSufix);
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.transform(new DOMSource(config), new StreamResult(configFile));
    } catch (TransformerConfigurationException tce) {
    } catch (TransformerException te) {
    }

}