Search in sources :

Example 6 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project rhino by PLOS.

the class AuthorsXmlExtractor method getOtherFootnotesMap.

/**
   * Grab all footnotes 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 footnote IDs and values
   */
private static Map<String, String> getOtherFootnotesMap(Document doc, XpathReader xpath) throws XPathException {
    Map<String, String> otherFootnotesMap = new HashMap<>();
    //Grab all 'other' footnotes and put them into their own map
    NodeList footnoteNodeList = xpath.selectNodes(doc, "//fn[@fn-type='other']");
    for (int a = 0; a < footnoteNodeList.getLength(); a++) {
        Node node = footnoteNodeList.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 footnote node: {}", id);
        DocumentFragment df = doc.createDocumentFragment();
        df.appendChild(node);
        String footnote;
        try {
            footnote = getAsXMLString(xpath.selectNode(df, "//p"));
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
        otherFootnotesMap.put(id, footnote);
    }
    return otherFootnotesMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DocumentFragment(org.w3c.dom.DocumentFragment) TransformerException(javax.xml.transform.TransformerException)

Example 7 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project rhino by PLOS.

the class AuthorsXmlExtractor method getAddressMap.

/**
   * Grab all addresses 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 address IDs and values
   */
private static Map<String, String> getAddressMap(Document doc, XpathReader xpath) throws XPathException {
    Map<String, String> addressMap = new HashMap<>();
    //Grab all the Current address information and place them into a map
    NodeList currentAddressNodeList = xpath.selectNodes(doc, "//fn[@fn-type='current-aff']");
    for (int a = 0; a < currentAddressNodeList.getLength(); a++) {
        Node node = currentAddressNodeList.item(a);
        String id = (node.getAttributes().getNamedItem("id") == null) ? "" : node.getAttributes().getNamedItem("id").getTextContent();
        log.debug("Current address node: {}", id);
        DocumentFragment df = doc.createDocumentFragment();
        df.appendChild(node);
        String address = xpath.selectString(df, "//p");
        addressMap.put(id, address);
    }
    return addressMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 8 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project uPortal by Jasig.

the class RenderingPipelineIntegrationTest method testRenderingPipeline.

@Test
public void testRenderingPipeline() throws Exception {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final DocumentFragment headFragment = doc.createDocumentFragment();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final IPortalUrlBuilder portalUrlBuilder = mock(IPortalUrlBuilder.class);
    final IPortletUrlBuilder portletUrlBuilder = mock(IPortletUrlBuilder.class);
    when(portalUrlBuilder.getUrlString()).thenReturn("URL_PLACEHOLDER");
    when(portletUrlBuilder.getPortalUrlBuilder()).thenReturn(portalUrlBuilder);
    when(portalUrlBuilder.getTargetedPortletUrlBuilder()).thenReturn(portletUrlBuilder);
    when(portalUrlBuilder.getPortletUrlBuilder(any(IPortletWindowId.class))).thenReturn(portletUrlBuilder);
    when(this.resourcesElementsProvider.getResourcesXmlFragment(any(HttpServletRequest.class), eq("/media/skins/respondr/defaultSkin/skin.xml"))).thenReturn(headFragment.getChildNodes());
    when(this.portalUrlProvider.getDefaultUrl(any(HttpServletRequest.class))).thenReturn(portalUrlBuilder);
    when(this.portalUrlProvider.getPortalUrlBuilderByLayoutNode(any(HttpServletRequest.class), any(String.class), any(UrlType.class))).thenReturn(portalUrlBuilder);
    when(this.portalUrlProvider.getPortalUrlBuilderByPortletFName(any(HttpServletRequest.class), any(String.class), any(UrlType.class))).thenReturn(portalUrlBuilder);
    final IPortletWindow portletWindow = mock(IPortletWindow.class);
    when(portletWindowRegistry.getPortletWindow(any(HttpServletRequest.class), any(StartElement.class))).thenReturn(new Tuple<IPortletWindow, StartElement>(portletWindow, null));
    when(portletWindowRegistry.getOrCreateDefaultPortletWindowByLayoutNodeId(any(HttpServletRequest.class), any(String.class))).thenReturn(portletWindow);
    when(portletWindowRegistry.getOrCreateDefaultPortletWindowByFname(any(HttpServletRequest.class), anyString())).thenReturn(portletWindow);
    when(portletWindow.getPortletWindowId()).thenReturn(new MockPortletWindowId("1"));
    final PipelineEventReader<?, ?> eventReader = this.component.getEventReader(request, response);
    for (final Object event : eventReader) {
        logger.debug(toString(event));
    }
    final String mediaType = eventReader.getOutputProperty(OutputKeys.MEDIA_TYPE);
    assertEquals("text/html", mediaType);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IPortletUrlBuilder(org.apereo.portal.url.IPortletUrlBuilder) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockPortletWindowId(org.apereo.portal.mock.portlet.om.MockPortletWindowId) Matchers.anyString(org.mockito.Matchers.anyString) Document(org.w3c.dom.Document) IPortalUrlBuilder(org.apereo.portal.url.IPortalUrlBuilder) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) StartElement(javax.xml.stream.events.StartElement) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UrlType(org.apereo.portal.url.UrlType) DocumentFragment(org.w3c.dom.DocumentFragment) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IPortletWindowId(org.apereo.portal.portlet.om.IPortletWindowId) Test(org.junit.Test)

Example 9 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project jdk8u_jdk by JetBrains.

the class DocumentSerializer method deserialize.

/**
     * @param ctx
     * @param inputSource
     * @return the Node resulting from the parse of the source
     * @throws XMLEncryptionException
     */
private Node deserialize(Node ctx, InputSource inputSource) throws XMLEncryptionException {
    try {
        if (dbf == null) {
            dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
            dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
            dbf.setValidating(false);
        }
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document d = db.parse(inputSource);
        Document contextDocument = null;
        if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
            contextDocument = (Document) ctx;
        } else {
            contextDocument = ctx.getOwnerDocument();
        }
        Element fragElt = (Element) contextDocument.importNode(d.getDocumentElement(), true);
        DocumentFragment result = contextDocument.createDocumentFragment();
        Node child = fragElt.getFirstChild();
        while (child != null) {
            fragElt.removeChild(child);
            result.appendChild(child);
            child = fragElt.getFirstChild();
        }
        return result;
    } catch (SAXException se) {
        throw new XMLEncryptionException("empty", se);
    } catch (ParserConfigurationException pce) {
        throw new XMLEncryptionException("empty", pce);
    } catch (IOException ioe) {
        throw new XMLEncryptionException("empty", ioe);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment) SAXException(org.xml.sax.SAXException)

Example 10 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project webservices-axiom by apache.

the class TestCreateDocumentFragmentInterfaces method runTest.

@Override
protected void runTest() throws Throwable {
    Document document = ((DOMMetaFactory) metaFactory).newDocumentBuilderFactory().newDocumentBuilder().newDocument();
    DocumentFragment fragment = document.createDocumentFragment();
    assertFalse(fragment instanceof OMInformationItem);
}
Also used : DOMMetaFactory(org.apache.axiom.om.dom.DOMMetaFactory) OMInformationItem(org.apache.axiom.om.OMInformationItem) Document(org.w3c.dom.Document) 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