Search in sources :

Example 16 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 17 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 18 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 19 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)

Example 20 with DocumentFragment

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

the class DomTest method testAddingADocumentFragmentAddsItsChildren.

/**
     * Documents shouldn't contain document fragments.
     * http://code.google.com/p/android/issues/detail?id=2735
     */
public void testAddingADocumentFragmentAddsItsChildren() {
    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.appendChild(fragment);
    assertSame(fragment, returned);
    NodeList children = menu.getChildNodes();
    assertEquals(6, children.getLength());
    // whitespace
    assertTrue(children.item(0) instanceof Text);
    assertEquals(item, children.item(1));
    // whitespace
    assertTrue(children.item(2) instanceof Text);
    assertEquals(a, children.item(3));
    assertEquals(b, children.item(4));
    assertEquals(c, children.item(5));
}
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)32 Document (org.w3c.dom.Document)20 NodeList (org.w3c.dom.NodeList)17 Node (org.w3c.dom.Node)13 Element (org.w3c.dom.Element)10 Text (org.w3c.dom.Text)4 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DOMException (org.w3c.dom.DOMException)3 HashMap (java.util.HashMap)2 DTM (org.apache.xml.dtm.DTM)2 NodeSetDTM (org.apache.xpath.NodeSetDTM)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 MessagingException (javax.mail.MessagingException)1