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

The following examples show how to use org.jsoup.nodes.Document.OutputSettings.Syntax. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test public void testHtmlAndXmlSyntax() {
    String h = "<!DOCTYPE html><body><img async checked='checked' src='&<>\"'>&lt;&gt;&amp;&quot;<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=\"&amp;<>&quot;\">&lt;&gt;&amp;\"\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=\"&amp;<>&quot;\" />&lt;&gt;&amp;\"\n" +
            "  <foo />bar\n" +
            " </body>\n" +
            "</html>", doc.html());
}
 
Example 2
Source Project: astor   Source File: DocumentType.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
    if (out.syntax() == Syntax.html && !has(PUBLIC_ID) && !has(SYSTEM_ID)) {
        // looks like a html5 doctype, go lowercase for aesthetics
        accum.append("<!doctype");
    } else {
        accum.append("<!DOCTYPE");
    }
    if (has(NAME))
        accum.append(" ").append(attr(NAME));
    if (has(PUB_SYS_KEY))
        accum.append(" ").append(attr(PUB_SYS_KEY));
    if (has(PUBLIC_ID))
        accum.append(" \"").append(attr(PUBLIC_ID)).append('"');
    if (has(SYSTEM_ID))
        accum.append(" \"").append(attr(SYSTEM_ID)).append('"');
    accum.append('>');
}
 
Example 3
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test public void testHtmlAndXmlSyntax() {
    String h = "<!DOCTYPE html><body><img async checked='checked' src='&<>\"'>&lt;&gt;&amp;&quot;<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=\"&amp;<>&quot;\">&lt;&gt;&amp;\"\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=\"&amp;<>&quot;\" />&lt;&gt;&amp;\"\n" +
            "  <foo />bar\n" +
            " </body>\n" +
            "</html>", doc.html());
}
 
Example 4
Source Project: astor   Source File: DocumentType.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
    if (out.syntax() == Syntax.html && !has(PUBLIC_ID) && !has(SYSTEM_ID)) {
        // looks like a html5 doctype, go lowercase for aesthetics
        accum.append("<!doctype");
    } else {
        accum.append("<!DOCTYPE");
    }
    if (has(NAME))
        accum.append(" ").append(attr(NAME));
    if (has(PUB_SYS_KEY))
        accum.append(" ").append(attr(PUB_SYS_KEY));
    if (has(PUBLIC_ID))
        accum.append(" \"").append(attr(PUBLIC_ID)).append('"');
    if (has(SYSTEM_ID))
        accum.append(" \"").append(attr(SYSTEM_ID)).append('"');
    accum.append('>');
}
 
Example 5
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test public void testHtmlAndXmlSyntax() {
    String h = "<!DOCTYPE html><body><img async checked='checked' src='&<>\"'>&lt;&gt;&amp;&quot;<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=\"&amp;<>&quot;\">&lt;&gt;&amp;\"\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=\"&amp;<>&quot;\" />&lt;&gt;&amp;\"\n" +
            "  <foo />bar\n" +
            " </body>\n" +
            "</html>", doc.html());
}
 
Example 6
@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
    super.initCfg(cfg);

    formatter = new OutputSettings();
    formatter.charset(Charset.forName(options.getOrDefault("charset", StandardCharsets.UTF_8.name())));
    formatter.escapeMode(EscapeMode.valueOf(options.getOrDefault("escapeMode", EscapeMode.xhtml.name())));
    formatter.indentAmount(Integer.parseInt(options.getOrDefault("indentAmount", "4")));
    formatter.outline(Boolean.parseBoolean(options.getOrDefault("outlineMode", Boolean.TRUE.toString())));
    formatter.prettyPrint(Boolean.parseBoolean(options.getOrDefault("pretty", Boolean.TRUE.toString())));
    formatter.syntax(Syntax.valueOf(options.getOrDefault("syntax", Syntax.html.name())));
}
 
Example 7
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Document createXmlDocument(String version, String charset, boolean addDecl) {
    final Document doc = new Document("");
    doc.appendElement("root").text("node");
    doc.outputSettings().syntax(Syntax.xml);
    
    if( addDecl == true ) {
        XmlDeclaration decl = new XmlDeclaration("xml", "", false);
        decl.attr("version", version);
        decl.attr("encoding", charset);
        doc.prependChild(decl);
    }
    
    return doc;
}
 
Example 8
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Document createXmlDocument(String version, String charset, boolean addDecl) {
    final Document doc = new Document("");
    doc.appendElement("root").text("node");
    doc.outputSettings().syntax(Syntax.xml);
    
    if( addDecl == true ) {
        XmlDeclaration decl = new XmlDeclaration("xml", false);
        decl.attr("version", version);
        decl.attr("encoding", charset);
        doc.prependChild(decl);
    }
    
    return doc;
}
 
Example 9
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Document createXmlDocument(String version, String charset, boolean addDecl) {
    final Document doc = new Document("");
    doc.appendElement("root").text("node");
    doc.outputSettings().syntax(Syntax.xml);
    
    if( addDecl == true ) {
        XmlDeclaration decl = new XmlDeclaration("xml", false);
        decl.attr("version", version);
        decl.attr("encoding", charset);
        doc.prependChild(decl);
    }
    
    return doc;
}
 
Example 10
Source Project: CrawlerPack   Source File: PrefixXmlTreeBuilder.java    License: Apache License 2.0 4 votes vote down vote up
protected void initialiseParse(String input, String baseUri, ParseErrorList errors) {
    super.initialiseParse(input, baseUri, errors);
    this.stack.add(this.doc);
    this.doc.outputSettings().syntax(Syntax.xml);
}
 
Example 11
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test public void htmlParseDefaultsToHtmlOutputSyntax() {
    Document doc = Jsoup.parse("x");
    assertEquals(Syntax.html, doc.outputSettings().syntax());
}
 
Example 12
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test public void htmlParseDefaultsToHtmlOutputSyntax() {
    Document doc = Jsoup.parse("x");
    assertEquals(Syntax.html, doc.outputSettings().syntax());
}
 
Example 13
Source Project: astor   Source File: DocumentTest.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test public void htmlParseDefaultsToHtmlOutputSyntax() {
    Document doc = Jsoup.parse("x");
    assertEquals(Syntax.html, doc.outputSettings().syntax());
}