Example usage for org.w3c.dom Node getAttributes

List of usage examples for org.w3c.dom Node getAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Node getAttributes.

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

private static Document insertEntryKey(Document doc, File[] runtimeJarFiles, File[] toolsJarFiles,
        String partnerName) throws IOException {

    NodeList beans_list = doc.getElementsByTagName("beans");
    Node beans_node = beans_list.item(0);

    // First delete old entries

    NodeList beansChildren = beans_node.getChildNodes();
    Node bean_node = null, prop_node = null, map_node = null;
    for (int i = 0; i < beansChildren.getLength(); i++) {
        if (!beansChildren.item(i).getNodeName().equals("bean")) {
            continue;
        }/*from   w  w w  . ja va 2 s .  co  m*/
        bean_node = beansChildren.item(i);
        NodeList beanChildren = bean_node.getChildNodes();
        for (int j = 0; j < beanChildren.getLength(); j++) {
            if (!beanChildren.item(j).getNodeName().equals("property")) {
                continue;
            }

            prop_node = beanChildren.item(j);
            break;
        }

        if (prop_node == null) {
            continue;
        }

        NodeList propChildren = prop_node.getChildNodes();
        for (int k = 0; k < propChildren.getLength(); k++) {
            if (!propChildren.item(k).getNodeName().equals("map")) {
                continue;
            }

            map_node = propChildren.item(k);
            break;
        }

        if (map_node == null) {
            continue;
        }

        NodeList mapChildren = map_node.getChildNodes();
        List<Node> oldEntryList = new ArrayList<Node>();

        for (int l = 0; l < mapChildren.getLength(); l++) {
            if (!mapChildren.item(l).getNodeName().equals("entry")) {
                continue;
            }
            Node target = mapChildren.item(l);
            NamedNodeMap entry_attributes = target.getAttributes();
            for (int m = 0; m < entry_attributes.getLength(); m++) {
                Node entryAttr = entry_attributes.item(m);
                if (entryAttr.getNodeName().equals("key") && entryAttr.getNodeValue().equals(partnerName)) {
                    oldEntryList.add(target);
                    break;
                }
            }
        }

        if (oldEntryList.size() > 0) {
            for (Node oldEntry : oldEntryList) {
                map_node.removeChild(oldEntry);
            }
        }

        // Now, add new entries

        NamedNodeMap bean_attributes = bean_node.getAttributes();
        for (int m = 0; m < bean_attributes.getLength(); m++) {
            Node beanAttr = bean_attributes.item(m);
            if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsLoginManagerBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, LOGIN_MANAGER)
                        || classExistsInJar(toolsJarFiles, partnerName, LOGIN_MANAGER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + LOGIN_MANAGER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsRestImporterBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, REST_IMPORTER)
                        || classExistsInJar(toolsJarFiles, partnerName, REST_IMPORTER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + REST_IMPORTER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsRestWsdlGeneratorBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, REST_WSDL_GENERATOR)
                        || classExistsInJar(toolsJarFiles, partnerName, REST_WSDL_GENERATOR)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + REST_WSDL_GENERATOR);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsServiceModifierBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, SERVICE_MODIFIER)
                        || classExistsInJar(toolsJarFiles, partnerName, SERVICE_MODIFIER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + SERVICE_MODIFIER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsRestServiceGeneratorBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, REST_SERVICE_IMPORTER)
                        || classExistsInJar(toolsJarFiles, partnerName, REST_SERVICE_IMPORTER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + REST_SERVICE_IMPORTER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsResponseProcessorBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, RESPONSE_PROCESSOR)
                        || classExistsInJar(toolsJarFiles, partnerName, RESPONSE_PROCESSOR)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + RESPONSE_PROCESSOR);
                    map_node.appendChild(newEntry);
                }
                break;
            }
        }
    }

    return doc;
}

From source file:Main.java

public static boolean updateDBConfig(String userName, String password, String name, String driver, String url,
        String dbConfigPath) throws XPathExpressionException, ParserConfigurationException, SAXException,
        IOException, TransformerException {
    Document document = loadXML(dbConfigPath);

    XPath path = XPathFactory.newInstance().newXPath();
    XPathExpression express = path.compile(
            "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Set[@name='className']");

    NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    Node node = nodes.item(0);
    node.setTextContent(driver);/*ww  w  . j a v  a 2  s  .c  o m*/
    path.reset();

    express = path.compile(
            "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Get['@name=driverProperties']/Put");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);

    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if ("user".equals(node.getAttributes().item(0).getTextContent())) {
            node.setTextContent(userName);
        } else if ("password".equals(node.getAttributes().item(0).getTextContent())) {
            node.setTextContent(password);
        } else if ("URL".equals(node.getAttributes().item(0).getTextContent())) {
            //            String url = node.getTextContent();
            //            System.out.println("basic url ---> " + url + "; name -->" + name);
            //            url = replaceDBName("examples", name, url);
            //            System.out.println("dist url ---> " + url);
            node.setTextContent(url);
        }
    }
    path.reset();

    return updateXML(document, dbConfigPath);
}

From source file:Main.java

/**
 * Print a Node tree recursively.//from  www  . ja  va 2 s  . c o  m
 * @param node A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    int type = node.getNodeType();

    switch (type) {
    // print element with attributes
    case Node.ELEMENT_NODE: {
        xml.append('<');
        xml.append(node.getNodeName());

        NamedNodeMap attrs = node.getAttributes();
        int length = attrs.getLength();
        ;

        for (int i = 0; i < length; i++) {
            Attr attr = (Attr) attrs.item(i);
            xml.append(' ');
            xml.append(attr.getNodeName());
            xml.append("=\"");

            //xml.append(normalize(attr.getNodeValue()));
            xml.append(attr.getNodeValue());
            xml.append('"');
        }

        xml.append('>');

        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        xml.append("<![CDATA[");
        xml.append(node.getNodeValue());
        xml.append("]]>");

        break;
    }

    // print text
    case Node.TEXT_NODE: {
        //xml.append(normalize(node.getNodeValue()));
        xml.append(node.getNodeValue());

        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        xml.append("<?");
        xml.append(node.getNodeName());

        String data = node.getNodeValue();

        if ((data != null) && (data.length() > 0)) {
            xml.append(' ');
            xml.append(data);
        }

        xml.append("?>");

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        xml.append("</");
        xml.append(node.getNodeName());
        xml.append('>');
    }

    return xml.toString();
}

From source file:com.idynin.ftbutils.ModPack.java

/**
 * Lightly modified from FTB net.ftb.workers.ModpackLoader
 * // w w w.  java 2  s.c om
 * @param modPackFilter
 */
public static void populateModpacks(String server, String modpackMetaPath, ModPackFilter modPackFilter) {
    InputStream modPackStream = null;
    if (modPackStream == null) {
        try {
            modPackStream = new URL("http://" + server + modpackMetaPath).openStream();
        } catch (IOException e) {
            System.err.println("Completely unable to download the modpack file - check your connection");
            e.printStackTrace();
        }
    }
    if (modPackStream != null) {
        Document doc;
        try {
            doc = getXML(modPackStream);
        } catch (Exception e) {
            System.err.println("Exception reading modpack file");
            e.printStackTrace();
            return;
        }
        if (doc == null) {
            System.err.println("Error: could not load modpack data!");
            return;
        }
        NodeList modPacks = doc.getElementsByTagName("modpack");
        ModPack mp;
        for (int i = 0; i < modPacks.getLength(); i++) {
            Node modPackNode = modPacks.item(i);
            NamedNodeMap modPackAttr = modPackNode.getAttributes();
            try {
                mp = new ModPack(modPackAttr.getNamedItem("name").getTextContent(),
                        modPackAttr.getNamedItem("author").getTextContent(),
                        modPackAttr.getNamedItem("version").getTextContent(),
                        modPackAttr.getNamedItem("logo").getTextContent(),
                        modPackAttr.getNamedItem("url").getTextContent(),
                        modPackAttr.getNamedItem("image").getTextContent(),
                        modPackAttr.getNamedItem("dir").getTextContent(),
                        modPackAttr.getNamedItem("mcVersion").getTextContent(),
                        modPackAttr.getNamedItem("serverPack").getTextContent(),
                        modPackAttr.getNamedItem("description").getTextContent(),
                        modPackAttr.getNamedItem("mods") != null
                                ? modPackAttr.getNamedItem("mods").getTextContent()
                                : "",
                        modPackAttr.getNamedItem("oldVersions") != null
                                ? modPackAttr.getNamedItem("oldVersions").getTextContent()
                                : "",
                        modPackAttr.getNamedItem("animation") != null
                                ? modPackAttr.getNamedItem("animation").getTextContent()
                                : "",
                        modPackAttr.getNamedItem("maxPermSize") != null
                                ? modPackAttr.getNamedItem("maxPermSize").getTextContent()
                                : "",
                        (ModPack.getPackArray().isEmpty() ? 0 : ModPack.getPackArray().size()), false,
                        "modpacks.xml",
                        modPackAttr.getNamedItem("bundledMap") != null
                                ? modPackAttr.getNamedItem("bundledMap").getTextContent()
                                : "",
                        modPackAttr.getNamedItem("customTP") != null ? true : false);
                if (modPackFilter == null || modPackFilter.accept(mp)) {
                    ModPack.addPack(mp);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        IOUtils.closeQuietly(modPackStream);
    }
}

From source file:eu.europa.ec.markt.dss.validation102853.xml.XmlNode.java

/**
 * @param xmlNode the {@code XmlNode} to which the element is added
 * @param element the {@code Node} to be copied
 */// w  w  w . ja v a2s.  c  o m
private static void recursiveCopy(final XmlNode xmlNode, final Node element) {

    final String name = element.getNodeName();
    final XmlNode _xmlNode = new XmlNode(name);
    final NamedNodeMap attributes = element.getAttributes();
    for (int jj = 0; jj < attributes.getLength(); jj++) {

        final Node attrNode = attributes.item(jj);
        final String attrName = attrNode.getNodeName();
        if (!"xmlns".equals(attrName)) {

            _xmlNode.setAttribute(attrName, attrNode.getNodeValue());
        }
    }

    final NodeList nodes = element.getChildNodes();
    boolean hasElementNodes = false;
    for (int ii = 0; ii < nodes.getLength(); ii++) {

        final Node node = nodes.item(ii);
        if (node.getNodeType() == Node.ELEMENT_NODE) {

            hasElementNodes = true;
            recursiveCopy(_xmlNode, node);
        }
    }
    if (!hasElementNodes) {

        final String value = element.getTextContent();
        _xmlNode.setValue(value);
    }
    _xmlNode.setParent(xmlNode);
}

From source file:Main.java

/**
 * Gets value of an attribute from node/*from  w  ww.java 2  s .  c o  m*/
 * 
 * @param node
 *            Node Object
 * @param attributeName
 *            Attribute Name
 * @return Attribute Value
 * @throws IllegalArgumentException
 *             for Invalid input
 */
public static String getAttribute(final Node node, final String attributeName) throws IllegalArgumentException {
    // Validate attribute name
    if (attributeName == null) {
        throw new IllegalArgumentException("Attribute Name cannot be null in XmlUtils.getAttribute method");
    }

    // Validate node
    if (node == null) {
        throw new IllegalArgumentException(
                "Node cannot be null in XmlUtils.getAttribute method for attribute name:" + attributeName);
    }

    final NamedNodeMap attributeList = node.getAttributes();
    if (attributeList != null) {
        final Node attribute = attributeList.getNamedItem(attributeName);
        return attribute == null ? null : ((Attr) attribute).getValue();
    } else {
        return null;
    }

}

From source file:jp.go.nict.langrid.bpel.ProcessAnalyzer.java

/**
 * //  w  w w .  j  a  va 2s  . com
 * 
 */
public static BPEL analyzeBPEL(byte[] body) throws SAXException, URISyntaxException, XPathExpressionException {
    Document doc;
    try {
        doc = builder.parse(new ByteArrayInputStream(body));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Node root = doc.getDocumentElement();
    NamespaceContextImpl nsc = new NamespaceContextImpl();
    addNamespaceMappings(nsc, root.getAttributes());
    String rootNs = root.getNamespaceURI();
    nsc.addMapping("_", rootNs);
    XPath xpath = DocumentUtil.getDefaultXPath();
    xpath.setNamespaceContext(nsc);
    String processName = NodeUtil.getAttribute(root, "name");
    BPEL bpel = new BPEL();
    bpel.setBody(body);
    if (rootNs.equals(bpel4ws_1_1_ns)) {
        bpel.setBpelVersion(BPELVersion.BPEL4WS_1_1);
    } else if (rootNs.equals(wsbpel_2_0_ns)) {
        bpel.setBpelVersion(BPELVersion.WSBPEL_2_0);
    } else {
        bpel.setBpelVersion(BPELVersion.UNKNOWN);
    }
    bpel.setTargetNamespace(new URI(xpath.evaluate("/_:process/@targetNamespace", root)));
    bpel.setProcessName(processName);
    bpel.setFilename(processName + BPEL_EXTENSION);
    ArrayList<PartnerLink> links = new ArrayList<PartnerLink>();
    NodeList list = (NodeList) xpath.evaluate("/_:process/_:partnerLinks/_:partnerLink", root,
            XPathConstants.NODESET);
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        PartnerLink pl = new PartnerLink(nsc, node);
        links.add(pl);
    }
    bpel.setPartnerLinks(links);
    return bpel;
}

From source file:de.akra.idocit.wsdl.services.DocumentationParser.java

/**
 * Extracts from the docpart element the addressees with their individual
 * documentation.//  w  ww.ja v a  2  s .  c o  m
 * 
 * @param docpartElem
 *            The docpart element that should be parsed.
 * @return A new {@link Documentation} with the documentations for the addresses.
 */
private static Documentation parseAddressees(Node docpartElem) {
    Documentation doc = new Documentation();
    Map<Addressee, String> docMap = doc.getDocumentation();
    List<Addressee> addresseeSequence = doc.getAddresseeSequence();

    NodeList childList = docpartElem.getChildNodes();

    // iterate all child nodes to find addressee elements
    for (int i = 0; i < childList.getLength(); i++) {
        Node item = childList.item(i);

        // if found addressee element, add the documentation to the map
        // in general there should be only addressee elements
        if (item.getNodeName().equals(ADDRESSEE_ELEMENT_NAME)) {
            NamedNodeMap attributes = item.getAttributes();
            Node attribute = attributes.getNamedItem(ADDRESSEE_GROUP_ATTRIBUTE_NAME);

            Addressee addressee = DescribedItemUtils.findAddressee(attribute.getNodeValue());
            logger.log(Level.FINE, attribute.getNodeName() + " -> " + attribute.getNodeValue() + " | Addressee="
                    + addressee.getName());

            String text = readTextFromAddresseElement(item);

            // add documentation for addressee
            docMap.put(addressee, text);

            // save sequence of adding addressees
            addresseeSequence.add(addressee);

        } else {
            logger.log(Level.FINE, "Expect <" + ADDRESSEE_ELEMENT_NAME + ">, but found: name="
                    + item.getNodeName() + ", type=" + item.getNodeType());
        }
    }

    return doc;
}

From source file:hoot.services.geo.BoundingBox.java

/**
 * Creates a bounding box from an OSM XML bounds node
 * /*  www . ja v a  2 s  .c  o  m*/
 * @param xmlBounds OSM XML bounds node
 * @return a bounding box
 * @throws Exception 
 * @throws DOMException 
 * @throws NumberFormatException 
 */
public static BoundingBox fromXml(final Node xml) throws NumberFormatException, DOMException, Exception {
    NamedNodeMap nodeAttributes = xml.getAttributes();
    return new BoundingBox(Double.parseDouble(nodeAttributes.getNamedItem("minLon").getNodeValue()),
            Double.parseDouble(nodeAttributes.getNamedItem("minLat").getNodeValue()),
            Double.parseDouble(nodeAttributes.getNamedItem("maxLon").getNodeValue()),
            Double.parseDouble(nodeAttributes.getNamedItem("maxLat").getNodeValue()));
}

From source file:Main.java

public static void collectNamespaces(Node node, Map namespaces, Map prefixes) {
    if (node == null) {
        throw new IllegalArgumentException("nullArgument: node");
        //                    i18n.getMessage("nullArgument", "node"));
    }/*  ww w .  ja  v a  2 s.co m*/
    if (namespaces == null) {
        throw new IllegalArgumentException("nullArgument: namespace");
        //                    i18n.getMessage("nullArgument", "namespaces"));
    }
    if (prefixes == null) {
        prefixes = new HashMap();
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            String name = attr.getName();
            String value = attr.getValue();
            if (name.startsWith("xmlns:")) {
                if (namespaces.get(value) == null) {
                    // ns not defined
                    if (prefixes.get(name) != null) {
                        // find unique prefix
                        int j = 1;
                        do {
                            name = "xmlns:ns" + j++;
                        } while (prefixes.get(name) != null);
                    }
                    prefixes.put(name, value);
                    namespaces.put(value, name);
                }
            }
        }
    }
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                collectNamespaces(child, namespaces, prefixes);
            }
        }
    }
}