Example usage for org.w3c.dom Node ATTRIBUTE_NODE

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

Introduction

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

Prototype

short ATTRIBUTE_NODE

To view the source code for org.w3c.dom Node ATTRIBUTE_NODE.

Click Source Link

Document

The node is an Attr.

Usage

From source file:com.gargoylesoftware.htmlunit.activex.javascript.msxml.XMLDOMDocument.java

/**
 * Creates a node using the supplied type, name, and namespace.
 * @param type a value that uniquely identifies the node type
 * @param name the value for the new node's <code>nodeName</code> property
 * @param namespaceURI the namespace URI.
 *        If specified, the node is created in the context of the namespaceURI parameter
 *        with the prefix specified on the node name.
 *        If the name parameter does not have a prefix, this is treated as the default namespace.
 * @return the newly created node//from   w  w  w . jav a  2  s. c o  m
 */
@JsxFunction
public Object createNode(final Object type, final String name, final Object namespaceURI) {
    switch ((short) Context.toNumber(type)) {
    case Node.ELEMENT_NODE:
        return createElementNS((String) namespaceURI, name);
    case Node.ATTRIBUTE_NODE:
        return createAttribute(name);

    default:
        throw Context
                .reportRuntimeError("xmlDoc.createNode(): Unsupported type " + (short) Context.toNumber(type));
    }
}

From source file:com.twinsoft.convertigo.beans.core.Step.java

protected String getNodeValue(Node node) {
    if (node != null) {
        int len;//  ww  w  .  j av a 2  s .  c o m
        int nodeType = node.getNodeType();
        switch (nodeType) {
        case Node.ELEMENT_NODE:
            if (sequence.getProject().isStrictMode()) {
                return XMLUtils.prettyPrintElement((Element) node, true, false);
            } else {
                len = node.getChildNodes().getLength();
                Node firstChild = node.getFirstChild();
                if (firstChild != null) {
                    int firstChildType = firstChild.getNodeType();
                    switch (firstChildType) {
                    case Node.CDATA_SECTION_NODE:
                    case Node.TEXT_NODE:
                        return ((len < 2) ? firstChild.getNodeValue() : XMLUtils.getNormalizedText(node));
                    case Node.ELEMENT_NODE:
                        return XMLUtils.prettyPrintElement((Element) node, true, false);
                    default:
                        return null;
                    }
                } else {
                    if (Engine.logBeans.isInfoEnabled())
                        Engine.logBeans.warn("Applied XPath on step '" + this
                                + "' returned node with null value ('" + node.getNodeName() + "')");
                    return null;
                }
            }
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            len = node.getChildNodes().getLength();
            return ((len < 2) ? node.getNodeValue() : XMLUtils.getNormalizedText(node));
        case Node.ATTRIBUTE_NODE:
            return node.getNodeValue();
        default:
            if (Engine.logBeans.isInfoEnabled())
                Engine.logBeans.warn("Applied XPath on step '" + this + "' is not supported");
            return null;
        }
    }
    return null;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * copies all attributes from one Element to another
 *
 * @param from   - the Element which the source attributes
 * @param to     - the target Element for the Attributes
 * @param filter - a NodeFilter to apply during copy
 */// www .ja  va2s  .  co  m
public static void copyAttributes(Element from, Element to, NodeFilter filter) {
    if ((from != null) && (to != null)) {
        NamedNodeMap map = from.getAttributes();

        /* if filter is null use our own default filter, which accepts
           everything (this saves us from always check if filter is
           null */
        if (filter == null) {
            filter = new NodeFilter() {
                public short acceptNode(Node n) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            };
        }

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

            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);

                if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (filter.acceptNode(attr) == NodeFilter.FILTER_ACCEPT) {
                        to.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
                    }
                }
            }
        }
    }
}

From source file:de.betterform.xml.xforms.model.Instance.java

/**
 * Deletes the specified node.//from   www. j a  v a 2s.c om
 *
 * @param path the path pointing to the node to be deleted.
 */
public boolean deleteNode(Node node, String path) throws XFormsException {
    String canonicalPath = DOMUtil.getCanonicalPath(node);
    if (node == null) {
        LOGGER.warn("Node is null - delete is terminated with no effect.");
        return false;
    }

    //don't delete readonly nodes
    if (isReadonly(node)) {
        LOGGER.warn("Node or one of it's parents is readonly - delete is terminated with no effect.");
        return false;
    }

    //don't delete content of a xmlns Attribute - not clear what Spec means by not allowing to delete a namespace node
    if (node.getNodeName().startsWith("xmlns")) {
        LOGGER.warn("Node is Namespace declaration - delete is terminated with no effect.");
        return false;
    }

    //don't delete root nodes
    if (node.getNodeType() != Node.ATTRIBUTE_NODE && node.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
        LOGGER.warn("Node is a root Node - delete is terminated with no effect.");
        return false;
    }

    //don't delete document nodes
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        LOGGER.warn("Node is a Document Node - delete is terminated with no effect.");
        return false;
    }

    Node canonNode = node;

    if (node.getNodeType() != Node.ATTRIBUTE_NODE) {
        node.getParentNode().removeChild(node);
    } else {
        Attr attr = (Attr) node;
        attr.getOwnerElement().removeAttributeNode(attr);
    }

    // dispatch internal betterform event (for instant repeat updating)
    String[] canonicalParts = XPathUtil.getNodesetAndPredicates(path);
    HashMap map = new HashMap();
    map.put("nodeset", canonicalParts[0]);
    map.put("position", canonicalParts[canonicalParts.length - 1]);
    map.put("canonPath", canonicalPath);
    this.container.dispatch(this.target, BetterFormEventNames.NODE_DELETED, map);

    if (getLogger().isDebugEnabled()) {
        getLogger().debug(
                this + " delete node: instance data after manipulation" + toString(this.instanceDocument));
    }
    return true;
}

From source file:com.crawljax.plugins.errorreport.ErrorReport.java

private Document addMarker(String id, Document doc, String xpath) {
    try {//www. ja  v a 2  s  .c  om

        String prefixMarker = "###BEGINMARKER" + id + "###";
        String suffixMarker = "###ENDMARKER###";

        NodeList nodeList = XPathHelper.evaluateXpathExpression(doc, xpath);

        if (nodeList.getLength() == 0 || nodeList.item(0) == null) {
            return doc;
        }
        Node element = nodeList.item(0);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            Node beginNode = doc.createTextNode(prefixMarker);
            Node endNode = doc.createTextNode(suffixMarker);

            element.getParentNode().insertBefore(beginNode, element);
            if (element.getNextSibling() == null) {
                element.getParentNode().appendChild(endNode);
            } else {
                element.getParentNode().insertBefore(endNode, element.getNextSibling());
            }
        } else if (element.getNodeType() == Node.TEXT_NODE && element.getTextContent() != null) {
            element.setTextContent(prefixMarker + element.getTextContent() + suffixMarker);
        } else if (element.getNodeType() == Node.ATTRIBUTE_NODE) {
            element.setNodeValue(prefixMarker + element.getTextContent() + suffixMarker);
        }

        return doc;
    } catch (Exception e) {
        return doc;
    }
}

From source file:mondrian.test.DiffRepository.java

private static void writeNode(Node node, XMLOutput out) {
    final NodeList childNodes;
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.print("<?xml version=\"1.0\" ?>" + Util.nl);
        childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            writeNode(child, out);//from w ww.j a v  a  2 s  .co m
        }
        //            writeNode(((Document) node).getDocumentElement(), out);
        break;

    case Node.ELEMENT_NODE:
        Element element = (Element) node;
        final String tagName = element.getTagName();
        out.beginBeginTag(tagName);
        // Attributes.
        final NamedNodeMap attributeMap = element.getAttributes();
        for (int i = 0; i < attributeMap.getLength(); i++) {
            final Node att = attributeMap.item(i);
            out.attribute(att.getNodeName(), att.getNodeValue());
        }
        out.endBeginTag(tagName);
        // Write child nodes, ignoring attributes but including text.
        childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
                continue;
            }
            writeNode(child, out);
        }
        out.endTag(tagName);
        break;

    case Node.ATTRIBUTE_NODE:
        out.attribute(node.getNodeName(), node.getNodeValue());
        break;

    case Node.CDATA_SECTION_NODE:
        CDATASection cdata = (CDATASection) node;
        out.cdata(cdata.getNodeValue(), true);
        break;

    case Node.TEXT_NODE:
        Text text = (Text) node;
        final String wholeText = text.getNodeValue();
        if (!isWhitespace(wholeText)) {
            out.cdata(wholeText, false);
        }
        break;

    case Node.COMMENT_NODE:
        Comment comment = (Comment) node;
        out.print("<!--" + comment.getNodeValue() + "-->" + Util.nl);
        break;

    default:
        throw new RuntimeException("unexpected node type: " + node.getNodeType() + " (" + node + ")");
    }
}

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

/**
 * Iterate through the DOM tree/* w  w  w .  ja  v a2  s.  c  o  m*/
 * 
 * @param node
 * @param output
 * @throws IOException
 */
public static void writeNode(Node node, Writer output) throws IOException {

    int type = node.getNodeType();

    switch (type) {
    case Node.ATTRIBUTE_NODE:
        output.write(' ');
        output.write(node.getNodeName());
        output.write("=\"");
        writeXMLData(node.getNodeValue(), true, output);
        output.write('"');
        break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        writeXMLData(node.getNodeValue(), false, output);
        break;
    case Node.COMMENT_NODE:
        output.write("<!--");
        output.write(((Comment) node).getNodeValue());
        output.write("-->");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ELEMENT_NODE: {
        NamedNodeMap atts = node.getAttributes();

        output.write('<');
        output.write(node.getNodeName());
        if (atts != null) {
            int length = atts.getLength();
            for (int i = 0; i < length; i++)
                writeNode(atts.item(i), output);
        }

        if (node.hasChildNodes()) {
            output.write('>');
            writeNodes(node.getChildNodes(), output);
            output.write("</");
            output.write(node.getNodeName());
            output.write('>');
        } else {
            output.write("/>");
        }
        break;
    }
    case Node.ENTITY_NODE:
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.NOTATION_NODE:
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        throw new Error("Unexpected DOM node type: " + type);
    }
}

From source file:cz.muni.fi.mir.mathmlunificator.MathMLUnificator.java

/**
 * <p>/*from   w ww.j a va2 s.c  o  m*/
 * Test whether the given node is unified math node. The node is considered
 * to be unified math node if
 * </p>
 * <ol>
 * <li>the node is not <code>null</code>,</li>
 * <li>the node is math node,</li>
 * <li>the node has current unification level attribute (see
 * {@link Constants#UNIFIED_MATHML_LEVEL_ATTR}) in MathML Unification XML
 * namespace (see {@link Constants#UNIFIED_MATHML_NS}) with valid value,
 * i.e. integer greather than 0.</li>
 * </ol>
 * <p>
 * The method is XML namespace aware and expects the node DOM to be build as
 * XML aware (see
 * {@link javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean)}).
 * </p>
 * <p>
 * In case the input DOM was created as namespace unaware or the input XML
 * document does not correctly use namespaces the method tries to fall back
 * to element-plain-name-only math node detection. The element is considered
 * to be math element if
 * <ul>
 * <li>the element is in MathML namespace XML namespace
 * {@code http://www.w3.org/1998/Math/MathML} (see
 * {@link Constants#MATHML_NS}),</li>
 * <li>or the local name of the element is {@code <math>} (see
 * {@link Constants#UNIFIED_MATHML_ROOT_ELEM}) without any namespace
 * definition.
 * </ul>
 * </p>
 *
 * @param node The node to test.
 * @return <code>true</code> if the above description is fulfilled,
 * <code>false</code> otherwise.
 */
public static boolean isUnifiedMathNode(Node node) {

    // Test the nodes is element
    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
        // Test the node is math node in MathML namesapce or at least with corrent name if no namesapce is defined at all
        if ((node.getNamespaceURI() == null && node.getNodeName().equals(MATHML_ROOT_ELEM))
                || (node.getNamespaceURI() != null && node.getNamespaceURI().equals(MATHML_NS))) {
            // Test presence of unification level attribute
            Node uniLevel = node.getAttributes().getNamedItemNS(UNIFIED_MATHML_NS, UNIFIED_MATHML_LEVEL_ATTR);
            if (uniLevel != null && uniLevel.getNodeType() == Node.ATTRIBUTE_NODE) {
                Integer value;
                try {
                    value = Integer.parseInt(((Attr) uniLevel).getTextContent());
                } catch (NumberFormatException ex) {
                    return false;
                }
                if (value >= 0) {
                    return true;
                }
            }

        }
    }

    return false;

}

From source file:cz.muni.fi.mir.mathmlunificator.MathMLUnificator.java

/**
 * <p>/*from   w w  w.  j a v a  2s  . com*/
 * Determine the unification level of the {@link Node} and max level of the
 * unification series. If the node does not possess the appropriate
 * unification level XML attributes the {@link UnificationLevel} object will
 * indicate that by having both {@link UnificationLevel#nodeLevel} and
 * {@link UnificationLevel#maxLevel} set to {@code null}.
 * </p>
 * <p>
 * <strong>Please note that only existance of valid unification level XML
 * attributes is tested. If you want to be sure the node is a valid unified
 * math node use {@link #isUnifiedMathNode(org.w3c.dom.Node)}!</strong>
 * </p>
 *
 * @param node The node to determine the unification level for.
 * @return The node's unification level represented by an instance of
 * {@link UnificationLevel}.
 * @see #unifyMathMLNode(org.w3c.dom.Node, boolean)
 */
public static UnificationLevel getNodeUnificationLevel(Node node) {

    Integer uniLevelValue = 0;
    Integer maxLevelValue = 0;

    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
        // Get unification level attributes if exists
        Node uniLevel = node.getAttributes().getNamedItemNS(UNIFIED_MATHML_NS, UNIFIED_MATHML_LEVEL_ATTR);
        Node maxLevel = node.getAttributes().getNamedItemNS(UNIFIED_MATHML_NS, UNIFIED_MATHML_MAX_LEVEL_ATTR);
        if (uniLevel != null && uniLevel.getNodeType() == Node.ATTRIBUTE_NODE && maxLevel != null
                && maxLevel.getNodeType() == Node.ATTRIBUTE_NODE) {
            try {
                uniLevelValue = Integer.parseInt(((Attr) uniLevel).getTextContent());
                maxLevelValue = Integer.parseInt(((Attr) maxLevel).getTextContent());
                if (uniLevelValue > 0 && maxLevelValue > 0) {
                    return new UnificationLevel(uniLevelValue, maxLevelValue);
                }
            } catch (NumberFormatException ex) {
                return new UnificationLevel();
            }
        }
    }

    return new UnificationLevel();

}

From source file:com.enonic.esl.xml.XMLTool.java

public static void removeChildNodes(Element root, boolean keepAttributeNodes) {

    if (root == null) {
        return;//from  www .j  av  a  2  s.c om
    }

    NodeList nodeList = root.getChildNodes();
    for (int i = 0; i < nodeList.getLength();) {
        Node n = nodeList.item(i);
        if ((!keepAttributeNodes && n.getNodeType() == Node.ATTRIBUTE_NODE)
                || n.getNodeType() != Node.ATTRIBUTE_NODE) {
            root.removeChild(n);
        } else {
            i++;
        }
    }
}