Search in sources :

Example 26 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project jOOQ by jOOQ.

the class XMLasDOMBinding method createContent.

/**
     * Create some content in the context of a given document
     *
     * @return <ul>
     *         <li>A {@link DocumentFragment} if <code>text</code> is
     *         well-formed.</li>
     *         <li><code>null</code>, if <code>text</code> is plain text or not
     *         well formed</li>
     *         </ul>
     */
static final DocumentFragment createContent(Document doc, String text) {
    // Text might hold XML content
    if (text != null && text.contains("<")) {
        DocumentBuilder builder = builder();
        try {
            // [#128] Trimming will get rid of leading and trailing whitespace, which would
            // otherwise cause a HIERARCHY_REQUEST_ERR raised by the parser
            text = text.trim();
            // valid XML and parse it as such
            if (text.startsWith("<?xml")) {
                Document parsed = builder.parse(new InputSource(new StringReader(text)));
                DocumentFragment fragment = parsed.createDocumentFragment();
                fragment.appendChild(parsed.getDocumentElement());
                return (DocumentFragment) doc.importNode(fragment, true);
            } else // Any XML document fragment. To be on the safe side, fragments
            // are wrapped in a dummy root node
            {
                String wrapped = "<dummy>" + text + "</dummy>";
                Document parsed = builder.parse(new InputSource(new StringReader(wrapped)));
                DocumentFragment fragment = parsed.createDocumentFragment();
                NodeList children = parsed.getDocumentElement().getChildNodes();
                // appendChild removes children also from NodeList!
                while (children.getLength() > 0) {
                    fragment.appendChild(children.item(0));
                }
                return (DocumentFragment) doc.importNode(fragment, true);
            }
        }// This does not occur
         catch (IOException ignore) {
        }// The XML content is invalid
         catch (SAXException ignore) {
        }
    }
    // Plain text or invalid XML
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment) SAXException(org.xml.sax.SAXException)

Example 27 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project j2objc by google.

the class InnerNodeImpl method insertChildAt.

/**
     * Inserts {@code newChild} at {@code index}. If it is already child of
     * another node, it is removed from there.
     */
Node insertChildAt(Node newChild, int index) throws DOMException {
    if (newChild instanceof DocumentFragment) {
        NodeList toAdd = newChild.getChildNodes();
        for (int i = 0; i < toAdd.getLength(); i++) {
            insertChildAt(toAdd.item(i), index + i);
        }
        return newChild;
    }
    LeafNodeImpl toInsert = (LeafNodeImpl) newChild;
    if (toInsert.document != null && document != null && toInsert.document != document) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
    }
    if (toInsert.isParentOf(this)) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
    }
    if (toInsert.parent != null) {
        int oldIndex = toInsert.index;
        toInsert.parent.children.remove(oldIndex);
        toInsert.parent.refreshIndices(oldIndex);
    }
    children.add(index, toInsert);
    toInsert.parent = this;
    refreshIndices(index);
    return newChild;
}
Also used : DOMException(org.w3c.dom.DOMException) NodeList(org.w3c.dom.NodeList) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 28 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project robovm by robovm.

the class XObject method rtree.

/**
   * Cast result object to a result tree fragment.
   *
   * @param support XPath context to use for the conversion
   *
   * @return the objec as a result tree fragment.
   */
public DocumentFragment rtree(XPathContext support) {
    DocumentFragment docFrag = null;
    int result = rtf();
    if (DTM.NULL == result) {
        DTM frag = support.createDocumentFragment();
        // %OPT%
        frag.appendTextChild(str());
        docFrag = (DocumentFragment) frag.getNode(frag.getDocument());
    } else {
        DTM frag = support.getDTM(result);
        docFrag = (DocumentFragment) frag.getNode(frag.getDocument());
    }
    return docFrag;
}
Also used : NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 29 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project robovm by robovm.

the class InnerNodeImpl method insertChildAt.

/**
     * Inserts {@code newChild} at {@code index}. If it is already child of
     * another node, it is removed from there.
     */
Node insertChildAt(Node newChild, int index) throws DOMException {
    if (newChild instanceof DocumentFragment) {
        NodeList toAdd = newChild.getChildNodes();
        for (int i = 0; i < toAdd.getLength(); i++) {
            insertChildAt(toAdd.item(i), index + i);
        }
        return newChild;
    }
    LeafNodeImpl toInsert = (LeafNodeImpl) newChild;
    if (toInsert.document != null && document != null && toInsert.document != document) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
    }
    if (toInsert.isParentOf(this)) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
    }
    if (toInsert.parent != null) {
        int oldIndex = toInsert.index;
        toInsert.parent.children.remove(oldIndex);
        toInsert.parent.refreshIndices(oldIndex);
    }
    children.add(index, toInsert);
    toInsert.parent = this;
    refreshIndices(index);
    return newChild;
}
Also used : DOMException(org.w3c.dom.DOMException) NodeList(org.w3c.dom.NodeList) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 30 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project robovm by robovm.

the class DomTest method testReplacingWithADocumentFragmentInsertsItsChildren.

public void testReplacingWithADocumentFragmentInsertsItsChildren() {
    Element a = document.createElement("a");
    Element b = document.createElement("b");
    Element c = document.createElement("c");
    DocumentFragment fragment = document.createDocumentFragment();
    fragment.appendChild(a);
    fragment.appendChild(b);
    fragment.appendChild(c);
    Node returned = menu.replaceChild(fragment, item);
    assertSame(item, returned);
    NodeList children = menu.getChildNodes();
    assertEquals(5, children.getLength());
    // whitespace
    assertTrue(children.item(0) instanceof Text);
    assertEquals(a, children.item(1));
    assertEquals(b, children.item(2));
    assertEquals(c, children.item(3));
    // whitespace
    assertTrue(children.item(4) instanceof Text);
}
Also used : Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text) DocumentFragment(org.w3c.dom.DocumentFragment)

Aggregations

DocumentFragment (org.w3c.dom.DocumentFragment)57 Document (org.w3c.dom.Document)27 Element (org.w3c.dom.Element)24 Node (org.w3c.dom.Node)20 NodeList (org.w3c.dom.NodeList)17 JAXBElement (javax.xml.bind.JAXBElement)8 Marshaller (javax.xml.bind.Marshaller)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 DOMException (org.w3c.dom.DOMException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 JAXBException (javax.xml.bind.JAXBException)4 DOMFragmentParser (org.cyberneko.html.parsers.DOMFragmentParser)4 Text (org.w3c.dom.Text)4 SAXException (org.xml.sax.SAXException)4 MalformedURLException (java.net.MalformedURLException)3 LinkedHashMap (java.util.LinkedHashMap)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3