Example usage for org.w3c.dom Node DOCUMENT_NODE

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

Introduction

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

Prototype

short DOCUMENT_NODE

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

Click Source Link

Document

The node is a Document.

Usage

From source file:Main.java

/**prints the type of the input node
 * @param node node to print type of//from w ww. j  a va2s  .  com
 * @param ident amount to indent*/
public static void printNodeType(Node node, int ident) {
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        for (int j = 0; j < 2 * ident; j++)
            System.out.print(" ");
        System.out.println("It has the following Children");
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                for (int j = 0; j < ident; j++)
                    System.out.print(" ");
                System.out.print("Child " + ident + "." + i + " = ");
                printNodeType(children.item(i), ident + 1);
            }
            System.out.println();
        }
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }
}

From source file:Main.java

/**
 * This method returns the first non-null owner document of the Nodes in this Set.
 * This method is necessary because it <I>always</I> returns a
 * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
 * if the {@link Node} is a {@link Document}.
 *
 * @param xpathNodeSet/* w ww . j  av a 2 s.c  o m*/
 * @return the owner document
 */
public static Document getOwnerDocument(Set<Node> xpathNodeSet) {
    NullPointerException npe = null;
    for (Node node : xpathNodeSet) {
        int nodeType = node.getNodeType();
        if (nodeType == Node.DOCUMENT_NODE) {
            return (Document) node;
        }
        try {
            if (nodeType == Node.ATTRIBUTE_NODE) {
                return ((Attr) node).getOwnerElement().getOwnerDocument();
            }
            return node.getOwnerDocument();
        } catch (NullPointerException e) {
            npe = e;
        }
    }

    throw new NullPointerException(npe.getMessage());
}

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

/**
 * Returns a document containing only the relevant model items of the
 * specified instance data./*from www  .  jav a 2  s . c  om*/
 *
 * @param instance the instance.
 * @param path the path denoting an instance subtree.
 * @return a document containing only the relevant model items of the
 *         specified instance data.
 */
public static Document selectRelevant(Instance instance, String path) throws XFormsException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("select relevant: processing " + path);
    }

    Document relevantDocument = DOMUtil.newDocument(true, false);
    Node instanceRoot = instance.getInstanceDocument().getDocumentElement();
    Node instanceNode = XPathCache.getInstance().evaluateAsSingleNode(instance.getRootContext(), path);

    if (instanceNode.getNodeType() == Node.DOCUMENT_NODE) {
        if (isEnabled(instanceRoot)) {
            // process document tree
            addElement(relevantDocument, instanceRoot);
        }
    }

    if (instanceNode.getNodeType() == Node.ELEMENT_NODE) {
        if (isEnabled(instanceNode)) {
            // process element subtree
            addElement(relevantDocument, instanceNode);

            // apply namespaces
            NamespaceResolver.applyNamespaces((Element) instanceNode, relevantDocument.getDocumentElement());
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RelevantSelector result document....");
        DOMUtil.prettyPrintDOM(relevantDocument);
    }

    return relevantDocument;
}

From source file:Main.java

@SuppressWarnings("fallthrough")
static final void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;//from  w w  w.java  2  s . co  m
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working - ignore fallthrough warning
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null)
                    return;
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
    return;
}

From source file:Main.java

/**
 * Gets Node type String for a given node type constant
 *//*from   www  .  jav a 2  s  .c  o m*/

public static String getNodeTypeStr(int nodeType)

{

    switch (nodeType)

    {

    case Node.ATTRIBUTE_NODE:

        return "ATTRIBUTE_NODE ";

    case Node.CDATA_SECTION_NODE:

        return "CDATA_SECTION_NODE";

    case Node.COMMENT_NODE:

        return "COMMENT_NODE";

    case Node.DOCUMENT_FRAGMENT_NODE:

        return "DOCUMENT_FRAGMENT_NODE";

    case Node.DOCUMENT_TYPE_NODE:

        return "DOCUMENT_TYPE_NODE";

    case Node.ELEMENT_NODE:

        return "ELEMENT_NODE";

    case Node.ENTITY_NODE:

        return "ENTITY_NODE";

    case Node.ENTITY_REFERENCE_NODE:

        return "ENTITY_REFERENCE_NODE";

    case Node.NOTATION_NODE:

        return "NOTATION_NODE";

    case Node.PROCESSING_INSTRUCTION_NODE:

        return "PROCESSING_INSTRUCTION_NODE";

    case Node.TEXT_NODE:

        return "TEXT_NODE";

    case Node.DOCUMENT_NODE:

        return "DOCUMENT_NODE";

    default:

        return "UN-INDENTIFIED NODE";

    }

}

From source file:Main.java

public String getNodeType(Node node) {
    String type;//w  w w. j  av  a2s. c o m

    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE: {
        type = "Element";
        break;
    }
    case Node.ATTRIBUTE_NODE: {
        type = "Attribute";
        break;
    }
    case Node.TEXT_NODE: {
        type = "Text";
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        type = "CData section";
        break;
    }
    case Node.ENTITY_REFERENCE_NODE: {
        type = "Entity reference";
        break;
    }
    case Node.ENTITY_NODE: {
        type = "Entity";
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        type = "Processing instruction";
        break;
    }
    case Node.COMMENT_NODE: {
        type = "Comment";
        break;
    }
    case Node.DOCUMENT_NODE: {
        type = "Document";
        break;
    }
    case Node.DOCUMENT_TYPE_NODE: {
        type = "Document type";
        break;
    }
    case Node.DOCUMENT_FRAGMENT_NODE: {
        type = "Document fragment";
        break;
    }
    case Node.NOTATION_NODE: {
        type = "Notation";
        break;
    }
    default: {
        type = "???";
        break;
    }
    }
    return type;
}

From source file:Main.java

/**
 * Returns parent attribute value for a specific element (2 levels of
 * parents)//from w  w w  .java2 s  . c o m
 * @param element
 * @param attr
 * @return
 */
protected static String getXsdParentsAtrValue(Element element, String attr) {
    Node tmpElement = null;
    String tmpRes = "";
    try {
        for (int i = 1; i < 3; i++) {
            if (i == 1) {
                tmpElement = element.getParentNode();
            } else {
                tmpElement = tmpElement.getParentNode();
            }
            if (tmpElement.getNodeName().equals("xs:schema")) {
                if (i == 1) {
                    tmpRes = "..";
                } else {
                    tmpRes = "." + tmpRes;
                }
                break;
            }
            while ((tmpElement.getNodeType() != Node.ELEMENT_NODE)
                    || ((tmpElement.getNodeType() != Node.DOCUMENT_NODE)
                            && !((Element) tmpElement).hasAttribute(attr))) {
                if (tmpElement.getNodeName().equals("xs:schema")) {
                    break;
                }
                tmpElement = tmpElement.getParentNode();
            }
            tmpRes = ((Element) tmpElement).getAttribute(attr) + "." + tmpRes;
        }
        return tmpRes;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:MainClass.java

public void processNode(Node node, String spacer) throws IOException {
    if (node == null)
        return;/*from www . j a v  a  2  s  .c  o  m*/
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        System.out.print(spacer + "<" + name);
        NamedNodeMap nnm = node.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node current = nnm.item(i);
            System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue());
        }
        System.out.print(">");
        NodeList nl = node.getChildNodes();
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                processNode(nl.item(i), "");
            }
        }
        System.out.println(spacer + "</" + name + ">");
        break;
    case Node.TEXT_NODE:
        System.out.print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.print("" + node.getNodeValue() + "");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.print("&" + node.getNodeName() + ";");
        break;
    case Node.ENTITY_NODE:
        System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>");
        break;
    case Node.DOCUMENT_NODE:
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                processNode(nodes.item(i), "");
            }
        }
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        System.out.print("<!DOCTYPE " + docType.getName());
        if (docType.getPublicId() != null) {
            System.out.print(" PUBLIC " + docType.getPublicId() + " ");
        } else {
            System.out.print(" SYSTEM ");
        }
        System.out.println(" " + docType.getSystemId() + ">");
        break;
    default:
        break;
    }
}

From source file:de.betterform.connector.serializer.MultipartRelatedSerializer.java

public void serialize(Submission submission, Node node, SerializerRequestWrapper wrapper,
        String defaultEncoding) throws Exception {
    Map cache = new HashMap();
    MimeMultipart multipart = new MimeMultipart();

    MimeBodyPart part = new MimeBodyPart();
    multipart.addBodyPart(part);//from  w w  w. j  a  v a 2 s  . c  om

    String encoding = defaultEncoding;
    if (submission.getEncoding() != null) {
        encoding = submission.getEncoding();
    }

    if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE) {
        part.setText(new String(serializeXML(multipart, cache, submission, node, encoding), encoding),
                encoding);
    } else {
        part.setText(node.getTextContent());
    }
    part.setContentID("<instance.xml@start>");
    part.addHeader("Content-Type", "application/xml");
    part.addHeader("Content-Transfer-Encoding", "base64");
    part.setDisposition("attachment");
    part.setFileName("instance.xml");

    multipart.setSubType("related; type=\"" + submission.getMediatype() + "\"; start=\"instance.xml@start\"");
    // FIXME: Is this a global http header or a local mime header?
    wrapper.getBodyStream()
            .write(("Content-Type: " + multipart.getContentType() + "\n\nThis is a MIME message.\n")
                    .getBytes(encoding));
    multipart.writeTo(wrapper.getBodyStream());
}

From source file:Main.java

private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException {
    if (node == null) {
        return false;
    }//from ww  w .j  a v a 2s .c o m
    short type = node.getNodeType();
    boolean result = true;
    switch (type) {
    case Node.ATTRIBUTE_NODE: {
        String text = ((Attr) node).getValue();
        writer.write(text);
        break;
    }
    case Node.TEXT_NODE: {
        String text = ((Text) node).getData();
        writer.write(text);
        break;
    }
    case Node.ELEMENT_NODE: {
        Element element = (Element) node;
        if (includeNode) {
            serializeXML(element, writer, false);
        } else {
            Node child = element.getFirstChild();
            while (child != null) {
                serializeXmlNode(child, writer, true);
                child = child.getNextSibling();
            }
        }
        break;
    }
    case Node.DOCUMENT_NODE: {
        Document doc = (Document) node;
        serializeXmlNode(doc.getDocumentElement(), writer, includeNode);
        break;
    }
    default:
        result = false;
        break;
    }
    return result;
}