Example usage for org.jsoup.nodes Document outerHtml

List of usage examples for org.jsoup.nodes Document outerHtml

Introduction

In this page you can find the example usage for org.jsoup.nodes Document outerHtml.

Prototype

@Override
    public String outerHtml() 

Source Link

Usage

From source file:org.eclipse.mylyn.docs.examples.ImproveFormatting.java

private static void improveCodeFormatting(File in) throws IOException {
    String contents = new String(Files.readAllBytes(in.toPath()));

    // parse the file contents as XML and put it into a DOM
    Document parse = Jsoup.parse(contents, "UTF-8", Parser.xmlParser());

    // obtain all the code sections we want to format
    Elements select = parse.select("pre[class=programlisting]");
    for (Element element : select) {
        // get to the actual code
        String text = element.html();

        // try to avoid xml and other code already formatted - we're not
        // able to handle that in this example
        if (!text.contains("<plugin>") && !text.contains("<span")) {
            // format all keywords by adding the "keyword" class
            String code = KEYWORDS.stream().reduce(text,
                    (str, keyword) -> str.replaceAll(keyword + "(\\s|\\(|\\{|\\))",
                            "<code class=\"keyword\">" + keyword + "</code>$1"));
            // replace the code with the coloured version
            element.html(code);//ww  w . j  a  v a 2  s.c o m
        }
    }

    // write out the modified code
    FileWriter fw = new FileWriter(in);
    fw.write(parse.outerHtml());
    fw.flush();
    fw.close();
}

From source file:org.silverpeas.mobile.server.servlets.PublicationContentServlet.java

private void displayWysiwyg(String html, HttpServletRequest request, HttpServletResponse response,
        String instanceId) throws IOException {
    html = "<html><body>" + html + "</body></html>";
    Document doc = Jsoup.parse(html);

    Elements body = doc.getElementsByTag("body");
    if (!body.isEmpty()) {
        html = body.first().html();//from w  w w  . j  a va  2 s.  c om
    }

    Elements images = doc.getElementsByTag("img");
    for (Element img : images) {
        String source = img.attr("src");
        String newSource = source;
        if (source.contains("/silverpeas")) {
            // need to convert in dataurl
            newSource = convertSpImageUrlToDataUrl(source);
        }
        img.attr("src", newSource);
    }
    Elements embeds = doc.getElementsByTag("embed");
    for (Element embed : embeds) {
        String htmlPart = embed.outerHtml();
        if (htmlPart.contains("flash")) {
            String attachmentId = htmlPart
                    .substring(htmlPart.indexOf("attachmentId/") + "attachmentId/".length());
            attachmentId = attachmentId.substring(0, attachmentId.indexOf("/"));
            SimpleDocument attachment = AttachmentServiceProvider.getAttachmentService().searchDocumentById(
                    new SimpleDocumentPK(attachmentId),
                    getUserInSession(request).getUserPreferences().getLanguage());
            String type = attachment.getContentType();
            String url = getServletContext().getContextPath() + "/services/spmobile/Attachment";
            url = url + "?id=" + attachmentId + "&instanceId=" + instanceId + "&lang="
                    + getUserInSession(request).getUserPreferences().getLanguage() + "&userId="
                    + getUserInSession(request).getId();
            if (type.equals("audio/mpeg") || type.equals("audio/ogg") || type.equals("audio/wav")) {
                embed.parent().append("<audio controls><source src='" + url + "' type='" + type + "'></audio>");
                embed.remove();
            } else if (type.equals("video/mp4") || type.equals("video/ogg") || type.equals("video/webm")) {
                embed.parent()
                        .append("<video controls='controls'><source src='" + url + "' type='" + type + "' />");
                embed.remove();
            }
        }
    }
    html = doc.outerHtml();
    OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
    writeContainer(out, html);
    out.flush();
}

From source file:org.silverpeas.mobile.server.servlets.PublicationContentServlet.java

private void displayFormView(Writer out, PublicationDetail pub, UserDetail user, String ua) throws Exception {

    PublicationTemplate pubTemplate = PublicationTemplateManager.getInstance()
            .getPublicationTemplate(pub.getInstanceId() + ":" + pub.getInfoId());
    DataRecord xmlData = pubTemplate.getRecordSet().getRecord(pub.getId());

    PagesContext xmlContext = new PagesContext("myForm", "0", user.getUserPreferences().getLanguage(), false,
            pub.getInstanceId(), "useless");
    xmlContext.setObjectId(pub.getId());
    xmlContext.setDesignMode(false);//  w w w. j  a v a 2s  .c  o  m
    xmlContext.setBorderPrinted(false);
    xmlContext.setContentLanguage(user.getUserPreferences().getLanguage());
    xmlContext.setCreation(false);

    StringWriter generatedHtml = new StringWriter();
    PrintWriter outTmp = new PrintWriter(generatedHtml);

    Form xmlForm = pubTemplate.getViewForm();
    if (xmlForm instanceof XmlForm) {
        Method m = XmlForm.class.getDeclaredMethod("display",
                new Class[] { PrintWriter.class, PagesContext.class, DataRecord.class });
        m.setAccessible(true);
        m.invoke(xmlForm, outTmp, xmlContext, xmlData);
        outTmp.flush();
    } else if (xmlForm instanceof HtmlForm) {
        String html = ((HtmlForm) xmlForm).toString(xmlContext, xmlData);
        outTmp.write(html);
        outTmp.flush();
    }
    String html = generatedHtml.toString();

    Document doc = Jsoup.parse(html);
    Elements images = doc.getElementsByTag("img");
    for (Element img : images) {
        if (img.attr("class").equals("preview-file")) {
            // remove preview for files
            img.remove();
        } else if (img.attr("src").startsWith("/silverpeas/attached_file/componentId/")) {
            // convert url to dataurl
            String data = img.attr("src");
            data = convertImageAttachmentUrl(data, data);
            img.attr("src", data);
        }
    }
    Elements links = doc.getElementsByTag("a");
    for (Element link : links) {
        if (link.attr("href").startsWith("/silverpeas/attached_file/componentId/")) {
            // link to file
            String url = link.attr("href");
            String attachmentId = url.substring(url.indexOf("attachmentId/") + "attachmentId/".length());
            attachmentId = attachmentId.substring(0, attachmentId.indexOf("/"));
            url = getServletContext().getContextPath() + "/services/spmobile/Attachment";
            url = url + "?id=" + attachmentId + "&instanceId=" + pub.getInstanceId() + "&lang="
                    + user.getUserPreferences().getLanguage() + "&userId=" + user.getId();
            link.attr("href", url);
            link.attr("target", "_self");

            if (link.attr("id").startsWith("player")) {

                boolean playable = false;

                SimpleDocument attachment = AttachmentServiceProvider.getAttachmentService().searchDocumentById(
                        new SimpleDocumentPK(attachmentId), user.getUserPreferences().getLanguage());
                String type = attachment.getContentType();
                if (type.contains("mp4") || type.contains("ogg") || type.contains("webm")) {
                    playable = true;
                }

                if (playable) {
                    String style = link.attr("style");
                    String width = style.substring(style.indexOf("width") + "width".length() + 1);
                    width = width.substring(0, width.indexOf("px"));
                    String height = style.substring(style.indexOf("height") + "height".length() + 1);
                    height = height.substring(0, height.indexOf("px"));
                    link.parent().append("<video width='" + width + "' height='" + height
                            + "' controls='controls'><source src='" + url + "' type='" + type + "' />");
                    link.remove();
                } else {
                    // display image instead of video player
                    String style = "display:block; width:150px; height:98px; background-repeat: no-repeat; ";
                    style += "background-image: url(data:image/jpeg;base64," + "/9j/4AAQSkZJRgABAQEBLAEsAAD"
                            + "/4QYfRXhpZgAATU0AKgAAAAgAAAAAAA4AAgIBAAQAAAABAAAALAICAAQAAAABAAAF6wAAAAD/2P"
                            + "/gABBKRklGAAEBAAABAAEAAP/bAEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQfJzk9ODI8LjM0Mv/AAAsIAEAAYgEBEQD/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/2gAIAQEAAD8A98orN1zXdO8OaVLqWqXS29tEOWPUnsAO5PoK8X1H9ouR7t00Xw600APEk8pDMP8AdUcfma3PCXx60nWr6Ow1qzbSp5CFWUvviLehOAV/Hj3r1me4htraS4mlSOGNS7yMcKqjnJPpXjPiL9oSxtL17XQNKfUQpI8+VyiN/uqASR+VQaJ+0TBJeLBr+iSWcZODPA5fb9UIB/In6V7TYX1tqVlDeWc6T20yho5EOQw9qs0UUUUV89/HO6udZ8f6D4WExitWWN+vG+Ryu78AP1Ne2eHvDGkeGNLisdLs4oY0UAuFG9z6sepNec/HHwVpVz4RuPEMFtFb6jZsrGSNQvmqWCkN6nnIPtXB6/4z1K7+Aei2ryuXuLp7SaXJy8cfIBP4r/3zXrnws8F6V4e8IabeR20cmoXlvHcTXLAFsuobaD2AzjitLx34K0nxb4fuoru2iF0kbNBchQHjYDI59OORXnf7OWs3Nx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YGExTJbxX5/dlUU3J0HUlBvHYfRHpiJEdRO2BxOJ71kePjwxUqD7LB6T0VXX3gAcpDhiXWmTB2SIbaBsMJC1ETypj2ybGUw8sXyeWEAO0Q5MeVTUTXNbmzloALE73AY05c5CSOtsFqDVz3NNiIx9HAWJN+gBU9hBsLE5woWZa7KTk74VtQSABABsRXRgNt5xXmVTJRiwqjcya8iaMGB4VzQKH58xLMEhiQit4lFmYJWAISLoAOybl5YWYJMstHYXYGIvWB69d4d3VDSdUkt5ws0MCCZC2Gw72lyeigBpqOBGfMFFaRuggTpnw79ZB878ZP8D8Z8O/WfEP1nyT9ZdPzPGBSPwdgNJozAE1v6CZSm0pKPVEuBlsY2IVuWWKhZ95OAbNN9BdRRc5J3o0JGsFd7bdeGjYSTZUKquq4JRtLf4EionmLogyImjjlCUFVdV/pVwKrgVOD2QSEAx64IcV9IY3oWk1sWdFfx0sWuMwJAdxOgJ8H1VEGTG1LCaJ9R0kh01hy2yMwfSnxKVUMb5SjLMsHRNu/QlEFAlKHSWwLt0O3bt27du2tci4ZBW2iE2jIO3OHkiM4WSU846pue/Qgutdeeh/37/wD/2Q==);";
                    link.attr("style", style);
                }
            }
        }
    }

    // remove all scripts
    Elements scripts = doc.getElementsByTag("script");
    for (Element script : scripts) {
        script.remove();
    }
    html = doc.outerHtml();
    writeContainer(out, html);
    out.flush();
}

From source file:org.structr.web.entity.dom.PageTest.java

public void testImportNodesDeep() {

    try (final Tx tx = app.tx()) {

        Page srcPage = Page.createNewPage(securityContext, "srcPage");

        assertTrue(srcPage != null);/*from  w w  w . j a  va  2  s .  com*/
        assertTrue(srcPage instanceof Page);

        Node html = srcPage.createElement("html");
        Node head = srcPage.createElement("head");
        Node body = srcPage.createElement("body");
        Node title = srcPage.createElement("title");
        Node h1 = srcPage.createElement("h1");
        Node div = srcPage.createElement("div");
        Node p = srcPage.createElement("p");

        // add HTML element to page
        srcPage.appendChild(html);

        // add HEAD and BODY elements to HTML
        html.appendChild(head);
        html.appendChild(body);

        // add TITLE element to HEAD
        head.appendChild(title);

        // add H1 element to BODY
        body.appendChild(h1);

        // add DIV element to BODY
        body.appendChild(div);
        div.appendChild(p);

        // add text element to P
        p.appendChild(srcPage.createTextNode("First Paragraph"));

        Page dstPage = Page.createNewPage(securityContext, "dstPage");
        assertNotNull(dstPage);

        // test
        assertEquals(srcPage, html.getOwnerDocument());

        makePublic(srcPage, dstPage, html, head, body, title, h1, div, p);

        // test import method
        dstPage.importNode(html, true);

        // has parent been removed for the source element?
        assertNull(html.getParentNode());

        // same owner for all elements?
        assertEquals(srcPage, html.getOwnerDocument());
        assertEquals(srcPage, head.getOwnerDocument());
        assertEquals(srcPage, body.getOwnerDocument());
        assertEquals(srcPage, title.getOwnerDocument());
        assertEquals(srcPage, h1.getOwnerDocument());
        assertEquals(srcPage, div.getOwnerDocument());
        assertEquals(srcPage, p.getOwnerDocument());

        // have parents been kept for all other elements?
        assertEquals(html, head.getParentNode());
        assertEquals(html, body.getParentNode());
        assertEquals(head, title.getParentNode());
        assertEquals(body, h1.getParentNode());
        assertEquals(body, div.getParentNode());
        assertEquals(div, p.getParentNode());

        tx.success();

    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception");
    }

    try (final Tx tx = app.tx()) {

        Document srcDoc = Jsoup.connect(baseUri + "srcPage").get();
        Document dstDoc = Jsoup.connect(baseUri + "dstPage").get();

        // pages should render exactly identical
        assertEquals(srcDoc.outerHtml(), dstDoc.outerHtml());

        tx.success();

    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:resources.HTMLManager.java

private static void saveFinalHtml(Document doc, String fileName) throws IOException {
    //System.out.println(doc.outerHtml());
    final File f = new File(fileName);
    FileUtils.writeStringToFile(f, doc.outerHtml(), "UTF-8");

}

From source file:test.Tab.java

private void exportFinalHtml(Document doc, String fileName) throws IOException {

    final File f = new File(fileName);
    FileUtils.writeStringToFile(f, doc.outerHtml(), "UTF-8");

}