use of org.w3c.dom.DocumentFragment in project robovm by robovm.
the class HCNodeDocumentFragmentNormalize method testNodeDocumentFragmentNormalize1.
/**
* Runs the test case.
*
* @throws Throwable
* Any uncaught exception causes test to fail
*/
public void testNodeDocumentFragmentNormalize1() throws Throwable {
Document doc;
DocumentFragment docFragment;
String nodeValue;
Text txtNode;
Node retval;
doc = (Document) load("hc_staff", builder);
docFragment = doc.createDocumentFragment();
txtNode = doc.createTextNode("foo");
retval = docFragment.appendChild(txtNode);
txtNode = doc.createTextNode("bar");
retval = docFragment.appendChild(txtNode);
docFragment.normalize();
txtNode = (Text) docFragment.getFirstChild();
nodeValue = txtNode.getNodeValue();
assertEquals("normalizedNodeValue", "foobar", nodeValue);
retval = txtNode.getNextSibling();
assertNull("singleChild", retval);
}
use of org.w3c.dom.DocumentFragment in project rhino by PLOS.
the class AuthorsXmlExtractor method buildAuthors.
private List<AuthorView> buildAuthors() throws XPathException {
List<AuthorView> list = new ArrayList<>();
//Get all the authors
NodeList authorList = xpath.selectNodes(doc, "//contrib-group/contrib[@contrib-type='author']");
for (int i = 0; i < authorList.getLength(); i++) {
Node authorNode = authorList.item(i);
//Create temp author document fragment to search out of
DocumentFragment authorDoc = doc.createDocumentFragment();
//I thought this strange, appendChild actually moves the node in the case of document fragment
//hence below I clone to keep the original DOM intact.
//re: http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#appendChild%28org.w3c.dom.Node%29
authorDoc.appendChild(authorNode.cloneNode(true));
Node surNameNode = xpath.selectNode(authorDoc, "./contrib/name/surname");
Node givenNameNode = xpath.selectNode(authorDoc, "./contrib/name/given-names");
Node collabNameNode = xpath.selectNode(authorDoc, "//collab");
Node behalfOfNode = xpath.selectNode(authorDoc, "//on-behalf-of");
NodeList otherFootnotesNodeList = xpath.selectNodes(authorDoc, "//xref[@ref-type='fn']");
//Note:10.1371/journal.pone.0032315
if (surNameNode == null && givenNameNode == null) {
if (collabNameNode != null) {
//Previous authors "on behalf of" node
if (list.size() > 0) {
if (list.get(list.size() - 1).getOnBehalfOf() != null) {
//footnotes from this contrib to that author!
for (int a = 0; a < otherFootnotesNodeList.getLength(); a++) {
Node node = otherFootnotesNodeList.item(a);
if (node.getAttributes().getNamedItem("rid") != null) {
String id = node.getAttributes().getNamedItem("rid").getTextContent();
String value = otherFootnotesMap.get(id);
if (value != null) {
AuthorView av = list.get(list.size() - 1);
//This may look a bit odd, but because the AuthorView is immutable
//I have to create a new copy to change any values
List<String> footnotes = new ArrayList<>();
footnotes.addAll(av.getCustomFootnotes());
value = fixPilcrow(value, false);
footnotes.add(value);
list.set(list.size() - 1, AuthorView.builder(av).setCustomFootnotes(footnotes).build());
}
}
}
break;
}
}
}
givenNameNode = collabNameNode;
}
// If both of these are null then don't bother to add
if (surNameNode == null && givenNameNode == null) {
continue;
}
AuthorView author = getAuthorView(authorDoc, surNameNode, givenNameNode, behalfOfNode, otherFootnotesNodeList);
list.add(author);
}
return list;
}
use of org.w3c.dom.DocumentFragment in project rhino by PLOS.
the class AuthorsXmlExtractor method getAffiliateMap.
/**
* Grab all affiliations and put them into their own map
*
* @param doc the article XML document
* @param xpath XpathReader to use to process xpath expressions
* @return a Map of affiliate IDs and values
*/
private static Map<String, String> getAffiliateMap(Document doc, XpathReader xpath) throws XPathException {
Map<String, String> affiliateMap = new LinkedHashMap<>();
NodeList affiliationNodeList = xpath.selectNodes(doc, "//aff");
//Map all affiliation id's to their affiliation strings
for (int a = 0; a < affiliationNodeList.getLength(); a++) {
Node node = affiliationNodeList.item(a);
// Not all <aff>'s have the 'id' attribute.
String id = (node.getAttributes().getNamedItem("id") == null) ? "" : node.getAttributes().getNamedItem("id").getTextContent();
log.debug("Found affiliation node: {}", id);
// Not all <aff> id's are affiliations.
if (id.startsWith("aff")) {
DocumentFragment df = doc.createDocumentFragment();
//because of a org.w3c.Document.dom.Document peculiarity, simple appellation will strip it from the source and
//cause bugs, so we need cloning technology
df.appendChild(node.cloneNode(true));
StringBuilder res = new StringBuilder();
if (xpath.selectNode(df, "//institution") != null) {
res.append(xpath.selectString(df, "//institution"));
}
if (xpath.selectNode(df, "//addr-line") != null) {
if (res.length() > 0) {
res.append(" ");
}
res.append(xpath.selectString(df, "//addr-line"));
}
affiliateMap.put(id, res.toString());
}
}
return affiliateMap;
}
use of org.w3c.dom.DocumentFragment in project webservices-axiom by apache.
the class TestCloneNodeDeep method runTest.
protected void runTest() throws Throwable {
Document document = dbf.newDocumentBuilder().newDocument();
DocumentFragment fragment = document.createDocumentFragment();
fragment.appendChild(document.createComment("comment"));
fragment.appendChild(document.createElementNS(null, "test"));
DocumentFragment clone = (DocumentFragment) fragment.cloneNode(true);
assertSame(document, clone.getOwnerDocument());
Node child = clone.getFirstChild();
assertNotNull(child);
assertEquals(Node.COMMENT_NODE, child.getNodeType());
child = child.getNextSibling();
assertNotNull(child);
assertEquals(Node.ELEMENT_NODE, child.getNodeType());
assertEquals("test", child.getLocalName());
child = child.getNextSibling();
assertNull(child);
}
use of org.w3c.dom.DocumentFragment in project webservices-axiom by apache.
the class TestCloneNodeShallow method runTest.
protected void runTest() throws Throwable {
Document document = dbf.newDocumentBuilder().newDocument();
DocumentFragment fragment = document.createDocumentFragment();
fragment.appendChild(document.createElementNS(null, "test"));
DocumentFragment clone = (DocumentFragment) fragment.cloneNode(false);
assertSame(document, clone.getOwnerDocument());
assertNull(clone.getFirstChild());
assertNull(clone.getLastChild());
assertEquals(0, clone.getChildNodes().getLength());
}
Aggregations