public class Jsoup
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
clean(java.lang.String bodyHtml,
java.lang.String baseUri,
Whitelist whitelist)
Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted
tags and attributes.
|
static java.lang.String |
clean(java.lang.String bodyHtml,
java.lang.String baseUri,
Whitelist whitelist,
Document.OutputSettings outputSettings)
Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of
permitted tags and attributes.
|
static java.lang.String |
clean(java.lang.String bodyHtml,
Whitelist whitelist)
Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted
tags and attributes.
|
static Connection |
connect(java.lang.String url)
Creates a new
Connection to a URL. |
static boolean |
isValid(java.lang.String bodyHtml,
Whitelist whitelist)
Test if the input body HTML has only tags and attributes allowed by the Whitelist.
|
static Document |
parse(java.io.File in,
java.lang.String charsetName)
Parse the contents of a file as HTML.
|
static Document |
parse(java.io.File in,
java.lang.String charsetName,
java.lang.String baseUri)
Parse the contents of a file as HTML.
|
static Document |
parse(java.io.InputStream in,
java.lang.String charsetName,
java.lang.String baseUri)
Read an input stream, and parse it to a Document.
|
static Document |
parse(java.io.InputStream in,
java.lang.String charsetName,
java.lang.String baseUri,
Parser parser)
Read an input stream, and parse it to a Document.
|
static Document |
parse(java.lang.String html)
Parse HTML into a Document.
|
static Document |
parse(java.lang.String html,
java.lang.String baseUri)
Parse HTML into a Document.
|
static Document |
parse(java.lang.String html,
java.lang.String baseUri,
Parser parser)
Parse HTML into a Document, using the provided Parser.
|
static Document |
parse(java.net.URL url,
int timeoutMillis)
Fetch a URL, and parse it as HTML.
|
static Document |
parseBodyFragment(java.lang.String bodyHtml)
Parse a fragment of HTML, with the assumption that it forms the
body of the HTML. |
static Document |
parseBodyFragment(java.lang.String bodyHtml,
java.lang.String baseUri)
Parse a fragment of HTML, with the assumption that it forms the
body of the HTML. |
public static Document parse(java.lang.String html, java.lang.String baseUri)
html
- HTML to parsebaseUri
- The URL where the HTML was retrieved from. Used to resolve relative URLs to absolute URLs, that occur
before the HTML declares a <base href>
tag.public static Document parse(java.lang.String html, java.lang.String baseUri, Parser parser)
html
- HTML to parsebaseUri
- The URL where the HTML was retrieved from. Used to resolve relative URLs to absolute URLs, that occur
before the HTML declares a <base href>
tag.parser
- alternate parser
to use.public static Document parse(java.lang.String html)
<base href>
tag.html
- HTML to parseparse(String, String)
public static Connection connect(java.lang.String url)
Connection
to a URL. Use to fetch and parse a HTML page.
Use examples:
Document doc = Jsoup.connect("http://example.com").userAgent("Mozilla").data("name", "jsoup").get();
Document doc = Jsoup.connect("http://example.com").cookie("auth", "token").post();
url
- URL to connect to. The protocol must be http
or https
.public static Document parse(java.io.File in, java.lang.String charsetName, java.lang.String baseUri) throws java.io.IOException
in
- file to load HTML fromcharsetName
- (optional) character set of file contents. Set to null
to determine from http-equiv
meta tag, if
present, or fall back to UTF-8
(which is often safe to do).baseUri
- The URL where the HTML was retrieved from, to resolve relative links against.java.io.IOException
- if the file could not be found, or read, or if the charsetName is invalid.public static Document parse(java.io.File in, java.lang.String charsetName) throws java.io.IOException
in
- file to load HTML fromcharsetName
- (optional) character set of file contents. Set to null
to determine from http-equiv
meta tag, if
present, or fall back to UTF-8
(which is often safe to do).java.io.IOException
- if the file could not be found, or read, or if the charsetName is invalid.parse(java.io.File, String, String)
public static Document parse(java.io.InputStream in, java.lang.String charsetName, java.lang.String baseUri) throws java.io.IOException
in
- input stream to read. Make sure to close it after parsing.charsetName
- (optional) character set of file contents. Set to null
to determine from http-equiv
meta tag, if
present, or fall back to UTF-8
(which is often safe to do).baseUri
- The URL where the HTML was retrieved from, to resolve relative links against.java.io.IOException
- if the file could not be found, or read, or if the charsetName is invalid.public static Document parse(java.io.InputStream in, java.lang.String charsetName, java.lang.String baseUri, Parser parser) throws java.io.IOException
in
- input stream to read. Make sure to close it after parsing.charsetName
- (optional) character set of file contents. Set to null
to determine from http-equiv
meta tag, if
present, or fall back to UTF-8
(which is often safe to do).baseUri
- The URL where the HTML was retrieved from, to resolve relative links against.parser
- alternate parser
to use.java.io.IOException
- if the file could not be found, or read, or if the charsetName is invalid.public static Document parseBodyFragment(java.lang.String bodyHtml, java.lang.String baseUri)
body
of the HTML.bodyHtml
- body HTML fragmentbaseUri
- URL to resolve relative URLs against.Document.body()
public static Document parseBodyFragment(java.lang.String bodyHtml)
body
of the HTML.bodyHtml
- body HTML fragmentDocument.body()
public static Document parse(java.net.URL url, int timeoutMillis) throws java.io.IOException
connect(String)
instead.
The encoding character set is determined by the content-type header or http-equiv meta tag, or falls back to UTF-8
.
url
- URL to fetch (with a GET). The protocol must be http
or https
.timeoutMillis
- Connection and read timeout, in milliseconds. If exceeded, IOException is thrown.java.net.MalformedURLException
- if the request URL is not a HTTP or HTTPS URL, or is otherwise malformedHttpStatusException
- if the response is not OK and HTTP response errors are not ignoredUnsupportedMimeTypeException
- if the response mime type is not supported and those errors are not ignoredjava.net.SocketTimeoutException
- if the connection times outjava.io.IOException
- if a connection or read error occursconnect(String)
public static java.lang.String clean(java.lang.String bodyHtml, java.lang.String baseUri, Whitelist whitelist)
bodyHtml
- input untrusted HTML (body fragment)baseUri
- URL to resolve relative URLs againstwhitelist
- white-list of permitted HTML elementsCleaner.clean(org.jsoup.nodes.Document)
public static java.lang.String clean(java.lang.String bodyHtml, Whitelist whitelist)
bodyHtml
- input untrusted HTML (body fragment)whitelist
- white-list of permitted HTML elementsCleaner.clean(org.jsoup.nodes.Document)
public static java.lang.String clean(java.lang.String bodyHtml, java.lang.String baseUri, Whitelist whitelist, Document.OutputSettings outputSettings)
The HTML is treated as a body fragment; it's expected the cleaned HTML will be used within the body of an
existing document. If you want to clean full documents, use Cleaner.clean(org.jsoup.nodes.Document)
instead, and add
structural tags (html, head, body
etc) to the whitelist.
bodyHtml
- input untrusted HTML (body fragment)baseUri
- URL to resolve relative URLs againstwhitelist
- white-list of permitted HTML elementsoutputSettings
- document output settings; use to control pretty-printing and entity escape modesCleaner.clean(org.jsoup.nodes.Document)
public static boolean isValid(java.lang.String bodyHtml, Whitelist whitelist)
The input HTML should still be run through the cleaner to set up enforced attributes, and to tidy the output.
Assumes the HTML is a body fragment (i.e. will be used in an existing HTML document body.)
bodyHtml
- HTML to testwhitelist
- whitelist to test againstclean(String, org.jsoup.safety.Whitelist)