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