Java Examples for org.jsoup.nodes.Document.OutputSettings

The following java examples will help you to understand the usage of org.jsoup.nodes.Document.OutputSettings. These source code samples are taken from different open source projects.

Example 1
Project: jsoup-master  File: EntitiesTest.java View source code
@Test
public void escape() {
    String text = "Hello &<> Å å π 新 there ¾ © »";
    String escapedAscii = Entities.escape(text, new OutputSettings().charset("ascii").escapeMode(base));
    String escapedAsciiFull = Entities.escape(text, new OutputSettings().charset("ascii").escapeMode(extended));
    String escapedAsciiXhtml = Entities.escape(text, new OutputSettings().charset("ascii").escapeMode(xhtml));
    String escapedUtfFull = Entities.escape(text, new OutputSettings().charset("UTF-8").escapeMode(extended));
    String escapedUtfMin = Entities.escape(text, new OutputSettings().charset("UTF-8").escapeMode(xhtml));
    assertEquals("Hello &<> Å å π 新 there ¾ © »", escapedAscii);
    assertEquals("Hello &<> Å å π 新 there ¾ © »", escapedAsciiFull);
    assertEquals("Hello &<> Å å π 新 there ¾ © »", escapedAsciiXhtml);
    assertEquals("Hello &<> Å å π 新 there ¾ © »", escapedUtfFull);
    assertEquals("Hello &<> Å å π 新 there ¾ © »", escapedUtfMin);
    // odd that it's defined as aring in base but angst in full
    // round trip
    assertEquals(text, Entities.unescape(escapedAscii));
    assertEquals(text, Entities.unescape(escapedAsciiFull));
    assertEquals(text, Entities.unescape(escapedAsciiXhtml));
    assertEquals(text, Entities.unescape(escapedUtfFull));
    assertEquals(text, Entities.unescape(escapedUtfMin));
}
Example 2
Project: CN1ML-NetbeansModule-master  File: DocumentTest.java View source code
@Test
public void testHtmlAndXmlSyntax() {
    String h = "<!DOCTYPE html><body><img async checked='checked' src='&<>\"'><>&"<foo />bar";
    Document doc = Jsoup.parse(h);
    doc.outputSettings().syntax(Syntax.html);
    assertEquals("<!DOCTYPE html>\n" + "<html>\n" + " <head></head>\n" + " <body>\n" + "  <img async checked src=\"&<>"\"><>&\"\n" + "  <foo />bar\n" + " </body>\n" + "</html>", doc.html());
    doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
    assertEquals("<!DOCTYPE html>\n" + "<html>\n" + " <head></head>\n" + " <body>\n" + "  <img async=\"\" checked=\"checked\" src=\"&<>"\" /><>&\"\n" + "  <foo />bar\n" + " </body>\n" + "</html>", doc.html());
}
Example 3
Project: commafeed-master  File: FeedUtils.java View source code
public static String handleContent(String content, String baseUri, boolean keepTextOnly) {
    if (StringUtils.isNotBlank(content)) {
        baseUri = StringUtils.trimToEmpty(baseUri);
        Document dirty = Jsoup.parseBodyFragment(content, baseUri);
        Cleaner cleaner = new Cleaner(WHITELIST);
        Document clean = cleaner.clean(dirty);
        for (Element e : clean.select("iframe[style]")) {
            String style = e.attr("style");
            String escaped = escapeIFrameCss(style);
            e.attr("style", escaped);
        }
        for (Element e : clean.select("img[style]")) {
            String style = e.attr("style");
            String escaped = escapeImgCss(style);
            e.attr("style", escaped);
        }
        clean.outputSettings(new OutputSettings().escapeMode(EscapeMode.base).prettyPrint(false));
        Element body = clean.body();
        if (keepTextOnly) {
            content = body.text();
        } else {
            content = body.html();
        }
    }
    return content;
}