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:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //w ww  .  ja  v a2  s. c  o  m
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:Main.java

public static void getNodeData(Node node, StringBuffer buf) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_NODE:
    case Node.ELEMENT_NODE: {
        for (Node child = node.getFirstChild(); null != child; child = child.getNextSibling()) {
            buf.append('<');
            buf.append(node.getNodeName());
            buf.append('>');
            getNodeData(child, buf);/*from  ww  w .  j ava 2  s . com*/
            buf.append("</");
            buf.append(node.getNodeName());
            buf.append('>');
        }
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.ATTRIBUTE_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
        break;
    default:
        // ignore
        break;
    }
}

From source file:com.qwazr.extractor.parser.ImageParser.java

private void browseNodes(String path, final Node root, final ParserFieldsBuilder result) {
    if (root == null)
        return;/*from   ww w.j  av  a2 s  .c om*/
    switch (root.getNodeType()) {
    case Node.TEXT_NODE:
        result.add(ParserField.newString(path, null), root.getNodeValue());
        break;
    case Node.ELEMENT_NODE:
        final NamedNodeMap nnm = root.getAttributes();
        if (nnm != null)
            for (int i = 0; i < nnm.getLength(); i++)
                browseNodes(path, nnm.item(i), result);
        Node child = root.getFirstChild();
        while (child != null) {
            browseNodes(path + "/" + child.getNodeName(), child, result);
            child = child.getNextSibling();
        }
        break;
    case Node.ATTRIBUTE_NODE:
        path = path + "#" + root.getNodeName();
        result.add(ParserField.newString(path, null), root.getNodeValue());
        break;
    default:
        throw new NotImplementedException("Unknown attribute: " + root.getNodeType());
    }
}

From source file:Main.java

/**
 * Serialise the supplied W3C DOM subtree.
 *
 * @param nodeList The DOM subtree as a NodeList.
 * @param format Format the output.//  w  w  w  .j av  a2  s. c o m
 * @param writer The target writer for serialization.
 * @throws DOMException Unable to serialise the DOM.
 */
public static void serialize(NodeList nodeList, boolean format, Writer writer) throws DOMException {

    if (nodeList == null) {
        throw new IllegalArgumentException("null 'subtree' NodeIterator arg in method call.");
    }

    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;

        if (format) {
            try {
                factory.setAttribute("indent-number", new Integer(4));
            } catch (Exception e) {
                // Ignore... Xalan may throw on this!!
                // We handle Xalan indentation below (yeuckkk) ...
            }
        }
        transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        if (format) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
        }

        int listLength = nodeList.getLength();

        // Iterate through the Node List.
        for (int i = 0; i < listLength; i++) {
            Node node = nodeList.item(i);

            if (isTextNode(node)) {
                writer.write(node.getNodeValue());
            } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                writer.write(((Attr) node).getValue());
            } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                transformer.transform(new DOMSource(node), new StreamResult(writer));
            }
        }
    } catch (Exception e) {
        DOMException domExcep = new DOMException(DOMException.INVALID_ACCESS_ERR,
                "Unable to serailise DOM subtree.");
        domExcep.initCause(e);
        throw domExcep;
    }
}

From source file:de.betterform.xml.xforms.model.constraints.CalculateVertex.java

/**
 * overwrites object toString()./*www .ja  v a 2 s  .  co m*/
 *
 * @return Vertex info as String
 */
public String toString() {
    if (this.xpathExpression == null) {
        return super.toString() + " - value("
                + (this.instanceNode.getNodeType() == Node.ATTRIBUTE_NODE ? "@" : "")
                + this.instanceNode.getNodeName() + ")";
    }

    return super.toString() + " - calculate(" + this.xpathExpression + ")";
}

From source file:de.codesourcery.spring.contextrewrite.XMLRewrite.java

private static Rule wrap(ReplaceRule r) {
    final String newValue;

    if (!ContextRewritingBootStrapper.NULL_STRING.equals(r.replacement())) {
        if (r.replacementClassName() != ReplaceRule.NULL_CLASS) {
            throw new RuntimeException("Either replacement or replacementClassName needs to be set");
        }/*from ww  w .ja va 2  s  . co m*/
        newValue = r.replacement();
    } else if (r.replacementClassName() != ReplaceRule.NULL_CLASS) {
        newValue = r.replacementClassName().getName();
    } else {
        throw new RuntimeException(
                "You need to provide EITHER 'replacement' OR 'replacementClassName' attributes");
    }
    return new Rule(r.xpath(), r.id()) {
        public void apply(Document document, Node matchedNode) throws Exception {
            switch (matchedNode.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                matchedNode.setNodeValue(newValue);
                break;
            case Node.ELEMENT_NODE:
                final Document replDocument = parseXMLFragment(newValue);
                final Node firstChild = replDocument.getFirstChild();
                final Node adoptedNode = document.importNode(firstChild, true);
                matchedNode.getParentNode().replaceChild(adoptedNode, matchedNode);
                break;
            default:
                throw new RuntimeException();
            }
        }

        @Override
        public String toString() {
            return "REPLACE: " + r.xpath() + " with '" + newValue;
        }
    };
}

From source file:Main.java

/**
 * Generates XPath expression with the option of the Node values appended
 *
 * @param node             the Node whose XPath is to be found
 * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored
 * @param includeValues    the flag to indicate if Node values will be included
 * @param noIndex          the flag to indicate if Node indexes are included
 * @return the XPath string representation of the Node
 *///from w  w w .j  a  v a  2 s  .  com

public static String generateXPath(Node node, boolean ignoreWhitespace, boolean includeValues, boolean noIndex)

{

    boolean noValues = !includeValues;

    if (node == null)

        return "";

    Node parent = node.getParentNode();

    int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace);

    String indexStr = "";

    if (index > 0)

        indexStr = "[" + Integer.toString(index) + "]";

    //printNode(node);

    //printNode(parent);

    if (node.getNodeType() == Node.DOCUMENT_NODE)

    {

        // return only the blank String, since all the other types are preceded with /

        return "";

    } else if (node.getNodeType() == Node.TEXT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr : "/TEXT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ELEMENT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                "/" + node.getNodeName() + indexStr;

    } else if (node.getNodeType() == Node.COMMENT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr
                        : "/COMMENT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr
                        : "/EntityReference(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr : "/PI(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)

    {

        return generateXPath(((Attr) node).getOwnerElement(), ignoreWhitespace, noValues, noIndex) +

                "/'@" + node.getNodeName() +

                (noValues ? "" : "=" + node.getNodeValue()) + "]";

    } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")");

    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")");

    }

    // Wont reach this far but just in case

    return "";

}

From source file:fr.gouv.finances.dgfip.xemelios.utils.XmlUtils.java

public static String getXmlDataSubstituteNode(Node node, String substituteWith, String substituteInWhat) {
    StringBuilder sb = new StringBuilder();
    switch (node.getNodeType()) {
    case Node.COMMENT_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.NOTATION_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;/*from ww w .j  a v  a  2 s  . co m*/
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.ELEMENT_NODE: {
        String nodeName = node.getNodeName();
        if (!substituteInWhat.equals(nodeName)) {
            sb.append("<").append(nodeName);
            StringBuilder attrs = new StringBuilder();
            StringBuilder children = new StringBuilder();
            NamedNodeMap nnm = node.getAttributes();
            if (nnm != null) {
                for (int i = 0; i < nnm.getLength(); i++) {
                    Node attr = nnm.item(i);
                    attrs.append(" ").append(getXmlDataSubstituteNode(attr, substituteWith, substituteInWhat));
                }
            }
            NodeList nl = node.getChildNodes();
            if (nl != null) {
                for (int i = 0; i < nl.getLength(); i++) {
                    Node child = nl.item(i);
                    if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
                        attrs.append(" ")
                                .append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat));
                    } else {
                        children.append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat));
                    }
                }
            }
            sb.append(attrs.toString());
            if (children.length() > 0) {
                sb.append(">").append(children.toString()).append("</").append(nodeName).append(">");
            } else {
                sb.append("/>");
            }
        } else {
            sb.append(substituteWith);
        }
        break;
    }
    case Node.ATTRIBUTE_NODE: {
        sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue()))
                .append("\"");
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>");
    }
    case Node.TEXT_NODE: {
        sb.append(StringEscapeUtils.escapeXml(node.getNodeValue()));
    }
    }
    return sb.toString();
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private short getNewNodeType(MessageContext msgCtxt) throws Exception {
    String nodetype = getSimpleRequiredProperty("new-node-type", msgCtxt);
    nodetype = nodetype.toLowerCase();/* w w w  .j  av  a 2  s . c o m*/
    if (nodetype.equals("element"))
        return Node.ELEMENT_NODE;
    if (nodetype.equals("attribute"))
        return Node.ATTRIBUTE_NODE;
    if (nodetype.equals("text"))
        return Node.TEXT_NODE;
    throw new IllegalStateException("new-node-type value is unknown: (" + nodetype + ")");
}

From source file:fr.gouv.finances.dgfip.xemelios.utils.TextWriter.java

private void writeNode(Writer writer, Node node) throws IOException {
    short type = node.getNodeType();
    switch (type) {

    case Node.DOCUMENT_NODE:
        document(writer, (Document) node);
        break;/* ww  w. j  a v  a  2s.  co  m*/

    case Node.DOCUMENT_FRAGMENT_NODE:
        documentFragment(writer, (DocumentFragment) node);
        break;

    case Node.DOCUMENT_TYPE_NODE:
        documentType(writer, (DocumentType) node);
        break;

    case Node.ELEMENT_NODE:
        element(writer, (Element) node);
        break;

    case Node.ATTRIBUTE_NODE:
        attribute(writer, (Attr) node);
        break;

    case Node.ENTITY_REFERENCE_NODE:
        entityReference(writer, (EntityReference) node);
        break;

    case Node.ENTITY_NODE:
        entity(writer, (Entity) node);
        break;

    case Node.NOTATION_NODE:
        notation(writer, (Notation) node);
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        procInst(writer, (ProcessingInstruction) node);
        break;

    case Node.TEXT_NODE:
        text(writer, (Text) node);
        break;

    case Node.CDATA_SECTION_NODE:
        cDataSection(writer, (CDATASection) node);
        break;

    case Node.COMMENT_NODE:
        comment(writer, (Comment) node);
        break;
    }
}