Example usage for org.w3c.dom Attr getValue

List of usage examples for org.w3c.dom Attr getValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr getValue.

Prototype

public String getValue();

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

From source file:Main.java

private static void convertAttribute(Attr toCopy, Element saveTo, Document doc) {
    if (toCopy.getNamespaceURI() == null || !toCopy.getNamespaceURI().equals(xmlnsURI)) {
        saveTo.setAttributeNS(toCopy.getNamespaceURI(), toCopy.getNodeName(), toCopy.getValue());
    }//from w  w w.j a va2 s.  c o m
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
 * If it is not found, returns a null.// www  .ja v  a  2  s  .co  m
 * @param nnm NamedNodeMap
 * @param name String
 * @return String
 */
public static String getAttributeValueByName(final NamedNodeMap nnm, final String name) {
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            return attr.getValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
 * If it is not found, returns a null.//from  w w  w  .  j a  v  a2s .  c  om
 * @param nnm NamedNodeMap
 * @param name String
 * @return String
 */
public static String getAttributeValueByName(NamedNodeMap nnm, String name) {
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            return attr.getValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
 * If it is not found, returns a null.//from  w  w w.j a v a 2  s .  co  m
 * @param nnm NamedNodeMap
 * @param name String
 * @return String
 */
public static String getAttributeValueByName(NamedNodeMap nnm, String name) {
    if (nnm == null)
        return null;
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            return attr.getValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean.
 * @param nnm NamedNodeMap//from   w ww.  ja v a 2 s .  c o  m
 * @param name String
 * @throws RuntimeException on any failure to parse a boolean
 * @return boolean
 */
public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException {
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            String tmp = attr.getValue().toLowerCase();
            if (tmp.equalsIgnoreCase("true"))
                return true;
            if (tmp.equalsIgnoreCase("false"))
                return false;
            throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp);
        }
    }
    throw new RuntimeException("Attribute " + name + " not found.");
}

From source file:Main.java

/**
 * This method is a tree-search to help prevent against wrapping attacks. It checks that no other
 * Element than the given "knownElement" argument has an ID attribute that matches the "value"
 * argument, which is the ID value of "knownElement". If this is the case then "false" is returned.
 *//* w  ww  . java 2  s  . c o m*/
public static boolean protectAgainstWrappingAttack(Node startNode, Element knownElement, String value) {
    Node startParent = startNode.getParentNode();
    Node processedNode = null;

    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }

    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;

            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr) attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) {
                        //log.debug("Multiple elements with the same 'Id' attribute value!");
                        return false;
                    }
                }
            }
        }

        processedNode = startNode;
        startNode = startNode.getFirstChild();

        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }

        // no more siblings, get parent, all children
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}

From source file:Main.java

private static void fixupAttrsSingle(Element e) throws DOMException {
    removeXmlBase(e);//w w w .j a va 2  s . co  m
    Map<String, String> replace = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    for (int j = 0; j < attrs.getLength(); j++) {
        Attr attr = (Attr) attrs.item(j);
        if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N
            replace.put(attr.getName(), attr.getValue());
        }
    }
    for (Map.Entry<String, String> entry : replace.entrySet()) {
        e.removeAttribute(entry.getKey());
        e.setAttributeNS(null, entry.getKey(), entry.getValue());
    }
}

From source file:Main.java

/**
 * This method is a tree-search to help prevent against wrapping attacks. It checks that no
 * two Elements have ID Attributes that match the "value" argument, if this is the case then
 * "false" is returned. Note that a return value of "true" does not necessarily mean that
 * a matching Element has been found, just that no wrapping attack has been detected.
 *///ww w .jav  a 2s .  co m
public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
    Node startParent = startNode.getParentNode();
    Node processedNode = null;
    Element foundElement = null;

    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }

    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;

            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr) attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue())) {
                        if (foundElement == null) {
                            // Continue searching to find duplicates
                            foundElement = attr.getOwnerElement();
                        } else {
                            //log.debug("Multiple elements with the same 'Id' attribute value!");
                            return false;
                        }
                    }
                }
            }
        }

        processedNode = startNode;
        startNode = startNode.getFirstChild();

        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }

        // no more siblings, get parent, all children
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}

From source file:DOMEdit.java

public static void dupAttributes(Document doc) {
        Element root = doc.getDocumentElement();
        Element personOne = (Element) root.getFirstChild();

        Attr deptAttr = personOne.getAttributeNode("dept");
        personOne.removeAttributeNode(deptAttr);
        String deptString = deptAttr.getValue();
        personOne.setAttribute("dept", deptString + " updated");

        String mailString = personOne.getAttribute("mail");
        personOne.setAttribute("mail", mailString + " updated");

        String titleString = personOne.getAttribute("title");
        //personOne.removeAttribute("title");
        personOne.setAttribute("title", titleString + " updated");
    }// w  ww  . ja  v a 2s  .  c  o m

From source file:Main.java

/**
 * Returns the value of the named attribute of the current element.
 *
 * @param parent// ww  w  .j a v a 2  s  . c  om
 * @param localName attribute local name or 'nodeName' if no namespace is
 * specified.
 * @param  namespaceURI or <code>null</code>
 * @return attribute value, or <code>null</code> if not found
 */
public static String getAttribute(Element parent, String localName, String namespaceURI) {
    if (parent == null) {
        return null;
    }
    Attr attribute;
    if (namespaceURI == null) {
        attribute = parent.getAttributeNode(localName);
    } else {
        attribute = parent.getAttributeNodeNS(namespaceURI, localName);
    }
    if (attribute != null) {
        return attribute.getValue();
    } else {
        return null;
    }
}