Example usage for org.w3c.dom Attr isId

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

Introduction

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

Prototype

public boolean isId();

Source Link

Document

Returns whether this attribute is known to be of type ID (i.e.

Usage

From source file:Main.java

/**
 * Setup the ID attribute into <code>destElement</code> depending on the <code>isId</code> flag of an attribute of
 * <code>sourceNode</code>./*ww  w .j a v a  2 s  . c o m*/
 *
 * @param sourceNode
 * @param destDocElement
 */
public static void propagateIDAttributeSetup(Node sourceNode, Element destElement) {
    NamedNodeMap nnm = sourceNode.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.isId()) {
            destElement.setIdAttribute(attr.getName(), true);
            break;
        }
    }
}

From source file:Main.java

/**
 * Gets the ID attribute of a DOM element.
 * /*from   w  w w. j a v  a2 s  .  com*/
 * @param domElement the DOM element
 * 
 * @return the ID attribute or null if there isn't one
 */
public static Attr getIdAttribute(Element domElement) {
    if (!domElement.hasAttributes()) {
        return null;
    }

    NamedNodeMap attributes = domElement.getAttributes();
    Attr attribute;
    for (int i = 0; i < attributes.getLength(); i++) {
        attribute = (Attr) attributes.item(i);
        if (attribute.isId()) {
            return attribute;
        }
    }

    return null;
}

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.
 *///from ww  w  . j a  va 2  s.co 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

/**
 * 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.
 *//*from w w w  . ja  v a  2  s  .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:org.dita.dost.util.XMLUtils.java

/**
 * Add or set attribute. Convenience method for {@link #addOrSetAttribute(AttributesImpl, String, String, String, String, String)}.
 * /*from   w ww. jav a  2 s  .c o  m*/
 * @param atts attributes
 * @param att attribute node
 */
public static void addOrSetAttribute(final AttributesImpl atts, final Node att) {
    if (att.getNodeType() != Node.ATTRIBUTE_NODE) {
        throw new IllegalArgumentException();
    }
    final Attr a = (Attr) att;
    String localName = a.getLocalName();
    if (localName == null) {
        localName = a.getName();
        final int i = localName.indexOf(':');
        if (i != -1) {
            localName = localName.substring(i + 1);
        }
    }
    addOrSetAttribute(atts, a.getNamespaceURI() != null ? a.getNamespaceURI() : NULL_NS_URI, localName,
            a.getName() != null ? a.getName() : localName, a.isId() ? "ID" : "CDATA", a.getValue());
}