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:kenh.xscript.ScriptUtils.java

/**
 * Use <code>Node</code> to initial an <code>Element</code>.
 * @param node/*from w  w w  .  j  ava 2s.  com*/
 * @param env
 * @return
 */
private static final Element getElement(Node node, Environment env) throws UnsupportedScriptException {
    if (env == null)
        return null;

    String ns = node.getNamespaceURI(); // name space
    String name = node.getLocalName(); // name
    //String prefix = node.getPrefix(); // prefix

    Element element = env.getElement(ns, name);
    if (element == null) {
        throw new UnsupportedScriptException(null,
                "Could't find the element.[" + (StringUtils.isBlank(ns) ? name : ns + ":" + name) + "]");
    }
    element.setEnvironment(env);

    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            String attrName = attr.getNodeName();
            String attrValue = attr.getNodeValue();

            if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                if (attrName.startsWith("xmlns:")) {
                    // to add function package
                    String abbr = StringUtils.substringAfter(attrName, "xmlns:");
                    if (StringUtils.startsWithAny(abbr, "f.", "func.", "function.")) {
                        abbr = StringUtils.substringAfter(abbr, ".");
                        env.setFunctionPackage(abbr, attrValue);
                    }
                }
            } else {
                element.setAttribute(attrName, attrValue);
            }
        }
    }

    if (includeTextNode(node)) {
        String text = node.getTextContent();
        element.setText(text);
    }

    NodeList nodes = node.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element child = getElement(n, env);
                element.addChild(child);
            }
        }
    }

    return element;
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * That is a helping method that take Node and it iterate backword till root node and return the xpath expression
 * to reach that Node.//from   ww  w. ja v a  2 s  .  c o  m
 * @param node
 * @return
 */
private static String fetchXPathXpressionOfNode(Node node) {
    String nodeXPathExpression = null;
    Node n = node.getParentNode();
    while (n != null) {
        if (n.getNodeName() != null
                && (n.getNodeName().endsWith("element") || n.getNodeName().endsWith("attribute"))) {
            if (nodeXPathExpression == null) {
                nodeXPathExpression = n.getAttributes().getNamedItem("name").getNodeValue();
            } else {
                nodeXPathExpression = n.getAttributes().getNamedItem("name").getNodeValue() + "/"
                        + nodeXPathExpression;
            }
        }
        n = n.getParentNode();
    }
    return nodeXPathExpression;
}

From source file:com.twinsoft.convertigo.engine.localbuild.BuildLocally.java

private static Element cloneNode(Node node, String newNodeName) {

    Element newElement = node.getOwnerDocument().createElement(newNodeName);

    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        newElement.setAttribute(attr.getName(), attr.getValue());
    }//from   w  w w. j  av a 2 s.c  o  m

    return newElement;

}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

private static HashMap<String, String> getAttributesMap(Node currentNode) {
    NamedNodeMap attributeMap = currentNode.getAttributes();
    int attributeCount = attributeMap.getLength();
    HashMap<String, String> attributes = new HashMap<>(attributeCount);
    for (int j = 0; j < attributeCount; j++) {
        Node attr = attributeMap.item(j);
        String nodeName = attr.getNodeName();
        if (nodeName.startsWith("android:"))
            nodeName = nodeName.substring(8);
        attributes.put(nodeName, attr.getNodeValue());
    }/*from  ww  w .j  a  v a  2  s.c om*/
    return attributes;
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Receive the node, the DOM object and the new name of the node
 * /*from  w  w w . j a v  a  2 s . c  o m*/
 * @param nodo
 * @param doc
 * @param newname
 */
public static Node changeNodename(Node nodo, org.w3c.dom.Document doc, String newname) {
    // Create an element with the new name
    Node element2 = doc.createElement(newname);

    // Copy the attributes to the new element
    NamedNodeMap attrs = nodo.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }

    // Move all the children
    while (nodo.hasChildNodes()) {
        element2.appendChild(nodo.getFirstChild());
    }

    // Replace the old node with the new node
    nodo.getParentNode().replaceChild(element2, nodo);
    return element2;

}

From source file:org.dasein.cloud.terremark.Terremark.java

public static String getTaskHref(Document doc, String taskName) {
    String href = null;/*from  www .  ja v a  2s .  c  om*/
    NodeList taskElements = doc.getElementsByTagName(Terremark.TASK_TAG);
    for (int i = 0; i < taskElements.getLength(); i++) {
        Node taskElement = taskElements.item(i);
        NodeList taskChildren = taskElement.getChildNodes();
        for (int j = 0; j < taskChildren.getLength(); j++) {
            Node taskChild = taskChildren.item(j);
            if (taskChild.getNodeName().equals(Terremark.OPERATION_TAG)) {
                if (taskChild.getTextContent().equals(taskName)) {
                    href = taskElement.getAttributes().getNamedItem(Terremark.HREF).getNodeValue();
                    break;
                }
            }
        }
        if (href != null) {
            break;
        }
    }
    return href;
}

From source file:edu.eurac.commul.pepperModules.mmax2.Salt2MMAXMapping.java

public static SAnnotationSNameMatchCondition parseSAnnotationSNameMatchCondition(Node node) {
    NamedNodeMap attributes = node.getAttributes();
    Node snameAttributeNode = attributes.getNamedItem(SANN_SNAME_REGEXP);
    String nameRegExp = null;/* w w w  . ja v  a  2  s .c o  m*/
    if (snameAttributeNode != null) {
        nameRegExp = snameAttributeNode.getNodeValue();
        attributes.removeNamedItem(SANN_SNAME_REGEXP);
    } else {
        throw new PepperModuleException("'" + SANN_SNAME_REGEXP
                + "' attribute not found on SAnnotation SName Match Condition '" + node + "'");
    }
    if (attributes.getLength() != 0) {
        throw new PepperModuleException(
                "Additional unexpected attributes found on SAnnotation SName Match Condition '" + node + "'");
    }

    return new SAnnotationSNameMatchCondition(Pattern.compile(nameRegExp));
}

From source file:edu.eurac.commul.pepperModules.mmax2.Salt2MMAXMapping.java

public static SAnnotationSLayerMatchCondition parseSAnnotationSLayerMatchCondition(Node node) {
    NamedNodeMap attributes = node.getAttributes();
    Node slayerAttributeNode = attributes.getNamedItem(SANN_SLAYER_REGEXP);
    String slayerRegExp = null;//from w  w  w  .ja v a  2  s  .c  om
    if (slayerAttributeNode != null) {
        slayerRegExp = slayerAttributeNode.getNodeValue();
        attributes.removeNamedItem(SANN_SLAYER_REGEXP);
    } else {
        throw new PepperModuleException("'" + SANN_SLAYER_REGEXP
                + " attribute not found on SAnnotation SLayer Match Condition '" + node + "'");
    }
    if (attributes.getLength() != 0) {
        throw new PepperModuleException(
                "Additional unexpected attributes found on SAnnnotation SLayer Match Condition '" + node + "'");
    }

    return new SAnnotationSLayerMatchCondition(Pattern.compile(slayerRegExp));
}

From source file:edu.eurac.commul.pepperModules.mmax2.Salt2MMAXMapping.java

public static SRelationSNameMatchCondition parseSRelationSNameMatchCondition(Node node) {
    NamedNodeMap attributes = node.getAttributes();
    Node snameAttributeNode = attributes.getNamedItem(SREL_SNAME_REGEXP);
    String snameRegExp = null;//  w w w.  j a  v a 2 s.  c  om
    if (snameAttributeNode != null) {
        snameRegExp = snameAttributeNode.getNodeValue();
        attributes.removeNamedItem(SREL_SNAME_REGEXP);
    } else {
        throw new PepperModuleException("'" + SREL_SNAME_REGEXP
                + " attribute not found on SRelation SName Match Condition '" + node + "'");
    }
    if (attributes.getLength() != 0) {
        throw new PepperModuleException(
                "Additional unexpected attributes found on SRelation SName Match Condition '" + node + "'");
    }

    return new SRelationSNameMatchCondition(Pattern.compile(snameRegExp));
}

From source file:edu.eurac.commul.pepperModules.mmax2.Salt2MMAXMapping.java

public static SRelationSTypeMatchCondition parseSRelationSTypeMatchCondition(Node node) {
    NamedNodeMap attributes = node.getAttributes();
    Node stypeAttributeNode = attributes.getNamedItem(SREL_STYPE_REGEXP);
    String stypeRegExp = null;/*from  w w w. j  a v  a2 s  . c  o m*/
    if (stypeAttributeNode != null) {
        stypeRegExp = stypeAttributeNode.getNodeValue();
        attributes.removeNamedItem(SREL_STYPE_REGEXP);
    } else {
        throw new PepperModuleException("'" + SREL_STYPE_REGEXP
                + " attribute not found on SRelation SType Match Condition '" + node + "'");
    }
    if (attributes.getLength() != 0) {
        throw new PepperModuleException(
                "Additional unexpected attributes found on SRelation SType Match Condition '" + node + "'");
    }

    return new SRelationSTypeMatchCondition(Pattern.compile(stypeRegExp));
}