Search in sources :

Example 36 with DocumentFragment

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);
}
Also used : Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 37 with DocumentFragment

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;
}
Also used : AuthorView(org.ambraproject.rhino.view.article.author.AuthorView) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 38 with DocumentFragment

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;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DocumentFragment(org.w3c.dom.DocumentFragment) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with DocumentFragment

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);
}
Also used : Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 40 with DocumentFragment

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());
}
Also used : Document(org.w3c.dom.Document) 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