Example usage for org.w3c.dom Node DOCUMENT_FRAGMENT_NODE

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

Introduction

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

Prototype

short DOCUMENT_FRAGMENT_NODE

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

Click Source Link

Document

The node is a DocumentFragment.

Usage

From source file:de.betterform.xml.xforms.ui.AbstractUIElement.java

protected String getTargetReference(Element node, String targetRef) {

    if (node == null)
        return targetRef;
    if (!(node instanceof Element))
        return targetRef;

    Object xfObject = ((Element) node).getUserData("");
    if (xfObject != null && xfObject instanceof RepeatItem) {
        int position = ((RepeatItem) xfObject).getPosition();
        targetRef = "," + position + "]/" + targetRef;

    } else if (xfObject != null && xfObject instanceof Repeat) {
        int position = ((Repeat) xfObject).getPosition();
        targetRef = "[" + position + "" + targetRef;

    }/*from  w  w  w . j a v  a  2s .  c o m*/
    Node parent = node.getParentNode();
    if (parent.getNodeType() == Node.DOCUMENT_NODE || parent.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE
            || parent == null) {
        return "/" + targetRef;
    } else if (parent.getNodeType() == Node.ELEMENT_NODE) {
        return getTargetReference((Element) parent, targetRef);
    } else {
        LOGGER.warn("Unkown type: " + parent);
        return targetRef;
    }
}

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;/*w  w  w.ja v a2 s .  com*/

    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;
    }
}

From source file:DOM2SAX.java

/**
 * Writes a node using the given writer.
 * @param node node to serialize/*from   w ww . j a  va 2 s .c o m*/
 * @throws SAXException In case of a problem while writing XML
 */
private void writeNode(Node node) throws SAXException {
    if (node == null) {
        return;
    }

    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.NOTATION_NODE:
        // These node types are ignored!!!
        break;
    case Node.CDATA_SECTION_NODE:
        final String cdata = node.getNodeValue();
        if (lexicalHandler != null) {
            lexicalHandler.startCDATA();
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
            lexicalHandler.endCDATA();
        } else {
            // in the case where there is no lex handler, we still
            // want the text of the cdate to make its way through.
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
        }
        break;

    case Node.COMMENT_NODE: // should be handled!!!
        if (lexicalHandler != null) {
            final String value = node.getNodeValue();
            lexicalHandler.comment(value.toCharArray(), 0, value.length());
        }
        break;
    case Node.DOCUMENT_NODE:
        contentHandler.startDocument();
        Node next = node.getFirstChild();
        while (next != null) {
            writeNode(next);
            next = next.getNextSibling();
        }
        contentHandler.endDocument();
        break;

    case Node.ELEMENT_NODE:
        String prefix;
        List pushedPrefixes = new java.util.ArrayList();
        final AttributesImpl attrs = new AttributesImpl();
        final NamedNodeMap map = node.getAttributes();
        final int length = map.getLength();

        // Process all namespace declarations
        for (int i = 0; i < length; i++) {
            final Node attr = map.item(i);
            final String qnameAttr = attr.getNodeName();

            // Ignore everything but NS declarations here
            if (qnameAttr.startsWith(XMLNS_PREFIX)) {
                final String uriAttr = attr.getNodeValue();
                final int colon = qnameAttr.lastIndexOf(':');
                prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING;
                if (startPrefixMapping(prefix, uriAttr)) {
                    pushedPrefixes.add(prefix);
                }
            }
        }

        // Process all other attributes
        for (int i = 0; i < length; i++) {
            final Node attr = map.item(i);
            final String qnameAttr = attr.getNodeName();

            // Ignore NS declarations here
            if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
                final String uriAttr = attr.getNamespaceURI();

                // Uri may be implicitly declared
                if (uriAttr != null) {
                    final int colon = qnameAttr.lastIndexOf(':');
                    prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING;
                    if (startPrefixMapping(prefix, uriAttr)) {
                        pushedPrefixes.add(prefix);
                    }
                }

                // Add attribute to list
                attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr), qnameAttr, "CDATA",
                        attr.getNodeValue());
            }
        }

        // Now process the element itself
        final String qname = node.getNodeName();
        final String uri = node.getNamespaceURI();
        final String localName = getLocalName(node);

        // Uri may be implicitly declared
        if (uri != null) {
            final int colon = qname.lastIndexOf(':');
            prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
            if (startPrefixMapping(prefix, uri)) {
                pushedPrefixes.add(prefix);
            }
        }

        // Generate SAX event to start element
        contentHandler.startElement(uri, localName, qname, attrs);

        // Traverse all child nodes of the element (if any)
        next = node.getFirstChild();
        while (next != null) {
            writeNode(next);
            next = next.getNextSibling();
        }

        // Generate SAX event to close element
        contentHandler.endElement(uri, localName, qname);

        // Generate endPrefixMapping() for all pushed prefixes
        final int nPushedPrefixes = pushedPrefixes.size();
        for (int i = 0; i < nPushedPrefixes; i++) {
            endPrefixMapping((String) pushedPrefixes.get(i));
        }
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue());
        break;

    case Node.TEXT_NODE:
        final String data = node.getNodeValue();
        contentHandler.characters(data.toCharArray(), 0, data.length());
        break;
    default:
        //nop
    }
}

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

/**
 * Iterate through the DOM tree/*from w  w  w  . j  ava2  s  .  co 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:de.betterform.xml.dom.DOMUtil.java

/**
 * returns a canonical XPath locationpath for a given Node. Each step in the path will contain the positional
 * predicate of the Element. Example '/root[1]/a[1]/b[2]/c[5]/@d'. This would point to<br/>
 * to Attribute named 'd'<br/>/*from ww  w.ja v  a  2s  .  c om*/
 * on 5th Element 'c"<br/>
 * on 2nd Element 'b'<br/>
 * on first Element a<br/>
 * which is a child of the Document Element.
 *
 * @param node the Node where to start
 * @return canonical XPath locationPath for given Node or the empty string if node is null
 */
public static String getCanonicalPath(Node node) {

    if (node == null) {
        return "";
    }

    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return "/";
    }

    //add ourselves
    String canonPath;
    String ns = node.getNamespaceURI();
    String nodeName1 = node.getNodeName();
    String nodeName2 = node.getLocalName();
    if (ns != null && ns.equals("http://www.w3.org/1999/xhtml")
            && node.getNodeName().equals(node.getLocalName())) {
        canonPath = "html:" + node.getNodeName();
    } else {
        canonPath = node.getNodeName();
    }
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        canonPath = "@" + canonPath;
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        int position = DOMUtil.getCurrentNodesetPosition(node);
        //append position if we are an Element
        canonPath += "[" + position + "]";
    }

    //check for parent - if there's none we're root
    Node parent = null;
    if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.TEXT_NODE) {
        parent = node.getParentNode();
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        parent = ((Attr) node).getOwnerElement();
    }
    if (parent == null) {
        parent = node.getOwnerDocument().getDocumentElement();
    }
    if (parent.getNodeType() == Node.DOCUMENT_NODE || parent.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
        canonPath = "/" + canonPath;
    } else {
        canonPath = DOMUtil.getCanonicalPath(parent) + "/" + canonPath;
    }

    return canonPath;
}

From source file:jef.tools.XMLUtils.java

/**
 * Element(??)//from  w w w.  j  av a 2  s.co m
 * 
 * @param node
 *            
 * @param tagName
 *            ????nullElement
 * @return ??
 */
public static List<Element> childElements(Node node, String... tagName) {
    if (node == null)
        throw new NullPointerException("the input node can not be null!");
    List<Element> list = new ArrayList<Element>();
    NodeList nds = node.getChildNodes();
    if (tagName.length == 0 || tagName[0] == null) {// ?API
        tagName = null;
    }
    for (int i = 0; i < nds.getLength(); i++) {
        Node child = nds.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            if (tagName == null || ArrayUtils.contains(tagName, e.getNodeName())) {
                list.add(e);
            }
        } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
        } else if (child.getNodeType() == Node.COMMENT_NODE) {
        } else if (child.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {

        } else if (child.getNodeType() == Node.DOCUMENT_NODE) {

        } else if (child.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        } else if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
        } else if (child.getNodeType() == Node.TEXT_NODE) {
        }
    }
    return list;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Convert a DOM node to a stringified XML representation.
 *//*from   www.  j a v a2  s  .  c  o m*/
static public String domToString(Node node) {
    if (node == null) {
        throw new IllegalArgumentException("Cannot stringify null Node!");
    }

    String value = null;
    short nodeType = node.getNodeType();
    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.DOCUMENT_NODE
            || nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
        // serializer doesn't handle Node type well, only Element
        DOMSerializerImpl ser = new DOMSerializerImpl();
        ser.setParameter(Constants.DOM_NAMESPACES, Boolean.TRUE);
        ser.setParameter(Constants.DOM_WELLFORMED, Boolean.FALSE);
        ser.setParameter(Constants.DOM_VALIDATE, Boolean.FALSE);

        // create a proper XML encoding header based on the input document;
        // default to UTF-8 if the parent document's encoding is not accessible
        String usedEncoding = "UTF-8";
        Document parent = node.getOwnerDocument();
        if (parent != null) {
            String parentEncoding = parent.getXmlEncoding();
            if (parentEncoding != null) {
                usedEncoding = parentEncoding;
            }
        }

        // the receiver of the DOM
        DOMOutputImpl out = new DOMOutputImpl();
        out.setEncoding(usedEncoding);

        // we write into a String
        StringWriter writer = new StringWriter(4096);
        out.setCharacterStream(writer);

        // out, ye characters!
        ser.write(node, out);
        writer.flush();

        // finally get the String
        value = writer.toString();
    } else {
        value = node.getNodeValue();
    }
    return value;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Deep clone, but don't fry, the given node in the context of the given document.
 * For all intents and purposes, the clone is the exact same copy of the node,
 * except that it might have a different owner document.
 *
 * This method is fool-proof, unlike the <code>adoptNode</code> or <code>adoptNode</code> methods,
 * in that it doesn't assume that the given node has a parent or a owner document.
 *
 * @param document//from w  w  w  .ja  v  a 2 s .  co  m
 * @param sourceNode
 * @return a clone of node
 */
public static Node cloneNode(Document document, Node sourceNode) {
    Node clonedNode = null;

    // what is my name?
    QName sourceQName = getNodeQName(sourceNode);
    String nodeName = sourceQName.getLocalPart();
    String namespaceURI = sourceQName.getNamespaceURI();

    // if the node is unqualified, don't assume that it inherits the WS-BPEL target namespace
    if (Namespaces.WSBPEL2_0_FINAL_EXEC.equals(namespaceURI)) {
        namespaceURI = null;
    }

    switch (sourceNode.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        if (namespaceURI == null) {
            clonedNode = document.createAttribute(nodeName);
        } else {
            String prefix = ((Attr) sourceNode).lookupPrefix(namespaceURI);
            // the prefix for the XML namespace can't be looked up, hence this...
            if (prefix == null && namespaceURI.equals(NS_URI_XMLNS)) {
                prefix = "xmlns";
            }
            // if a prefix exists, qualify the name with it
            if (prefix != null && !"".equals(prefix)) {
                nodeName = prefix + ":" + nodeName;
            }
            // create the appropriate type of attribute
            if (prefix != null) {
                clonedNode = document.createAttributeNS(namespaceURI, nodeName);
            } else {
                clonedNode = document.createAttribute(nodeName);
            }
        }
        break;
    case Node.CDATA_SECTION_NODE:
        clonedNode = document.createCDATASection(((CDATASection) sourceNode).getData());
        break;
    case Node.COMMENT_NODE:
        clonedNode = document.createComment(((Comment) sourceNode).getData());
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        clonedNode = document.createDocumentFragment();
        break;
    case Node.DOCUMENT_NODE:
        clonedNode = document;
        break;
    case Node.ELEMENT_NODE:
        // create the appropriate type of element
        if (namespaceURI == null) {
            clonedNode = document.createElement(nodeName);
        } else {
            String prefix = namespaceURI.equals(Namespaces.XMLNS_URI) ? "xmlns"
                    : ((Element) sourceNode).lookupPrefix(namespaceURI);
            if (prefix != null && !"".equals(prefix)) {
                nodeName = prefix + ":" + nodeName;
                clonedNode = document.createElementNS(namespaceURI, nodeName);
            } else {
                clonedNode = document.createElement(nodeName);
            }
        }
        // attributes are not treated as child nodes, so copy them explicitly
        NamedNodeMap attributes = ((Element) sourceNode).getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attributeClone = (Attr) cloneNode(document, attributes.item(i));
            if (attributeClone.getNamespaceURI() == null) {
                ((Element) clonedNode).setAttributeNode(attributeClone);
            } else {
                ((Element) clonedNode).setAttributeNodeNS(attributeClone);
            }
        }
        break;
    case Node.ENTITY_NODE:
        // TODO
        break;
    case Node.ENTITY_REFERENCE_NODE:
        clonedNode = document.createEntityReference(nodeName);
        // TODO
        break;
    case Node.NOTATION_NODE:
        // TODO
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        clonedNode = document.createProcessingInstruction(((ProcessingInstruction) sourceNode).getData(),
                nodeName);
        break;
    case Node.TEXT_NODE:
        clonedNode = document.createTextNode(((Text) sourceNode).getData());
        break;
    default:
        break;
    }

    // clone children of element and attribute nodes
    NodeList sourceChildren = sourceNode.getChildNodes();
    if (sourceChildren != null) {
        for (int i = 0; i < sourceChildren.getLength(); i++) {
            Node sourceChild = sourceChildren.item(i);
            Node clonedChild = cloneNode(document, sourceChild);
            clonedNode.appendChild(clonedChild);
            // if the child has a textual value, parse it for any embedded prefixes
            if (clonedChild.getNodeType() == Node.TEXT_NODE
                    || clonedChild.getNodeType() == Node.CDATA_SECTION_NODE) {
                parseEmbeddedPrefixes(sourceNode, clonedNode, clonedChild);
            }
        }
    }
    return clonedNode;
}

From source file:org.apache.xml.security.utils.IdResolver.java

private static int getEl(Node currentNode, String id, Element[] els) {
    Node sibling = null;//from ww  w .  jav a  2s .  c  om
    Node parentNode = null;
    do {
        switch (currentNode.getNodeType()) {
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_NODE:
            sibling = currentNode.getFirstChild();
            break;

        case Node.ELEMENT_NODE:
            Element currentElement = (Element) currentNode;
            if (isElement(currentElement, id, els) == 1) {
                return 1;
            }
            sibling = currentNode.getFirstChild();
            if (sibling == null) {
                if (parentNode != null) {
                    sibling = currentNode.getNextSibling();
                }
            } else {
                parentNode = currentElement;
            }
            break;
        }
        while (sibling == null && parentNode != null) {
            sibling = parentNode.getNextSibling();
            parentNode = parentNode.getParentNode();
            if (parentNode != null && Node.ELEMENT_NODE != parentNode.getNodeType()) {
                parentNode = null;
            }
        }
        if (sibling == null) {
            return 1;
        }
        currentNode = sibling;
        sibling = currentNode.getNextSibling();
    } while (true);
}

From source file:org.dita.dost.writer.ConrefPushParser.java

private void writeNode(final Node node) throws SAXException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE: {
        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            writeNode(children.item(i));
        }//w  w  w. j av  a  2s  . co m
        break;
    }
    case Node.ELEMENT_NODE:
        final Element e = (Element) node;
        final AttributesBuilder b = new AttributesBuilder();
        final NamedNodeMap atts = e.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            b.add((Attr) atts.item(i));
        }
        final String ns = e.getNamespaceURI() != null ? e.getNamespaceURI() : NULL_NS_URI;
        getContentHandler().startElement(ns, e.getTagName(), e.getNodeName(), b.build());
        final NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            writeNode(children.item(i));
        }
        getContentHandler().endElement(ns, e.getTagName(), e.getNodeName());
        break;
    case Node.TEXT_NODE:
        final char[] data = node.getNodeValue().toCharArray();
        getContentHandler().characters(data, 0, data.length);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        getContentHandler().processingInstruction(node.getNodeName(), node.getNodeValue());
        break;
    default:
        throw new UnsupportedOperationException();
    }
}