diff --git a/build.gradle b/build.gradle index ee6ac15..07be04a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,48 +1,49 @@ buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' } } apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion '23.0.3' // was 23.0.2 compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } lintOptions { abortOnError false } repositories { mavenCentral() mavenLocal() } dependencies { compile 'com.android.support:support-v4:23.2.1' compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.melnykov:floatingactionbutton:1.1.0' + compile 'org.jsoup:jsoup:1.7.3' } } \ No newline at end of file diff --git a/src/org/jsoup/Connection.java b/src/org/jsoup/Connection.java deleted file mode 100644 index 44307a6..0000000 --- a/src/org/jsoup/Connection.java +++ /dev/null @@ -1,572 +0,0 @@ -package org.jsoup; - -import org.jsoup.nodes.Document; -import org.jsoup.parser.Parser; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Collection; -import java.util.Map; - -/** - * A Connection provides a convenient interface to fetch content from the web, and parse them into Documents. - *

- * To get a new Connection, use {@link org.jsoup.Jsoup#connect(String)}. Connections contain {@link Connection.Request} - * and {@link Connection.Response} objects. The request objects are reusable as prototype requests. - *

- * Request configuration can be made using either the shortcut methods in Connection (e.g. {@link #userAgent(String)}), - * or by methods in the Connection.Request object directly. All request configuration must be made before the request is - * executed. - */ -public interface Connection { - - /** - * GET and POST http methods. - */ - public enum Method { - GET(false), POST(true), PUT(true), DELETE(false), PATCH(true); - - private final boolean hasBody; - - private Method(boolean hasBody) { - this.hasBody = hasBody; - } - - /** - * Check if this HTTP method has/needs a request body - * @return if body needed - */ - public final boolean hasBody() { - return hasBody; - } - } - - /** - * Set the request URL to fetch. The protocol must be HTTP or HTTPS. - * @param url URL to connect to - * @return this Connection, for chaining - */ - public Connection url(URL url); - - /** - * Set the request URL to fetch. The protocol must be HTTP or HTTPS. - * @param url URL to connect to - * @return this Connection, for chaining - */ - public Connection url(String url); - - /** - * Set the request user-agent header. - * @param userAgent user-agent to use - * @return this Connection, for chaining - */ - public Connection userAgent(String userAgent); - - /** - * Set the request timeouts (connect and read). If a timeout occurs, an IOException will be thrown. The default - * timeout is 3 seconds (3000 millis). A timeout of zero is treated as an infinite timeout. - * @param millis number of milliseconds (thousandths of a second) before timing out connects or reads. - * @return this Connection, for chaining - */ - public Connection timeout(int millis); - - /** - * Set the maximum bytes to read from the (uncompressed) connection into the body, before the connection is closed, - * and the input truncated. The default maximum is 1MB. A max size of zero is treated as an infinite amount (bounded - * only by your patience and the memory available on your machine). - * @param bytes number of bytes to read from the input before truncating - * @return this Connection, for chaining - */ - public Connection maxBodySize(int bytes); - - /** - * Set the request referrer (aka "referer") header. - * @param referrer referrer to use - * @return this Connection, for chaining - */ - public Connection referrer(String referrer); - - /** - * Configures the connection to (not) follow server redirects. By default this is true. - * @param followRedirects true if server redirects should be followed. - * @return this Connection, for chaining - */ - public Connection followRedirects(boolean followRedirects); - - /** - * Set the request method to use, GET or POST. Default is GET. - * @param method HTTP request method - * @return this Connection, for chaining - */ - public Connection method(Method method); - - /** - * Configures the connection to not throw exceptions when a HTTP error occurs. (4xx - 5xx, e.g. 404 or 500). By - * default this is false; an IOException is thrown if an error is encountered. If set to true, the - * response is populated with the error body, and the status message will reflect the error. - * @param ignoreHttpErrors - false (default) if HTTP errors should be ignored. - * @return this Connection, for chaining - */ - public Connection ignoreHttpErrors(boolean ignoreHttpErrors); - - /** - * Ignore the document's Content-Type when parsing the response. By default this is false, an unrecognised - * content-type will cause an IOException to be thrown. (This is to prevent producing garbage by attempting to parse - * a JPEG binary image, for example.) Set to true to force a parse attempt regardless of content type. - * @param ignoreContentType set to true if you would like the content type ignored on parsing the response into a - * Document. - * @return this Connection, for chaining - */ - public Connection ignoreContentType(boolean ignoreContentType); - - /** - * Add a request data parameter. Request parameters are sent in the request query string for GETs, and in the - * request body for POSTs. A request may have multiple values of the same name. - * @param key data key - * @param value data value - * @return this Connection, for chaining - */ - public Connection data(String key, String value); - - /** - * Add an input stream as a request data paramater. For GETs, has no effect, but for POSTS this will upload the - * input stream. - * @param key data key (form item name) - * @param filename the name of the file to present to the remove server. Typically just the name, not path, - * component. - * @param inputStream the input stream to upload, that you probably obtained from a {@link java.io.FileInputStream}. - * You must close the InputStream in a {@code finally} block. - * @return this Connections, for chaining - */ - public Connection data(String key, String filename, InputStream inputStream); - - /** - * Adds all of the supplied data to the request data parameters - * @param data collection of data parameters - * @return this Connection, for chaining - */ - public Connection data(Collection data); - - /** - * Adds all of the supplied data to the request data parameters - * @param data map of data parameters - * @return this Connection, for chaining - */ - public Connection data(Map data); - - /** - * Add a number of request data parameters. Multiple parameters may be set at once, e.g.: .data("name", - * "jsoup", "language", "Java", "language", "English"); creates a query string like: - * ?name=jsoup&language=Java&language=English - * @param keyvals a set of key value pairs. - * @return this Connection, for chaining - */ - public Connection data(String... keyvals); - - /** - * Set a request header. - * @param name header name - * @param value header value - * @return this Connection, for chaining - * @see org.jsoup.Connection.Request#headers() - */ - public Connection header(String name, String value); - - /** - * Set a cookie to be sent in the request. - * @param name name of cookie - * @param value value of cookie - * @return this Connection, for chaining - */ - public Connection cookie(String name, String value); - - /** - * Adds each of the supplied cookies to the request. - * @param cookies map of cookie name -> value pairs - * @return this Connection, for chaining - */ - public Connection cookies(Map cookies); - - /** - * Provide an alternate parser to use when parsing the response to a Document. - * @param parser alternate parser - * @return this Connection, for chaining - */ - public Connection parser(Parser parser); - - /** - * Execute the request as a GET, and parse the result. - * @return parsed Document - * @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed - * @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored - * @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored - * @throws java.net.SocketTimeoutException if the connection times out - * @throws IOException on error - */ - public Document get() throws IOException; - - /** - * Execute the request as a POST, and parse the result. - * @return parsed Document - * @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed - * @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored - * @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored - * @throws java.net.SocketTimeoutException if the connection times out - * @throws IOException on error - */ - public Document post() throws IOException; - - /** - * Execute the request. - * @return a response object - * @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed - * @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored - * @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored - * @throws java.net.SocketTimeoutException if the connection times out - * @throws IOException on error - */ - public Response execute() throws IOException; - - /** - * Get the request object associated with this connection - * @return request - */ - public Request request(); - - /** - * Set the connection's request - * @param request new request object - * @return this Connection, for chaining - */ - public Connection request(Request request); - - /** - * Get the response, once the request has been executed - * @return response - */ - public Response response(); - - /** - * Set the connection's response - * @param response new response - * @return this Connection, for chaining - */ - public Connection response(Response response); - - /** - * Common methods for Requests and Responses - * @param Type of Base, either Request or Response - */ - interface Base { - - /** - * Get the URL - * @return URL - */ - public URL url(); - - /** - * Set the URL - * @param url new URL - * @return this, for chaining - */ - public T url(URL url); - - /** - * Get the request method - * @return method - */ - public Method method(); - - /** - * Set the request method - * @param method new method - * @return this, for chaining - */ - public T method(Method method); - - /** - * Get the value of a header. This is a simplified header model, where a header may only have one value. - *

- * Header names are case insensitive. - * @param name name of header (case insensitive) - * @return value of header, or null if not set. - * @see #hasHeader(String) - * @see #cookie(String) - */ - public String header(String name); - - /** - * Set a header. This method will overwrite any existing header with the same case insensitive name. - * @param name Name of header - * @param value Value of header - * @return this, for chaining - */ - public T header(String name, String value); - - /** - * Check if a header is present - * @param name name of header (case insensitive) - * @return if the header is present in this request/response - */ - public boolean hasHeader(String name); - - /** - * Check if a header is present, with the given value - * @param name header name (case insensitive) - * @param value value (case insensitive) - * @return if the header and value pair are set in this req/res - */ - public boolean hasHeaderWithValue(String name, String value); - - /** - * Remove a header by name - * @param name name of header to remove (case insensitive) - * @return this, for chaining - */ - public T removeHeader(String name); - - /** - * Retrieve all of the request/response headers as a map - * @return headers - */ - public Map headers(); - - /** - * Get a cookie value by name from this request/response. - *

- * Response objects have a simplified cookie model. Each cookie set in the response is added to the response - * object's cookie key=value map. The cookie's path, domain, and expiry date are ignored. - * @param name name of cookie to retrieve. - * @return value of cookie, or null if not set - */ - public String cookie(String name); - - /** - * Set a cookie in this request/response. - * @param name name of cookie - * @param value value of cookie - * @return this, for chaining - */ - public T cookie(String name, String value); - - /** - * Check if a cookie is present - * @param name name of cookie - * @return if the cookie is present in this request/response - */ - public boolean hasCookie(String name); - - /** - * Remove a cookie by name - * @param name name of cookie to remove - * @return this, for chaining - */ - public T removeCookie(String name); - - /** - * Retrieve all of the request/response cookies as a map - * @return cookies - */ - public Map cookies(); - } - - /** - * Represents a HTTP request. - */ - public interface Request extends Base { - /** - * Get the request timeout, in milliseconds. - * @return the timeout in milliseconds. - */ - public int timeout(); - - /** - * Update the request timeout. - * @param millis timeout, in milliseconds - * @return this Request, for chaining - */ - public Request timeout(int millis); - - /** - * Get the maximum body size, in bytes. - * @return the maximum body size, in bytes. - */ - public int maxBodySize(); - - /** - * Update the maximum body size, in bytes. - * @param bytes maximum body size, in bytes. - * @return this Request, for chaining - */ - public Request maxBodySize(int bytes); - - /** - * Get the current followRedirects configuration. - * @return true if followRedirects is enabled. - */ - public boolean followRedirects(); - - /** - * Configures the request to (not) follow server redirects. By default this is true. - * @param followRedirects true if server redirects should be followed. - * @return this Request, for chaining - */ - public Request followRedirects(boolean followRedirects); - - /** - * Get the current ignoreHttpErrors configuration. - * @return true if errors will be ignored; false (default) if HTTP errors will cause an IOException to be - * thrown. - */ - public boolean ignoreHttpErrors(); - - /** - * Configures the request to ignore HTTP errors in the response. - * @param ignoreHttpErrors set to true to ignore HTTP errors. - * @return this Request, for chaining - */ - public Request ignoreHttpErrors(boolean ignoreHttpErrors); - - /** - * Get the current ignoreContentType configuration. - * @return true if invalid content-types will be ignored; false (default) if they will cause an IOException to - * be thrown. - */ - public boolean ignoreContentType(); - - /** - * Configures the request to ignore the Content-Type of the response. - * @param ignoreContentType set to true to ignore the content type. - * @return this Request, for chaining - */ - public Request ignoreContentType(boolean ignoreContentType); - - /** - * Add a data parameter to the request - * @param keyval data to add. - * @return this Request, for chaining - */ - public Request data(KeyVal keyval); - - /** - * Get all of the request's data parameters - * @return collection of keyvals - */ - public Collection data(); - - /** - * Specify the parser to use when parsing the document. - * @param parser parser to use. - * @return this Request, for chaining - */ - public Request parser(Parser parser); - - /** - * Get the current parser to use when parsing the document. - * @return current Parser - */ - public Parser parser(); - } - - /** - * Represents a HTTP response. - */ - public interface Response extends Base { - - /** - * Get the status code of the response. - * @return status code - */ - public int statusCode(); - - /** - * Get the status message of the response. - * @return status message - */ - public String statusMessage(); - - /** - * Get the character set name of the response. - * @return character set name - */ - public String charset(); - - /** - * Get the response content type (e.g. "text/html"); - * @return the response content type - */ - public String contentType(); - - /** - * Parse the body of the response as a Document. - * @return a parsed Document - * @throws IOException on error - */ - public Document parse() throws IOException; - - /** - * Get the body of the response as a plain string. - * @return body - */ - public String body(); - - /** - * Get the body of the response as an array of bytes. - * @return body bytes - */ - public byte[] bodyAsBytes(); - } - - /** - * A Key Value tuple. - */ - public interface KeyVal { - - /** - * Update the key of a keyval - * @param key new key - * @return this KeyVal, for chaining - */ - public KeyVal key(String key); - - /** - * Get the key of a keyval - * @return the key - */ - public String key(); - - /** - * Update the value of a keyval - * @param value the new value - * @return this KeyVal, for chaining - */ - public KeyVal value(String value); - - /** - * Get the value of a keyval - * @return the value - */ - public String value(); - - /** - * Add or update an input stream to this keyVal - * @param inputStream new input stream - * @return this KeyVal, for chaining - */ - public KeyVal inputStream(InputStream inputStream); - - /** - * Get the input stream associated with this keyval, if any - * @return input stream if set, or null - */ - public InputStream inputStream(); - - /** - * Does this keyval have an input stream? - * @return true if this keyval does indeed have an input stream - */ - public boolean hasInputStream(); - } -} diff --git a/src/org/jsoup/HttpStatusException.java b/src/org/jsoup/HttpStatusException.java deleted file mode 100644 index 75cd6cd..0000000 --- a/src/org/jsoup/HttpStatusException.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.jsoup; - -import java.io.IOException; - -/** - * Signals that a HTTP request resulted in a not OK HTTP response. - */ -public class HttpStatusException extends IOException { - private int statusCode; - private String url; - - public HttpStatusException(String message, int statusCode, String url) { - super(message); - this.statusCode = statusCode; - this.url = url; - } - - public int getStatusCode() { - return statusCode; - } - - public String getUrl() { - return url; - } - - @Override - public String toString() { - return super.toString() + ". Status=" + statusCode + ", URL=" + url; - } -} diff --git a/src/org/jsoup/Jsoup.java b/src/org/jsoup/Jsoup.java deleted file mode 100644 index 3252daf..0000000 --- a/src/org/jsoup/Jsoup.java +++ /dev/null @@ -1,252 +0,0 @@ -package org.jsoup; - -import org.jsoup.nodes.Document; -import org.jsoup.parser.Parser; -import org.jsoup.safety.Cleaner; -import org.jsoup.safety.Whitelist; -import org.jsoup.helper.DataUtil; -import org.jsoup.helper.HttpConnection; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; - -/** - The core public access point to the jsoup functionality. - - @author Jonathan Hedley */ -public class Jsoup { - private Jsoup() {} - - /** - Parse HTML into a Document. The parser will make a sensible, balanced document tree out of any HTML. - - @param html HTML to parse - @param baseUri The URL where the HTML was retrieved from. Used to resolve relative URLs to absolute URLs, that occur - before the HTML declares a {@code } tag. - @return sane HTML - */ - public static Document parse(String html, String baseUri) { - return Parser.parse(html, baseUri); - } - - /** - Parse HTML into a Document, using the provided Parser. You can provide an alternate parser, such as a simple XML - (non-HTML) parser. - - @param html HTML to parse - @param baseUri The URL where the HTML was retrieved from. Used to resolve relative URLs to absolute URLs, that occur - before the HTML declares a {@code } tag. - @param parser alternate {@link Parser#xmlParser() parser} to use. - @return sane HTML - */ - public static Document parse(String html, String baseUri, Parser parser) { - return parser.parseInput(html, baseUri); - } - - /** - Parse HTML into a Document. As no base URI is specified, absolute URL detection relies on the HTML including a - {@code } tag. - - @param html HTML to parse - @return sane HTML - - @see #parse(String, String) - */ - public static Document parse(String html) { - return Parser.parse(html, ""); - } - - /** - * Creates a new {@link Connection} to a URL. Use to fetch and parse a HTML page. - *

- * Use examples: - *

- * @param url URL to connect to. The protocol must be {@code http} or {@code https}. - * @return the connection. You can add data, cookies, and headers; set the user-agent, referrer, method; and then execute. - */ - public static Connection connect(String url) { - return HttpConnection.connect(url); - } - - /** - Parse the contents of a file as HTML. - - @param in file to load HTML from - @param charsetName (optional) character set of file contents. Set to {@code null} to determine from {@code http-equiv} meta tag, if - present, or fall back to {@code UTF-8} (which is often safe to do). - @param baseUri The URL where the HTML was retrieved from, to resolve relative links against. - @return sane HTML - - @throws IOException if the file could not be found, or read, or if the charsetName is invalid. - */ - public static Document parse(File in, String charsetName, String baseUri) throws IOException { - return DataUtil.load(in, charsetName, baseUri); - } - - /** - Parse the contents of a file as HTML. The location of the file is used as the base URI to qualify relative URLs. - - @param in file to load HTML from - @param charsetName (optional) character set of file contents. Set to {@code null} to determine from {@code http-equiv} meta tag, if - present, or fall back to {@code UTF-8} (which is often safe to do). - @return sane HTML - - @throws IOException if the file could not be found, or read, or if the charsetName is invalid. - @see #parse(File, String, String) - */ - public static Document parse(File in, String charsetName) throws IOException { - return DataUtil.load(in, charsetName, in.getAbsolutePath()); - } - - /** - Read an input stream, and parse it to a Document. - - @param in input stream to read. Make sure to close it after parsing. - @param charsetName (optional) character set of file contents. Set to {@code null} to determine from {@code http-equiv} meta tag, if - present, or fall back to {@code UTF-8} (which is often safe to do). - @param baseUri The URL where the HTML was retrieved from, to resolve relative links against. - @return sane HTML - - @throws IOException if the file could not be found, or read, or if the charsetName is invalid. - */ - public static Document parse(InputStream in, String charsetName, String baseUri) throws IOException { - return DataUtil.load(in, charsetName, baseUri); - } - - /** - Read an input stream, and parse it to a Document. You can provide an alternate parser, such as a simple XML - (non-HTML) parser. - - @param in input stream to read. Make sure to close it after parsing. - @param charsetName (optional) character set of file contents. Set to {@code null} to determine from {@code http-equiv} meta tag, if - present, or fall back to {@code UTF-8} (which is often safe to do). - @param baseUri The URL where the HTML was retrieved from, to resolve relative links against. - @param parser alternate {@link Parser#xmlParser() parser} to use. - @return sane HTML - - @throws IOException if the file could not be found, or read, or if the charsetName is invalid. - */ - public static Document parse(InputStream in, String charsetName, String baseUri, Parser parser) throws IOException { - return DataUtil.load(in, charsetName, baseUri, parser); - } - - /** - Parse a fragment of HTML, with the assumption that it forms the {@code body} of the HTML. - - @param bodyHtml body HTML fragment - @param baseUri URL to resolve relative URLs against. - @return sane HTML document - - @see Document#body() - */ - public static Document parseBodyFragment(String bodyHtml, String baseUri) { - return Parser.parseBodyFragment(bodyHtml, baseUri); - } - - /** - Parse a fragment of HTML, with the assumption that it forms the {@code body} of the HTML. - - @param bodyHtml body HTML fragment - @return sane HTML document - - @see Document#body() - */ - public static Document parseBodyFragment(String bodyHtml) { - return Parser.parseBodyFragment(bodyHtml, ""); - } - - /** - Fetch a URL, and parse it as HTML. Provided for compatibility; in most cases use {@link #connect(String)} instead. -

- The encoding character set is determined by the content-type header or http-equiv meta tag, or falls back to {@code UTF-8}. - - @param url URL to fetch (with a GET). The protocol must be {@code http} or {@code https}. - @param timeoutMillis Connection and read timeout, in milliseconds. If exceeded, IOException is thrown. - @return The parsed HTML. - - @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed - @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored - @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored - @throws java.net.SocketTimeoutException if the connection times out - @throws IOException if a connection or read error occurs - - @see #connect(String) - */ - public static Document parse(URL url, int timeoutMillis) throws IOException { - Connection con = HttpConnection.connect(url); - con.timeout(timeoutMillis); - return con.get(); - } - - /** - Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted - tags and attributes. - - @param bodyHtml input untrusted HTML (body fragment) - @param baseUri URL to resolve relative URLs against - @param whitelist white-list of permitted HTML elements - @return safe HTML (body fragment) - - @see Cleaner#clean(Document) - */ - public static String clean(String bodyHtml, String baseUri, Whitelist whitelist) { - Document dirty = parseBodyFragment(bodyHtml, baseUri); - Cleaner cleaner = new Cleaner(whitelist); - Document clean = cleaner.clean(dirty); - return clean.body().html(); - } - - /** - Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted - tags and attributes. - - @param bodyHtml input untrusted HTML (body fragment) - @param whitelist white-list of permitted HTML elements - @return safe HTML (body fragment) - - @see Cleaner#clean(Document) - */ - public static String clean(String bodyHtml, Whitelist whitelist) { - return clean(bodyHtml, "", whitelist); - } - - /** - * Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of - * permitted - * tags and attributes. - * - * @param bodyHtml input untrusted HTML (body fragment) - * @param baseUri URL to resolve relative URLs against - * @param whitelist white-list of permitted HTML elements - * @param outputSettings document output settings; use to control pretty-printing and entity escape modes - * @return safe HTML (body fragment) - * @see Cleaner#clean(Document) - */ - public static String clean(String bodyHtml, String baseUri, Whitelist whitelist, Document.OutputSettings outputSettings) { - Document dirty = parseBodyFragment(bodyHtml, baseUri); - Cleaner cleaner = new Cleaner(whitelist); - Document clean = cleaner.clean(dirty); - clean.outputSettings(outputSettings); - return clean.body().html(); - } - - /** - Test if the input HTML has only tags and attributes allowed by the Whitelist. Useful for form validation. The input HTML should - still be run through the cleaner to set up enforced attributes, and to tidy the output. - @param bodyHtml HTML to test - @param whitelist whitelist to test against - @return true if no tags or attributes were removed; false otherwise - @see #clean(String, org.jsoup.safety.Whitelist) - */ - public static boolean isValid(String bodyHtml, Whitelist whitelist) { - Document dirty = parseBodyFragment(bodyHtml, ""); - Cleaner cleaner = new Cleaner(whitelist); - return cleaner.isValid(dirty); - } - -} diff --git a/src/org/jsoup/UnsupportedMimeTypeException.java b/src/org/jsoup/UnsupportedMimeTypeException.java deleted file mode 100644 index 124bc97..0000000 --- a/src/org/jsoup/UnsupportedMimeTypeException.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.jsoup; - -import java.io.IOException; - -/** - * Signals that a HTTP response returned a mime type that is not supported. - */ -public class UnsupportedMimeTypeException extends IOException { - private String mimeType; - private String url; - - public UnsupportedMimeTypeException(String message, String mimeType, String url) { - super(message); - this.mimeType = mimeType; - this.url = url; - } - - public String getMimeType() { - return mimeType; - } - - public String getUrl() { - return url; - } - - @Override - public String toString() { - return super.toString() + ". Mimetype=" + mimeType + ", URL="+url; - } -} diff --git a/src/org/jsoup/helper/DataUtil.java b/src/org/jsoup/helper/DataUtil.java deleted file mode 100644 index 95d1200..0000000 --- a/src/org/jsoup/helper/DataUtil.java +++ /dev/null @@ -1,224 +0,0 @@ -package org.jsoup.helper; - -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Parser; - -import java.io.*; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; -import java.util.Random; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.Locale; - -/** - * Internal static utilities for handling data. - * - */ -public final class DataUtil { - private static final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*(?:\"|')?([^\\s,;\"']*)"); - static final String defaultCharset = "UTF-8"; // used if not found in header or meta charset - private static final int bufferSize = 0x20000; // ~130K. - private static final int UNICODE_BOM = 0xFEFF; - private static final char[] mimeBoundaryChars = - "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); - static final int boundaryLength = 32; - - private DataUtil() {} - - /** - * Loads a file to a Document. - * @param in file to load - * @param charsetName character set of input - * @param baseUri base URI of document, to resolve relative links against - * @return Document - * @throws IOException on IO error - */ - public static Document load(File in, String charsetName, String baseUri) throws IOException { - ByteBuffer byteData = readFileToByteBuffer(in); - return parseByteData(byteData, charsetName, baseUri, Parser.htmlParser()); - } - - /** - * Parses a Document from an input steam. - * @param in input stream to parse. You will need to close it. - * @param charsetName character set of input - * @param baseUri base URI of document, to resolve relative links against - * @return Document - * @throws IOException on IO error - */ - public static Document load(InputStream in, String charsetName, String baseUri) throws IOException { - ByteBuffer byteData = readToByteBuffer(in); - return parseByteData(byteData, charsetName, baseUri, Parser.htmlParser()); - } - - /** - * Parses a Document from an input steam, using the provided Parser. - * @param in input stream to parse. You will need to close it. - * @param charsetName character set of input - * @param baseUri base URI of document, to resolve relative links against - * @param parser alternate {@link Parser#xmlParser() parser} to use. - * @return Document - * @throws IOException on IO error - */ - public static Document load(InputStream in, String charsetName, String baseUri, Parser parser) throws IOException { - ByteBuffer byteData = readToByteBuffer(in); - return parseByteData(byteData, charsetName, baseUri, parser); - } - - /** - * Writes the input stream to the output stream. Doesn't close them. - * @param in input stream to read from - * @param out output stream to write to - * @throws IOException on IO error - */ - static void crossStreams(final InputStream in, final OutputStream out) throws IOException { - final byte[] buffer = new byte[bufferSize]; - int len; - while ((len = in.read(buffer)) != -1) { - out.write(buffer, 0, len); - } - } - - // reads bytes first into a buffer, then decodes with the appropriate charset. done this way to support - // switching the chartset midstream when a meta http-equiv tag defines the charset. - // todo - this is getting gnarly. needs a rewrite. - static Document parseByteData(ByteBuffer byteData, String charsetName, String baseUri, Parser parser) { - String docData; - Document doc = null; - if (charsetName == null) { // determine from meta. safe parse as UTF-8 - // look for or HTML5 - docData = Charset.forName(defaultCharset).decode(byteData).toString(); - doc = parser.parseInput(docData, baseUri); - Element meta = doc.select("meta[http-equiv=content-type], meta[charset]").first(); - if (meta != null) { // if not found, will keep utf-8 as best attempt - String foundCharset; - if (meta.hasAttr("http-equiv")) { - foundCharset = getCharsetFromContentType(meta.attr("content")); - if (foundCharset == null && meta.hasAttr("charset")) { - try { - if (Charset.isSupported(meta.attr("charset"))) { - foundCharset = meta.attr("charset"); - } - } catch (IllegalCharsetNameException e) { - foundCharset = null; - } - } - } else { - foundCharset = meta.attr("charset"); - } - - if (foundCharset != null && foundCharset.length() != 0 && !foundCharset.equals(defaultCharset)) { // need to re-decode - foundCharset = foundCharset.trim().replaceAll("[\"']", ""); - charsetName = foundCharset; - byteData.rewind(); - docData = Charset.forName(foundCharset).decode(byteData).toString(); - doc = null; - } - } - } else { // specified by content type header (or by user on file load) - Validate.notEmpty(charsetName, "Must set charset arg to character set of file to parse. Set to null to attempt to detect from HTML"); - docData = Charset.forName(charsetName).decode(byteData).toString(); - } - // UTF-8 BOM indicator. takes precedence over everything else. rarely used. re-decodes incase above decoded incorrectly - if (docData.length() > 0 && docData.charAt(0) == UNICODE_BOM) { - byteData.rewind(); - docData = Charset.forName(defaultCharset).decode(byteData).toString(); - docData = docData.substring(1); - charsetName = defaultCharset; - doc = null; - } - if (doc == null) { - doc = parser.parseInput(docData, baseUri); - doc.outputSettings().charset(charsetName); - } - return doc; - } - - /** - * Read the input stream into a byte buffer. - * @param inStream the input stream to read from - * @param maxSize the maximum size in bytes to read from the stream. Set to 0 to be unlimited. - * @return the filled byte buffer - * @throws IOException if an exception occurs whilst reading from the input stream. - */ - static ByteBuffer readToByteBuffer(InputStream inStream, int maxSize) throws IOException { - Validate.isTrue(maxSize >= 0, "maxSize must be 0 (unlimited) or larger"); - final boolean capped = maxSize > 0; - byte[] buffer = new byte[bufferSize]; - ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize); - int read; - int remaining = maxSize; - - while (true) { - read = inStream.read(buffer); - if (read == -1) break; - if (capped) { - if (read > remaining) { - outStream.write(buffer, 0, remaining); - break; - } - remaining -= read; - } - outStream.write(buffer, 0, read); - } - ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray()); - return byteData; - } - - static ByteBuffer readToByteBuffer(InputStream inStream) throws IOException { - return readToByteBuffer(inStream, 0); - } - - static ByteBuffer readFileToByteBuffer(File file) throws IOException { - RandomAccessFile randomAccessFile = null; - try { - randomAccessFile = new RandomAccessFile(file, "r"); - byte[] bytes = new byte[(int) randomAccessFile.length()]; - randomAccessFile.readFully(bytes); - return ByteBuffer.wrap(bytes); - } finally { - if (randomAccessFile != null) - randomAccessFile.close(); - } - } - - /** - * Parse out a charset from a content type header. If the charset is not supported, returns null (so the default - * will kick in.) - * @param contentType e.g. "text/html; charset=EUC-JP" - * @return "EUC-JP", or null if not found. Charset is trimmed and uppercased. - */ - static String getCharsetFromContentType(String contentType) { - if (contentType == null) return null; - Matcher m = charsetPattern.matcher(contentType); - if (m.find()) { - String charset = m.group(1).trim(); - charset = charset.replace("charset=", ""); - if (charset.length() == 0) return null; - try { - if (Charset.isSupported(charset)) return charset; - charset = charset.toUpperCase(Locale.ENGLISH); - if (Charset.isSupported(charset)) return charset; - } catch (IllegalCharsetNameException e) { - // if our advanced charset matching fails.... we just take the default - return null; - } - } - return null; - } - - /** - * Creates a random string, suitable for use as a mime boundary - */ - static String mimeBoundary() { - final StringBuilder mime = new StringBuilder(boundaryLength); - final Random rand = new Random(); - for (int i = 0; i < boundaryLength; i++) { - mime.append(mimeBoundaryChars[rand.nextInt(mimeBoundaryChars.length)]); - } - return mime.toString(); - } -} diff --git a/src/org/jsoup/helper/DescendableLinkedList.java b/src/org/jsoup/helper/DescendableLinkedList.java deleted file mode 100644 index 01bed86..0000000 --- a/src/org/jsoup/helper/DescendableLinkedList.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.jsoup.helper; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.ListIterator; - -/** - * Provides a descending iterator and other 1.6 methods to allow support on the 1.5 JRE. - * @param Type of elements - */ -public class DescendableLinkedList extends LinkedList { - - /** - * Create a new DescendableLinkedList. - */ - public DescendableLinkedList() { - super(); - } - - /** - * Add a new element to the start of the list. - * @param e element to add - */ - @Override - public void push(E e) { - addFirst(e); - } - - /** - * Look at the last element, if there is one. - * @return the last element, or null - */ - @Override - public E peekLast() { - return size() == 0 ? null : getLast(); - } - - /** - * Remove and return the last element, if there is one - * @return the last element, or null - */ - @Override - public E pollLast() { - return size() == 0 ? null : removeLast(); - } - - /** - * Get an iterator that starts and the end of the list and works towards the start. - * @return an iterator that starts and the end of the list and works towards the start. - */ - @Override - public Iterator descendingIterator() { - return new DescendingIterator(size()); - } - - private class DescendingIterator implements Iterator { - private final ListIterator iter; - - @SuppressWarnings("unchecked") - private DescendingIterator(int index) { - iter = (ListIterator) listIterator(index); - } - - /** - * Check if there is another element on the list. - * @return if another element - */ - public boolean hasNext() { - return iter.hasPrevious(); - } - - /** - * Get the next element. - * @return the next element. - */ - public E next() { - return iter.previous(); - } - - /** - * Remove the current element. - */ - public void remove() { - iter.remove(); - } - } -} diff --git a/src/org/jsoup/helper/HttpConnection.java b/src/org/jsoup/helper/HttpConnection.java deleted file mode 100644 index 9daabc1..0000000 --- a/src/org/jsoup/helper/HttpConnection.java +++ /dev/null @@ -1,809 +0,0 @@ -package org.jsoup.helper; - -import org.jsoup.Connection; -import org.jsoup.HttpStatusException; -import org.jsoup.UnsupportedMimeTypeException; -import org.jsoup.nodes.Document; -import org.jsoup.parser.Parser; -import org.jsoup.parser.TokenQueue; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.util.*; -import java.util.regex.Pattern; -import java.util.zip.GZIPInputStream; - -import static org.jsoup.Connection.Method; - -/** - * Implementation of {@link Connection}. - * @see org.jsoup.Jsoup#connect(String) - */ -public class HttpConnection implements Connection { - public static final String CONTENT_ENCODING = "Content-Encoding"; - private static final String CONTENT_TYPE = "Content-Type"; - private static final String MULTIPART_FORM_DATA = "multipart/form-data"; - private static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded"; - - public static Connection connect(String url) { - Connection con = new HttpConnection(); - con.url(url); - return con; - } - - public static Connection connect(URL url) { - Connection con = new HttpConnection(); - con.url(url); - return con; - } - - private static String encodeUrl(String url) { - if(url == null) - return null; - return url.replaceAll(" ", "%20"); - } - - private static String encodeMimeName(String val) { - if (val == null) - return null; - return val.replaceAll("\"", "%22"); - } - - private Connection.Request req; - private Connection.Response res; - - private HttpConnection() { - req = new Request(); - res = new Response(); - } - - public Connection url(URL url) { - req.url(url); - return this; - } - - public Connection url(String url) { - Validate.notEmpty(url, "Must supply a valid URL"); - try { - req.url(new URL(encodeUrl(url))); - } catch (MalformedURLException e) { - throw new IllegalArgumentException("Malformed URL: " + url, e); - } - return this; - } - - public Connection userAgent(String userAgent) { - Validate.notNull(userAgent, "User agent must not be null"); - req.header("User-Agent", userAgent); - return this; - } - - public Connection timeout(int millis) { - req.timeout(millis); - return this; - } - - public Connection maxBodySize(int bytes) { - req.maxBodySize(bytes); - return this; - } - - public Connection followRedirects(boolean followRedirects) { - req.followRedirects(followRedirects); - return this; - } - - public Connection referrer(String referrer) { - Validate.notNull(referrer, "Referrer must not be null"); - req.header("Referer", referrer); - return this; - } - - public Connection method(Method method) { - req.method(method); - return this; - } - - public Connection ignoreHttpErrors(boolean ignoreHttpErrors) { - req.ignoreHttpErrors(ignoreHttpErrors); - return this; - } - - public Connection ignoreContentType(boolean ignoreContentType) { - req.ignoreContentType(ignoreContentType); - return this; - } - - public Connection data(String key, String value) { - req.data(KeyVal.create(key, value)); - return this; - } - - public Connection data(String key, String filename, InputStream inputStream) { - req.data(KeyVal.create(key, filename, inputStream)); - return this; - } - - public Connection data(Map data) { - Validate.notNull(data, "Data map must not be null"); - for (Map.Entry entry : data.entrySet()) { - req.data(KeyVal.create(entry.getKey(), entry.getValue())); - } - return this; - } - - public Connection data(String... keyvals) { - Validate.notNull(keyvals, "Data key value pairs must not be null"); - Validate.isTrue(keyvals.length %2 == 0, "Must supply an even number of key value pairs"); - for (int i = 0; i < keyvals.length; i += 2) { - String key = keyvals[i]; - String value = keyvals[i+1]; - Validate.notEmpty(key, "Data key must not be empty"); - Validate.notNull(value, "Data value must not be null"); - req.data(KeyVal.create(key, value)); - } - return this; - } - - public Connection data(Collection data) { - Validate.notNull(data, "Data collection must not be null"); - for (Connection.KeyVal entry: data) { - req.data(entry); - } - return this; - } - - public Connection header(String name, String value) { - req.header(name, value); - return this; - } - - public Connection cookie(String name, String value) { - req.cookie(name, value); - return this; - } - - public Connection cookies(Map cookies) { - Validate.notNull(cookies, "Cookie map must not be null"); - for (Map.Entry entry : cookies.entrySet()) { - req.cookie(entry.getKey(), entry.getValue()); - } - return this; - } - - public Connection parser(Parser parser) { - req.parser(parser); - return this; - } - - public Document get() throws IOException { - req.method(Method.GET); - execute(); - return res.parse(); - } - - public Document post() throws IOException { - req.method(Method.POST); - execute(); - return res.parse(); - } - - public Connection.Response execute() throws IOException { - res = Response.execute(req); - return res; - } - - public Connection.Request request() { - return req; - } - - public Connection request(Connection.Request request) { - req = request; - return this; - } - - public Connection.Response response() { - return res; - } - - public Connection response(Connection.Response response) { - res = response; - return this; - } - - @SuppressWarnings({"unchecked"}) - private static abstract class Base implements Connection.Base { - URL url; - Method method; - Map headers; - Map cookies; - - private Base() { - headers = new LinkedHashMap(); - cookies = new LinkedHashMap(); - } - - public URL url() { - return url; - } - - public T url(URL url) { - Validate.notNull(url, "URL must not be null"); - this.url = url; - return (T) this; - } - - public Method method() { - return method; - } - - public T method(Method method) { - Validate.notNull(method, "Method must not be null"); - this.method = method; - return (T) this; - } - - public String header(String name) { - Validate.notNull(name, "Header name must not be null"); - return getHeaderCaseInsensitive(name); - } - - public T header(String name, String value) { - Validate.notEmpty(name, "Header name must not be empty"); - Validate.notNull(value, "Header value must not be null"); - removeHeader(name); // ensures we don't get an "accept-encoding" and a "Accept-Encoding" - headers.put(name, value); - return (T) this; - } - - public boolean hasHeader(String name) { - Validate.notEmpty(name, "Header name must not be empty"); - return getHeaderCaseInsensitive(name) != null; - } - - /** - * Test if the request has a header with this value (case insensitive). - */ - public boolean hasHeaderWithValue(String name, String value) { - return hasHeader(name) && header(name).equalsIgnoreCase(value); - } - - public T removeHeader(String name) { - Validate.notEmpty(name, "Header name must not be empty"); - Map.Entry entry = scanHeaders(name); // remove is case insensitive too - if (entry != null) - headers.remove(entry.getKey()); // ensures correct case - return (T) this; - } - - public Map headers() { - return headers; - } - - private String getHeaderCaseInsensitive(String name) { - Validate.notNull(name, "Header name must not be null"); - // quick evals for common case of title case, lower case, then scan for mixed - String value = headers.get(name); - if (value == null) - value = headers.get(name.toLowerCase()); - if (value == null) { - Map.Entry entry = scanHeaders(name); - if (entry != null) - value = entry.getValue(); - } - return value; - } - - private Map.Entry scanHeaders(String name) { - String lc = name.toLowerCase(); - for (Map.Entry entry : headers.entrySet()) { - if (entry.getKey().toLowerCase().equals(lc)) - return entry; - } - return null; - } - - public String cookie(String name) { - Validate.notEmpty(name, "Cookie name must not be empty"); - return cookies.get(name); - } - - public T cookie(String name, String value) { - Validate.notEmpty(name, "Cookie name must not be empty"); - Validate.notNull(value, "Cookie value must not be null"); - cookies.put(name, value); - return (T) this; - } - - public boolean hasCookie(String name) { - Validate.notEmpty(name, "Cookie name must not be empty"); - return cookies.containsKey(name); - } - - public T removeCookie(String name) { - Validate.notEmpty(name, "Cookie name must not be empty"); - cookies.remove(name); - return (T) this; - } - - public Map cookies() { - return cookies; - } - } - - public static class Request extends HttpConnection.Base implements Connection.Request { - private int timeoutMilliseconds; - private int maxBodySizeBytes; - private boolean followRedirects; - private Collection data; - private boolean ignoreHttpErrors = false; - private boolean ignoreContentType = false; - private Parser parser; - - private Request() { - timeoutMilliseconds = 3000; - maxBodySizeBytes = 1024 * 1024; // 1MB - followRedirects = true; - data = new ArrayList(); - method = Method.GET; - headers.put("Accept-Encoding", "gzip"); - parser = Parser.htmlParser(); - } - - public int timeout() { - return timeoutMilliseconds; - } - - public Request timeout(int millis) { - Validate.isTrue(millis >= 0, "Timeout milliseconds must be 0 (infinite) or greater"); - timeoutMilliseconds = millis; - return this; - } - - public int maxBodySize() { - return maxBodySizeBytes; - } - - public Connection.Request maxBodySize(int bytes) { - Validate.isTrue(bytes >= 0, "maxSize must be 0 (unlimited) or larger"); - maxBodySizeBytes = bytes; - return this; - } - - public boolean followRedirects() { - return followRedirects; - } - - public Connection.Request followRedirects(boolean followRedirects) { - this.followRedirects = followRedirects; - return this; - } - - public boolean ignoreHttpErrors() { - return ignoreHttpErrors; - } - - public Connection.Request ignoreHttpErrors(boolean ignoreHttpErrors) { - this.ignoreHttpErrors = ignoreHttpErrors; - return this; - } - - public boolean ignoreContentType() { - return ignoreContentType; - } - - public Connection.Request ignoreContentType(boolean ignoreContentType) { - this.ignoreContentType = ignoreContentType; - return this; - } - - public Request data(Connection.KeyVal keyval) { - Validate.notNull(keyval, "Key val must not be null"); - data.add(keyval); - return this; - } - - public Collection data() { - return data; - } - - public Request parser(Parser parser) { - this.parser = parser; - return this; - } - - public Parser parser() { - return parser; - } - } - - public static class Response extends HttpConnection.Base implements Connection.Response { - private static final int MAX_REDIRECTS = 20; - private static final String LOCATION = "Location"; - private int statusCode; - private String statusMessage; - private ByteBuffer byteData; - private String charset; - private String contentType; - private boolean executed = false; - private int numRedirects = 0; - private Connection.Request req; - - /* - * For example {@code application/atom+xml;charset=utf-8}. - * Stepping through it: start with {@code "application/"}, follow with word - * characters up to a {@code "+xml"}, and then maybe more ({@code .*}). - */ - private static final Pattern xmlContentTypeRxp = Pattern.compile("application/\\w+\\+xml.*"); - - Response() { - super(); - } - - private Response(Response previousResponse) throws IOException { - super(); - if (previousResponse != null) { - numRedirects = previousResponse.numRedirects + 1; - if (numRedirects >= MAX_REDIRECTS) - throw new IOException(String.format("Too many redirects occurred trying to load URL %s", previousResponse.url())); - } - } - - static Response execute(Connection.Request req) throws IOException { - return execute(req, null); - } - - static Response execute(Connection.Request req, Response previousResponse) throws IOException { - Validate.notNull(req, "Request must not be null"); - String protocol = req.url().getProtocol(); - if (!protocol.equals("http") && !protocol.equals("https")) - throw new MalformedURLException("Only http & https protocols supported"); - - // set up the request for execution - String mimeBoundary = null; - if (!req.method().hasBody() && req.data().size() > 0) { - serialiseRequestUrl(req); // appends query string - } else if (req.method().hasBody()) { - mimeBoundary = setOutputContentType(req); - } - HttpURLConnection conn = createConnection(req); - Response res; - try { - conn.connect(); - if (conn.getDoOutput()) - writePost(req, conn.getOutputStream(), mimeBoundary); - - int status = conn.getResponseCode(); - res = new Response(previousResponse); - res.setupFromConnection(conn, previousResponse); - res.req = req; - - // redirect if there's a location header (from 3xx, or 201 etc) - if (res.hasHeader(LOCATION) && req.followRedirects()) { - req.method(Method.GET); // always redirect with a get. any data param from original req are dropped. - req.data().clear(); - - String location = res.header(LOCATION); - if (location != null && location.startsWith("http:/") && location.charAt(6) != '/') // fix broken Location: http:/temp/AAG_New/en/index.php - location = location.substring(6); - req.url(new URL(req.url(), encodeUrl(location))); - - for (Map.Entry cookie : res.cookies.entrySet()) { // add response cookies to request (for e.g. login posts) - req.cookie(cookie.getKey(), cookie.getValue()); - } - return execute(req, res); - } - if ((status < 200 || status >= 400) && !req.ignoreHttpErrors()) - throw new HttpStatusException("HTTP error fetching URL", status, req.url().toString()); - - // check that we can handle the returned content type; if not, abort before fetching it - String contentType = res.contentType(); - if (contentType != null - && !req.ignoreContentType() - && !contentType.startsWith("text/") - && !contentType.startsWith("application/xml") - && !xmlContentTypeRxp.matcher(contentType).matches() - ) - throw new UnsupportedMimeTypeException("Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml", - contentType, req.url().toString()); - - InputStream bodyStream = null; - InputStream dataStream = null; - try { - dataStream = conn.getErrorStream() != null ? conn.getErrorStream() : conn.getInputStream(); - bodyStream = res.hasHeaderWithValue(CONTENT_ENCODING, "gzip") ? - new BufferedInputStream(new GZIPInputStream(dataStream)) : - new BufferedInputStream(dataStream); - - res.byteData = DataUtil.readToByteBuffer(bodyStream, req.maxBodySize()); - res.charset = DataUtil.getCharsetFromContentType(res.contentType); // may be null, readInputStream deals with it - } finally { - if (bodyStream != null) bodyStream.close(); - if (dataStream != null) dataStream.close(); - } - } finally { - // per Java's documentation, this is not necessary, and precludes keepalives. However in practise, - // connection errors will not be released quickly enough and can cause a too many open files error. - conn.disconnect(); - } - - res.executed = true; - return res; - } - - public int statusCode() { - return statusCode; - } - - public String statusMessage() { - return statusMessage; - } - - public String charset() { - return charset; - } - - public String contentType() { - return contentType; - } - - public Document parse() throws IOException { - Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before parsing response"); - Document doc = DataUtil.parseByteData(byteData, charset, url.toExternalForm(), req.parser()); - byteData.rewind(); - charset = doc.outputSettings().charset().name(); // update charset from meta-equiv, possibly - return doc; - } - - public String body() { - Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body"); - // charset gets set from header on execute, and from meta-equiv on parse. parse may not have happened yet - String body; - if (charset == null) - body = Charset.forName(DataUtil.defaultCharset).decode(byteData).toString(); - else - body = Charset.forName(charset).decode(byteData).toString(); - byteData.rewind(); - return body; - } - - public byte[] bodyAsBytes() { - Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body"); - return byteData.array(); - } - - // set up connection defaults, and details from request - private static HttpURLConnection createConnection(Connection.Request req) throws IOException { - HttpURLConnection conn = (HttpURLConnection) req.url().openConnection(); - conn.setRequestMethod(req.method().name()); - conn.setInstanceFollowRedirects(false); // don't rely on native redirection support - conn.setConnectTimeout(req.timeout()); - conn.setReadTimeout(req.timeout()); - if (req.method().hasBody()) - conn.setDoOutput(true); - if (req.cookies().size() > 0) - conn.addRequestProperty("Cookie", getRequestCookieString(req)); - for (Map.Entry header : req.headers().entrySet()) { - conn.addRequestProperty(header.getKey(), header.getValue()); - } - return conn; - } - - // set up url, method, header, cookies - private void setupFromConnection(HttpURLConnection conn, Connection.Response previousResponse) throws IOException { - method = Method.valueOf(conn.getRequestMethod()); - url = conn.getURL(); - statusCode = conn.getResponseCode(); - statusMessage = conn.getResponseMessage(); - contentType = conn.getContentType(); - - Map> resHeaders = conn.getHeaderFields(); - processResponseHeaders(resHeaders); - - // if from a redirect, map previous response cookies into this response - if (previousResponse != null) { - for (Map.Entry prevCookie : previousResponse.cookies().entrySet()) { - if (!hasCookie(prevCookie.getKey())) - cookie(prevCookie.getKey(), prevCookie.getValue()); - } - } - } - - void processResponseHeaders(Map> resHeaders) { - for (Map.Entry> entry : resHeaders.entrySet()) { - String name = entry.getKey(); - if (name == null) - continue; // http/1.1 line - - List values = entry.getValue(); - if (name.equalsIgnoreCase("Set-Cookie")) { - for (String value : values) { - if (value == null) - continue; - TokenQueue cd = new TokenQueue(value); - String cookieName = cd.chompTo("=").trim(); - String cookieVal = cd.consumeTo(";").trim(); - if (cookieVal == null) - cookieVal = ""; - // ignores path, date, domain, secure et al. req'd? - // name not blank, value not null - if (cookieName != null && cookieName.length() > 0) - cookie(cookieName, cookieVal); - } - } else { // only take the first instance of each header - if (!values.isEmpty()) - header(name, values.get(0)); - } - } - } - - private static String setOutputContentType(final Connection.Request req) { - // multipart mode, for files. add the header if we see something with an inputstream, and return a non-null boundary - boolean needsMulti = false; - for (Connection.KeyVal keyVal : req.data()) { - if (keyVal.hasInputStream()) { - needsMulti = true; - break; - } - } - String bound = null; - if (needsMulti) { - bound = DataUtil.mimeBoundary(); - req.header(CONTENT_TYPE, MULTIPART_FORM_DATA + "; boundary=" + bound); - } else { - req.header(CONTENT_TYPE, FORM_URL_ENCODED); - } - return bound; - } - - private static void writePost(final Connection.Request req, final OutputStream outputStream, final String bound) throws IOException { - final Collection data = req.data(); - final BufferedWriter w = new BufferedWriter(new OutputStreamWriter(outputStream, DataUtil.defaultCharset)); - - if (bound != null) { - // boundary will be set if we're in multipart mode - for (Connection.KeyVal keyVal : data) { - w.write("--"); - w.write(bound); - w.write("\r\n"); - w.write("Content-Disposition: form-data; name=\""); - w.write(encodeMimeName(keyVal.key())); // encodes " to %22 - w.write("\""); - if (keyVal.hasInputStream()) { - w.write("; filename=\""); - w.write(encodeMimeName(keyVal.value())); - w.write("\"\r\nContent-Type: application/octet-stream\r\n\r\n"); - w.flush(); // flush - DataUtil.crossStreams(keyVal.inputStream(), outputStream); - outputStream.flush(); - } else { - w.write("\r\n\r\n"); - w.write(keyVal.value()); - } - w.write("\r\n"); - } - w.write("--"); - w.write(bound); - w.write("--"); - } else { - // regular form data (application/x-www-form-urlencoded) - boolean first = true; - for (Connection.KeyVal keyVal : data) { - if (!first) - w.append('&'); - else - first = false; - - w.write(URLEncoder.encode(keyVal.key(), DataUtil.defaultCharset)); - w.write('='); - w.write(URLEncoder.encode(keyVal.value(), DataUtil.defaultCharset)); - } - } - w.close(); - } - - private static String getRequestCookieString(Connection.Request req) { - StringBuilder sb = new StringBuilder(); - boolean first = true; - for (Map.Entry cookie : req.cookies().entrySet()) { - if (!first) - sb.append("; "); - else - first = false; - sb.append(cookie.getKey()).append('=').append(cookie.getValue()); - // todo: spec says only ascii, no escaping / encoding defined. validate on set? or escape somehow here? - } - return sb.toString(); - } - - // for get url reqs, serialise the data map into the url - private static void serialiseRequestUrl(Connection.Request req) throws IOException { - URL in = req.url(); - StringBuilder url = new StringBuilder(); - boolean first = true; - // reconstitute the query, ready for appends - url - .append(in.getProtocol()) - .append("://") - .append(in.getAuthority()) // includes host, port - .append(in.getPath()) - .append("?"); - if (in.getQuery() != null) { - url.append(in.getQuery()); - first = false; - } - for (Connection.KeyVal keyVal : req.data()) { - if (!first) - url.append('&'); - else - first = false; - url - .append(URLEncoder.encode(keyVal.key(), DataUtil.defaultCharset)) - .append('=') - .append(URLEncoder.encode(keyVal.value(), DataUtil.defaultCharset)); - } - req.url(new URL(url.toString())); - req.data().clear(); // moved into url as get params - } - } - - public static class KeyVal implements Connection.KeyVal { - private String key; - private String value; - private InputStream stream; - - public static KeyVal create(String key, String value) { - return new KeyVal().key(key).value(value); - } - - public static KeyVal create(String key, String filename, InputStream stream) { - return new KeyVal().key(key).value(filename).inputStream(stream); - } - - private KeyVal() {} - - public KeyVal key(String key) { - Validate.notEmpty(key, "Data key must not be empty"); - this.key = key; - return this; - } - - public String key() { - return key; - } - - public KeyVal value(String value) { - Validate.notNull(value, "Data value must not be null"); - this.value = value; - return this; - } - - public String value() { - return value; - } - - public KeyVal inputStream(InputStream inputStream) { - Validate.notNull(value, "Data input stream must not be null"); - this.stream = inputStream; - return this; - } - - public InputStream inputStream() { - return stream; - } - - public boolean hasInputStream() { - return stream != null; - } - - @Override - public String toString() { - return key + "=" + value; - } - } -} diff --git a/src/org/jsoup/helper/StringUtil.java b/src/org/jsoup/helper/StringUtil.java deleted file mode 100644 index 779f40c..0000000 --- a/src/org/jsoup/helper/StringUtil.java +++ /dev/null @@ -1,153 +0,0 @@ -package org.jsoup.helper; - -import java.util.Collection; -import java.util.Iterator; - -/** - * A minimal String utility class. Designed for internal jsoup use only. - */ -public final class StringUtil { - // memoised padding up to 10 - private static final String[] padding = {"", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; - - /** - * Join a collection of strings by a seperator - * @param strings collection of string objects - * @param sep string to place between strings - * @return joined string - */ - public static String join(Collection strings, String sep) { - return join(strings.iterator(), sep); - } - - /** - * Join a collection of strings by a seperator - * @param strings iterator of string objects - * @param sep string to place between strings - * @return joined string - */ - public static String join(Iterator strings, String sep) { - if (!strings.hasNext()) - return ""; - - String start = strings.next().toString(); - if (!strings.hasNext()) // only one, avoid builder - return start; - - StringBuilder sb = new StringBuilder(64).append(start); - while (strings.hasNext()) { - sb.append(sep); - sb.append(strings.next()); - } - return sb.toString(); - } - - /** - * Returns space padding - * @param width amount of padding desired - * @return string of spaces * width - */ - public static String padding(int width) { - if (width < 0) - throw new IllegalArgumentException("width must be > 0"); - - if (width < padding.length) - return padding[width]; - - char[] out = new char[width]; - for (int i = 0; i < width; i++) - out[i] = ' '; - return String.valueOf(out); - } - - /** - * Tests if a string is blank: null, emtpy, or only whitespace (" ", \r\n, \t, etc) - * @param string string to test - * @return if string is blank - */ - public static boolean isBlank(String string) { - if (string == null || string.length() == 0) - return true; - - int l = string.length(); - for (int i = 0; i < l; i++) { - if (!StringUtil.isWhitespace(string.codePointAt(i))) - return false; - } - return true; - } - - /** - * Tests if a string is numeric, i.e. contains only digit characters - * @param string string to test - * @return true if only digit chars, false if empty or null or contains non-digit chrs - */ - public static boolean isNumeric(String string) { - if (string == null || string.length() == 0) - return false; - - int l = string.length(); - for (int i = 0; i < l; i++) { - if (!Character.isDigit(string.codePointAt(i))) - return false; - } - return true; - } - - /** - * Tests if a code point is "whitespace" as defined in the HTML spec. - * @param c code point to test - * @return true if code point is whitespace, false otherwise - */ - public static boolean isWhitespace(int c){ - return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'; - } - - /** - * Normalise the whitespace within this string; multiple spaces collapse to a single, and all whitespace characters - * (e.g. newline, tab) convert to a simple space - * @param string content to normalise - * @return normalised string - */ - public static String normaliseWhitespace(String string) { - StringBuilder sb = new StringBuilder(string.length()); - appendNormalisedWhitespace(sb, string, false); - return sb.toString(); - } - - /** - * After normalizing the whitespace within a string, appends it to a string builder. - * @param accum builder to append to - * @param string string to normalize whitespace within - * @param stripLeading set to true if you wish to remove any leading whitespace - */ - public static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading) { - boolean lastWasWhite = false; - boolean reachedNonWhite = false; - - int len = string.length(); - int c; - for (int i = 0; i < len; i+= Character.charCount(c)) { - c = string.codePointAt(i); - if (isWhitespace(c)) { - if ((stripLeading && !reachedNonWhite) || lastWasWhite) - continue; - accum.append(' '); - lastWasWhite = true; - } - else { - accum.appendCodePoint(c); - lastWasWhite = false; - reachedNonWhite = true; - } - } - } - - public static boolean in(String needle, String... haystack) { - for (String hay : haystack) { - if (hay.equals(needle)) - return true; - } - return false; - } -} diff --git a/src/org/jsoup/helper/Validate.java b/src/org/jsoup/helper/Validate.java deleted file mode 100644 index 814bcc3..0000000 --- a/src/org/jsoup/helper/Validate.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.jsoup.helper; - -/** - * Simple validation methods. Designed for jsoup internal use - */ -public final class Validate { - - private Validate() {} - - /** - * Validates that the object is not null - * @param obj object to test - */ - public static void notNull(Object obj) { - if (obj == null) - throw new IllegalArgumentException("Object must not be null"); - } - - /** - * Validates that the object is not null - * @param obj object to test - * @param msg message to output if validation fails - */ - public static void notNull(Object obj, String msg) { - if (obj == null) - throw new IllegalArgumentException(msg); - } - - /** - * Validates that the value is true - * @param val object to test - */ - public static void isTrue(boolean val) { - if (!val) - throw new IllegalArgumentException("Must be true"); - } - - /** - * Validates that the value is true - * @param val object to test - * @param msg message to output if validation fails - */ - public static void isTrue(boolean val, String msg) { - if (!val) - throw new IllegalArgumentException(msg); - } - - /** - * Validates that the value is false - * @param val object to test - */ - public static void isFalse(boolean val) { - if (val) - throw new IllegalArgumentException("Must be false"); - } - - /** - * Validates that the value is false - * @param val object to test - * @param msg message to output if validation fails - */ - public static void isFalse(boolean val, String msg) { - if (val) - throw new IllegalArgumentException(msg); - } - - /** - * Validates that the array contains no null elements - * @param objects the array to test - */ - public static void noNullElements(Object[] objects) { - noNullElements(objects, "Array must not contain any null objects"); - } - - /** - * Validates that the array contains no null elements - * @param objects the array to test - * @param msg message to output if validation fails - */ - public static void noNullElements(Object[] objects, String msg) { - for (Object obj : objects) - if (obj == null) - throw new IllegalArgumentException(msg); - } - - /** - * Validates that the string is not empty - * @param string the string to test - */ - public static void notEmpty(String string) { - if (string == null || string.length() == 0) - throw new IllegalArgumentException("String must not be empty"); - } - - /** - * Validates that the string is not empty - * @param string the string to test - * @param msg message to output if validation fails - */ - public static void notEmpty(String string, String msg) { - if (string == null || string.length() == 0) - throw new IllegalArgumentException(msg); - } - - /** - Cause a failure. - @param msg message to output. - */ - public static void fail(String msg) { - throw new IllegalArgumentException(msg); - } -} diff --git a/src/org/jsoup/helper/W3CDom.java b/src/org/jsoup/helper/W3CDom.java deleted file mode 100644 index 989f9c7..0000000 --- a/src/org/jsoup/helper/W3CDom.java +++ /dev/null @@ -1,134 +0,0 @@ -package org.jsoup.helper; - -import org.jsoup.nodes.Attribute; -import org.jsoup.select.NodeTraversor; -import org.jsoup.select.NodeVisitor; -import org.w3c.dom.Comment; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Text; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import java.io.StringWriter; -import javax.xml.transform.TransformerException; - -/** - * Helper class to transform a {@link org.jsoup.nodes.Document} to a {@link org.w3c.dom.Document org.w3c.dom.Document}, - * for integration with toolsets that use the W3C DOM. - *

- * This class is currently experimental, please provide feedback on utility and any problems experienced. - */ -public class W3CDom { - protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - - /** - * Convert a jsoup Document to a W3C Document. - * @param in jsoup doc - * @return w3c doc - */ - public Document fromJsoup(org.jsoup.nodes.Document in) { - Validate.notNull(in); - DocumentBuilder builder; - try { - builder = factory.newDocumentBuilder(); - Document out = builder.newDocument(); - convert(in, out); - return out; - } catch (ParserConfigurationException e) { - throw new IllegalStateException(e); - } - } - - /** - * Converts a jsoup document into the provided W3C Document. If required, you can set options on the output document - * before converting. - * @param in jsoup doc - * @param out w3c doc - * @see org.jsoup.helper.W3CDom#fromJsoup(org.jsoup.nodes.Document) - */ - public void convert(org.jsoup.nodes.Document in, Document out) { - if (!StringUtil.isBlank(in.location())) - out.setDocumentURI(in.location()); - - org.jsoup.nodes.Element rootEl = in.child(0); // skip the #root node - NodeTraversor traversor = new NodeTraversor(new W3CBuilder(out)); - traversor.traverse(rootEl); - } - - /** - * Implements the conversion by walking the input. - */ - protected class W3CBuilder implements NodeVisitor { - private final Document doc; - private Element dest; - - public W3CBuilder(Document doc) { - this.doc = doc; - } - - public void head(org.jsoup.nodes.Node source, int depth) { - if (source instanceof org.jsoup.nodes.Element) { - org.jsoup.nodes.Element sourceEl = (org.jsoup.nodes.Element) source; - Element el = doc.createElement(sourceEl.tagName()); - copyAttributes(sourceEl, el); - if (dest == null) { // sets up the root - doc.appendChild(el); - } else { - dest.appendChild(el); - } - dest = el; // descend - } else if (source instanceof org.jsoup.nodes.TextNode) { - org.jsoup.nodes.TextNode sourceText = (org.jsoup.nodes.TextNode) source; - Text text = doc.createTextNode(sourceText.getWholeText()); - dest.appendChild(text); - } else if (source instanceof org.jsoup.nodes.Comment) { - org.jsoup.nodes.Comment sourceComment = (org.jsoup.nodes.Comment) source; - Comment comment = doc.createComment(sourceComment.getData()); - dest.appendChild(comment); - } else if (source instanceof org.jsoup.nodes.DataNode) { - org.jsoup.nodes.DataNode sourceData = (org.jsoup.nodes.DataNode) source; - Text node = doc.createTextNode(sourceData.getWholeData()); - dest.appendChild(node); - } else { - // unhandled - } - } - - public void tail(org.jsoup.nodes.Node source, int depth) { - if (source instanceof org.jsoup.nodes.Element && dest.getParentNode() instanceof Element) { - dest = (Element) dest.getParentNode(); // undescend. cromulent. - } - } - - private void copyAttributes(org.jsoup.nodes.Node source, Element el) { - for (Attribute attribute : source.attributes()) { - el.setAttribute(attribute.getKey(), attribute.getValue()); - } - } - } - - /** - * Serialize a W3C document to a String. - * @param doc Document - * @return Document as string - */ - public String asString(Document doc) { - try { - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - return writer.toString(); - } catch (TransformerException e) { - throw new IllegalStateException(e); - } - } -} diff --git a/src/org/jsoup/nodes/Attribute.java b/src/org/jsoup/nodes/Attribute.java deleted file mode 100644 index 14cfaf4..0000000 --- a/src/org/jsoup/nodes/Attribute.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.Validate; - -import java.util.Arrays; -import java.util.Map; - -/** - A single key + value attribute. Keys are trimmed and normalised to lower-case. - - @author Jonathan Hedley, jonathan@hedley.net */ -public class Attribute implements Map.Entry, Cloneable { - private static final String[] booleanAttributes = { - "allowfullscreen", "async", "autofocus", "checked", "compact", "declare", "default", "defer", "disabled", - "formnovalidate", "hidden", "inert", "ismap", "itemscope", "multiple", "muted", "nohref", "noresize", - "noshade", "novalidate", "nowrap", "open", "readonly", "required", "reversed", "seamless", "selected", - "sortable", "truespeed", "typemustmatch" - }; - - private String key; - private String value; - - /** - * Create a new attribute from unencoded (raw) key and value. - * @param key attribute key - * @param value attribute value - * @see #createFromEncoded - */ - public Attribute(String key, String value) { - Validate.notEmpty(key); - Validate.notNull(value); - this.key = key.trim().toLowerCase(); - this.value = value; - } - - /** - Get the attribute key. - @return the attribute key - */ - public String getKey() { - return key; - } - - /** - Set the attribute key. Gets normalised as per the constructor method. - @param key the new key; must not be null - */ - public void setKey(String key) { - Validate.notEmpty(key); - this.key = key.trim().toLowerCase(); - } - - /** - Get the attribute value. - @return the attribute value - */ - public String getValue() { - return value; - } - - /** - Set the attribute value. - @param value the new attribute value; must not be null - */ - public String setValue(String value) { - Validate.notNull(value); - String old = this.value; - this.value = value; - return old; - } - - /** - Get the HTML representation of this attribute; e.g. {@code href="index.html"}. - @return HTML - */ - public String html() { - StringBuilder accum = new StringBuilder(); - html(accum, (new Document("")).outputSettings()); - return accum.toString(); - } - - protected void html(StringBuilder accum, Document.OutputSettings out) { - accum.append(key); - if (!shouldCollapseAttribute(out)) { - accum.append("=\""); - Entities.escape(accum, value, out, true, false, false); - accum.append('"'); - } - } - - /** - Get the string representation of this attribute, implemented as {@link #html()}. - @return string - */ - @Override - public String toString() { - return html(); - } - - /** - * Create a new Attribute from an unencoded key and a HTML attribute encoded value. - * @param unencodedKey assumes the key is not encoded, as can be only run of simple \w chars. - * @param encodedValue HTML attribute encoded value - * @return attribute - */ - public static Attribute createFromEncoded(String unencodedKey, String encodedValue) { - String value = Entities.unescape(encodedValue, true); - return new Attribute(unencodedKey, value); - } - - protected boolean isDataAttribute() { - return key.startsWith(Attributes.dataPrefix) && key.length() > Attributes.dataPrefix.length(); - } - - /** - * Collapsible if it's a boolean attribute and value is empty or same as name - */ - protected final boolean shouldCollapseAttribute(Document.OutputSettings out) { - return ("".equals(value) || value.equalsIgnoreCase(key)) - && out.syntax() == Document.OutputSettings.Syntax.html - && Arrays.binarySearch(booleanAttributes, key) >= 0; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Attribute)) return false; - - Attribute attribute = (Attribute) o; - - if (key != null ? !key.equals(attribute.key) : attribute.key != null) return false; - if (value != null ? !value.equals(attribute.value) : attribute.value != null) return false; - - return true; - } - - @Override - public int hashCode() { - int result = key != null ? key.hashCode() : 0; - result = 31 * result + (value != null ? value.hashCode() : 0); - return result; - } - - @Override - public Attribute clone() { - try { - return (Attribute) super.clone(); // only fields are immutable strings key and value, so no more deep copy required - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/org/jsoup/nodes/Attributes.java b/src/org/jsoup/nodes/Attributes.java deleted file mode 100644 index 1f6d8d5..0000000 --- a/src/org/jsoup/nodes/Attributes.java +++ /dev/null @@ -1,248 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.Validate; - -import java.util.*; - -/** - * The attributes of an Element. - *

- * Attributes are treated as a map: there can be only one value associated with an attribute key. - *

- * Attribute key and value comparisons are done case insensitively, and keys are normalised to - * lower-case. - * - * @author Jonathan Hedley, jonathan@hedley.net - */ -public class Attributes implements Iterable, Cloneable { - protected static final String dataPrefix = "data-"; - - private LinkedHashMap attributes = null; - // linked hash map to preserve insertion order. - // null be default as so many elements have no attributes -- saves a good chunk of memory - - /** - Get an attribute value by key. - @param key the attribute key - @return the attribute value if set; or empty string if not set. - @see #hasKey(String) - */ - public String get(String key) { - Validate.notEmpty(key); - - if (attributes == null) - return ""; - - Attribute attr = attributes.get(key.toLowerCase()); - return attr != null ? attr.getValue() : ""; - } - - /** - Set a new attribute, or replace an existing one by key. - @param key attribute key - @param value attribute value - */ - public void put(String key, String value) { - Attribute attr = new Attribute(key, value); - put(attr); - } - - /** - Set a new attribute, or replace an existing one by key. - @param attribute attribute - */ - public void put(Attribute attribute) { - Validate.notNull(attribute); - if (attributes == null) - attributes = new LinkedHashMap(2); - attributes.put(attribute.getKey(), attribute); - } - - /** - Remove an attribute by key. - @param key attribute key to remove - */ - public void remove(String key) { - Validate.notEmpty(key); - if (attributes == null) - return; - attributes.remove(key.toLowerCase()); - } - - /** - Tests if these attributes contain an attribute with this key. - @param key key to check for - @return true if key exists, false otherwise - */ - public boolean hasKey(String key) { - return attributes != null && attributes.containsKey(key.toLowerCase()); - } - - /** - Get the number of attributes in this set. - @return size - */ - public int size() { - if (attributes == null) - return 0; - return attributes.size(); - } - - /** - Add all the attributes from the incoming set to this set. - @param incoming attributes to add to these attributes. - */ - public void addAll(Attributes incoming) { - if (incoming.size() == 0) - return; - if (attributes == null) - attributes = new LinkedHashMap(incoming.size()); - attributes.putAll(incoming.attributes); - } - - public Iterator iterator() { - return asList().iterator(); - } - - /** - Get the attributes as a List, for iteration. Do not modify the keys of the attributes via this view, as changes - to keys will not be recognised in the containing set. - @return an view of the attributes as a List. - */ - public List asList() { - if (attributes == null) - return Collections.emptyList(); - - List list = new ArrayList(attributes.size()); - for (Map.Entry entry : attributes.entrySet()) { - list.add(entry.getValue()); - } - return Collections.unmodifiableList(list); - } - - /** - * Retrieves a filtered view of attributes that are HTML5 custom data attributes; that is, attributes with keys - * starting with {@code data-}. - * @return map of custom data attributes. - */ - public Map dataset() { - return new Dataset(); - } - - /** - Get the HTML representation of these attributes. - @return HTML - */ - public String html() { - StringBuilder accum = new StringBuilder(); - html(accum, (new Document("")).outputSettings()); // output settings a bit funky, but this html() seldom used - return accum.toString(); - } - - void html(StringBuilder accum, Document.OutputSettings out) { - if (attributes == null) - return; - - for (Map.Entry entry : attributes.entrySet()) { - Attribute attribute = entry.getValue(); - accum.append(" "); - attribute.html(accum, out); - } - } - - @Override - public String toString() { - return html(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Attributes)) return false; - - Attributes that = (Attributes) o; - - return !(attributes != null ? !attributes.equals(that.attributes) : that.attributes != null); - } - - @Override - public int hashCode() { - return attributes != null ? attributes.hashCode() : 0; - } - - @Override - public Attributes clone() { - if (attributes == null) - return new Attributes(); - - Attributes clone; - try { - clone = (Attributes) super.clone(); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - clone.attributes = new LinkedHashMap(attributes.size()); - for (Attribute attribute: this) - clone.attributes.put(attribute.getKey(), attribute.clone()); - return clone; - } - - private class Dataset extends AbstractMap { - - private Dataset() { - if (attributes == null) - attributes = new LinkedHashMap(2); - } - - public Set> entrySet() { - return new EntrySet(); - } - - @Override - public String put(String key, String value) { - String dataKey = dataKey(key); - String oldValue = hasKey(dataKey) ? attributes.get(dataKey).getValue() : null; - Attribute attr = new Attribute(dataKey, value); - attributes.put(dataKey, attr); - return oldValue; - } - - private class EntrySet extends AbstractSet> { - public Iterator> iterator() { - return new DatasetIterator(); - } - - public int size() { - int count = 0; - Iterator iter = new DatasetIterator(); - while (iter.hasNext()) - count++; - return count; - } - } - - private class DatasetIterator implements Iterator> { - private Iterator attrIter = attributes.values().iterator(); - private Attribute attr; - public boolean hasNext() { - while (attrIter.hasNext()) { - attr = attrIter.next(); - if (attr.isDataAttribute()) return true; - } - return false; - } - - public Entry next() { - return new Attribute(attr.getKey().substring(dataPrefix.length()), attr.getValue()); - } - - public void remove() { - attributes.remove(attr.getKey()); - } - } - } - - private static String dataKey(String key) { - return dataPrefix + key; - } -} diff --git a/src/org/jsoup/nodes/Comment.java b/src/org/jsoup/nodes/Comment.java deleted file mode 100644 index 2962dd5..0000000 --- a/src/org/jsoup/nodes/Comment.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.jsoup.nodes; - -/** - A comment node. - - @author Jonathan Hedley, jonathan@hedley.net */ -public class Comment extends Node { - private static final String COMMENT_KEY = "comment"; - - /** - Create a new comment node. - @param data The contents of the comment - @param baseUri base URI - */ - public Comment(String data, String baseUri) { - super(baseUri); - attributes.put(COMMENT_KEY, data); - } - - public String nodeName() { - return "#comment"; - } - - /** - Get the contents of the comment. - @return comment content - */ - public String getData() { - return attributes.get(COMMENT_KEY); - } - - void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) { - if (out.prettyPrint()) - indent(accum, depth, out); - accum - .append(""); - } - - void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {} - - @Override - public String toString() { - return outerHtml(); - } -} diff --git a/src/org/jsoup/nodes/DataNode.java b/src/org/jsoup/nodes/DataNode.java deleted file mode 100644 index 4046959..0000000 --- a/src/org/jsoup/nodes/DataNode.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.jsoup.nodes; - -/** - A data node, for contents of style, script tags etc, where contents should not show in text(). - - @author Jonathan Hedley, jonathan@hedley.net */ -public class DataNode extends Node{ - private static final String DATA_KEY = "data"; - - /** - Create a new DataNode. - @param data data contents - @param baseUri base URI - */ - public DataNode(String data, String baseUri) { - super(baseUri); - attributes.put(DATA_KEY, data); - } - - public String nodeName() { - return "#data"; - } - - /** - Get the data contents of this node. Will be unescaped and with original new lines, space etc. - @return data - */ - public String getWholeData() { - return attributes.get(DATA_KEY); - } - - /** - * Set the data contents of this node. - * @param data unencoded data - * @return this node, for chaining - */ - public DataNode setWholeData(String data) { - attributes.put(DATA_KEY, data); - return this; - } - - void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) { - accum.append(getWholeData()); // data is not escaped in return from data nodes, so " in script, style is plain - } - - void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {} - - @Override - public String toString() { - return outerHtml(); - } - - /** - Create a new DataNode from HTML encoded data. - @param encodedData encoded data - @param baseUri bass URI - @return new DataNode - */ - public static DataNode createFromEncoded(String encodedData, String baseUri) { - String data = Entities.unescape(encodedData); - return new DataNode(data, baseUri); - } -} diff --git a/src/org/jsoup/nodes/Document.java b/src/org/jsoup/nodes/Document.java deleted file mode 100644 index 571a512..0000000 --- a/src/org/jsoup/nodes/Document.java +++ /dev/null @@ -1,425 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.StringUtil; -import org.jsoup.helper.Validate; -import org.jsoup.parser.Tag; -import org.jsoup.select.Elements; - -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; -import java.util.ArrayList; -import java.util.List; - -/** - A HTML Document. - - @author Jonathan Hedley, jonathan@hedley.net */ -public class Document extends Element { - private OutputSettings outputSettings = new OutputSettings(); - private QuirksMode quirksMode = QuirksMode.noQuirks; - private String location; - - /** - Create a new, empty Document. - @param baseUri base URI of document - @see org.jsoup.Jsoup#parse - @see #createShell - */ - public Document(String baseUri) { - super(Tag.valueOf("#root"), baseUri); - this.location = baseUri; - } - - /** - Create a valid, empty shell of a document, suitable for adding more elements to. - @param baseUri baseUri of document - @return document with html, head, and body elements. - */ - static public Document createShell(String baseUri) { - Validate.notNull(baseUri); - - Document doc = new Document(baseUri); - Element html = doc.appendElement("html"); - html.appendElement("head"); - html.appendElement("body"); - - return doc; - } - - /** - * Get the URL this Document was parsed from. If the starting URL is a redirect, - * this will return the final URL from which the document was served from. - * @return location - */ - public String location() { - return location; - } - - /** - Accessor to the document's {@code head} element. - @return {@code head} - */ - public Element head() { - return findFirstElementByTagName("head", this); - } - - /** - Accessor to the document's {@code body} element. - @return {@code body} - */ - public Element body() { - return findFirstElementByTagName("body", this); - } - - /** - Get the string contents of the document's {@code title} element. - @return Trimmed title, or empty string if none set. - */ - public String title() { - // title is a preserve whitespace tag (for document output), but normalised here - Element titleEl = getElementsByTag("title").first(); - return titleEl != null ? StringUtil.normaliseWhitespace(titleEl.text()).trim() : ""; - } - - /** - Set the document's {@code title} element. Updates the existing element, or adds {@code title} to {@code head} if - not present - @param title string to set as title - */ - public void title(String title) { - Validate.notNull(title); - Element titleEl = getElementsByTag("title").first(); - if (titleEl == null) { // add to head - head().appendElement("title").text(title); - } else { - titleEl.text(title); - } - } - - /** - Create a new Element, with this document's base uri. Does not make the new element a child of this document. - @param tagName element tag name (e.g. {@code a}) - @return new element - */ - public Element createElement(String tagName) { - return new Element(Tag.valueOf(tagName), this.baseUri()); - } - - /** - Normalise the document. This happens after the parse phase so generally does not need to be called. - Moves any text content that is not in the body element into the body. - @return this document after normalisation - */ - public Document normalise() { - Element htmlEl = findFirstElementByTagName("html", this); - if (htmlEl == null) - htmlEl = appendElement("html"); - if (head() == null) - htmlEl.prependElement("head"); - if (body() == null) - htmlEl.appendElement("body"); - - // pull text nodes out of root, html, and head els, and push into body. non-text nodes are already taken care - // of. do in inverse order to maintain text order. - normaliseTextNodes(head()); - normaliseTextNodes(htmlEl); - normaliseTextNodes(this); - - normaliseStructure("head", htmlEl); - normaliseStructure("body", htmlEl); - - return this; - } - - // does not recurse. - private void normaliseTextNodes(Element element) { - List toMove = new ArrayList(); - for (Node node: element.childNodes) { - if (node instanceof TextNode) { - TextNode tn = (TextNode) node; - if (!tn.isBlank()) - toMove.add(tn); - } - } - - for (int i = toMove.size()-1; i >= 0; i--) { - Node node = toMove.get(i); - element.removeChild(node); - body().prependChild(new TextNode(" ", "")); - body().prependChild(node); - } - } - - // merge multiple or contents into one, delete the remainder, and ensure they are owned by - private void normaliseStructure(String tag, Element htmlEl) { - Elements elements = this.getElementsByTag(tag); - Element master = elements.first(); // will always be available as created above if not existent - if (elements.size() > 1) { // dupes, move contents to master - List toMove = new ArrayList(); - for (int i = 1; i < elements.size(); i++) { - Node dupe = elements.get(i); - for (Node node : dupe.childNodes) - toMove.add(node); - dupe.remove(); - } - - for (Node dupe : toMove) - master.appendChild(dupe); - } - // ensure parented by - if (!master.parent().equals(htmlEl)) { - htmlEl.appendChild(master); // includes remove() - } - } - - // fast method to get first by tag name, used for html, head, body finders - private Element findFirstElementByTagName(String tag, Node node) { - if (node.nodeName().equals(tag)) - return (Element) node; - else { - for (Node child: node.childNodes) { - Element found = findFirstElementByTagName(tag, child); - if (found != null) - return found; - } - } - return null; - } - - @Override - public String outerHtml() { - return super.html(); // no outer wrapper tag - } - - /** - Set the text of the {@code body} of this document. Any existing nodes within the body will be cleared. - @param text unencoded text - @return this document - */ - @Override - public Element text(String text) { - body().text(text); // overridden to not nuke doc structure - return this; - } - - @Override - public String nodeName() { - return "#document"; - } - - @Override - public Document clone() { - Document clone = (Document) super.clone(); - clone.outputSettings = this.outputSettings.clone(); - return clone; - } - - /** - * A Document's output settings control the form of the text() and html() methods. - */ - public static class OutputSettings implements Cloneable { - /** - * The output serialization syntax. - */ - public enum Syntax {html, xml} - - private Entities.EscapeMode escapeMode = Entities.EscapeMode.base; - private Charset charset = Charset.forName("UTF-8"); - private CharsetEncoder charsetEncoder = charset.newEncoder(); - private boolean prettyPrint = true; - private boolean outline = false; - private int indentAmount = 1; - private Syntax syntax = Syntax.html; - - public OutputSettings() {} - - /** - * Get the document's current HTML escape mode: base, which provides a limited set of named HTML - * entities and escapes other characters as numbered entities for maximum compatibility; or extended, - * which uses the complete set of HTML named entities. - *

- * The default escape mode is base. - * @return the document's current escape mode - */ - public Entities.EscapeMode escapeMode() { - return escapeMode; - } - - /** - * Set the document's escape mode, which determines how characters are escaped when the output character set - * does not support a given character:- using either a named or a numbered escape. - * @param escapeMode the new escape mode to use - * @return the document's output settings, for chaining - */ - public OutputSettings escapeMode(Entities.EscapeMode escapeMode) { - this.escapeMode = escapeMode; - return this; - } - - /** - * Get the document's current output charset, which is used to control which characters are escaped when - * generating HTML (via the html() methods), and which are kept intact. - *

- * Where possible (when parsing from a URL or File), the document's output charset is automatically set to the - * input charset. Otherwise, it defaults to UTF-8. - * @return the document's current charset. - */ - public Charset charset() { - return charset; - } - - /** - * Update the document's output charset. - * @param charset the new charset to use. - * @return the document's output settings, for chaining - */ - public OutputSettings charset(Charset charset) { - // todo: this should probably update the doc's meta charset - this.charset = charset; - charsetEncoder = charset.newEncoder(); - return this; - } - - /** - * Update the document's output charset. - * @param charset the new charset (by name) to use. - * @return the document's output settings, for chaining - */ - public OutputSettings charset(String charset) { - charset(Charset.forName(charset)); - return this; - } - - CharsetEncoder encoder() { - return charsetEncoder; - } - - /** - * Get the document's current output syntax. - * @return current syntax - */ - public Syntax syntax() { - return syntax; - } - - /** - * Set the document's output syntax. Either {@code html}, with empty tags and boolean attributes (etc), or - * {@code xml}, with self-closing tags. - * @param syntax serialization syntax - * @return the document's output settings, for chaining - */ - public OutputSettings syntax(Syntax syntax) { - this.syntax = syntax; - return this; - } - - /** - * Get if pretty printing is enabled. Default is true. If disabled, the HTML output methods will not re-format - * the output, and the output will generally look like the input. - * @return if pretty printing is enabled. - */ - public boolean prettyPrint() { - return prettyPrint; - } - - /** - * Enable or disable pretty printing. - * @param pretty new pretty print setting - * @return this, for chaining - */ - public OutputSettings prettyPrint(boolean pretty) { - prettyPrint = pretty; - return this; - } - - /** - * Get if outline mode is enabled. Default is false. If enabled, the HTML output methods will consider - * all tags as block. - * @return if outline mode is enabled. - */ - public boolean outline() { - return outline; - } - - /** - * Enable or disable HTML outline mode. - * @param outlineMode new outline setting - * @return this, for chaining - */ - public OutputSettings outline(boolean outlineMode) { - outline = outlineMode; - return this; - } - - /** - * Get the current tag indent amount, used when pretty printing. - * @return the current indent amount - */ - public int indentAmount() { - return indentAmount; - } - - /** - * Set the indent amount for pretty printing - * @param indentAmount number of spaces to use for indenting each level. Must be >= 0. - * @return this, for chaining - */ - public OutputSettings indentAmount(int indentAmount) { - Validate.isTrue(indentAmount >= 0); - this.indentAmount = indentAmount; - return this; - } - - @Override - public OutputSettings clone() { - OutputSettings clone; - try { - clone = (OutputSettings) super.clone(); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - clone.charset(charset.name()); // new charset and charset encoder - clone.escapeMode = Entities.EscapeMode.valueOf(escapeMode.name()); - // indentAmount, prettyPrint are primitives so object.clone() will handle - return clone; - } - } - - /** - * Get the document's current output settings. - * @return the document's current output settings. - */ - public OutputSettings outputSettings() { - return outputSettings; - } - - /** - * Set the document's output settings. - * @param outputSettings new output settings. - * @return this document, for chaining. - */ - public Document outputSettings(OutputSettings outputSettings) { - Validate.notNull(outputSettings); - this.outputSettings = outputSettings; - return this; - } - - public enum QuirksMode { - noQuirks, quirks, limitedQuirks; - } - - public QuirksMode quirksMode() { - return quirksMode; - } - - public Document quirksMode(QuirksMode quirksMode) { - this.quirksMode = quirksMode; - return this; - } - - @Override - public boolean equals(Object o) { - return super.equals(o); - } -} - diff --git a/src/org/jsoup/nodes/DocumentType.java b/src/org/jsoup/nodes/DocumentType.java deleted file mode 100644 index 9b7ead4..0000000 --- a/src/org/jsoup/nodes/DocumentType.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.StringUtil; -import org.jsoup.nodes.Document.OutputSettings.*; - -/** - * A {@code } node. - */ -public class DocumentType extends Node { - private static final String NAME = "name"; - private static final String PUBLIC_ID = "publicId"; - private static final String SYSTEM_ID = "systemId"; - // todo: quirk mode from publicId and systemId - - /** - * Create a new doctype element. - * @param name the doctype's name - * @param publicId the doctype's public ID - * @param systemId the doctype's system ID - * @param baseUri the doctype's base URI - */ - public DocumentType(String name, String publicId, String systemId, String baseUri) { - super(baseUri); - - attr(NAME, name); - attr(PUBLIC_ID, publicId); - attr(SYSTEM_ID, systemId); - } - - @Override - public String nodeName() { - return "#doctype"; - } - - @Override - void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) { - if (out.syntax() == Syntax.html && !has(PUBLIC_ID) && !has(SYSTEM_ID)) { - // looks like a html5 doctype, go lowercase for aesthetics - accum.append("'); - } - - @Override - void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) { - } - - private boolean has(final String attribute) { - return !StringUtil.isBlank(attr(attribute)); - } -} diff --git a/src/org/jsoup/nodes/Element.java b/src/org/jsoup/nodes/Element.java deleted file mode 100644 index 3ea9780..0000000 --- a/src/org/jsoup/nodes/Element.java +++ /dev/null @@ -1,1174 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.StringUtil; -import org.jsoup.helper.Validate; -import org.jsoup.parser.Parser; -import org.jsoup.parser.Tag; -import org.jsoup.select.*; - -import java.util.*; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -/** - * A HTML element consists of a tag name, attributes, and child nodes (including text nodes and - * other elements). - * - * From an Element, you can extract data, traverse the node graph, and manipulate the HTML. - * - * @author Jonathan Hedley, jonathan@hedley.net - */ -public class Element extends Node { - private Tag tag; - /** - * Create a new, standalone Element. (Standalone in that is has no parent.) - * - * @param tag tag of this element - * @param baseUri the base URI - * @param attributes initial attributes - * @see #appendChild(Node) - * @see #appendElement(String) - */ - public Element(Tag tag, String baseUri, Attributes attributes) { - super(baseUri, attributes); - - Validate.notNull(tag); - this.tag = tag; - } - - /** - * Create a new Element from a tag and a base URI. - * - * @param tag element tag - * @param baseUri the base URI of this element. It is acceptable for the base URI to be an empty - * string, but not null. - * @see Tag#valueOf(String) - */ - public Element(Tag tag, String baseUri) { - this(tag, baseUri, new Attributes()); - } - - @Override - public String nodeName() { - return tag.getName(); - } - - /** - * Get the name of the tag for this element. E.g. {@code div} - * - * @return the tag name - */ - public String tagName() { - return tag.getName(); - } - - /** - * Change the tag of this element. For example, convert a {@code } to a {@code

} with - * {@code el.tagName("div");}. - * - * @param tagName new tag name for this element - * @return this element, for chaining - */ - public Element tagName(String tagName) { - Validate.notEmpty(tagName, "Tag name must not be empty."); - tag = Tag.valueOf(tagName); - return this; - } - - /** - * Get the Tag for this element. - * - * @return the tag object - */ - public Tag tag() { - return tag; - } - - /** - * Test if this element is a block-level element. (E.g. {@code
== true} or an inline element - * {@code

== false}). - * - * @return true if block, false if not (and thus inline) - */ - public boolean isBlock() { - return tag.isBlock(); - } - - /** - * Get the {@code id} attribute of this element. - * - * @return The id attribute, if present, or an empty string if not. - */ - public String id() { - String id = attr("id"); - return id == null ? "" : id; - } - - /** - * Set an attribute value on this element. If this element already has an attribute with the - * key, its value is updated; otherwise, a new attribute is added. - * - * @return this element - */ - public Element attr(String attributeKey, String attributeValue) { - super.attr(attributeKey, attributeValue); - return this; - } - - /** - * Get this element's HTML5 custom data attributes. Each attribute in the element that has a key - * starting with "data-" is included the dataset. - *

- * E.g., the element {@code

...} has the dataset - * {@code package=jsoup, language=java}. - *

- * This map is a filtered view of the element's attribute map. Changes to one map (add, remove, update) are reflected - * in the other map. - *

- * You can find elements that have data attributes using the {@code [^data-]} attribute key prefix selector. - * @return a map of {@code key=value} custom data attributes. - */ - public Map dataset() { - return attributes.dataset(); - } - - @Override - public final Element parent() { - return (Element) parentNode; - } - - /** - * Get this element's parent and ancestors, up to the document root. - * @return this element's stack of parents, closest first. - */ - public Elements parents() { - Elements parents = new Elements(); - accumulateParents(this, parents); - return parents; - } - - private static void accumulateParents(Element el, Elements parents) { - Element parent = el.parent(); - if (parent != null && !parent.tagName().equals("#root")) { - parents.add(parent); - accumulateParents(parent, parents); - } - } - - /** - * Get a child element of this element, by its 0-based index number. - *

- * Note that an element can have both mixed Nodes and Elements as children. This method inspects - * a filtered list of children that are elements, and the index is based on that filtered list. - * - * @param index the index number of the element to retrieve - * @return the child element, if it exists, otherwise throws an {@code IndexOutOfBoundsException} - * @see #childNode(int) - */ - public Element child(int index) { - return children().get(index); - } - - /** - * Get this element's child elements. - *

- * This is effectively a filter on {@link #childNodes()} to get Element nodes. - * @return child elements. If this element has no children, returns an - * empty list. - * @see #childNodes() - */ - public Elements children() { - // create on the fly rather than maintaining two lists. if gets slow, memoize, and mark dirty on change - List elements = new ArrayList(childNodes.size()); - for (Node node : childNodes) { - if (node instanceof Element) - elements.add((Element) node); - } - return new Elements(elements); - } - - /** - * Get this element's child text nodes. The list is unmodifiable but the text nodes may be manipulated. - *

- * This is effectively a filter on {@link #childNodes()} to get Text nodes. - * @return child text nodes. If this element has no text nodes, returns an - * empty list. - *

- * For example, with the input HTML: {@code

One Two Three
Four

} with the {@code p} element selected: - *
    - *
  • {@code p.text()} = {@code "One Two Three Four"}
  • - *
  • {@code p.ownText()} = {@code "One Three Four"}
  • - *
  • {@code p.children()} = {@code Elements[,
    ]}
  • - *
  • {@code p.childNodes()} = {@code List["One ", , " Three ",
    , " Four"]}
  • - *
  • {@code p.textNodes()} = {@code List["One ", " Three ", " Four"]}
  • - *
- */ - public List textNodes() { - List textNodes = new ArrayList(); - for (Node node : childNodes) { - if (node instanceof TextNode) - textNodes.add((TextNode) node); - } - return Collections.unmodifiableList(textNodes); - } - - /** - * Get this element's child data nodes. The list is unmodifiable but the data nodes may be manipulated. - *

- * This is effectively a filter on {@link #childNodes()} to get Data nodes. - * @return child data nodes. If this element has no data nodes, returns an - * empty list. - * @see #data() - */ - public List dataNodes() { - List dataNodes = new ArrayList(); - for (Node node : childNodes) { - if (node instanceof DataNode) - dataNodes.add((DataNode) node); - } - return Collections.unmodifiableList(dataNodes); - } - - /** - * Find elements that match the {@link Selector} CSS query, with this element as the starting context. Matched elements - * may include this element, or any of its children. - *

- * This method is generally more powerful to use than the DOM-type {@code getElementBy*} methods, because - * multiple filters can be combined, e.g.: - *

    - *
  • {@code el.select("a[href]")} - finds links ({@code a} tags with {@code href} attributes) - *
  • {@code el.select("a[href*=example.com]")} - finds links pointing to example.com (loosely) - *
- *

- * See the query syntax documentation in {@link org.jsoup.select.Selector}. - * - * @param cssQuery a {@link Selector} CSS-like query - * @return elements that match the query (empty if none match) - * @see org.jsoup.select.Selector - */ - public Elements select(String cssQuery) { - return Selector.select(cssQuery, this); - } - - /** - * Add a node child node to this element. - * - * @param child node to add. - * @return this element, so that you can add more child nodes or elements. - */ - public Element appendChild(Node child) { - Validate.notNull(child); - - addChildren(child); - return this; - } - - /** - * Add a node to the start of this element's children. - * - * @param child node to add. - * @return this element, so that you can add more child nodes or elements. - */ - public Element prependChild(Node child) { - Validate.notNull(child); - - addChildren(0, child); - return this; - } - - - /** - * Inserts the given child nodes into this element at the specified index. Current nodes will be shifted to the - * right. The inserted nodes will be moved from their current parent. To prevent moving, copy the nodes first. - * - * @param index 0-based index to insert children at. Specify {@code 0} to insert at the start, {@code -1} at the - * end - * @param children child nodes to insert - * @return this element, for chaining. - */ - public Element insertChildren(int index, Collection children) { - Validate.notNull(children, "Children collection to be inserted must not be null."); - int currentSize = childNodeSize(); - if (index < 0) index += currentSize +1; // roll around - Validate.isTrue(index >= 0 && index <= currentSize, "Insert position out of bounds."); - - ArrayList nodes = new ArrayList(children); - Node[] nodeArray = nodes.toArray(new Node[nodes.size()]); - addChildren(index, nodeArray); - return this; - } - - /** - * Create a new element by tag name, and add it as the last child. - * - * @param tagName the name of the tag (e.g. {@code div}). - * @return the new element, to allow you to add content to it, e.g.: - * {@code parent.appendElement("h1").attr("id", "header").text("Welcome");} - */ - public Element appendElement(String tagName) { - Element child = new Element(Tag.valueOf(tagName), baseUri()); - appendChild(child); - return child; - } - - /** - * Create a new element by tag name, and add it as the first child. - * - * @param tagName the name of the tag (e.g. {@code div}). - * @return the new element, to allow you to add content to it, e.g.: - * {@code parent.prependElement("h1").attr("id", "header").text("Welcome");} - */ - public Element prependElement(String tagName) { - Element child = new Element(Tag.valueOf(tagName), baseUri()); - prependChild(child); - return child; - } - - /** - * Create and append a new TextNode to this element. - * - * @param text the unencoded text to add - * @return this element - */ - public Element appendText(String text) { - TextNode node = new TextNode(text, baseUri()); - appendChild(node); - return this; - } - - /** - * Create and prepend a new TextNode to this element. - * - * @param text the unencoded text to add - * @return this element - */ - public Element prependText(String text) { - TextNode node = new TextNode(text, baseUri()); - prependChild(node); - return this; - } - - /** - * Add inner HTML to this element. The supplied HTML will be parsed, and each node appended to the end of the children. - * @param html HTML to add inside this element, after the existing HTML - * @return this element - * @see #html(String) - */ - public Element append(String html) { - Validate.notNull(html); - - List nodes = Parser.parseFragment(html, this, baseUri()); - addChildren(nodes.toArray(new Node[nodes.size()])); - return this; - } - - /** - * Add inner HTML into this element. The supplied HTML will be parsed, and each node prepended to the start of the element's children. - * @param html HTML to add inside this element, before the existing HTML - * @return this element - * @see #html(String) - */ - public Element prepend(String html) { - Validate.notNull(html); - - List nodes = Parser.parseFragment(html, this, baseUri()); - addChildren(0, nodes.toArray(new Node[nodes.size()])); - return this; - } - - /** - * Insert the specified HTML into the DOM before this element (as a preceding sibling). - * - * @param html HTML to add before this element - * @return this element, for chaining - * @see #after(String) - */ - @Override - public Element before(String html) { - return (Element) super.before(html); - } - - /** - * Insert the specified node into the DOM before this node (as a preceding sibling). - * @param node to add before this element - * @return this Element, for chaining - * @see #after(Node) - */ - @Override - public Element before(Node node) { - return (Element) super.before(node); - } - - /** - * Insert the specified HTML into the DOM after this element (as a following sibling). - * - * @param html HTML to add after this element - * @return this element, for chaining - * @see #before(String) - */ - @Override - public Element after(String html) { - return (Element) super.after(html); - } - - /** - * Insert the specified node into the DOM after this node (as a following sibling). - * @param node to add after this element - * @return this element, for chaining - * @see #before(Node) - */ - @Override - public Element after(Node node) { - return (Element) super.after(node); - } - - /** - * Remove all of the element's child nodes. Any attributes are left as-is. - * @return this element - */ - public Element empty() { - childNodes.clear(); - return this; - } - - /** - * Wrap the supplied HTML around this element. - * - * @param html HTML to wrap around this element, e.g. {@code

}. Can be arbitrarily deep. - * @return this element, for chaining. - */ - @Override - public Element wrap(String html) { - return (Element) super.wrap(html); - } - - /** - * Get a CSS selector that will uniquely select this element. - *

If the element has an ID, returns #id; - * otherwise returns the parent (if any) CSS selector, followed by '>', - * followed by a unique selector for the element (tag.class.class:nth-child(n)). - * - * @return the CSS Path that can be used to retrieve the element in a selector. - */ - public String cssSelector() { - if (id().length() > 0) - return "#" + id(); - - StringBuilder selector = new StringBuilder(tagName()); - String classes = StringUtil.join(classNames(), "."); - if (classes.length() > 0) - selector.append('.').append(classes); - - if (parent() == null || parent() instanceof Document) // don't add Document to selector, as will always have a html node - return selector.toString(); - - selector.insert(0, " > "); - if (parent().select(selector.toString()).size() > 1) - selector.append(String.format( - ":nth-child(%d)", elementSiblingIndex() + 1)); - - return parent().cssSelector() + selector.toString(); - } - - /** - * Get sibling elements. If the element has no sibling elements, returns an empty list. An element is not a sibling - * of itself, so will not be included in the returned list. - * @return sibling elements - */ - public Elements siblingElements() { - if (parentNode == null) - return new Elements(0); - - List elements = parent().children(); - Elements siblings = new Elements(elements.size() - 1); - for (Element el: elements) - if (el != this) - siblings.add(el); - return siblings; - } - - /** - * Gets the next sibling element of this element. E.g., if a {@code div} contains two {@code p}s, - * the {@code nextElementSibling} of the first {@code p} is the second {@code p}. - *

- * This is similar to {@link #nextSibling()}, but specifically finds only Elements - * @return the next element, or null if there is no next element - * @see #previousElementSibling() - */ - public Element nextElementSibling() { - if (parentNode == null) return null; - List siblings = parent().children(); - Integer index = indexInList(this, siblings); - Validate.notNull(index); - if (siblings.size() > index+1) - return siblings.get(index+1); - else - return null; - } - - /** - * Gets the previous element sibling of this element. - * @return the previous element, or null if there is no previous element - * @see #nextElementSibling() - */ - public Element previousElementSibling() { - if (parentNode == null) return null; - List siblings = parent().children(); - Integer index = indexInList(this, siblings); - Validate.notNull(index); - if (index > 0) - return siblings.get(index-1); - else - return null; - } - - /** - * Gets the first element sibling of this element. - * @return the first sibling that is an element (aka the parent's first element child) - */ - public Element firstElementSibling() { - // todo: should firstSibling() exclude this? - List siblings = parent().children(); - return siblings.size() > 1 ? siblings.get(0) : null; - } - - /** - * Get the list index of this element in its element sibling list. I.e. if this is the first element - * sibling, returns 0. - * @return position in element sibling list - */ - public Integer elementSiblingIndex() { - if (parent() == null) return 0; - return indexInList(this, parent().children()); - } - - /** - * Gets the last element sibling of this element - * @return the last sibling that is an element (aka the parent's last element child) - */ - public Element lastElementSibling() { - List siblings = parent().children(); - return siblings.size() > 1 ? siblings.get(siblings.size() - 1) : null; - } - - private static Integer indexInList(Element search, List elements) { - Validate.notNull(search); - Validate.notNull(elements); - - for (int i = 0; i < elements.size(); i++) { - E element = elements.get(i); - if (element.equals(search)) - return i; - } - return null; - } - - // DOM type methods - - /** - * Finds elements, including and recursively under this element, with the specified tag name. - * @param tagName The tag name to search for (case insensitively). - * @return a matching unmodifiable list of elements. Will be empty if this element and none of its children match. - */ - public Elements getElementsByTag(String tagName) { - Validate.notEmpty(tagName); - tagName = tagName.toLowerCase().trim(); - - return Collector.collect(new Evaluator.Tag(tagName), this); - } - - /** - * Find an element by ID, including or under this element. - *

- * Note that this finds the first matching ID, starting with this element. If you search down from a different - * starting point, it is possible to find a different element by ID. For unique element by ID within a Document, - * use {@link Document#getElementById(String)} - * @param id The ID to search for. - * @return The first matching element by ID, starting with this element, or null if none found. - */ - public Element getElementById(String id) { - Validate.notEmpty(id); - - Elements elements = Collector.collect(new Evaluator.Id(id), this); - if (elements.size() > 0) - return elements.get(0); - else - return null; - } - - /** - * Find elements that have this class, including or under this element. Case insensitive. - *

- * Elements can have multiple classes (e.g. {@code

}. This method - * checks each class, so you can find the above with {@code el.getElementsByClass("header");}. - * - * @param className the name of the class to search for. - * @return elements with the supplied class name, empty if none - * @see #hasClass(String) - * @see #classNames() - */ - public Elements getElementsByClass(String className) { - Validate.notEmpty(className); - - return Collector.collect(new Evaluator.Class(className), this); - } - - /** - * Find elements that have a named attribute set. Case insensitive. - * - * @param key name of the attribute, e.g. {@code href} - * @return elements that have this attribute, empty if none - */ - public Elements getElementsByAttribute(String key) { - Validate.notEmpty(key); - key = key.trim().toLowerCase(); - - return Collector.collect(new Evaluator.Attribute(key), this); - } - - /** - * Find elements that have an attribute name starting with the supplied prefix. Use {@code data-} to find elements - * that have HTML5 datasets. - * @param keyPrefix name prefix of the attribute e.g. {@code data-} - * @return elements that have attribute names that start with with the prefix, empty if none. - */ - public Elements getElementsByAttributeStarting(String keyPrefix) { - Validate.notEmpty(keyPrefix); - keyPrefix = keyPrefix.trim().toLowerCase(); - - return Collector.collect(new Evaluator.AttributeStarting(keyPrefix), this); - } - - /** - * Find elements that have an attribute with the specific value. Case insensitive. - * - * @param key name of the attribute - * @param value value of the attribute - * @return elements that have this attribute with this value, empty if none - */ - public Elements getElementsByAttributeValue(String key, String value) { - return Collector.collect(new Evaluator.AttributeWithValue(key, value), this); - } - - /** - * Find elements that either do not have this attribute, or have it with a different value. Case insensitive. - * - * @param key name of the attribute - * @param value value of the attribute - * @return elements that do not have a matching attribute - */ - public Elements getElementsByAttributeValueNot(String key, String value) { - return Collector.collect(new Evaluator.AttributeWithValueNot(key, value), this); - } - - /** - * Find elements that have attributes that start with the value prefix. Case insensitive. - * - * @param key name of the attribute - * @param valuePrefix start of attribute value - * @return elements that have attributes that start with the value prefix - */ - public Elements getElementsByAttributeValueStarting(String key, String valuePrefix) { - return Collector.collect(new Evaluator.AttributeWithValueStarting(key, valuePrefix), this); - } - - /** - * Find elements that have attributes that end with the value suffix. Case insensitive. - * - * @param key name of the attribute - * @param valueSuffix end of the attribute value - * @return elements that have attributes that end with the value suffix - */ - public Elements getElementsByAttributeValueEnding(String key, String valueSuffix) { - return Collector.collect(new Evaluator.AttributeWithValueEnding(key, valueSuffix), this); - } - - /** - * Find elements that have attributes whose value contains the match string. Case insensitive. - * - * @param key name of the attribute - * @param match substring of value to search for - * @return elements that have attributes containing this text - */ - public Elements getElementsByAttributeValueContaining(String key, String match) { - return Collector.collect(new Evaluator.AttributeWithValueContaining(key, match), this); - } - - /** - * Find elements that have attributes whose values match the supplied regular expression. - * @param key name of the attribute - * @param pattern compiled regular expression to match against attribute values - * @return elements that have attributes matching this regular expression - */ - public Elements getElementsByAttributeValueMatching(String key, Pattern pattern) { - return Collector.collect(new Evaluator.AttributeWithValueMatching(key, pattern), this); - - } - - /** - * Find elements that have attributes whose values match the supplied regular expression. - * @param key name of the attribute - * @param regex regular expression to match against attribute values. You can use embedded flags (such as (?i) and (?m) to control regex options. - * @return elements that have attributes matching this regular expression - */ - public Elements getElementsByAttributeValueMatching(String key, String regex) { - Pattern pattern; - try { - pattern = Pattern.compile(regex); - } catch (PatternSyntaxException e) { - throw new IllegalArgumentException("Pattern syntax error: " + regex, e); - } - return getElementsByAttributeValueMatching(key, pattern); - } - - /** - * Find elements whose sibling index is less than the supplied index. - * @param index 0-based index - * @return elements less than index - */ - public Elements getElementsByIndexLessThan(int index) { - return Collector.collect(new Evaluator.IndexLessThan(index), this); - } - - /** - * Find elements whose sibling index is greater than the supplied index. - * @param index 0-based index - * @return elements greater than index - */ - public Elements getElementsByIndexGreaterThan(int index) { - return Collector.collect(new Evaluator.IndexGreaterThan(index), this); - } - - /** - * Find elements whose sibling index is equal to the supplied index. - * @param index 0-based index - * @return elements equal to index - */ - public Elements getElementsByIndexEquals(int index) { - return Collector.collect(new Evaluator.IndexEquals(index), this); - } - - /** - * Find elements that contain the specified string. The search is case insensitive. The text may appear directly - * in the element, or in any of its descendants. - * @param searchText to look for in the element's text - * @return elements that contain the string, case insensitive. - * @see Element#text() - */ - public Elements getElementsContainingText(String searchText) { - return Collector.collect(new Evaluator.ContainsText(searchText), this); - } - - /** - * Find elements that directly contain the specified string. The search is case insensitive. The text must appear directly - * in the element, not in any of its descendants. - * @param searchText to look for in the element's own text - * @return elements that contain the string, case insensitive. - * @see Element#ownText() - */ - public Elements getElementsContainingOwnText(String searchText) { - return Collector.collect(new Evaluator.ContainsOwnText(searchText), this); - } - - /** - * Find elements whose text matches the supplied regular expression. - * @param pattern regular expression to match text against - * @return elements matching the supplied regular expression. - * @see Element#text() - */ - public Elements getElementsMatchingText(Pattern pattern) { - return Collector.collect(new Evaluator.Matches(pattern), this); - } - - /** - * Find elements whose text matches the supplied regular expression. - * @param regex regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options. - * @return elements matching the supplied regular expression. - * @see Element#text() - */ - public Elements getElementsMatchingText(String regex) { - Pattern pattern; - try { - pattern = Pattern.compile(regex); - } catch (PatternSyntaxException e) { - throw new IllegalArgumentException("Pattern syntax error: " + regex, e); - } - return getElementsMatchingText(pattern); - } - - /** - * Find elements whose own text matches the supplied regular expression. - * @param pattern regular expression to match text against - * @return elements matching the supplied regular expression. - * @see Element#ownText() - */ - public Elements getElementsMatchingOwnText(Pattern pattern) { - return Collector.collect(new Evaluator.MatchesOwn(pattern), this); - } - - /** - * Find elements whose text matches the supplied regular expression. - * @param regex regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options. - * @return elements matching the supplied regular expression. - * @see Element#ownText() - */ - public Elements getElementsMatchingOwnText(String regex) { - Pattern pattern; - try { - pattern = Pattern.compile(regex); - } catch (PatternSyntaxException e) { - throw new IllegalArgumentException("Pattern syntax error: " + regex, e); - } - return getElementsMatchingOwnText(pattern); - } - - /** - * Find all elements under this element (including self, and children of children). - * - * @return all elements - */ - public Elements getAllElements() { - return Collector.collect(new Evaluator.AllElements(), this); - } - - /** - * Gets the combined text of this element and all its children. Whitespace is normalized and trimmed. - *

- * For example, given HTML {@code

Hello there now!

}, {@code p.text()} returns {@code "Hello there now!"} - * - * @return unencoded text, or empty string if none. - * @see #ownText() - * @see #textNodes() - */ - public String text() { - final StringBuilder accum = new StringBuilder(); - new NodeTraversor(new NodeVisitor() { - public void head(Node node, int depth) { - if (node instanceof TextNode) { - TextNode textNode = (TextNode) node; - appendNormalisedText(accum, textNode); - } else if (node instanceof Element) { - Element element = (Element) node; - if (accum.length() > 0 && - (element.isBlock() || element.tag.getName().equals("br")) && - !TextNode.lastCharIsWhitespace(accum)) - accum.append(" "); - } - } - - public void tail(Node node, int depth) { - } - }).traverse(this); - return accum.toString().trim(); - } - - /** - * Gets the text owned by this element only; does not get the combined text of all children. - *

- * For example, given HTML {@code

Hello there now!

}, {@code p.ownText()} returns {@code "Hello now!"}, - * whereas {@code p.text()} returns {@code "Hello there now!"}. - * Note that the text within the {@code b} element is not returned, as it is not a direct child of the {@code p} element. - * - * @return unencoded text, or empty string if none. - * @see #text() - * @see #textNodes() - */ - public String ownText() { - StringBuilder sb = new StringBuilder(); - ownText(sb); - return sb.toString().trim(); - } - - private void ownText(StringBuilder accum) { - for (Node child : childNodes) { - if (child instanceof TextNode) { - TextNode textNode = (TextNode) child; - appendNormalisedText(accum, textNode); - } else if (child instanceof Element) { - appendWhitespaceIfBr((Element) child, accum); - } - } - } - - private static void appendNormalisedText(StringBuilder accum, TextNode textNode) { - String text = textNode.getWholeText(); - - if (preserveWhitespace(textNode.parentNode)) - accum.append(text); - else - StringUtil.appendNormalisedWhitespace(accum, text, TextNode.lastCharIsWhitespace(accum)); - } - - private static void appendWhitespaceIfBr(Element element, StringBuilder accum) { - if (element.tag.getName().equals("br") && !TextNode.lastCharIsWhitespace(accum)) - accum.append(" "); - } - - static boolean preserveWhitespace(Node node) { - // looks only at this element and one level up, to prevent recursion & needless stack searches - if (node != null && node instanceof Element) { - Element element = (Element) node; - return element.tag.preserveWhitespace() || - element.parent() != null && element.parent().tag.preserveWhitespace(); - } - return false; - } - - /** - * Set the text of this element. Any existing contents (text or elements) will be cleared - * @param text unencoded text - * @return this element - */ - public Element text(String text) { - Validate.notNull(text); - - empty(); - TextNode textNode = new TextNode(text, baseUri); - appendChild(textNode); - - return this; - } - - /** - Test if this element has any text content (that is not just whitespace). - @return true if element has non-blank text content. - */ - public boolean hasText() { - for (Node child: childNodes) { - if (child instanceof TextNode) { - TextNode textNode = (TextNode) child; - if (!textNode.isBlank()) - return true; - } else if (child instanceof Element) { - Element el = (Element) child; - if (el.hasText()) - return true; - } - } - return false; - } - - /** - * Get the combined data of this element. Data is e.g. the inside of a {@code script} tag. - * @return the data, or empty string if none - * - * @see #dataNodes() - */ - public String data() { - StringBuilder sb = new StringBuilder(); - - for (Node childNode : childNodes) { - if (childNode instanceof DataNode) { - DataNode data = (DataNode) childNode; - sb.append(data.getWholeData()); - } else if (childNode instanceof Element) { - Element element = (Element) childNode; - String elementData = element.data(); - sb.append(elementData); - } - } - return sb.toString(); - } - - /** - * Gets the literal value of this element's "class" attribute, which may include multiple class names, space - * separated. (E.g. on <div class="header gray"> returns, "header gray") - * @return The literal class attribute, or empty string if no class attribute set. - */ - public String className() { - return attr("class").trim(); - } - - /** - * Get all of the element's class names. E.g. on element {@code
}, - * returns a set of two elements {@code "header", "gray"}. Note that modifications to this set are not pushed to - * the backing {@code class} attribute; use the {@link #classNames(java.util.Set)} method to persist them. - * @return set of classnames, empty if no class attribute - */ - public Set classNames() { - String[] names = className().split("\\s+"); - Set classNames = new LinkedHashSet(Arrays.asList(names)); - classNames.remove(""); // if classNames() was empty, would include an empty class - - return classNames; - } - - /** - Set the element's {@code class} attribute to the supplied class names. - @param classNames set of classes - @return this element, for chaining - */ - public Element classNames(Set classNames) { - Validate.notNull(classNames); - attributes.put("class", StringUtil.join(classNames, " ")); - return this; - } - - /** - * Tests if this element has a class. Case insensitive. - * @param className name of class to check for - * @return true if it does, false if not - */ - public boolean hasClass(String className) { - Set classNames = classNames(); - for (String name : classNames) { - if (className.equalsIgnoreCase(name)) - return true; - } - return false; - } - - /** - Add a class name to this element's {@code class} attribute. - @param className class name to add - @return this element - */ - public Element addClass(String className) { - Validate.notNull(className); - - Set classes = classNames(); - classes.add(className); - classNames(classes); - - return this; - } - - /** - Remove a class name from this element's {@code class} attribute. - @param className class name to remove - @return this element - */ - public Element removeClass(String className) { - Validate.notNull(className); - - Set classes = classNames(); - classes.remove(className); - classNames(classes); - - return this; - } - - /** - Toggle a class name on this element's {@code class} attribute: if present, remove it; otherwise add it. - @param className class name to toggle - @return this element - */ - public Element toggleClass(String className) { - Validate.notNull(className); - - Set classes = classNames(); - if (classes.contains(className)) - classes.remove(className); - else - classes.add(className); - classNames(classes); - - return this; - } - - /** - * Get the value of a form element (input, textarea, etc). - * @return the value of the form element, or empty string if not set. - */ - public String val() { - if (tagName().equals("textarea")) - return text(); - else - return attr("value"); - } - - /** - * Set the value of a form element (input, textarea, etc). - * @param value value to set - * @return this element (for chaining) - */ - public Element val(String value) { - if (tagName().equals("textarea")) - text(value); - else - attr("value", value); - return this; - } - - void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) { - if (accum.length() > 0 && out.prettyPrint() && (tag.formatAsBlock() || (parent() != null && parent().tag().formatAsBlock()) || out.outline()) ) - indent(accum, depth, out); - accum - .append("<") - .append(tagName()); - attributes.html(accum, out); - - // selfclosing includes unknown tags, isEmpty defines tags that are always empty - if (childNodes.isEmpty() && tag.isSelfClosing()) { - if (out.syntax() == Document.OutputSettings.Syntax.html && tag.isEmpty()) - accum.append('>'); - else - accum.append(" />"); // in html, in xml - } - else - accum.append(">"); - } - - void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) { - if (!(childNodes.isEmpty() && tag.isSelfClosing())) { - if (out.prettyPrint() && (!childNodes.isEmpty() && ( - tag.formatAsBlock() || (out.outline() && (childNodes.size()>1 || (childNodes.size()==1 && !(childNodes.get(0) instanceof TextNode)))) - ))) - indent(accum, depth, out); - accum.append(""); - } - } - - /** - * Retrieves the element's inner HTML. E.g. on a {@code
} with one empty {@code

}, would return - * {@code

}. (Whereas {@link #outerHtml()} would return {@code

}.) - * - * @return String of HTML. - * @see #outerHtml() - */ - public String html() { - StringBuilder accum = new StringBuilder(); - html(accum); - return getOutputSettings().prettyPrint() ? accum.toString().trim() : accum.toString(); - } - - private void html(StringBuilder accum) { - for (Node node : childNodes) - node.outerHtml(accum); - } - - /** - * Set this element's inner HTML. Clears the existing HTML first. - * @param html HTML to parse and set into this element - * @return this element - * @see #append(String) - */ - public Element html(String html) { - empty(); - append(html); - return this; - } - - public String toString() { - return outerHtml(); - } - - @Override - public boolean equals(Object o) { - return this == o; - } - - @Override - public int hashCode() { - // todo: fixup, not very useful - int result = super.hashCode(); - result = 31 * result + (tag != null ? tag.hashCode() : 0); - return result; - } - - @Override - public Element clone() { - Element clone = (Element) super.clone(); - return clone; - } -} diff --git a/src/org/jsoup/nodes/Entities.java b/src/org/jsoup/nodes/Entities.java deleted file mode 100644 index 3cba5da..0000000 --- a/src/org/jsoup/nodes/Entities.java +++ /dev/null @@ -1,226 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.StringUtil; -import org.jsoup.parser.Parser; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.CharsetEncoder; -import java.util.*; - -/** - * HTML entities, and escape routines. - * Source: W3C HTML - * named character references. - */ -public class Entities { - public enum EscapeMode { - /** Restricted entities suitable for XHTML output: lt, gt, amp, and quot only. */ - xhtml(xhtmlByVal), - /** Default HTML output entities. */ - base(baseByVal), - /** Complete HTML entities. */ - extended(fullByVal); - - private Map map; - - EscapeMode(Map map) { - this.map = map; - } - - public Map getMap() { - return map; - } - } - - private static final Map full; - private static final Map xhtmlByVal; - private static final Map base; - private static final Map baseByVal; - private static final Map fullByVal; - - private Entities() {} - - /** - * Check if the input is a known named entity - * @param name the possible entity name (e.g. "lt" or "amp") - * @return true if a known named entity - */ - public static boolean isNamedEntity(String name) { - return full.containsKey(name); - } - - /** - * Check if the input is a known named entity in the base entity set. - * @param name the possible entity name (e.g. "lt" or "amp") - * @return true if a known named entity in the base set - * @see #isNamedEntity(String) - */ - public static boolean isBaseNamedEntity(String name) { - return base.containsKey(name); - } - - /** - * Get the Character value of the named entity - * @param name named entity (e.g. "lt" or "amp") - * @return the Character value of the named entity (e.g. '<' or '&') - */ - public static Character getCharacterByName(String name) { - return full.get(name); - } - - static String escape(String string, Document.OutputSettings out) { - StringBuilder accum = new StringBuilder(string.length() * 2); - escape(accum, string, out, false, false, false); - return accum.toString(); - } - - // this method is ugly, and does a lot. but other breakups cause rescanning and stringbuilder generations - static void escape(StringBuilder accum, String string, Document.OutputSettings out, - boolean inAttribute, boolean normaliseWhite, boolean stripLeadingWhite) { - - boolean lastWasWhite = false; - boolean reachedNonWhite = false; - EscapeMode escapeMode = out.escapeMode(); - CharsetEncoder encoder = out.encoder(); - Map map = escapeMode.getMap(); - final int length = string.length(); - - int codePoint; - for (int offset = 0; offset < length; offset += Character.charCount(codePoint)) { - codePoint = string.codePointAt(offset); - - if (normaliseWhite) { - if (StringUtil.isWhitespace(codePoint)) { - if ((stripLeadingWhite && !reachedNonWhite) || lastWasWhite) - continue; - accum.append(' '); - lastWasWhite = true; - continue; - } else { - lastWasWhite = false; - reachedNonWhite = true; - } - } - // surrogate pairs, split implementation for efficiency on single char common case (saves creating strings, char[]): - if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT) { - final char c = (char) codePoint; - // html specific and required escapes: - switch (c) { - case '&': - accum.append("&"); - break; - case 0xA0: - if (escapeMode != EscapeMode.xhtml) - accum.append(" "); - else - accum.append(c); - break; - case '<': - if (!inAttribute) - accum.append("<"); - else - accum.append(c); - break; - case '>': - if (!inAttribute) - accum.append(">"); - else - accum.append(c); - break; - case '"': - if (inAttribute) - accum.append("""); - else - accum.append(c); - break; - default: - if (encoder.canEncode(c)) - accum.append(c); - else if (map.containsKey(c)) - accum.append('&').append(map.get(c)).append(';'); - else - accum.append("&#x").append(Integer.toHexString(codePoint)).append(';'); - } - } else { - final String c = new String(Character.toChars(codePoint)); - if (encoder.canEncode(c)) - accum.append(c); - else - accum.append("&#x").append(Integer.toHexString(codePoint)).append(';'); - } - } - } - - static String unescape(String string) { - return unescape(string, false); - } - - /** - * Unescape the input string. - * @param string - * @param strict if "strict" (that is, requires trailing ';' char, otherwise that's optional) - * @return - */ - static String unescape(String string, boolean strict) { - return Parser.unescapeEntities(string, strict); - } - - - // xhtml has restricted entities - private static final Object[][] xhtmlArray = { - {"quot", 0x00022}, - {"amp", 0x00026}, - {"lt", 0x0003C}, - {"gt", 0x0003E} - }; - - static { - xhtmlByVal = new HashMap(); - base = loadEntities("entities-base.properties"); // most common / default - baseByVal = toCharacterKey(base); - full = loadEntities("entities-full.properties"); // extended and overblown. - fullByVal = toCharacterKey(full); - - for (Object[] entity : xhtmlArray) { - Character c = Character.valueOf((char) ((Integer) entity[1]).intValue()); - xhtmlByVal.put(c, ((String) entity[0])); - } - } - - private static Map loadEntities(String filename) { - Properties properties = new Properties(); - Map entities = new HashMap(); - try { - InputStream in = Entities.class.getResourceAsStream(filename); - properties.load(in); - in.close(); - } catch (IOException e) { - throw new MissingResourceException("Error loading entities resource: " + e.getMessage(), "Entities", filename); - } - - for (Map.Entry entry: properties.entrySet()) { - Character val = Character.valueOf((char) Integer.parseInt((String) entry.getValue(), 16)); - String name = (String) entry.getKey(); - entities.put(name, val); - } - return entities; - } - - private static Map toCharacterKey(Map inMap) { - Map outMap = new HashMap(); - for (Map.Entry entry: inMap.entrySet()) { - Character character = entry.getValue(); - String name = entry.getKey(); - - if (outMap.containsKey(character)) { - // dupe, prefer the lower case version - if (name.toLowerCase().equals(name)) - outMap.put(character, name); - } else { - outMap.put(character, name); - } - } - return outMap; - } -} diff --git a/src/org/jsoup/nodes/FormElement.java b/src/org/jsoup/nodes/FormElement.java deleted file mode 100644 index 7c5b7a5..0000000 --- a/src/org/jsoup/nodes/FormElement.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.Connection; -import org.jsoup.Jsoup; -import org.jsoup.helper.HttpConnection; -import org.jsoup.helper.Validate; -import org.jsoup.parser.Tag; -import org.jsoup.select.Elements; - -import java.util.ArrayList; -import java.util.List; - -/** - * A HTML Form Element provides ready access to the form fields/controls that are associated with it. It also allows a - * form to easily be submitted. - */ -public class FormElement extends Element { - private final Elements elements = new Elements(); - - /** - * Create a new, standalone form element. - * - * @param tag tag of this element - * @param baseUri the base URI - * @param attributes initial attributes - */ - public FormElement(Tag tag, String baseUri, Attributes attributes) { - super(tag, baseUri, attributes); - } - - /** - * Get the list of form control elements associated with this form. - * @return form controls associated with this element. - */ - public Elements elements() { - return elements; - } - - /** - * Add a form control element to this form. - * @param element form control to add - * @return this form element, for chaining - */ - public FormElement addElement(Element element) { - elements.add(element); - return this; - } - - /** - * Prepare to submit this form. A Connection object is created with the request set up from the form values. You - * can then set up other options (like user-agent, timeout, cookies), then execute it. - * @return a connection prepared from the values of this form. - * @throws IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the - * document's base URI when parsing. - */ - public Connection submit() { - String action = hasAttr("action") ? absUrl("action") : baseUri(); - Validate.notEmpty(action, "Could not determine a form action URL for submit. Ensure you set a base URI when parsing."); - Connection.Method method = attr("method").toUpperCase().equals("POST") ? - Connection.Method.POST : Connection.Method.GET; - - Connection con = Jsoup.connect(action) - .data(formData()) - .method(method); - - return con; - } - - /** - * Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the - * list will not be reflected in the DOM. - * @return a list of key vals - */ - public List formData() { - ArrayList data = new ArrayList(); - - // iterate the form control elements and accumulate their values - for (Element el: elements) { - if (!el.tag().isFormSubmittable()) continue; // contents are form listable, superset of submitable - String name = el.attr("name"); - if (name.length() == 0) continue; - - if ("select".equals(el.tagName())) { - Elements options = el.select("option[selected]"); - boolean set = false; - for (Element option: options) { - data.add(HttpConnection.KeyVal.create(name, option.val())); - set = true; - } - if (!set) { - Element option = el.select("option").first(); - if (option != null) - data.add(HttpConnection.KeyVal.create(name, option.val())); - } - } else { - data.add(HttpConnection.KeyVal.create(name, el.val())); - } - } - return data; - } - - @Override - public boolean equals(Object o) { - return super.equals(o); - } -} diff --git a/src/org/jsoup/nodes/Node.java b/src/org/jsoup/nodes/Node.java deleted file mode 100644 index 6ee4242..0000000 --- a/src/org/jsoup/nodes/Node.java +++ /dev/null @@ -1,668 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.StringUtil; -import org.jsoup.helper.Validate; -import org.jsoup.parser.Parser; -import org.jsoup.select.NodeTraversor; -import org.jsoup.select.NodeVisitor; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -/** - The base, abstract Node model. Elements, Documents, Comments etc are all Node instances. - - @author Jonathan Hedley, jonathan@hedley.net */ -public abstract class Node implements Cloneable { - Node parentNode; - List childNodes; - Attributes attributes; - String baseUri; - int siblingIndex; - - /** - Create a new Node. - @param baseUri base URI - @param attributes attributes (not null, but may be empty) - */ - protected Node(String baseUri, Attributes attributes) { - Validate.notNull(baseUri); - Validate.notNull(attributes); - - childNodes = new ArrayList(4); - this.baseUri = baseUri.trim(); - this.attributes = attributes; - } - - protected Node(String baseUri) { - this(baseUri, new Attributes()); - } - - /** - * Default constructor. Doesn't setup base uri, children, or attributes; use with caution. - */ - protected Node() { - childNodes = Collections.emptyList(); - attributes = null; - } - - /** - Get the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof). - @return node name - */ - public abstract String nodeName(); - - /** - * Get an attribute's value by its key. - *

- * To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs, - * which is a shortcut to the {@link #absUrl} method. - * E.g.:

String url = a.attr("abs:href");
- * @param attributeKey The attribute key. - * @return The attribute, or empty string if not present (to avoid nulls). - * @see #attributes() - * @see #hasAttr(String) - * @see #absUrl(String) - */ - public String attr(String attributeKey) { - Validate.notNull(attributeKey); - - if (attributes.hasKey(attributeKey)) - return attributes.get(attributeKey); - else if (attributeKey.toLowerCase().startsWith("abs:")) - return absUrl(attributeKey.substring("abs:".length())); - else return ""; - } - - /** - * Get all of the element's attributes. - * @return attributes (which implements iterable, in same order as presented in original HTML). - */ - public Attributes attributes() { - return attributes; - } - - /** - * Set an attribute (key=value). If the attribute already exists, it is replaced. - * @param attributeKey The attribute key. - * @param attributeValue The attribute value. - * @return this (for chaining) - */ - public Node attr(String attributeKey, String attributeValue) { - attributes.put(attributeKey, attributeValue); - return this; - } - - /** - * Test if this element has an attribute. - * @param attributeKey The attribute key to check. - * @return true if the attribute exists, false if not. - */ - public boolean hasAttr(String attributeKey) { - Validate.notNull(attributeKey); - - if (attributeKey.startsWith("abs:")) { - String key = attributeKey.substring("abs:".length()); - if (attributes.hasKey(key) && !absUrl(key).equals("")) - return true; - } - return attributes.hasKey(attributeKey); - } - - /** - * Remove an attribute from this element. - * @param attributeKey The attribute to remove. - * @return this (for chaining) - */ - public Node removeAttr(String attributeKey) { - Validate.notNull(attributeKey); - attributes.remove(attributeKey); - return this; - } - - /** - Get the base URI of this node. - @return base URI - */ - public String baseUri() { - return baseUri; - } - - /** - Update the base URI of this node and all of its descendants. - @param baseUri base URI to set - */ - public void setBaseUri(final String baseUri) { - Validate.notNull(baseUri); - - traverse(new NodeVisitor() { - public void head(Node node, int depth) { - node.baseUri = baseUri; - } - - public void tail(Node node, int depth) { - } - }); - } - - /** - * Get an absolute URL from a URL attribute that may be relative (i.e. an <a href> or - * <img src>). - *

- * E.g.: String absUrl = linkEl.absUrl("href"); - *

- * If the attribute value is already absolute (i.e. it starts with a protocol, like - * http:// or https:// etc), and it successfully parses as a URL, the attribute is - * returned directly. Otherwise, it is treated as a URL relative to the element's {@link #baseUri}, and made - * absolute using that. - *

- * As an alternate, you can use the {@link #attr} method with the abs: prefix, e.g.: - * String absUrl = linkEl.attr("abs:href"); - * - * @param attributeKey The attribute key - * @return An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or - * could not be made successfully into a URL. - * @see #attr - * @see java.net.URL#URL(java.net.URL, String) - */ - public String absUrl(String attributeKey) { - Validate.notEmpty(attributeKey); - - String relUrl = attr(attributeKey); - if (!hasAttr(attributeKey)) { - return ""; // nothing to make absolute with - } else { - URL base; - try { - try { - base = new URL(baseUri); - } catch (MalformedURLException e) { - // the base is unsuitable, but the attribute may be abs on its own, so try that - URL abs = new URL(relUrl); - return abs.toExternalForm(); - } - // workaround: java resolves '//path/file + ?foo' to '//path/?foo', not '//path/file?foo' as desired - if (relUrl.startsWith("?")) - relUrl = base.getPath() + relUrl; - URL abs = new URL(base, relUrl); - return abs.toExternalForm(); - } catch (MalformedURLException e) { - return ""; - } - } - } - - /** - Get a child node by its 0-based index. - @param index index of child node - @return the child node at this index. Throws a {@code IndexOutOfBoundsException} if the index is out of bounds. - */ - public Node childNode(int index) { - return childNodes.get(index); - } - - /** - Get this node's children. Presented as an unmodifiable list: new children can not be added, but the child nodes - themselves can be manipulated. - @return list of children. If no children, returns an empty list. - */ - public List childNodes() { - return Collections.unmodifiableList(childNodes); - } - - /** - * Returns a deep copy of this node's children. Changes made to these nodes will not be reflected in the original - * nodes - * @return a deep copy of this node's children - */ - public List childNodesCopy() { - List children = new ArrayList(childNodes.size()); - for (Node node : childNodes) { - children.add(node.clone()); - } - return children; - } - - /** - * Get the number of child nodes that this node holds. - * @return the number of child nodes that this node holds. - */ - public final int childNodeSize() { - return childNodes.size(); - } - - protected Node[] childNodesAsArray() { - return childNodes.toArray(new Node[childNodeSize()]); - } - - /** - Gets this node's parent node. - @return parent node; or null if no parent. - */ - public Node parent() { - return parentNode; - } - - /** - Gets this node's parent node. Node overridable by extending classes, so useful if you really just need the Node type. - @return parent node; or null if no parent. - */ - public final Node parentNode() { - return parentNode; - } - - /** - * Gets the Document associated with this Node. - * @return the Document associated with this Node, or null if there is no such Document. - */ - public Document ownerDocument() { - if (this instanceof Document) - return (Document) this; - else if (parentNode == null) - return null; - else - return parentNode.ownerDocument(); - } - - /** - * Remove (delete) this node from the DOM tree. If this node has children, they are also removed. - */ - public void remove() { - Validate.notNull(parentNode); - parentNode.removeChild(this); - } - - /** - * Insert the specified HTML into the DOM before this node (i.e. as a preceding sibling). - * @param html HTML to add before this node - * @return this node, for chaining - * @see #after(String) - */ - public Node before(String html) { - addSiblingHtml(siblingIndex(), html); - return this; - } - - /** - * Insert the specified node into the DOM before this node (i.e. as a preceding sibling). - * @param node to add before this node - * @return this node, for chaining - * @see #after(Node) - */ - public Node before(Node node) { - Validate.notNull(node); - Validate.notNull(parentNode); - - parentNode.addChildren(siblingIndex(), node); - return this; - } - - /** - * Insert the specified HTML into the DOM after this node (i.e. as a following sibling). - * @param html HTML to add after this node - * @return this node, for chaining - * @see #before(String) - */ - public Node after(String html) { - addSiblingHtml(siblingIndex()+1, html); - return this; - } - - /** - * Insert the specified node into the DOM after this node (i.e. as a following sibling). - * @param node to add after this node - * @return this node, for chaining - * @see #before(Node) - */ - public Node after(Node node) { - Validate.notNull(node); - Validate.notNull(parentNode); - - parentNode.addChildren(siblingIndex()+1, node); - return this; - } - - private void addSiblingHtml(int index, String html) { - Validate.notNull(html); - Validate.notNull(parentNode); - - Element context = parent() instanceof Element ? (Element) parent() : null; - List nodes = Parser.parseFragment(html, context, baseUri()); - parentNode.addChildren(index, nodes.toArray(new Node[nodes.size()])); - } - - /** - Wrap the supplied HTML around this node. - @param html HTML to wrap around this element, e.g. {@code

}. Can be arbitrarily deep. - @return this node, for chaining. - */ - public Node wrap(String html) { - Validate.notEmpty(html); - - Element context = parent() instanceof Element ? (Element) parent() : null; - List wrapChildren = Parser.parseFragment(html, context, baseUri()); - Node wrapNode = wrapChildren.get(0); - if (wrapNode == null || !(wrapNode instanceof Element)) // nothing to wrap with; noop - return null; - - Element wrap = (Element) wrapNode; - Element deepest = getDeepChild(wrap); - parentNode.replaceChild(this, wrap); - deepest.addChildren(this); - - // remainder (unbalanced wrap, like

-- The

is remainder - if (wrapChildren.size() > 0) { - for (int i = 0; i < wrapChildren.size(); i++) { - Node remainder = wrapChildren.get(i); - remainder.parentNode.removeChild(remainder); - wrap.appendChild(remainder); - } - } - return this; - } - - /** - * Removes this node from the DOM, and moves its children up into the node's parent. This has the effect of dropping - * the node but keeping its children. - *

- * For example, with the input html:
- * {@code

One Two Three
}
- * Calling {@code element.unwrap()} on the {@code span} element will result in the html:
- * {@code
One Two Three
}
- * and the {@code "Two "} {@link TextNode} being returned. - * @return the first child of this node, after the node has been unwrapped. Null if the node had no children. - * @see #remove() - * @see #wrap(String) - */ - public Node unwrap() { - Validate.notNull(parentNode); - - int index = siblingIndex; - Node firstChild = childNodes.size() > 0 ? childNodes.get(0) : null; - parentNode.addChildren(index, this.childNodesAsArray()); - this.remove(); - - return firstChild; - } - - private Element getDeepChild(Element el) { - List children = el.children(); - if (children.size() > 0) - return getDeepChild(children.get(0)); - else - return el; - } - - /** - * Replace this node in the DOM with the supplied node. - * @param in the node that will will replace the existing node. - */ - public void replaceWith(Node in) { - Validate.notNull(in); - Validate.notNull(parentNode); - parentNode.replaceChild(this, in); - } - - protected void setParentNode(Node parentNode) { - if (this.parentNode != null) - this.parentNode.removeChild(this); - this.parentNode = parentNode; - } - - protected void replaceChild(Node out, Node in) { - Validate.isTrue(out.parentNode == this); - Validate.notNull(in); - if (in.parentNode != null) - in.parentNode.removeChild(in); - - Integer index = out.siblingIndex(); - childNodes.set(index, in); - in.parentNode = this; - in.setSiblingIndex(index); - out.parentNode = null; - } - - protected void removeChild(Node out) { - Validate.isTrue(out.parentNode == this); - int index = out.siblingIndex(); - childNodes.remove(index); - reindexChildren(); - out.parentNode = null; - } - - protected void addChildren(Node... children) { - //most used. short circuit addChildren(int), which hits reindex children and array copy - for (Node child: children) { - reparentChild(child); - childNodes.add(child); - child.setSiblingIndex(childNodes.size()-1); - } - } - - protected void addChildren(int index, Node... children) { - Validate.noNullElements(children); - for (int i = children.length - 1; i >= 0; i--) { - Node in = children[i]; - reparentChild(in); - childNodes.add(index, in); - } - reindexChildren(); - } - - private void reparentChild(Node child) { - if (child.parentNode != null) - child.parentNode.removeChild(child); - child.setParentNode(this); - } - - private void reindexChildren() { - for (int i = 0; i < childNodes.size(); i++) { - childNodes.get(i).setSiblingIndex(i); - } - } - - /** - Retrieves this node's sibling nodes. Similar to {@link #childNodes() node.parent.childNodes()}, but does not - include this node (a node is not a sibling of itself). - @return node siblings. If the node has no parent, returns an empty list. - */ - public List siblingNodes() { - if (parentNode == null) - return Collections.emptyList(); - - List nodes = parentNode.childNodes; - List siblings = new ArrayList(nodes.size() - 1); - for (Node node: nodes) - if (node != this) - siblings.add(node); - return siblings; - } - - /** - Get this node's next sibling. - @return next sibling, or null if this is the last sibling - */ - public Node nextSibling() { - if (parentNode == null) - return null; // root - - List siblings = parentNode.childNodes; - Integer index = siblingIndex(); - Validate.notNull(index); - if (siblings.size() > index+1) - return siblings.get(index+1); - else - return null; - } - - /** - Get this node's previous sibling. - @return the previous sibling, or null if this is the first sibling - */ - public Node previousSibling() { - if (parentNode == null) - return null; // root - - List siblings = parentNode.childNodes; - Integer index = siblingIndex(); - Validate.notNull(index); - if (index > 0) - return siblings.get(index-1); - else - return null; - } - - /** - * Get the list index of this node in its node sibling list. I.e. if this is the first node - * sibling, returns 0. - * @return position in node sibling list - * @see org.jsoup.nodes.Element#elementSiblingIndex() - */ - public int siblingIndex() { - return siblingIndex; - } - - protected void setSiblingIndex(int siblingIndex) { - this.siblingIndex = siblingIndex; - } - - /** - * Perform a depth-first traversal through this node and its descendants. - * @param nodeVisitor the visitor callbacks to perform on each node - * @return this node, for chaining - */ - public Node traverse(NodeVisitor nodeVisitor) { - Validate.notNull(nodeVisitor); - NodeTraversor traversor = new NodeTraversor(nodeVisitor); - traversor.traverse(this); - return this; - } - - /** - Get the outer HTML of this node. - @return HTML - */ - public String outerHtml() { - StringBuilder accum = new StringBuilder(128); - outerHtml(accum); - return accum.toString(); - } - - protected void outerHtml(StringBuilder accum) { - new NodeTraversor(new OuterHtmlVisitor(accum, getOutputSettings())).traverse(this); - } - - // if this node has no document (or parent), retrieve the default output settings - Document.OutputSettings getOutputSettings() { - return ownerDocument() != null ? ownerDocument().outputSettings() : (new Document("")).outputSettings(); - } - - /** - Get the outer HTML of this node. - @param accum accumulator to place HTML into - */ - abstract void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out); - - abstract void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out); - - @Override - public String toString() { - return outerHtml(); - } - - protected void indent(StringBuilder accum, int depth, Document.OutputSettings out) { - accum.append("\n").append(StringUtil.padding(depth * out.indentAmount())); - } - - @Override - public boolean equals(Object o) { - // todo: have nodes hold a child index, compare against that and parent (not children) - - return this == o; - } - - @Override - public int hashCode() { - int result = parentNode != null ? parentNode.hashCode() : 0; - // not children, or will block stack as they go back up to parent) - result = 31 * result + (attributes != null ? attributes.hashCode() : 0); - return result; - } - - /** - * Create a stand-alone, deep copy of this node, and all of its children. The cloned node will have no siblings or - * parent node. As a stand-alone object, any changes made to the clone or any of its children will not impact the - * original node. - *

- * The cloned node may be adopted into another Document or node structure using {@link Element#appendChild(Node)}. - * @return stand-alone cloned node - */ - @Override - public Node clone() { - Node thisClone = doClone(null); // splits for orphan - - // Queue up nodes that need their children cloned (BFS). - LinkedList nodesToProcess = new LinkedList(); - nodesToProcess.add(thisClone); - - while (!nodesToProcess.isEmpty()) { - Node currParent = nodesToProcess.remove(); - - for (int i = 0; i < currParent.childNodes.size(); i++) { - Node childClone = currParent.childNodes.get(i).doClone(currParent); - currParent.childNodes.set(i, childClone); - nodesToProcess.add(childClone); - } - } - - return thisClone; - } - - /* - * Return a clone of the node using the given parent (which can be null). - * Not a deep copy of children. - */ - protected Node doClone(Node parent) { - Node clone; - - try { - clone = (Node) super.clone(); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - - clone.parentNode = parent; // can be null, to create an orphan split - clone.siblingIndex = parent == null ? 0 : siblingIndex; - clone.attributes = attributes != null ? attributes.clone() : null; - clone.baseUri = baseUri; - clone.childNodes = new ArrayList(childNodes.size()); - - for (Node child: childNodes) - clone.childNodes.add(child); - - return clone; - } - - private static class OuterHtmlVisitor implements NodeVisitor { - private StringBuilder accum; - private Document.OutputSettings out; - - OuterHtmlVisitor(StringBuilder accum, Document.OutputSettings out) { - this.accum = accum; - this.out = out; - } - - public void head(Node node, int depth) { - node.outerHtmlHead(accum, depth, out); - } - - public void tail(Node node, int depth) { - if (!node.nodeName().equals("#text")) // saves a void hit. - node.outerHtmlTail(accum, depth, out); - } - } -} diff --git a/src/org/jsoup/nodes/TextNode.java b/src/org/jsoup/nodes/TextNode.java deleted file mode 100644 index d218b89..0000000 --- a/src/org/jsoup/nodes/TextNode.java +++ /dev/null @@ -1,175 +0,0 @@ -package org.jsoup.nodes; - -import org.jsoup.helper.StringUtil; -import org.jsoup.helper.Validate; - -/** - A text node. - - @author Jonathan Hedley, jonathan@hedley.net */ -public class TextNode extends Node { - /* - TextNode is a node, and so by default comes with attributes and children. The attributes are seldom used, but use - memory, and the child nodes are never used. So we don't have them, and override accessors to attributes to create - them as needed on the fly. - */ - private static final String TEXT_KEY = "text"; - String text; - - /** - Create a new TextNode representing the supplied (unencoded) text). - - @param text raw text - @param baseUri base uri - @see #createFromEncoded(String, String) - */ - public TextNode(String text, String baseUri) { - this.baseUri = baseUri; - this.text = text; - } - - public String nodeName() { - return "#text"; - } - - /** - * Get the text content of this text node. - * @return Unencoded, normalised text. - * @see TextNode#getWholeText() - */ - public String text() { - return normaliseWhitespace(getWholeText()); - } - - /** - * Set the text content of this text node. - * @param text unencoded text - * @return this, for chaining - */ - public TextNode text(String text) { - this.text = text; - if (attributes != null) - attributes.put(TEXT_KEY, text); - return this; - } - - /** - Get the (unencoded) text of this text node, including any newlines and spaces present in the original. - @return text - */ - public String getWholeText() { - return attributes == null ? text : attributes.get(TEXT_KEY); - } - - /** - Test if this text node is blank -- that is, empty or only whitespace (including newlines). - @return true if this document is empty or only whitespace, false if it contains any text content. - */ - public boolean isBlank() { - return StringUtil.isBlank(getWholeText()); - } - - /** - * Split this text node into two nodes at the specified string offset. After splitting, this node will contain the - * original text up to the offset, and will have a new text node sibling containing the text after the offset. - * @param offset string offset point to split node at. - * @return the newly created text node containing the text after the offset. - */ - public TextNode splitText(int offset) { - Validate.isTrue(offset >= 0, "Split offset must be not be negative"); - Validate.isTrue(offset < text.length(), "Split offset must not be greater than current text length"); - - String head = getWholeText().substring(0, offset); - String tail = getWholeText().substring(offset); - text(head); - TextNode tailNode = new TextNode(tail, this.baseUri()); - if (parent() != null) - parent().addChildren(siblingIndex()+1, tailNode); - - return tailNode; - } - - void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) { - if (out.prettyPrint() && ((siblingIndex() == 0 && parentNode instanceof Element && ((Element) parentNode).tag().formatAsBlock() && !isBlank()) || (out.outline() && siblingNodes().size()>0 && !isBlank()) )) - indent(accum, depth, out); - - boolean normaliseWhite = out.prettyPrint() && parent() instanceof Element - && !Element.preserveWhitespace((Element) parent()); - Entities.escape(accum, getWholeText(), out, false, normaliseWhite, false); - } - - void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {} - - @Override - public String toString() { - return outerHtml(); - } - - /** - * Create a new TextNode from HTML encoded (aka escaped) data. - * @param encodedText Text containing encoded HTML (e.g. &lt;) - * @param baseUri Base uri - * @return TextNode containing unencoded data (e.g. <) - */ - public static TextNode createFromEncoded(String encodedText, String baseUri) { - String text = Entities.unescape(encodedText); - return new TextNode(text, baseUri); - } - - static String normaliseWhitespace(String text) { - text = StringUtil.normaliseWhitespace(text); - return text; - } - - static String stripLeadingWhitespace(String text) { - return text.replaceFirst("^\\s+", ""); - } - - static boolean lastCharIsWhitespace(StringBuilder sb) { - return sb.length() != 0 && sb.charAt(sb.length() - 1) == ' '; - } - - // attribute fiddling. create on first access. - private void ensureAttributes() { - if (attributes == null) { - attributes = new Attributes(); - attributes.put(TEXT_KEY, text); - } - } - - @Override - public String attr(String attributeKey) { - ensureAttributes(); - return super.attr(attributeKey); - } - - @Override - public Attributes attributes() { - ensureAttributes(); - return super.attributes(); - } - - @Override - public Node attr(String attributeKey, String attributeValue) { - ensureAttributes(); - return super.attr(attributeKey, attributeValue); - } - - @Override - public boolean hasAttr(String attributeKey) { - ensureAttributes(); - return super.hasAttr(attributeKey); - } - - @Override - public Node removeAttr(String attributeKey) { - ensureAttributes(); - return super.removeAttr(attributeKey); - } - - @Override - public String absUrl(String attributeKey) { - ensureAttributes(); - return super.absUrl(attributeKey); - } -} diff --git a/src/org/jsoup/nodes/XmlDeclaration.java b/src/org/jsoup/nodes/XmlDeclaration.java deleted file mode 100644 index 6660593..0000000 --- a/src/org/jsoup/nodes/XmlDeclaration.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.jsoup.nodes; - -/** - An XML Declaration. - - @author Jonathan Hedley, jonathan@hedley.net */ -public class XmlDeclaration extends Node { - private static final String DECL_KEY = "declaration"; - private final boolean isProcessingInstruction; // "); - } - - void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {} - - @Override - public String toString() { - return outerHtml(); - } -} diff --git a/src/org/jsoup/nodes/entities-base.properties b/src/org/jsoup/nodes/entities-base.properties deleted file mode 100644 index 3d1d11e..0000000 --- a/src/org/jsoup/nodes/entities-base.properties +++ /dev/null @@ -1,106 +0,0 @@ -AElig=000C6 -AMP=00026 -Aacute=000C1 -Acirc=000C2 -Agrave=000C0 -Aring=000C5 -Atilde=000C3 -Auml=000C4 -COPY=000A9 -Ccedil=000C7 -ETH=000D0 -Eacute=000C9 -Ecirc=000CA -Egrave=000C8 -Euml=000CB -GT=0003E -Iacute=000CD -Icirc=000CE -Igrave=000CC -Iuml=000CF -LT=0003C -Ntilde=000D1 -Oacute=000D3 -Ocirc=000D4 -Ograve=000D2 -Oslash=000D8 -Otilde=000D5 -Ouml=000D6 -QUOT=00022 -REG=000AE -THORN=000DE -Uacute=000DA -Ucirc=000DB -Ugrave=000D9 -Uuml=000DC -Yacute=000DD -aacute=000E1 -acirc=000E2 -acute=000B4 -aelig=000E6 -agrave=000E0 -amp=00026 -aring=000E5 -atilde=000E3 -auml=000E4 -brvbar=000A6 -ccedil=000E7 -cedil=000B8 -cent=000A2 -copy=000A9 -curren=000A4 -deg=000B0 -divide=000F7 -eacute=000E9 -ecirc=000EA -egrave=000E8 -eth=000F0 -euml=000EB -frac12=000BD -frac14=000BC -frac34=000BE -gt=0003E -iacute=000ED -icirc=000EE -iexcl=000A1 -igrave=000EC -iquest=000BF -iuml=000EF -laquo=000AB -lt=0003C -macr=000AF -micro=000B5 -middot=000B7 -nbsp=000A0 -not=000AC -ntilde=000F1 -oacute=000F3 -ocirc=000F4 -ograve=000F2 -ordf=000AA -ordm=000BA -oslash=000F8 -otilde=000F5 -ouml=000F6 -para=000B6 -plusmn=000B1 -pound=000A3 -quot=00022 -raquo=000BB -reg=000AE -sect=000A7 -shy=000AD -sup1=000B9 -sup2=000B2 -sup3=000B3 -szlig=000DF -thorn=000FE -times=000D7 -uacute=000FA -ucirc=000FB -ugrave=000F9 -uml=000A8 -uuml=000FC -yacute=000FD -yen=000A5 -yuml=000FF diff --git a/src/org/jsoup/nodes/entities-full.properties b/src/org/jsoup/nodes/entities-full.properties deleted file mode 100644 index 92f124f..0000000 --- a/src/org/jsoup/nodes/entities-full.properties +++ /dev/null @@ -1,2032 +0,0 @@ -AElig=000C6 -AMP=00026 -Aacute=000C1 -Abreve=00102 -Acirc=000C2 -Acy=00410 -Afr=1D504 -Agrave=000C0 -Alpha=00391 -Amacr=00100 -And=02A53 -Aogon=00104 -Aopf=1D538 -ApplyFunction=02061 -Aring=000C5 -Ascr=1D49C -Assign=02254 -Atilde=000C3 -Auml=000C4 -Backslash=02216 -Barv=02AE7 -Barwed=02306 -Bcy=00411 -Because=02235 -Bernoullis=0212C -Beta=00392 -Bfr=1D505 -Bopf=1D539 -Breve=002D8 -Bscr=0212C -Bumpeq=0224E -CHcy=00427 -COPY=000A9 -Cacute=00106 -Cap=022D2 -CapitalDifferentialD=02145 -Cayleys=0212D -Ccaron=0010C -Ccedil=000C7 -Ccirc=00108 -Cconint=02230 -Cdot=0010A -Cedilla=000B8 -CenterDot=000B7 -Cfr=0212D -Chi=003A7 -CircleDot=02299 -CircleMinus=02296 -CirclePlus=02295 -CircleTimes=02297 -ClockwiseContourIntegral=02232 -CloseCurlyDoubleQuote=0201D -CloseCurlyQuote=02019 -Colon=02237 -Colone=02A74 -Congruent=02261 -Conint=0222F -ContourIntegral=0222E -Copf=02102 -Coproduct=02210 -CounterClockwiseContourIntegral=02233 -Cross=02A2F -Cscr=1D49E -Cup=022D3 -CupCap=0224D -DD=02145 -DDotrahd=02911 -DJcy=00402 -DScy=00405 -DZcy=0040F -Dagger=02021 -Darr=021A1 -Dashv=02AE4 -Dcaron=0010E -Dcy=00414 -Del=02207 -Delta=00394 -Dfr=1D507 -DiacriticalAcute=000B4 -DiacriticalDot=002D9 -DiacriticalDoubleAcute=002DD -DiacriticalGrave=00060 -DiacriticalTilde=002DC -Diamond=022C4 -DifferentialD=02146 -Dopf=1D53B -Dot=000A8 -DotDot=020DC -DotEqual=02250 -DoubleContourIntegral=0222F -DoubleDot=000A8 -DoubleDownArrow=021D3 -DoubleLeftArrow=021D0 -DoubleLeftRightArrow=021D4 -DoubleLeftTee=02AE4 -DoubleLongLeftArrow=027F8 -DoubleLongLeftRightArrow=027FA -DoubleLongRightArrow=027F9 -DoubleRightArrow=021D2 -DoubleRightTee=022A8 -DoubleUpArrow=021D1 -DoubleUpDownArrow=021D5 -DoubleVerticalBar=02225 -DownArrow=02193 -DownArrowBar=02913 -DownArrowUpArrow=021F5 -DownBreve=00311 -DownLeftRightVector=02950 -DownLeftTeeVector=0295E -DownLeftVector=021BD -DownLeftVectorBar=02956 -DownRightTeeVector=0295F -DownRightVector=021C1 -DownRightVectorBar=02957 -DownTee=022A4 -DownTeeArrow=021A7 -Downarrow=021D3 -Dscr=1D49F -Dstrok=00110 -ENG=0014A -ETH=000D0 -Eacute=000C9 -Ecaron=0011A -Ecirc=000CA -Ecy=0042D -Edot=00116 -Efr=1D508 -Egrave=000C8 -Element=02208 -Emacr=00112 -EmptySmallSquare=025FB -EmptyVerySmallSquare=025AB -Eogon=00118 -Eopf=1D53C -Epsilon=00395 -Equal=02A75 -EqualTilde=02242 -Equilibrium=021CC -Escr=02130 -Esim=02A73 -Eta=00397 -Euml=000CB -Exists=02203 -ExponentialE=02147 -Fcy=00424 -Ffr=1D509 -FilledSmallSquare=025FC -FilledVerySmallSquare=025AA -Fopf=1D53D -ForAll=02200 -Fouriertrf=02131 -Fscr=02131 -GJcy=00403 -GT=0003E -Gamma=00393 -Gammad=003DC -Gbreve=0011E -Gcedil=00122 -Gcirc=0011C -Gcy=00413 -Gdot=00120 -Gfr=1D50A -Gg=022D9 -Gopf=1D53E -GreaterEqual=02265 -GreaterEqualLess=022DB -GreaterFullEqual=02267 -GreaterGreater=02AA2 -GreaterLess=02277 -GreaterSlantEqual=02A7E -GreaterTilde=02273 -Gscr=1D4A2 -Gt=0226B -HARDcy=0042A -Hacek=002C7 -Hat=0005E -Hcirc=00124 -Hfr=0210C -HilbertSpace=0210B -Hopf=0210D -HorizontalLine=02500 -Hscr=0210B -Hstrok=00126 -HumpDownHump=0224E -HumpEqual=0224F -IEcy=00415 -IJlig=00132 -IOcy=00401 -Iacute=000CD -Icirc=000CE -Icy=00418 -Idot=00130 -Ifr=02111 -Igrave=000CC -Im=02111 -Imacr=0012A -ImaginaryI=02148 -Implies=021D2 -Int=0222C -Integral=0222B -Intersection=022C2 -InvisibleComma=02063 -InvisibleTimes=02062 -Iogon=0012E -Iopf=1D540 -Iota=00399 -Iscr=02110 -Itilde=00128 -Iukcy=00406 -Iuml=000CF -Jcirc=00134 -Jcy=00419 -Jfr=1D50D -Jopf=1D541 -Jscr=1D4A5 -Jsercy=00408 -Jukcy=00404 -KHcy=00425 -KJcy=0040C -Kappa=0039A -Kcedil=00136 -Kcy=0041A -Kfr=1D50E -Kopf=1D542 -Kscr=1D4A6 -LJcy=00409 -LT=0003C -Lacute=00139 -Lambda=0039B -Lang=027EA -Laplacetrf=02112 -Larr=0219E -Lcaron=0013D -Lcedil=0013B -Lcy=0041B -LeftAngleBracket=027E8 -LeftArrow=02190 -LeftArrowBar=021E4 -LeftArrowRightArrow=021C6 -LeftCeiling=02308 -LeftDoubleBracket=027E6 -LeftDownTeeVector=02961 -LeftDownVector=021C3 -LeftDownVectorBar=02959 -LeftFloor=0230A -LeftRightArrow=02194 -LeftRightVector=0294E -LeftTee=022A3 -LeftTeeArrow=021A4 -LeftTeeVector=0295A -LeftTriangle=022B2 -LeftTriangleBar=029CF -LeftTriangleEqual=022B4 -LeftUpDownVector=02951 -LeftUpTeeVector=02960 -LeftUpVector=021BF -LeftUpVectorBar=02958 -LeftVector=021BC -LeftVectorBar=02952 -Leftarrow=021D0 -Leftrightarrow=021D4 -LessEqualGreater=022DA -LessFullEqual=02266 -LessGreater=02276 -LessLess=02AA1 -LessSlantEqual=02A7D -LessTilde=02272 -Lfr=1D50F -Ll=022D8 -Lleftarrow=021DA -Lmidot=0013F -LongLeftArrow=027F5 -LongLeftRightArrow=027F7 -LongRightArrow=027F6 -Longleftarrow=027F8 -Longleftrightarrow=027FA -Longrightarrow=027F9 -Lopf=1D543 -LowerLeftArrow=02199 -LowerRightArrow=02198 -Lscr=02112 -Lsh=021B0 -Lstrok=00141 -Lt=0226A -Map=02905 -Mcy=0041C -MediumSpace=0205F -Mellintrf=02133 -Mfr=1D510 -MinusPlus=02213 -Mopf=1D544 -Mscr=02133 -Mu=0039C -NJcy=0040A -Nacute=00143 -Ncaron=00147 -Ncedil=00145 -Ncy=0041D -NegativeMediumSpace=0200B -NegativeThickSpace=0200B -NegativeThinSpace=0200B -NegativeVeryThinSpace=0200B -NestedGreaterGreater=0226B -NestedLessLess=0226A -NewLine=0000A -Nfr=1D511 -NoBreak=02060 -NonBreakingSpace=000A0 -Nopf=02115 -Not=02AEC -NotCongruent=02262 -NotCupCap=0226D -NotDoubleVerticalBar=02226 -NotElement=02209 -NotEqual=02260 -NotExists=02204 -NotGreater=0226F -NotGreaterEqual=02271 -NotGreaterLess=02279 -NotGreaterTilde=02275 -NotLeftTriangle=022EA -NotLeftTriangleEqual=022EC -NotLess=0226E -NotLessEqual=02270 -NotLessGreater=02278 -NotLessTilde=02274 -NotPrecedes=02280 -NotPrecedesSlantEqual=022E0 -NotReverseElement=0220C -NotRightTriangle=022EB -NotRightTriangleEqual=022ED -NotSquareSubsetEqual=022E2 -NotSquareSupersetEqual=022E3 -NotSubsetEqual=02288 -NotSucceeds=02281 -NotSucceedsSlantEqual=022E1 -NotSupersetEqual=02289 -NotTilde=02241 -NotTildeEqual=02244 -NotTildeFullEqual=02247 -NotTildeTilde=02249 -NotVerticalBar=02224 -Nscr=1D4A9 -Ntilde=000D1 -Nu=0039D -OElig=00152 -Oacute=000D3 -Ocirc=000D4 -Ocy=0041E -Odblac=00150 -Ofr=1D512 -Ograve=000D2 -Omacr=0014C -Omega=003A9 -Omicron=0039F -Oopf=1D546 -OpenCurlyDoubleQuote=0201C -OpenCurlyQuote=02018 -Or=02A54 -Oscr=1D4AA -Oslash=000D8 -Otilde=000D5 -Otimes=02A37 -Ouml=000D6 -OverBar=0203E -OverBrace=023DE -OverBracket=023B4 -OverParenthesis=023DC -PartialD=02202 -Pcy=0041F -Pfr=1D513 -Phi=003A6 -Pi=003A0 -PlusMinus=000B1 -Poincareplane=0210C -Popf=02119 -Pr=02ABB -Precedes=0227A -PrecedesEqual=02AAF -PrecedesSlantEqual=0227C -PrecedesTilde=0227E -Prime=02033 -Product=0220F -Proportion=02237 -Proportional=0221D -Pscr=1D4AB -Psi=003A8 -QUOT=00022 -Qfr=1D514 -Qopf=0211A -Qscr=1D4AC -RBarr=02910 -REG=000AE -Racute=00154 -Rang=027EB -Rarr=021A0 -Rarrtl=02916 -Rcaron=00158 -Rcedil=00156 -Rcy=00420 -Re=0211C -ReverseElement=0220B -ReverseEquilibrium=021CB -ReverseUpEquilibrium=0296F -Rfr=0211C -Rho=003A1 -RightAngleBracket=027E9 -RightArrow=02192 -RightArrowBar=021E5 -RightArrowLeftArrow=021C4 -RightCeiling=02309 -RightDoubleBracket=027E7 -RightDownTeeVector=0295D -RightDownVector=021C2 -RightDownVectorBar=02955 -RightFloor=0230B -RightTee=022A2 -RightTeeArrow=021A6 -RightTeeVector=0295B -RightTriangle=022B3 -RightTriangleBar=029D0 -RightTriangleEqual=022B5 -RightUpDownVector=0294F -RightUpTeeVector=0295C -RightUpVector=021BE -RightUpVectorBar=02954 -RightVector=021C0 -RightVectorBar=02953 -Rightarrow=021D2 -Ropf=0211D -RoundImplies=02970 -Rrightarrow=021DB -Rscr=0211B -Rsh=021B1 -RuleDelayed=029F4 -SHCHcy=00429 -SHcy=00428 -SOFTcy=0042C -Sacute=0015A -Sc=02ABC -Scaron=00160 -Scedil=0015E -Scirc=0015C -Scy=00421 -Sfr=1D516 -ShortDownArrow=02193 -ShortLeftArrow=02190 -ShortRightArrow=02192 -ShortUpArrow=02191 -Sigma=003A3 -SmallCircle=02218 -Sopf=1D54A -Sqrt=0221A -Square=025A1 -SquareIntersection=02293 -SquareSubset=0228F -SquareSubsetEqual=02291 -SquareSuperset=02290 -SquareSupersetEqual=02292 -SquareUnion=02294 -Sscr=1D4AE -Star=022C6 -Sub=022D0 -Subset=022D0 -SubsetEqual=02286 -Succeeds=0227B -SucceedsEqual=02AB0 -SucceedsSlantEqual=0227D -SucceedsTilde=0227F -SuchThat=0220B -Sum=02211 -Sup=022D1 -Superset=02283 -SupersetEqual=02287 -Supset=022D1 -THORN=000DE -TRADE=02122 -TSHcy=0040B -TScy=00426 -Tab=00009 -Tau=003A4 -Tcaron=00164 -Tcedil=00162 -Tcy=00422 -Tfr=1D517 -Therefore=02234 -Theta=00398 -ThinSpace=02009 -Tilde=0223C -TildeEqual=02243 -TildeFullEqual=02245 -TildeTilde=02248 -Topf=1D54B -TripleDot=020DB -Tscr=1D4AF -Tstrok=00166 -Uacute=000DA -Uarr=0219F -Uarrocir=02949 -Ubrcy=0040E -Ubreve=0016C -Ucirc=000DB -Ucy=00423 -Udblac=00170 -Ufr=1D518 -Ugrave=000D9 -Umacr=0016A -UnderBar=0005F -UnderBrace=023DF -UnderBracket=023B5 -UnderParenthesis=023DD -Union=022C3 -UnionPlus=0228E -Uogon=00172 -Uopf=1D54C -UpArrow=02191 -UpArrowBar=02912 -UpArrowDownArrow=021C5 -UpDownArrow=02195 -UpEquilibrium=0296E -UpTee=022A5 -UpTeeArrow=021A5 -Uparrow=021D1 -Updownarrow=021D5 -UpperLeftArrow=02196 -UpperRightArrow=02197 -Upsi=003D2 -Upsilon=003A5 -Uring=0016E -Uscr=1D4B0 -Utilde=00168 -Uuml=000DC -VDash=022AB -Vbar=02AEB -Vcy=00412 -Vdash=022A9 -Vdashl=02AE6 -Vee=022C1 -Verbar=02016 -Vert=02016 -VerticalBar=02223 -VerticalLine=0007C -VerticalSeparator=02758 -VerticalTilde=02240 -VeryThinSpace=0200A -Vfr=1D519 -Vopf=1D54D -Vscr=1D4B1 -Vvdash=022AA -Wcirc=00174 -Wedge=022C0 -Wfr=1D51A -Wopf=1D54E -Wscr=1D4B2 -Xfr=1D51B -Xi=0039E -Xopf=1D54F -Xscr=1D4B3 -YAcy=0042F -YIcy=00407 -YUcy=0042E -Yacute=000DD -Ycirc=00176 -Ycy=0042B -Yfr=1D51C -Yopf=1D550 -Yscr=1D4B4 -Yuml=00178 -ZHcy=00416 -Zacute=00179 -Zcaron=0017D -Zcy=00417 -Zdot=0017B -ZeroWidthSpace=0200B -Zeta=00396 -Zfr=02128 -Zopf=02124 -Zscr=1D4B5 -aacute=000E1 -abreve=00103 -ac=0223E -acd=0223F -acirc=000E2 -acute=000B4 -acy=00430 -aelig=000E6 -af=02061 -afr=1D51E -agrave=000E0 -alefsym=02135 -aleph=02135 -alpha=003B1 -amacr=00101 -amalg=02A3F -amp=00026 -and=02227 -andand=02A55 -andd=02A5C -andslope=02A58 -andv=02A5A -ang=02220 -ange=029A4 -angle=02220 -angmsd=02221 -angmsdaa=029A8 -angmsdab=029A9 -angmsdac=029AA -angmsdad=029AB -angmsdae=029AC -angmsdaf=029AD -angmsdag=029AE -angmsdah=029AF -angrt=0221F -angrtvb=022BE -angrtvbd=0299D -angsph=02222 -angst=000C5 -angzarr=0237C -aogon=00105 -aopf=1D552 -ap=02248 -apE=02A70 -apacir=02A6F -ape=0224A -apid=0224B -apos=00027 -approx=02248 -approxeq=0224A -aring=000E5 -ascr=1D4B6 -ast=0002A -asymp=02248 -asympeq=0224D -atilde=000E3 -auml=000E4 -awconint=02233 -awint=02A11 -bNot=02AED -backcong=0224C -backepsilon=003F6 -backprime=02035 -backsim=0223D -backsimeq=022CD -barvee=022BD -barwed=02305 -barwedge=02305 -bbrk=023B5 -bbrktbrk=023B6 -bcong=0224C -bcy=00431 -bdquo=0201E -becaus=02235 -because=02235 -bemptyv=029B0 -bepsi=003F6 -bernou=0212C -beta=003B2 -beth=02136 -between=0226C -bfr=1D51F -bigcap=022C2 -bigcirc=025EF -bigcup=022C3 -bigodot=02A00 -bigoplus=02A01 -bigotimes=02A02 -bigsqcup=02A06 -bigstar=02605 -bigtriangledown=025BD -bigtriangleup=025B3 -biguplus=02A04 -bigvee=022C1 -bigwedge=022C0 -bkarow=0290D -blacklozenge=029EB -blacksquare=025AA -blacktriangle=025B4 -blacktriangledown=025BE -blacktriangleleft=025C2 -blacktriangleright=025B8 -blank=02423 -blk12=02592 -blk14=02591 -blk34=02593 -block=02588 -bnot=02310 -bopf=1D553 -bot=022A5 -bottom=022A5 -bowtie=022C8 -boxDL=02557 -boxDR=02554 -boxDl=02556 -boxDr=02553 -boxH=02550 -boxHD=02566 -boxHU=02569 -boxHd=02564 -boxHu=02567 -boxUL=0255D -boxUR=0255A -boxUl=0255C -boxUr=02559 -boxV=02551 -boxVH=0256C -boxVL=02563 -boxVR=02560 -boxVh=0256B -boxVl=02562 -boxVr=0255F -boxbox=029C9 -boxdL=02555 -boxdR=02552 -boxdl=02510 -boxdr=0250C -boxh=02500 -boxhD=02565 -boxhU=02568 -boxhd=0252C -boxhu=02534 -boxminus=0229F -boxplus=0229E -boxtimes=022A0 -boxuL=0255B -boxuR=02558 -boxul=02518 -boxur=02514 -boxv=02502 -boxvH=0256A -boxvL=02561 -boxvR=0255E -boxvh=0253C -boxvl=02524 -boxvr=0251C -bprime=02035 -breve=002D8 -brvbar=000A6 -bscr=1D4B7 -bsemi=0204F -bsim=0223D -bsime=022CD -bsol=0005C -bsolb=029C5 -bsolhsub=027C8 -bull=02022 -bullet=02022 -bump=0224E -bumpE=02AAE -bumpe=0224F -bumpeq=0224F -cacute=00107 -cap=02229 -capand=02A44 -capbrcup=02A49 -capcap=02A4B -capcup=02A47 -capdot=02A40 -caret=02041 -caron=002C7 -ccaps=02A4D -ccaron=0010D -ccedil=000E7 -ccirc=00109 -ccups=02A4C -ccupssm=02A50 -cdot=0010B -cedil=000B8 -cemptyv=029B2 -cent=000A2 -centerdot=000B7 -cfr=1D520 -chcy=00447 -check=02713 -checkmark=02713 -chi=003C7 -cir=025CB -cirE=029C3 -circ=002C6 -circeq=02257 -circlearrowleft=021BA -circlearrowright=021BB -circledR=000AE -circledS=024C8 -circledast=0229B -circledcirc=0229A -circleddash=0229D -cire=02257 -cirfnint=02A10 -cirmid=02AEF -cirscir=029C2 -clubs=02663 -clubsuit=02663 -colon=0003A -colone=02254 -coloneq=02254 -comma=0002C -commat=00040 -comp=02201 -compfn=02218 -complement=02201 -complexes=02102 -cong=02245 -congdot=02A6D -conint=0222E -copf=1D554 -coprod=02210 -copy=000A9 -copysr=02117 -crarr=021B5 -cross=02717 -cscr=1D4B8 -csub=02ACF -csube=02AD1 -csup=02AD0 -csupe=02AD2 -ctdot=022EF -cudarrl=02938 -cudarrr=02935 -cuepr=022DE -cuesc=022DF -cularr=021B6 -cularrp=0293D -cup=0222A -cupbrcap=02A48 -cupcap=02A46 -cupcup=02A4A -cupdot=0228D -cupor=02A45 -curarr=021B7 -curarrm=0293C -curlyeqprec=022DE -curlyeqsucc=022DF -curlyvee=022CE -curlywedge=022CF -curren=000A4 -curvearrowleft=021B6 -curvearrowright=021B7 -cuvee=022CE -cuwed=022CF -cwconint=02232 -cwint=02231 -cylcty=0232D -dArr=021D3 -dHar=02965 -dagger=02020 -daleth=02138 -darr=02193 -dash=02010 -dashv=022A3 -dbkarow=0290F -dblac=002DD -dcaron=0010F -dcy=00434 -dd=02146 -ddagger=02021 -ddarr=021CA -ddotseq=02A77 -deg=000B0 -delta=003B4 -demptyv=029B1 -dfisht=0297F -dfr=1D521 -dharl=021C3 -dharr=021C2 -diam=022C4 -diamond=022C4 -diamondsuit=02666 -diams=02666 -die=000A8 -digamma=003DD -disin=022F2 -div=000F7 -divide=000F7 -divideontimes=022C7 -divonx=022C7 -djcy=00452 -dlcorn=0231E -dlcrop=0230D -dollar=00024 -dopf=1D555 -dot=002D9 -doteq=02250 -doteqdot=02251 -dotminus=02238 -dotplus=02214 -dotsquare=022A1 -doublebarwedge=02306 -downarrow=02193 -downdownarrows=021CA -downharpoonleft=021C3 -downharpoonright=021C2 -drbkarow=02910 -drcorn=0231F -drcrop=0230C -dscr=1D4B9 -dscy=00455 -dsol=029F6 -dstrok=00111 -dtdot=022F1 -dtri=025BF -dtrif=025BE -duarr=021F5 -duhar=0296F -dwangle=029A6 -dzcy=0045F -dzigrarr=027FF -eDDot=02A77 -eDot=02251 -eacute=000E9 -easter=02A6E -ecaron=0011B -ecir=02256 -ecirc=000EA -ecolon=02255 -ecy=0044D -edot=00117 -ee=02147 -efDot=02252 -efr=1D522 -eg=02A9A -egrave=000E8 -egs=02A96 -egsdot=02A98 -el=02A99 -elinters=023E7 -ell=02113 -els=02A95 -elsdot=02A97 -emacr=00113 -empty=02205 -emptyset=02205 -emptyv=02205 -emsp13=02004 -emsp14=02005 -emsp=02003 -eng=0014B -ensp=02002 -eogon=00119 -eopf=1D556 -epar=022D5 -eparsl=029E3 -eplus=02A71 -epsi=003B5 -epsilon=003B5 -epsiv=003F5 -eqcirc=02256 -eqcolon=02255 -eqsim=02242 -eqslantgtr=02A96 -eqslantless=02A95 -equals=0003D -equest=0225F -equiv=02261 -equivDD=02A78 -eqvparsl=029E5 -erDot=02253 -erarr=02971 -escr=0212F -esdot=02250 -esim=02242 -eta=003B7 -eth=000F0 -euml=000EB -euro=020AC -excl=00021 -exist=02203 -expectation=02130 -exponentiale=02147 -fallingdotseq=02252 -fcy=00444 -female=02640 -ffilig=0FB03 -fflig=0FB00 -ffllig=0FB04 -ffr=1D523 -filig=0FB01 -flat=0266D -fllig=0FB02 -fltns=025B1 -fnof=00192 -fopf=1D557 -forall=02200 -fork=022D4 -forkv=02AD9 -fpartint=02A0D -frac12=000BD -frac13=02153 -frac14=000BC -frac15=02155 -frac16=02159 -frac18=0215B -frac23=02154 -frac25=02156 -frac34=000BE -frac35=02157 -frac38=0215C -frac45=02158 -frac56=0215A -frac58=0215D -frac78=0215E -frasl=02044 -frown=02322 -fscr=1D4BB -gE=02267 -gEl=02A8C -gacute=001F5 -gamma=003B3 -gammad=003DD -gap=02A86 -gbreve=0011F -gcirc=0011D -gcy=00433 -gdot=00121 -ge=02265 -gel=022DB -geq=02265 -geqq=02267 -geqslant=02A7E -ges=02A7E -gescc=02AA9 -gesdot=02A80 -gesdoto=02A82 -gesdotol=02A84 -gesles=02A94 -gfr=1D524 -gg=0226B -ggg=022D9 -gimel=02137 -gjcy=00453 -gl=02277 -glE=02A92 -gla=02AA5 -glj=02AA4 -gnE=02269 -gnap=02A8A -gnapprox=02A8A -gne=02A88 -gneq=02A88 -gneqq=02269 -gnsim=022E7 -gopf=1D558 -grave=00060 -gscr=0210A -gsim=02273 -gsime=02A8E -gsiml=02A90 -gt=0003E -gtcc=02AA7 -gtcir=02A7A -gtdot=022D7 -gtlPar=02995 -gtquest=02A7C -gtrapprox=02A86 -gtrarr=02978 -gtrdot=022D7 -gtreqless=022DB -gtreqqless=02A8C -gtrless=02277 -gtrsim=02273 -hArr=021D4 -hairsp=0200A -half=000BD -hamilt=0210B -hardcy=0044A -harr=02194 -harrcir=02948 -harrw=021AD -hbar=0210F -hcirc=00125 -hearts=02665 -heartsuit=02665 -hellip=02026 -hercon=022B9 -hfr=1D525 -hksearow=02925 -hkswarow=02926 -hoarr=021FF -homtht=0223B -hookleftarrow=021A9 -hookrightarrow=021AA -hopf=1D559 -horbar=02015 -hscr=1D4BD -hslash=0210F -hstrok=00127 -hybull=02043 -hyphen=02010 -iacute=000ED -ic=02063 -icirc=000EE -icy=00438 -iecy=00435 -iexcl=000A1 -iff=021D4 -ifr=1D526 -igrave=000EC -ii=02148 -iiiint=02A0C -iiint=0222D -iinfin=029DC -iiota=02129 -ijlig=00133 -imacr=0012B -image=02111 -imagline=02110 -imagpart=02111 -imath=00131 -imof=022B7 -imped=001B5 -in=02208 -incare=02105 -infin=0221E -infintie=029DD -inodot=00131 -int=0222B -intcal=022BA -integers=02124 -intercal=022BA -intlarhk=02A17 -intprod=02A3C -iocy=00451 -iogon=0012F -iopf=1D55A -iota=003B9 -iprod=02A3C -iquest=000BF -iscr=1D4BE -isin=02208 -isinE=022F9 -isindot=022F5 -isins=022F4 -isinsv=022F3 -isinv=02208 -it=02062 -itilde=00129 -iukcy=00456 -iuml=000EF -jcirc=00135 -jcy=00439 -jfr=1D527 -jmath=00237 -jopf=1D55B -jscr=1D4BF -jsercy=00458 -jukcy=00454 -kappa=003BA -kappav=003F0 -kcedil=00137 -kcy=0043A -kfr=1D528 -kgreen=00138 -khcy=00445 -kjcy=0045C -kopf=1D55C -kscr=1D4C0 -lAarr=021DA -lArr=021D0 -lAtail=0291B -lBarr=0290E -lE=02266 -lEg=02A8B -lHar=02962 -lacute=0013A -laemptyv=029B4 -lagran=02112 -lambda=003BB -lang=027E8 -langd=02991 -langle=027E8 -lap=02A85 -laquo=000AB -larr=02190 -larrb=021E4 -larrbfs=0291F -larrfs=0291D -larrhk=021A9 -larrlp=021AB -larrpl=02939 -larrsim=02973 -larrtl=021A2 -lat=02AAB -latail=02919 -late=02AAD -lbarr=0290C -lbbrk=02772 -lbrace=0007B -lbrack=0005B -lbrke=0298B -lbrksld=0298F -lbrkslu=0298D -lcaron=0013E -lcedil=0013C -lceil=02308 -lcub=0007B -lcy=0043B -ldca=02936 -ldquo=0201C -ldquor=0201E -ldrdhar=02967 -ldrushar=0294B -ldsh=021B2 -le=02264 -leftarrow=02190 -leftarrowtail=021A2 -leftharpoondown=021BD -leftharpoonup=021BC -leftleftarrows=021C7 -leftrightarrow=02194 -leftrightarrows=021C6 -leftrightharpoons=021CB -leftrightsquigarrow=021AD -leftthreetimes=022CB -leg=022DA -leq=02264 -leqq=02266 -leqslant=02A7D -les=02A7D -lescc=02AA8 -lesdot=02A7F -lesdoto=02A81 -lesdotor=02A83 -lesges=02A93 -lessapprox=02A85 -lessdot=022D6 -lesseqgtr=022DA -lesseqqgtr=02A8B -lessgtr=02276 -lesssim=02272 -lfisht=0297C -lfloor=0230A -lfr=1D529 -lg=02276 -lgE=02A91 -lhard=021BD -lharu=021BC -lharul=0296A -lhblk=02584 -ljcy=00459 -ll=0226A -llarr=021C7 -llcorner=0231E -llhard=0296B -lltri=025FA -lmidot=00140 -lmoust=023B0 -lmoustache=023B0 -lnE=02268 -lnap=02A89 -lnapprox=02A89 -lne=02A87 -lneq=02A87 -lneqq=02268 -lnsim=022E6 -loang=027EC -loarr=021FD -lobrk=027E6 -longleftarrow=027F5 -longleftrightarrow=027F7 -longmapsto=027FC -longrightarrow=027F6 -looparrowleft=021AB -looparrowright=021AC -lopar=02985 -lopf=1D55D -loplus=02A2D -lotimes=02A34 -lowast=02217 -lowbar=0005F -loz=025CA -lozenge=025CA -lozf=029EB -lpar=00028 -lparlt=02993 -lrarr=021C6 -lrcorner=0231F -lrhar=021CB -lrhard=0296D -lrm=0200E -lrtri=022BF -lsaquo=02039 -lscr=1D4C1 -lsh=021B0 -lsim=02272 -lsime=02A8D -lsimg=02A8F -lsqb=0005B -lsquo=02018 -lsquor=0201A -lstrok=00142 -lt=0003C -ltcc=02AA6 -ltcir=02A79 -ltdot=022D6 -lthree=022CB -ltimes=022C9 -ltlarr=02976 -ltquest=02A7B -ltrPar=02996 -ltri=025C3 -ltrie=022B4 -ltrif=025C2 -lurdshar=0294A -luruhar=02966 -mDDot=0223A -macr=000AF -male=02642 -malt=02720 -maltese=02720 -map=021A6 -mapsto=021A6 -mapstodown=021A7 -mapstoleft=021A4 -mapstoup=021A5 -marker=025AE -mcomma=02A29 -mcy=0043C -mdash=02014 -measuredangle=02221 -mfr=1D52A -mho=02127 -micro=000B5 -mid=02223 -midast=0002A -midcir=02AF0 -middot=000B7 -minus=02212 -minusb=0229F -minusd=02238 -minusdu=02A2A -mlcp=02ADB -mldr=02026 -mnplus=02213 -models=022A7 -mopf=1D55E -mp=02213 -mscr=1D4C2 -mstpos=0223E -mu=003BC -multimap=022B8 -mumap=022B8 -nLeftarrow=021CD -nLeftrightarrow=021CE -nRightarrow=021CF -nVDash=022AF -nVdash=022AE -nabla=02207 -nacute=00144 -nap=02249 -napos=00149 -napprox=02249 -natur=0266E -natural=0266E -naturals=02115 -nbsp=000A0 -ncap=02A43 -ncaron=00148 -ncedil=00146 -ncong=02247 -ncup=02A42 -ncy=0043D -ndash=02013 -ne=02260 -neArr=021D7 -nearhk=02924 -nearr=02197 -nearrow=02197 -nequiv=02262 -nesear=02928 -nexist=02204 -nexists=02204 -nfr=1D52B -nge=02271 -ngeq=02271 -ngsim=02275 -ngt=0226F -ngtr=0226F -nhArr=021CE -nharr=021AE -nhpar=02AF2 -ni=0220B -nis=022FC -nisd=022FA -niv=0220B -njcy=0045A -nlArr=021CD -nlarr=0219A -nldr=02025 -nle=02270 -nleftarrow=0219A -nleftrightarrow=021AE -nleq=02270 -nless=0226E -nlsim=02274 -nlt=0226E -nltri=022EA -nltrie=022EC -nmid=02224 -nopf=1D55F -not=000AC -notin=02209 -notinva=02209 -notinvb=022F7 -notinvc=022F6 -notni=0220C -notniva=0220C -notnivb=022FE -notnivc=022FD -npar=02226 -nparallel=02226 -npolint=02A14 -npr=02280 -nprcue=022E0 -nprec=02280 -nrArr=021CF -nrarr=0219B -nrightarrow=0219B -nrtri=022EB -nrtrie=022ED -nsc=02281 -nsccue=022E1 -nscr=1D4C3 -nshortmid=02224 -nshortparallel=02226 -nsim=02241 -nsime=02244 -nsimeq=02244 -nsmid=02224 -nspar=02226 -nsqsube=022E2 -nsqsupe=022E3 -nsub=02284 -nsube=02288 -nsubseteq=02288 -nsucc=02281 -nsup=02285 -nsupe=02289 -nsupseteq=02289 -ntgl=02279 -ntilde=000F1 -ntlg=02278 -ntriangleleft=022EA -ntrianglelefteq=022EC -ntriangleright=022EB -ntrianglerighteq=022ED -nu=003BD -num=00023 -numero=02116 -numsp=02007 -nvDash=022AD -nvHarr=02904 -nvdash=022AC -nvinfin=029DE -nvlArr=02902 -nvrArr=02903 -nwArr=021D6 -nwarhk=02923 -nwarr=02196 -nwarrow=02196 -nwnear=02927 -oS=024C8 -oacute=000F3 -oast=0229B -ocir=0229A -ocirc=000F4 -ocy=0043E -odash=0229D -odblac=00151 -odiv=02A38 -odot=02299 -odsold=029BC -oelig=00153 -ofcir=029BF -ofr=1D52C -ogon=002DB -ograve=000F2 -ogt=029C1 -ohbar=029B5 -ohm=003A9 -oint=0222E -olarr=021BA -olcir=029BE -olcross=029BB -oline=0203E -olt=029C0 -omacr=0014D -omega=003C9 -omicron=003BF -omid=029B6 -ominus=02296 -oopf=1D560 -opar=029B7 -operp=029B9 -oplus=02295 -or=02228 -orarr=021BB -ord=02A5D -order=02134 -orderof=02134 -ordf=000AA -ordm=000BA -origof=022B6 -oror=02A56 -orslope=02A57 -orv=02A5B -oscr=02134 -oslash=000F8 -osol=02298 -otilde=000F5 -otimes=02297 -otimesas=02A36 -ouml=000F6 -ovbar=0233D -par=02225 -para=000B6 -parallel=02225 -parsim=02AF3 -parsl=02AFD -part=02202 -pcy=0043F -percnt=00025 -period=0002E -permil=02030 -perp=022A5 -pertenk=02031 -pfr=1D52D -phi=003C6 -phiv=003D5 -phmmat=02133 -phone=0260E -pi=003C0 -pitchfork=022D4 -piv=003D6 -planck=0210F -planckh=0210E -plankv=0210F -plus=0002B -plusacir=02A23 -plusb=0229E -pluscir=02A22 -plusdo=02214 -plusdu=02A25 -pluse=02A72 -plusmn=000B1 -plussim=02A26 -plustwo=02A27 -pm=000B1 -pointint=02A15 -popf=1D561 -pound=000A3 -pr=0227A -prE=02AB3 -prap=02AB7 -prcue=0227C -pre=02AAF -prec=0227A -precapprox=02AB7 -preccurlyeq=0227C -preceq=02AAF -precnapprox=02AB9 -precneqq=02AB5 -precnsim=022E8 -precsim=0227E -prime=02032 -primes=02119 -prnE=02AB5 -prnap=02AB9 -prnsim=022E8 -prod=0220F -profalar=0232E -profline=02312 -profsurf=02313 -prop=0221D -propto=0221D -prsim=0227E -prurel=022B0 -pscr=1D4C5 -psi=003C8 -puncsp=02008 -qfr=1D52E -qint=02A0C -qopf=1D562 -qprime=02057 -qscr=1D4C6 -quaternions=0210D -quatint=02A16 -quest=0003F -questeq=0225F -quot=00022 -rAarr=021DB -rArr=021D2 -rAtail=0291C -rBarr=0290F -rHar=02964 -racute=00155 -radic=0221A -raemptyv=029B3 -rang=027E9 -rangd=02992 -range=029A5 -rangle=027E9 -raquo=000BB -rarr=02192 -rarrap=02975 -rarrb=021E5 -rarrbfs=02920 -rarrc=02933 -rarrfs=0291E -rarrhk=021AA -rarrlp=021AC -rarrpl=02945 -rarrsim=02974 -rarrtl=021A3 -rarrw=0219D -ratail=0291A -ratio=02236 -rationals=0211A -rbarr=0290D -rbbrk=02773 -rbrace=0007D -rbrack=0005D -rbrke=0298C -rbrksld=0298E -rbrkslu=02990 -rcaron=00159 -rcedil=00157 -rceil=02309 -rcub=0007D -rcy=00440 -rdca=02937 -rdldhar=02969 -rdquo=0201D -rdquor=0201D -rdsh=021B3 -real=0211C -realine=0211B -realpart=0211C -reals=0211D -rect=025AD -reg=000AE -rfisht=0297D -rfloor=0230B -rfr=1D52F -rhard=021C1 -rharu=021C0 -rharul=0296C -rho=003C1 -rhov=003F1 -rightarrow=02192 -rightarrowtail=021A3 -rightharpoondown=021C1 -rightharpoonup=021C0 -rightleftarrows=021C4 -rightleftharpoons=021CC -rightrightarrows=021C9 -rightsquigarrow=0219D -rightthreetimes=022CC -ring=002DA -risingdotseq=02253 -rlarr=021C4 -rlhar=021CC -rlm=0200F -rmoust=023B1 -rmoustache=023B1 -rnmid=02AEE -roang=027ED -roarr=021FE -robrk=027E7 -ropar=02986 -ropf=1D563 -roplus=02A2E -rotimes=02A35 -rpar=00029 -rpargt=02994 -rppolint=02A12 -rrarr=021C9 -rsaquo=0203A -rscr=1D4C7 -rsh=021B1 -rsqb=0005D -rsquo=02019 -rsquor=02019 -rthree=022CC -rtimes=022CA -rtri=025B9 -rtrie=022B5 -rtrif=025B8 -rtriltri=029CE -ruluhar=02968 -rx=0211E -sacute=0015B -sbquo=0201A -sc=0227B -scE=02AB4 -scap=02AB8 -scaron=00161 -sccue=0227D -sce=02AB0 -scedil=0015F -scirc=0015D -scnE=02AB6 -scnap=02ABA -scnsim=022E9 -scpolint=02A13 -scsim=0227F -scy=00441 -sdot=022C5 -sdotb=022A1 -sdote=02A66 -seArr=021D8 -searhk=02925 -searr=02198 -searrow=02198 -sect=000A7 -semi=0003B -seswar=02929 -setminus=02216 -setmn=02216 -sext=02736 -sfr=1D530 -sfrown=02322 -sharp=0266F -shchcy=00449 -shcy=00448 -shortmid=02223 -shortparallel=02225 -shy=000AD -sigma=003C3 -sigmaf=003C2 -sigmav=003C2 -sim=0223C -simdot=02A6A -sime=02243 -simeq=02243 -simg=02A9E -simgE=02AA0 -siml=02A9D -simlE=02A9F -simne=02246 -simplus=02A24 -simrarr=02972 -slarr=02190 -smallsetminus=02216 -smashp=02A33 -smeparsl=029E4 -smid=02223 -smile=02323 -smt=02AAA -smte=02AAC -softcy=0044C -sol=0002F -solb=029C4 -solbar=0233F -sopf=1D564 -spades=02660 -spadesuit=02660 -spar=02225 -sqcap=02293 -sqcup=02294 -sqsub=0228F -sqsube=02291 -sqsubset=0228F -sqsubseteq=02291 -sqsup=02290 -sqsupe=02292 -sqsupset=02290 -sqsupseteq=02292 -squ=025A1 -square=025A1 -squarf=025AA -squf=025AA -srarr=02192 -sscr=1D4C8 -ssetmn=02216 -ssmile=02323 -sstarf=022C6 -star=02606 -starf=02605 -straightepsilon=003F5 -straightphi=003D5 -strns=000AF -sub=02282 -subE=02AC5 -subdot=02ABD -sube=02286 -subedot=02AC3 -submult=02AC1 -subnE=02ACB -subne=0228A -subplus=02ABF -subrarr=02979 -subset=02282 -subseteq=02286 -subseteqq=02AC5 -subsetneq=0228A -subsetneqq=02ACB -subsim=02AC7 -subsub=02AD5 -subsup=02AD3 -succ=0227B -succapprox=02AB8 -succcurlyeq=0227D -succeq=02AB0 -succnapprox=02ABA -succneqq=02AB6 -succnsim=022E9 -succsim=0227F -sum=02211 -sung=0266A -sup1=000B9 -sup2=000B2 -sup3=000B3 -sup=02283 -supE=02AC6 -supdot=02ABE -supdsub=02AD8 -supe=02287 -supedot=02AC4 -suphsol=027C9 -suphsub=02AD7 -suplarr=0297B -supmult=02AC2 -supnE=02ACC -supne=0228B -supplus=02AC0 -supset=02283 -supseteq=02287 -supseteqq=02AC6 -supsetneq=0228B -supsetneqq=02ACC -supsim=02AC8 -supsub=02AD4 -supsup=02AD6 -swArr=021D9 -swarhk=02926 -swarr=02199 -swarrow=02199 -swnwar=0292A -szlig=000DF -target=02316 -tau=003C4 -tbrk=023B4 -tcaron=00165 -tcedil=00163 -tcy=00442 -tdot=020DB -telrec=02315 -tfr=1D531 -there4=02234 -therefore=02234 -theta=003B8 -thetasym=003D1 -thetav=003D1 -thickapprox=02248 -thicksim=0223C -thinsp=02009 -thkap=02248 -thksim=0223C -thorn=000FE -tilde=002DC -times=000D7 -timesb=022A0 -timesbar=02A31 -timesd=02A30 -tint=0222D -toea=02928 -top=022A4 -topbot=02336 -topcir=02AF1 -topf=1D565 -topfork=02ADA -tosa=02929 -tprime=02034 -trade=02122 -triangle=025B5 -triangledown=025BF -triangleleft=025C3 -trianglelefteq=022B4 -triangleq=0225C -triangleright=025B9 -trianglerighteq=022B5 -tridot=025EC -trie=0225C -triminus=02A3A -triplus=02A39 -trisb=029CD -tritime=02A3B -trpezium=023E2 -tscr=1D4C9 -tscy=00446 -tshcy=0045B -tstrok=00167 -twixt=0226C -twoheadleftarrow=0219E -twoheadrightarrow=021A0 -uArr=021D1 -uHar=02963 -uacute=000FA -uarr=02191 -ubrcy=0045E -ubreve=0016D -ucirc=000FB -ucy=00443 -udarr=021C5 -udblac=00171 -udhar=0296E -ufisht=0297E -ufr=1D532 -ugrave=000F9 -uharl=021BF -uharr=021BE -uhblk=02580 -ulcorn=0231C -ulcorner=0231C -ulcrop=0230F -ultri=025F8 -umacr=0016B -uml=000A8 -uogon=00173 -uopf=1D566 -uparrow=02191 -updownarrow=02195 -upharpoonleft=021BF -upharpoonright=021BE -uplus=0228E -upsi=003C5 -upsih=003D2 -upsilon=003C5 -upuparrows=021C8 -urcorn=0231D -urcorner=0231D -urcrop=0230E -uring=0016F -urtri=025F9 -uscr=1D4CA -utdot=022F0 -utilde=00169 -utri=025B5 -utrif=025B4 -uuarr=021C8 -uuml=000FC -uwangle=029A7 -vArr=021D5 -vBar=02AE8 -vBarv=02AE9 -vDash=022A8 -vangrt=0299C -varepsilon=003F5 -varkappa=003F0 -varnothing=02205 -varphi=003D5 -varpi=003D6 -varpropto=0221D -varr=02195 -varrho=003F1 -varsigma=003C2 -vartheta=003D1 -vartriangleleft=022B2 -vartriangleright=022B3 -vcy=00432 -vdash=022A2 -vee=02228 -veebar=022BB -veeeq=0225A -vellip=022EE -verbar=0007C -vert=0007C -vfr=1D533 -vltri=022B2 -vopf=1D567 -vprop=0221D -vrtri=022B3 -vscr=1D4CB -vzigzag=0299A -wcirc=00175 -wedbar=02A5F -wedge=02227 -wedgeq=02259 -weierp=02118 -wfr=1D534 -wopf=1D568 -wp=02118 -wr=02240 -wreath=02240 -wscr=1D4CC -xcap=022C2 -xcirc=025EF -xcup=022C3 -xdtri=025BD -xfr=1D535 -xhArr=027FA -xharr=027F7 -xi=003BE -xlArr=027F8 -xlarr=027F5 -xmap=027FC -xnis=022FB -xodot=02A00 -xopf=1D569 -xoplus=02A01 -xotime=02A02 -xrArr=027F9 -xrarr=027F6 -xscr=1D4CD -xsqcup=02A06 -xuplus=02A04 -xutri=025B3 -xvee=022C1 -xwedge=022C0 -yacute=000FD -yacy=0044F -ycirc=00177 -ycy=0044B -yen=000A5 -yfr=1D536 -yicy=00457 -yopf=1D56A -yscr=1D4CE -yucy=0044E -yuml=000FF -zacute=0017A -zcaron=0017E -zcy=00437 -zdot=0017C -zeetrf=02128 -zeta=003B6 -zfr=1D537 -zhcy=00436 -zigrarr=021DD -zopf=1D56B -zscr=1D4CF -zwj=0200D -zwnj=0200C diff --git a/src/org/jsoup/nodes/package-info.java b/src/org/jsoup/nodes/package-info.java deleted file mode 100644 index 24b1280..0000000 --- a/src/org/jsoup/nodes/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - HTML document structure nodes. - */ -package org.jsoup.nodes; \ No newline at end of file diff --git a/src/org/jsoup/package-info.java b/src/org/jsoup/package-info.java deleted file mode 100644 index 4952611..0000000 --- a/src/org/jsoup/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - Contains the main {@link org.jsoup.Jsoup} class, which provides convenient static access to the jsoup functionality. - */ -package org.jsoup; \ No newline at end of file diff --git a/src/org/jsoup/parser/CharacterReader.java b/src/org/jsoup/parser/CharacterReader.java deleted file mode 100644 index 03a1ec9..0000000 --- a/src/org/jsoup/parser/CharacterReader.java +++ /dev/null @@ -1,284 +0,0 @@ -package org.jsoup.parser; - -import org.jsoup.helper.Validate; - -import java.util.Locale; - -/** - CharacterReader consumes tokens off a string. To replace the old TokenQueue. - */ -class CharacterReader { - static final char EOF = (char) -1; - - private final char[] input; - private final int length; - private int pos = 0; - private int mark = 0; - - CharacterReader(String input) { - Validate.notNull(input); - this.input = input.toCharArray(); - this.length = this.input.length; - } - - int pos() { - return pos; - } - - boolean isEmpty() { - return pos >= length; - } - - char current() { - return pos >= length ? EOF : input[pos]; - } - - char consume() { - char val = pos >= length ? EOF : input[pos]; - pos++; - return val; - } - - void unconsume() { - pos--; - } - - void advance() { - pos++; - } - - void mark() { - mark = pos; - } - - void rewindToMark() { - pos = mark; - } - - String consumeAsString() { - return new String(input, pos++, 1); - } - - /** - * Returns the number of characters between the current position and the next instance of the input char - * @param c scan target - * @return offset between current position and next instance of target. -1 if not found. - */ - int nextIndexOf(char c) { - // doesn't handle scanning for surrogates - for (int i = pos; i < length; i++) { - if (c == input[i]) - return i - pos; - } - return -1; - } - - /** - * Returns the number of characters between the current position and the next instance of the input sequence - * - * @param seq scan target - * @return offset between current position and next instance of target. -1 if not found. - */ - int nextIndexOf(CharSequence seq) { - // doesn't handle scanning for surrogates - char startChar = seq.charAt(0); - for (int offset = pos; offset < length; offset++) { - // scan to first instance of startchar: - if (startChar != input[offset]) - while(++offset < length && startChar != input[offset]) { /* empty */ } - int i = offset + 1; - int last = i + seq.length()-1; - if (offset < length && last <= length) { - for (int j = 1; i < last && seq.charAt(j) == input[i]; i++, j++) { /* empty */ } - if (i == last) // found full sequence - return offset - pos; - } - } - return -1; - } - - String consumeTo(char c) { - int offset = nextIndexOf(c); - if (offset != -1) { - String consumed = new String(input, pos, offset); - pos += offset; - return consumed; - } else { - return consumeToEnd(); - } - } - - String consumeTo(String seq) { - int offset = nextIndexOf(seq); - if (offset != -1) { - String consumed = new String(input, pos, offset); - pos += offset; - return consumed; - } else { - return consumeToEnd(); - } - } - - String consumeToAny(final char... chars) { - int start = pos; - - OUTER: while (pos < length) { - for (int i = 0; i < chars.length; i++) { - if (input[pos] == chars[i]) - break OUTER; - } - pos++; - } - - return pos > start ? new String(input, start, pos-start) : ""; - } - - String consumeToEnd() { - String data = new String(input, pos, length-pos); - pos = length; - return data; - } - - String consumeLetterSequence() { - int start = pos; - while (pos < length) { - char c = input[pos]; - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) - pos++; - else - break; - } - - return new String(input, start, pos - start); - } - - String consumeLetterThenDigitSequence() { - int start = pos; - while (pos < length) { - char c = input[pos]; - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) - pos++; - else - break; - } - while (!isEmpty()) { - char c = input[pos]; - if (c >= '0' && c <= '9') - pos++; - else - break; - } - - return new String(input, start, pos - start); - } - - String consumeHexSequence() { - int start = pos; - while (pos < length) { - char c = input[pos]; - if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) - pos++; - else - break; - } - return new String(input, start, pos - start); - } - - String consumeDigitSequence() { - int start = pos; - while (pos < length) { - char c = input[pos]; - if (c >= '0' && c <= '9') - pos++; - else - break; - } - return new String(input, start, pos - start); - } - - boolean matches(char c) { - return !isEmpty() && input[pos] == c; - - } - - boolean matches(String seq) { - int scanLength = seq.length(); - if (scanLength > length - pos) - return false; - - for (int offset = 0; offset < scanLength; offset++) - if (seq.charAt(offset) != input[pos+offset]) - return false; - return true; - } - - boolean matchesIgnoreCase(String seq) { - int scanLength = seq.length(); - if (scanLength > length - pos) - return false; - - for (int offset = 0; offset < scanLength; offset++) { - char upScan = Character.toUpperCase(seq.charAt(offset)); - char upTarget = Character.toUpperCase(input[pos + offset]); - if (upScan != upTarget) - return false; - } - return true; - } - - boolean matchesAny(char... seq) { - if (isEmpty()) - return false; - - char c = input[pos]; - for (char seek : seq) { - if (seek == c) - return true; - } - return false; - } - - boolean matchesLetter() { - if (isEmpty()) - return false; - char c = input[pos]; - return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); - } - - boolean matchesDigit() { - if (isEmpty()) - return false; - char c = input[pos]; - return (c >= '0' && c <= '9'); - } - - boolean matchConsume(String seq) { - if (matches(seq)) { - pos += seq.length(); - return true; - } else { - return false; - } - } - - boolean matchConsumeIgnoreCase(String seq) { - if (matchesIgnoreCase(seq)) { - pos += seq.length(); - return true; - } else { - return false; - } - } - - boolean containsIgnoreCase(String seq) { - // used to check presence of , . only finds consistent case. - String loScan = seq.toLowerCase(Locale.ENGLISH); - String hiScan = seq.toUpperCase(Locale.ENGLISH); - return (nextIndexOf(loScan) > -1) || (nextIndexOf(hiScan) > -1); - } - - @Override - public String toString() { - return new String(input, pos, length - pos); - } -} diff --git a/src/org/jsoup/parser/HtmlTreeBuilder.java b/src/org/jsoup/parser/HtmlTreeBuilder.java deleted file mode 100644 index ffd4aca..0000000 --- a/src/org/jsoup/parser/HtmlTreeBuilder.java +++ /dev/null @@ -1,716 +0,0 @@ -package org.jsoup.parser; - -import org.jsoup.helper.DescendableLinkedList; -import org.jsoup.helper.StringUtil; -import org.jsoup.helper.Validate; -import org.jsoup.nodes.*; -import org.jsoup.select.Elements; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -/** - * HTML Tree Builder; creates a DOM from Tokens. - */ -class HtmlTreeBuilder extends TreeBuilder { - // tag searches - private static final String[] TagsScriptStyle = new String[]{"script", "style"}; - public static final String[] TagsSearchInScope = new String[]{"applet", "caption", "html", "table", "td", "th", "marquee", "object"}; - private static final String[] TagSearchList = new String[]{"ol", "ul"}; - private static final String[] TagSearchButton = new String[]{"button"}; - private static final String[] TagSearchTableScope = new String[]{"html", "table"}; - private static final String[] TagSearchSelectScope = new String[]{"optgroup", "option"}; - private static final String[] TagSearchEndTags = new String[]{"dd", "dt", "li", "option", "optgroup", "p", "rp", "rt"}; - private static final String[] TagSearchSpecial = new String[]{"address", "applet", "area", "article", "aside", "base", "basefont", "bgsound", - "blockquote", "body", "br", "button", "caption", "center", "col", "colgroup", "command", "dd", - "details", "dir", "div", "dl", "dt", "embed", "fieldset", "figcaption", "figure", "footer", "form", - "frame", "frameset", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html", - "iframe", "img", "input", "isindex", "li", "link", "listing", "marquee", "menu", "meta", "nav", - "noembed", "noframes", "noscript", "object", "ol", "p", "param", "plaintext", "pre", "script", - "section", "select", "style", "summary", "table", "tbody", "td", "textarea", "tfoot", "th", "thead", - "title", "tr", "ul", "wbr", "xmp"}; - - private HtmlTreeBuilderState state; // the current state - private HtmlTreeBuilderState originalState; // original / marked state - - private boolean baseUriSetFromDoc = false; - private Element headElement; // the current head element - private FormElement formElement; // the current form element - private Element contextElement; // fragment parse context -- could be null even if fragment parsing - private DescendableLinkedList formattingElements = new DescendableLinkedList(); // active (open) formatting elements - private List pendingTableCharacters = new ArrayList(); // chars in table to be shifted out - - private boolean framesetOk = true; // if ok to go into frameset - private boolean fosterInserts = false; // if next inserts should be fostered - private boolean fragmentParsing = false; // if parsing a fragment of html - - HtmlTreeBuilder() {} - - @Override - Document parse(String input, String baseUri, ParseErrorList errors) { - state = HtmlTreeBuilderState.Initial; - baseUriSetFromDoc = false; - return super.parse(input, baseUri, errors); - } - - List parseFragment(String inputFragment, Element context, String baseUri, ParseErrorList errors) { - // context may be null - state = HtmlTreeBuilderState.Initial; - initialiseParse(inputFragment, baseUri, errors); - contextElement = context; - fragmentParsing = true; - Element root = null; - - if (context != null) { - if (context.ownerDocument() != null) // quirks setup: - doc.quirksMode(context.ownerDocument().quirksMode()); - - // initialise the tokeniser state: - String contextTag = context.tagName(); - if (StringUtil.in(contextTag, "title", "textarea")) - tokeniser.transition(TokeniserState.Rcdata); - else if (StringUtil.in(contextTag, "iframe", "noembed", "noframes", "style", "xmp")) - tokeniser.transition(TokeniserState.Rawtext); - else if (contextTag.equals("script")) - tokeniser.transition(TokeniserState.ScriptData); - else if (contextTag.equals(("noscript"))) - tokeniser.transition(TokeniserState.Data); // if scripting enabled, rawtext - else if (contextTag.equals("plaintext")) - tokeniser.transition(TokeniserState.Data); - else - tokeniser.transition(TokeniserState.Data); // default - - root = new Element(Tag.valueOf("html"), baseUri); - doc.appendChild(root); - stack.push(root); - resetInsertionMode(); - - // setup form element to nearest form on context (up ancestor chain). ensures form controls are associated - // with form correctly - Elements contextChain = context.parents(); - contextChain.add(0, context); - for (Element parent: contextChain) { - if (parent instanceof FormElement) { - formElement = (FormElement) parent; - break; - } - } - } - - runParser(); - if (context != null && root != null) - return root.childNodes(); - else - return doc.childNodes(); - } - - @Override - protected boolean process(Token token) { - currentToken = token; - return this.state.process(token, this); - } - - boolean process(Token token, HtmlTreeBuilderState state) { - currentToken = token; - return state.process(token, this); - } - - void transition(HtmlTreeBuilderState state) { - this.state = state; - } - - HtmlTreeBuilderState state() { - return state; - } - - void markInsertionMode() { - originalState = state; - } - - HtmlTreeBuilderState originalState() { - return originalState; - } - - void framesetOk(boolean framesetOk) { - this.framesetOk = framesetOk; - } - - boolean framesetOk() { - return framesetOk; - } - - Document getDocument() { - return doc; - } - - String getBaseUri() { - return baseUri; - } - - void maybeSetBaseUri(Element base) { - if (baseUriSetFromDoc) // only listen to the first in parse - return; - - String href = base.absUrl("href"); - if (href.length() != 0) { // ignore etc - baseUri = href; - baseUriSetFromDoc = true; - doc.setBaseUri(href); // set on the doc so doc.createElement(Tag) will get updated base, and to update all descendants - } - } - - boolean isFragmentParsing() { - return fragmentParsing; - } - - void error(HtmlTreeBuilderState state) { - if (errors.canAddError()) - errors.add(new ParseError(reader.pos(), "Unexpected token [%s] when in state [%s]", currentToken.tokenType(), state)); - } - - Element insert(Token.StartTag startTag) { - // handle empty unknown tags - // when the spec expects an empty tag, will directly hit insertEmpty, so won't generate this fake end tag. - if (startTag.isSelfClosing()) { - Element el = insertEmpty(startTag); - stack.add(el); - tokeniser.transition(TokeniserState.Data); // handles - - Token.Tag tagPending; // tag we are building up - Token.Doctype doctypePending; // doctype building up - Token.Comment commentPending; // comment building up - private Token.StartTag lastStartTag; // the last start tag emitted, to test appropriate end tag - private boolean selfClosingFlagAcknowledged = true; - - Tokeniser(CharacterReader reader, ParseErrorList errors) { - this.reader = reader; - this.errors = errors; - } - - Token read() { - if (!selfClosingFlagAcknowledged) { - error("Self closing flag not acknowledged"); - selfClosingFlagAcknowledged = true; - } - - while (!isEmitPending) - state.read(this, reader); - - // if emit is pending, a non-character token was found: return any chars in buffer, and leave token for next read: - if (charBuffer.length() > 0) { - String str = charBuffer.toString(); - charBuffer.delete(0, charBuffer.length()); - return new Token.Character(str); - } else { - isEmitPending = false; - return emitPending; - } - } - - void emit(Token token) { - Validate.isFalse(isEmitPending, "There is an unread token pending!"); - - emitPending = token; - isEmitPending = true; - - if (token.type == Token.TokenType.StartTag) { - Token.StartTag startTag = (Token.StartTag) token; - lastStartTag = startTag; - if (startTag.selfClosing) - selfClosingFlagAcknowledged = false; - } else if (token.type == Token.TokenType.EndTag) { - Token.EndTag endTag = (Token.EndTag) token; - if (endTag.attributes != null) - error("Attributes incorrectly present on end tag"); - } - } - - void emit(String str) { - // buffer strings up until last string token found, to emit only one token for a run of character refs etc. - // does not set isEmitPending; read checks that - charBuffer.append(str); - } - - void emit(char[] chars) { - charBuffer.append(chars); - } - - void emit(char c) { - charBuffer.append(c); - } - - TokeniserState getState() { - return state; - } - - void transition(TokeniserState state) { - this.state = state; - } - - void advanceTransition(TokeniserState state) { - reader.advance(); - this.state = state; - } - - void acknowledgeSelfClosingFlag() { - selfClosingFlagAcknowledged = true; - } - - char[] consumeCharacterReference(Character additionalAllowedCharacter, boolean inAttribute) { - if (reader.isEmpty()) - return null; - if (additionalAllowedCharacter != null && additionalAllowedCharacter == reader.current()) - return null; - if (reader.matchesAny('\t', '\n', '\r', '\f', ' ', '<', '&')) - return null; - - reader.mark(); - if (reader.matchConsume("#")) { // numbered - boolean isHexMode = reader.matchConsumeIgnoreCase("X"); - String numRef = isHexMode ? reader.consumeHexSequence() : reader.consumeDigitSequence(); - if (numRef.length() == 0) { // didn't match anything - characterReferenceError("numeric reference with no numerals"); - reader.rewindToMark(); - return null; - } - if (!reader.matchConsume(";")) - characterReferenceError("missing semicolon"); // missing semi - int charval = -1; - try { - int base = isHexMode ? 16 : 10; - charval = Integer.valueOf(numRef, base); - } catch (NumberFormatException e) { - } // skip - if (charval == -1 || (charval >= 0xD800 && charval <= 0xDFFF) || charval > 0x10FFFF) { - characterReferenceError("character outside of valid range"); - return new char[]{replacementChar}; - } else { - // todo: implement number replacement table - // todo: check for extra illegal unicode points as parse errors - return Character.toChars(charval); - } - } else { // named - // get as many letters as possible, and look for matching entities. - String nameRef = reader.consumeLetterThenDigitSequence(); - boolean looksLegit = reader.matches(';'); - // found if a base named entity without a ;, or an extended entity with the ;. - boolean found = (Entities.isBaseNamedEntity(nameRef) || (Entities.isNamedEntity(nameRef) && looksLegit)); - - if (!found) { - reader.rewindToMark(); - if (looksLegit) // named with semicolon - characterReferenceError(String.format("invalid named referenece '%s'", nameRef)); - return null; - } - if (inAttribute && (reader.matchesLetter() || reader.matchesDigit() || reader.matchesAny('=', '-', '_'))) { - // don't want that to match - reader.rewindToMark(); - return null; - } - if (!reader.matchConsume(";")) - characterReferenceError("missing semicolon"); // missing semi - return new char[]{Entities.getCharacterByName(nameRef)}; - } - } - - Token.Tag createTagPending(boolean start) { - tagPending = start ? new Token.StartTag() : new Token.EndTag(); - return tagPending; - } - - void emitTagPending() { - tagPending.finaliseTag(); - emit(tagPending); - } - - void createCommentPending() { - commentPending = new Token.Comment(); - } - - void emitCommentPending() { - emit(commentPending); - } - - void createDoctypePending() { - doctypePending = new Token.Doctype(); - } - - void emitDoctypePending() { - emit(doctypePending); - } - - void createTempBuffer() { - dataBuffer = new StringBuilder(); - } - - boolean isAppropriateEndTagToken() { - if (lastStartTag == null) - return false; - return tagPending.tagName.equals(lastStartTag.tagName); - } - - String appropriateEndTagName() { - if (lastStartTag == null) - return null; - return lastStartTag.tagName; - } - - void error(TokeniserState state) { - if (errors.canAddError()) - errors.add(new ParseError(reader.pos(), "Unexpected character '%s' in input state [%s]", reader.current(), state)); - } - - void eofError(TokeniserState state) { - if (errors.canAddError()) - errors.add(new ParseError(reader.pos(), "Unexpectedly reached end of file (EOF) in input state [%s]", state)); - } - - private void characterReferenceError(String message) { - if (errors.canAddError()) - errors.add(new ParseError(reader.pos(), "Invalid character reference: %s", message)); - } - - private void error(String errorMsg) { - if (errors.canAddError()) - errors.add(new ParseError(reader.pos(), errorMsg)); - } - - boolean currentNodeInHtmlNS() { - // todo: implement namespaces correctly - return true; - // Element currentNode = currentNode(); - // return currentNode != null && currentNode.namespace().equals("HTML"); - } - - /** - * Utility method to consume reader and unescape entities found within. - * @param inAttribute - * @return unescaped string from reader - */ - String unescapeEntities(boolean inAttribute) { - StringBuilder builder = new StringBuilder(); - while (!reader.isEmpty()) { - builder.append(reader.consumeTo('&')); - if (reader.matches('&')) { - reader.consume(); - char[] c = consumeCharacterReference(null, inAttribute); - if (c == null || c.length==0) - builder.append('&'); - else - builder.append(c); - } - } - return builder.toString(); - } -} diff --git a/src/org/jsoup/parser/TokeniserState.java b/src/org/jsoup/parser/TokeniserState.java deleted file mode 100644 index 786e84a..0000000 --- a/src/org/jsoup/parser/TokeniserState.java +++ /dev/null @@ -1,1725 +0,0 @@ -package org.jsoup.parser; - -/** - * States and transition activations for the Tokeniser. - */ -enum TokeniserState { - Data { - // in data state, gather characters until a character reference or tag is found - void read(Tokeniser t, CharacterReader r) { - switch (r.current()) { - case '&': - t.advanceTransition(CharacterReferenceInData); - break; - case '<': - t.advanceTransition(TagOpen); - break; - case nullChar: - t.error(this); // NOT replacement character (oddly?) - t.emit(r.consume()); - break; - case eof: - t.emit(new Token.EOF()); - break; - default: - String data = r.consumeToAny('&', '<', nullChar); - t.emit(data); - break; - } - } - }, - CharacterReferenceInData { - // from & in data - void read(Tokeniser t, CharacterReader r) { - char[] c = t.consumeCharacterReference(null, false); - if (c == null) - t.emit('&'); - else - t.emit(c); - t.transition(Data); - } - }, - Rcdata { - /// handles data in title, textarea etc - void read(Tokeniser t, CharacterReader r) { - switch (r.current()) { - case '&': - t.advanceTransition(CharacterReferenceInRcdata); - break; - case '<': - t.advanceTransition(RcdataLessthanSign); - break; - case nullChar: - t.error(this); - r.advance(); - t.emit(replacementChar); - break; - case eof: - t.emit(new Token.EOF()); - break; - default: - String data = r.consumeToAny('&', '<', nullChar); - t.emit(data); - break; - } - } - }, - CharacterReferenceInRcdata { - void read(Tokeniser t, CharacterReader r) { - char[] c = t.consumeCharacterReference(null, false); - if (c == null) - t.emit('&'); - else - t.emit(c); - t.transition(Rcdata); - } - }, - Rawtext { - void read(Tokeniser t, CharacterReader r) { - switch (r.current()) { - case '<': - t.advanceTransition(RawtextLessthanSign); - break; - case nullChar: - t.error(this); - r.advance(); - t.emit(replacementChar); - break; - case eof: - t.emit(new Token.EOF()); - break; - default: - String data = r.consumeToAny('<', nullChar); - t.emit(data); - break; - } - } - }, - ScriptData { - void read(Tokeniser t, CharacterReader r) { - switch (r.current()) { - case '<': - t.advanceTransition(ScriptDataLessthanSign); - break; - case nullChar: - t.error(this); - r.advance(); - t.emit(replacementChar); - break; - case eof: - t.emit(new Token.EOF()); - break; - default: - String data = r.consumeToAny('<', nullChar); - t.emit(data); - break; - } - } - }, - PLAINTEXT { - void read(Tokeniser t, CharacterReader r) { - switch (r.current()) { - case nullChar: - t.error(this); - r.advance(); - t.emit(replacementChar); - break; - case eof: - t.emit(new Token.EOF()); - break; - default: - String data = r.consumeTo(nullChar); - t.emit(data); - break; - } - } - }, - TagOpen { - // from < in data - void read(Tokeniser t, CharacterReader r) { - switch (r.current()) { - case '!': - t.advanceTransition(MarkupDeclarationOpen); - break; - case '/': - t.advanceTransition(EndTagOpen); - break; - case '?': - t.advanceTransition(BogusComment); - break; - default: - if (r.matchesLetter()) { - t.createTagPending(true); - t.transition(TagName); - } else { - t.error(this); - t.emit('<'); // char that got us here - t.transition(Data); - } - break; - } - } - }, - EndTagOpen { - void read(Tokeniser t, CharacterReader r) { - if (r.isEmpty()) { - t.eofError(this); - t.emit("')) { - t.error(this); - t.advanceTransition(Data); - } else { - t.error(this); - t.advanceTransition(BogusComment); - } - } - }, - TagName { - // from < or ', nullChar).toLowerCase(); - t.tagPending.appendTagName(tagName); - - switch (r.consume()) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeAttributeName); - break; - case '/': - t.transition(SelfClosingStartTag); - break; - case '>': - t.emitTagPending(); - t.transition(Data); - break; - case nullChar: // replacement - t.tagPending.appendTagName(replacementStr); - break; - case eof: // should emit pending tag? - t.eofError(this); - t.transition(Data); - // no default, as covered with above consumeToAny - } - } - }, - RcdataLessthanSign { - // from < in rcdata - void read(Tokeniser t, CharacterReader r) { - if (r.matches('/')) { - t.createTempBuffer(); - t.advanceTransition(RCDATAEndTagOpen); - } else if (r.matchesLetter() && t.appropriateEndTagName() != null && !r.containsIgnoreCase("), so rather than - // consuming to EOF; break out here - t.tagPending = new Token.EndTag(t.appropriateEndTagName()); - t.emitTagPending(); - r.unconsume(); // undo "<" - t.transition(Data); - } else { - t.emit("<"); - t.transition(Rcdata); - } - } - }, - RCDATAEndTagOpen { - void read(Tokeniser t, CharacterReader r) { - if (r.matchesLetter()) { - t.createTagPending(false); - t.tagPending.appendTagName(Character.toLowerCase(r.current())); - t.dataBuffer.append(Character.toLowerCase(r.current())); - t.advanceTransition(RCDATAEndTagName); - } else { - t.emit("': - if (t.isAppropriateEndTagToken()) { - t.emitTagPending(); - t.transition(Data); - } - else - anythingElse(t, r); - break; - default: - anythingElse(t, r); - } - } - - private void anythingElse(Tokeniser t, CharacterReader r) { - t.emit("': - t.emit(c); - t.transition(ScriptData); - break; - case nullChar: - t.error(this); - t.emit(replacementChar); - t.transition(ScriptDataEscaped); - break; - default: - t.emit(c); - t.transition(ScriptDataEscaped); - } - } - }, - ScriptDataEscapedLessthanSign { - void read(Tokeniser t, CharacterReader r) { - if (r.matchesLetter()) { - t.createTempBuffer(); - t.dataBuffer.append(Character.toLowerCase(r.current())); - t.emit("<" + r.current()); - t.advanceTransition(ScriptDataDoubleEscapeStart); - } else if (r.matches('/')) { - t.createTempBuffer(); - t.advanceTransition(ScriptDataEscapedEndTagOpen); - } else { - t.emit('<'); - t.transition(ScriptDataEscaped); - } - } - }, - ScriptDataEscapedEndTagOpen { - void read(Tokeniser t, CharacterReader r) { - if (r.matchesLetter()) { - t.createTagPending(false); - t.tagPending.appendTagName(Character.toLowerCase(r.current())); - t.dataBuffer.append(r.current()); - t.advanceTransition(ScriptDataEscapedEndTagName); - } else { - t.emit("': - t.emit(c); - t.transition(ScriptData); - break; - case nullChar: - t.error(this); - t.emit(replacementChar); - t.transition(ScriptDataDoubleEscaped); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - default: - t.emit(c); - t.transition(ScriptDataDoubleEscaped); - } - } - }, - ScriptDataDoubleEscapedLessthanSign { - void read(Tokeniser t, CharacterReader r) { - if (r.matches('/')) { - t.emit('/'); - t.createTempBuffer(); - t.advanceTransition(ScriptDataDoubleEscapeEnd); - } else { - t.transition(ScriptDataDoubleEscaped); - } - } - }, - ScriptDataDoubleEscapeEnd { - void read(Tokeniser t, CharacterReader r) { - handleDataDoubleEscapeTag(t,r, ScriptDataEscaped, ScriptDataDoubleEscaped); - } - }, - BeforeAttributeName { - // from tagname ': - t.emitTagPending(); - t.transition(Data); - break; - case nullChar: - t.error(this); - t.tagPending.newAttribute(); - r.unconsume(); - t.transition(AttributeName); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - case '"': - case '\'': - case '<': - case '=': - t.error(this); - t.tagPending.newAttribute(); - t.tagPending.appendAttributeName(c); - t.transition(AttributeName); - break; - default: // A-Z, anything else - t.tagPending.newAttribute(); - r.unconsume(); - t.transition(AttributeName); - } - } - }, - AttributeName { - // from before attribute name - void read(Tokeniser t, CharacterReader r) { - String name = r.consumeToAny('\t', '\n', '\r', '\f', ' ', '/', '=', '>', nullChar, '"', '\'', '<'); - t.tagPending.appendAttributeName(name.toLowerCase()); - - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(AfterAttributeName); - break; - case '/': - t.transition(SelfClosingStartTag); - break; - case '=': - t.transition(BeforeAttributeValue); - break; - case '>': - t.emitTagPending(); - t.transition(Data); - break; - case nullChar: - t.error(this); - t.tagPending.appendAttributeName(replacementChar); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - case '"': - case '\'': - case '<': - t.error(this); - t.tagPending.appendAttributeName(c); - // no default, as covered in consumeToAny - } - } - }, - AfterAttributeName { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - // ignore - break; - case '/': - t.transition(SelfClosingStartTag); - break; - case '=': - t.transition(BeforeAttributeValue); - break; - case '>': - t.emitTagPending(); - t.transition(Data); - break; - case nullChar: - t.error(this); - t.tagPending.appendAttributeName(replacementChar); - t.transition(AttributeName); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - case '"': - case '\'': - case '<': - t.error(this); - t.tagPending.newAttribute(); - t.tagPending.appendAttributeName(c); - t.transition(AttributeName); - break; - default: // A-Z, anything else - t.tagPending.newAttribute(); - r.unconsume(); - t.transition(AttributeName); - } - } - }, - BeforeAttributeValue { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - // ignore - break; - case '"': - t.transition(AttributeValue_doubleQuoted); - break; - case '&': - r.unconsume(); - t.transition(AttributeValue_unquoted); - break; - case '\'': - t.transition(AttributeValue_singleQuoted); - break; - case nullChar: - t.error(this); - t.tagPending.appendAttributeValue(replacementChar); - t.transition(AttributeValue_unquoted); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - case '>': - t.error(this); - t.emitTagPending(); - t.transition(Data); - break; - case '<': - case '=': - case '`': - t.error(this); - t.tagPending.appendAttributeValue(c); - t.transition(AttributeValue_unquoted); - break; - default: - r.unconsume(); - t.transition(AttributeValue_unquoted); - } - } - }, - AttributeValue_doubleQuoted { - void read(Tokeniser t, CharacterReader r) { - String value = r.consumeToAny('"', '&', nullChar); - if (value.length() > 0) - t.tagPending.appendAttributeValue(value); - - char c = r.consume(); - switch (c) { - case '"': - t.transition(AfterAttributeValue_quoted); - break; - case '&': - char[] ref = t.consumeCharacterReference('"', true); - if (ref != null) - t.tagPending.appendAttributeValue(ref); - else - t.tagPending.appendAttributeValue('&'); - break; - case nullChar: - t.error(this); - t.tagPending.appendAttributeValue(replacementChar); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - // no default, handled in consume to any above - } - } - }, - AttributeValue_singleQuoted { - void read(Tokeniser t, CharacterReader r) { - String value = r.consumeToAny('\'', '&', nullChar); - if (value.length() > 0) - t.tagPending.appendAttributeValue(value); - - char c = r.consume(); - switch (c) { - case '\'': - t.transition(AfterAttributeValue_quoted); - break; - case '&': - char[] ref = t.consumeCharacterReference('\'', true); - if (ref != null) - t.tagPending.appendAttributeValue(ref); - else - t.tagPending.appendAttributeValue('&'); - break; - case nullChar: - t.error(this); - t.tagPending.appendAttributeValue(replacementChar); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - // no default, handled in consume to any above - } - } - }, - AttributeValue_unquoted { - void read(Tokeniser t, CharacterReader r) { - String value = r.consumeToAny('\t', '\n', '\r', '\f', ' ', '&', '>', nullChar, '"', '\'', '<', '=', '`'); - if (value.length() > 0) - t.tagPending.appendAttributeValue(value); - - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeAttributeName); - break; - case '&': - char[] ref = t.consumeCharacterReference('>', true); - if (ref != null) - t.tagPending.appendAttributeValue(ref); - else - t.tagPending.appendAttributeValue('&'); - break; - case '>': - t.emitTagPending(); - t.transition(Data); - break; - case nullChar: - t.error(this); - t.tagPending.appendAttributeValue(replacementChar); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - case '"': - case '\'': - case '<': - case '=': - case '`': - t.error(this); - t.tagPending.appendAttributeValue(c); - break; - // no default, handled in consume to any above - } - - } - }, - // CharacterReferenceInAttributeValue state handled inline - AfterAttributeValue_quoted { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeAttributeName); - break; - case '/': - t.transition(SelfClosingStartTag); - break; - case '>': - t.emitTagPending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - default: - t.error(this); - r.unconsume(); - t.transition(BeforeAttributeName); - } - - } - }, - SelfClosingStartTag { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '>': - t.tagPending.selfClosing = true; - t.emitTagPending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.transition(Data); - break; - default: - t.error(this); - t.transition(BeforeAttributeName); - } - } - }, - BogusComment { - void read(Tokeniser t, CharacterReader r) { - // todo: handle bogus comment starting from eof. when does that trigger? - // rewind to capture character that lead us here - r.unconsume(); - Token.Comment comment = new Token.Comment(); - comment.bogus = true; - comment.data.append(r.consumeTo('>')); - // todo: replace nullChar with replaceChar - t.emit(comment); - t.advanceTransition(Data); - } - }, - MarkupDeclarationOpen { - void read(Tokeniser t, CharacterReader r) { - if (r.matchConsume("--")) { - t.createCommentPending(); - t.transition(CommentStart); - } else if (r.matchConsumeIgnoreCase("DOCTYPE")) { - t.transition(Doctype); - } else if (r.matchConsume("[CDATA[")) { - // todo: should actually check current namepspace, and only non-html allows cdata. until namespace - // is implemented properly, keep handling as cdata - //} else if (!t.currentNodeInHtmlNS() && r.matchConsume("[CDATA[")) { - t.transition(CdataSection); - } else { - t.error(this); - t.advanceTransition(BogusComment); // advance so this character gets in bogus comment data's rewind - } - } - }, - CommentStart { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '-': - t.transition(CommentStartDash); - break; - case nullChar: - t.error(this); - t.commentPending.data.append(replacementChar); - t.transition(Comment); - break; - case '>': - t.error(this); - t.emitCommentPending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.emitCommentPending(); - t.transition(Data); - break; - default: - t.commentPending.data.append(c); - t.transition(Comment); - } - } - }, - CommentStartDash { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '-': - t.transition(CommentStartDash); - break; - case nullChar: - t.error(this); - t.commentPending.data.append(replacementChar); - t.transition(Comment); - break; - case '>': - t.error(this); - t.emitCommentPending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.emitCommentPending(); - t.transition(Data); - break; - default: - t.commentPending.data.append(c); - t.transition(Comment); - } - } - }, - Comment { - void read(Tokeniser t, CharacterReader r) { - char c = r.current(); - switch (c) { - case '-': - t.advanceTransition(CommentEndDash); - break; - case nullChar: - t.error(this); - r.advance(); - t.commentPending.data.append(replacementChar); - break; - case eof: - t.eofError(this); - t.emitCommentPending(); - t.transition(Data); - break; - default: - t.commentPending.data.append(r.consumeToAny('-', nullChar)); - } - } - }, - CommentEndDash { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '-': - t.transition(CommentEnd); - break; - case nullChar: - t.error(this); - t.commentPending.data.append('-').append(replacementChar); - t.transition(Comment); - break; - case eof: - t.eofError(this); - t.emitCommentPending(); - t.transition(Data); - break; - default: - t.commentPending.data.append('-').append(c); - t.transition(Comment); - } - } - }, - CommentEnd { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '>': - t.emitCommentPending(); - t.transition(Data); - break; - case nullChar: - t.error(this); - t.commentPending.data.append("--").append(replacementChar); - t.transition(Comment); - break; - case '!': - t.error(this); - t.transition(CommentEndBang); - break; - case '-': - t.error(this); - t.commentPending.data.append('-'); - break; - case eof: - t.eofError(this); - t.emitCommentPending(); - t.transition(Data); - break; - default: - t.error(this); - t.commentPending.data.append("--").append(c); - t.transition(Comment); - } - } - }, - CommentEndBang { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '-': - t.commentPending.data.append("--!"); - t.transition(CommentEndDash); - break; - case '>': - t.emitCommentPending(); - t.transition(Data); - break; - case nullChar: - t.error(this); - t.commentPending.data.append("--!").append(replacementChar); - t.transition(Comment); - break; - case eof: - t.eofError(this); - t.emitCommentPending(); - t.transition(Data); - break; - default: - t.commentPending.data.append("--!").append(c); - t.transition(Comment); - } - } - }, - Doctype { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeDoctypeName); - break; - case eof: - t.eofError(this); - // note: fall through to > case - case '>': // catch invalid - t.error(this); - t.createDoctypePending(); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.transition(BeforeDoctypeName); - } - } - }, - BeforeDoctypeName { - void read(Tokeniser t, CharacterReader r) { - if (r.matchesLetter()) { - t.createDoctypePending(); - t.transition(DoctypeName); - return; - } - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - break; // ignore whitespace - case nullChar: - t.error(this); - t.createDoctypePending(); - t.doctypePending.name.append(replacementChar); - t.transition(DoctypeName); - break; - case eof: - t.eofError(this); - t.createDoctypePending(); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.createDoctypePending(); - t.doctypePending.name.append(c); - t.transition(DoctypeName); - } - } - }, - DoctypeName { - void read(Tokeniser t, CharacterReader r) { - if (r.matchesLetter()) { - String name = r.consumeLetterSequence(); - t.doctypePending.name.append(name.toLowerCase()); - return; - } - char c = r.consume(); - switch (c) { - case '>': - t.emitDoctypePending(); - t.transition(Data); - break; - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(AfterDoctypeName); - break; - case nullChar: - t.error(this); - t.doctypePending.name.append(replacementChar); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.doctypePending.name.append(c); - } - } - }, - AfterDoctypeName { - void read(Tokeniser t, CharacterReader r) { - if (r.isEmpty()) { - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - return; - } - if (r.matchesAny('\t', '\n', '\r', '\f', ' ')) - r.advance(); // ignore whitespace - else if (r.matches('>')) { - t.emitDoctypePending(); - t.advanceTransition(Data); - } else if (r.matchConsumeIgnoreCase("PUBLIC")) { - t.transition(AfterDoctypePublicKeyword); - } else if (r.matchConsumeIgnoreCase("SYSTEM")) { - t.transition(AfterDoctypeSystemKeyword); - } else { - t.error(this); - t.doctypePending.forceQuirks = true; - t.advanceTransition(BogusDoctype); - } - - } - }, - AfterDoctypePublicKeyword { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeDoctypePublicIdentifier); - break; - case '"': - t.error(this); - // set public id to empty string - t.transition(DoctypePublicIdentifier_doubleQuoted); - break; - case '\'': - t.error(this); - // set public id to empty string - t.transition(DoctypePublicIdentifier_singleQuoted); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.doctypePending.forceQuirks = true; - t.transition(BogusDoctype); - } - } - }, - BeforeDoctypePublicIdentifier { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - break; - case '"': - // set public id to empty string - t.transition(DoctypePublicIdentifier_doubleQuoted); - break; - case '\'': - // set public id to empty string - t.transition(DoctypePublicIdentifier_singleQuoted); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.doctypePending.forceQuirks = true; - t.transition(BogusDoctype); - } - } - }, - DoctypePublicIdentifier_doubleQuoted { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '"': - t.transition(AfterDoctypePublicIdentifier); - break; - case nullChar: - t.error(this); - t.doctypePending.publicIdentifier.append(replacementChar); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.doctypePending.publicIdentifier.append(c); - } - } - }, - DoctypePublicIdentifier_singleQuoted { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\'': - t.transition(AfterDoctypePublicIdentifier); - break; - case nullChar: - t.error(this); - t.doctypePending.publicIdentifier.append(replacementChar); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.doctypePending.publicIdentifier.append(c); - } - } - }, - AfterDoctypePublicIdentifier { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BetweenDoctypePublicAndSystemIdentifiers); - break; - case '>': - t.emitDoctypePending(); - t.transition(Data); - break; - case '"': - t.error(this); - // system id empty - t.transition(DoctypeSystemIdentifier_doubleQuoted); - break; - case '\'': - t.error(this); - // system id empty - t.transition(DoctypeSystemIdentifier_singleQuoted); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.doctypePending.forceQuirks = true; - t.transition(BogusDoctype); - } - } - }, - BetweenDoctypePublicAndSystemIdentifiers { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - break; - case '>': - t.emitDoctypePending(); - t.transition(Data); - break; - case '"': - t.error(this); - // system id empty - t.transition(DoctypeSystemIdentifier_doubleQuoted); - break; - case '\'': - t.error(this); - // system id empty - t.transition(DoctypeSystemIdentifier_singleQuoted); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.doctypePending.forceQuirks = true; - t.transition(BogusDoctype); - } - } - }, - AfterDoctypeSystemKeyword { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeDoctypeSystemIdentifier); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case '"': - t.error(this); - // system id empty - t.transition(DoctypeSystemIdentifier_doubleQuoted); - break; - case '\'': - t.error(this); - // system id empty - t.transition(DoctypeSystemIdentifier_singleQuoted); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - } - } - }, - BeforeDoctypeSystemIdentifier { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - break; - case '"': - // set system id to empty string - t.transition(DoctypeSystemIdentifier_doubleQuoted); - break; - case '\'': - // set public id to empty string - t.transition(DoctypeSystemIdentifier_singleQuoted); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.doctypePending.forceQuirks = true; - t.transition(BogusDoctype); - } - } - }, - DoctypeSystemIdentifier_doubleQuoted { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '"': - t.transition(AfterDoctypeSystemIdentifier); - break; - case nullChar: - t.error(this); - t.doctypePending.systemIdentifier.append(replacementChar); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.doctypePending.systemIdentifier.append(c); - } - } - }, - DoctypeSystemIdentifier_singleQuoted { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\'': - t.transition(AfterDoctypeSystemIdentifier); - break; - case nullChar: - t.error(this); - t.doctypePending.systemIdentifier.append(replacementChar); - break; - case '>': - t.error(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.doctypePending.systemIdentifier.append(c); - } - } - }, - AfterDoctypeSystemIdentifier { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - break; - case '>': - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.eofError(this); - t.doctypePending.forceQuirks = true; - t.emitDoctypePending(); - t.transition(Data); - break; - default: - t.error(this); - t.transition(BogusDoctype); - // NOT force quirks - } - } - }, - BogusDoctype { - void read(Tokeniser t, CharacterReader r) { - char c = r.consume(); - switch (c) { - case '>': - t.emitDoctypePending(); - t.transition(Data); - break; - case eof: - t.emitDoctypePending(); - t.transition(Data); - break; - default: - // ignore char - break; - } - } - }, - CdataSection { - void read(Tokeniser t, CharacterReader r) { - String data = r.consumeTo("]]>"); - t.emit(data); - r.matchConsume("]]>"); - t.transition(Data); - } - }; - - - abstract void read(Tokeniser t, CharacterReader r); - - private static final char nullChar = '\u0000'; - private static final char replacementChar = Tokeniser.replacementChar; - private static final String replacementStr = String.valueOf(Tokeniser.replacementChar); - private static final char eof = CharacterReader.EOF; - - /** - * Handles RawtextEndTagName, ScriptDataEndTagName, and ScriptDataEscapedEndTagName. Same body impl, just - * different else exit transitions. - */ - private static final void handleDataEndTag(Tokeniser t, CharacterReader r, TokeniserState elseTransition) { - if (r.matchesLetter()) { - String name = r.consumeLetterSequence(); - t.tagPending.appendTagName(name.toLowerCase()); - t.dataBuffer.append(name); - return; - } - - boolean needsExitTransition = false; - if (t.isAppropriateEndTagToken() && !r.isEmpty()) { - char c = r.consume(); - switch (c) { - case '\t': - case '\n': - case '\r': - case '\f': - case ' ': - t.transition(BeforeAttributeName); - break; - case '/': - t.transition(SelfClosingStartTag); - break; - case '>': - t.emitTagPending(); - t.transition(Data); - break; - default: - t.dataBuffer.append(c); - needsExitTransition = true; - } - } else { - needsExitTransition = true; - } - - if (needsExitTransition) { - t.emit("': - if (t.dataBuffer.toString().equals("script")) - t.transition(primary); - else - t.transition(fallback); - t.emit(c); - break; - default: - r.unconsume(); - t.transition(fallback); - } - } -} diff --git a/src/org/jsoup/parser/TreeBuilder.java b/src/org/jsoup/parser/TreeBuilder.java deleted file mode 100644 index aa04a25..0000000 --- a/src/org/jsoup/parser/TreeBuilder.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.jsoup.parser; - -import org.jsoup.helper.DescendableLinkedList; -import org.jsoup.helper.Validate; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; - - -/** - * @author Jonathan Hedley - */ -abstract class TreeBuilder { - CharacterReader reader; - Tokeniser tokeniser; - protected Document doc; // current doc we are building into - protected DescendableLinkedList stack; // the stack of open elements - protected String baseUri; // current base uri, for creating new elements - protected Token currentToken; // currentToken is used only for error tracking. - protected ParseErrorList errors; // null when not tracking errors - - protected void initialiseParse(String input, String baseUri, ParseErrorList errors) { - Validate.notNull(input, "String input must not be null"); - Validate.notNull(baseUri, "BaseURI must not be null"); - - doc = new Document(baseUri); - reader = new CharacterReader(input); - this.errors = errors; - tokeniser = new Tokeniser(reader, errors); - stack = new DescendableLinkedList(); - this.baseUri = baseUri; - } - - Document parse(String input, String baseUri) { - return parse(input, baseUri, ParseErrorList.noTracking()); - } - - Document parse(String input, String baseUri, ParseErrorList errors) { - initialiseParse(input, baseUri, errors); - runParser(); - return doc; - } - - protected void runParser() { - while (true) { - Token token = tokeniser.read(); - process(token); - - if (token.type == Token.TokenType.EOF) - break; - } - } - - protected abstract boolean process(Token token); - - protected Element currentElement() { - return stack.getLast(); - } -} diff --git a/src/org/jsoup/parser/XmlTreeBuilder.java b/src/org/jsoup/parser/XmlTreeBuilder.java deleted file mode 100644 index 633720f..0000000 --- a/src/org/jsoup/parser/XmlTreeBuilder.java +++ /dev/null @@ -1,131 +0,0 @@ -package org.jsoup.parser; - -import org.jsoup.helper.Validate; -import org.jsoup.nodes.*; - -import java.util.Iterator; -import java.util.List; - -/** - * Use the {@code XmlTreeBuilder} when you want to parse XML without any of the HTML DOM rules being applied to the - * document. - *

Usage example: {@code Document xmlDoc = Jsoup.parse(html, baseUrl, Parser.xmlParser());}

- * - * @author Jonathan Hedley - */ -public class XmlTreeBuilder extends TreeBuilder { - @Override - protected void initialiseParse(String input, String baseUri, ParseErrorList errors) { - super.initialiseParse(input, baseUri, errors); - stack.add(doc); // place the document onto the stack. differs from HtmlTreeBuilder (not on stack) - doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml); - } - - @Override - protected boolean process(Token token) { - // start tag, end tag, doctype, comment, character, eof - switch (token.type) { - case StartTag: - insert(token.asStartTag()); - break; - case EndTag: - popStackToClose(token.asEndTag()); - break; - case Comment: - insert(token.asComment()); - break; - case Character: - insert(token.asCharacter()); - break; - case Doctype: - insert(token.asDoctype()); - break; - case EOF: // could put some normalisation here if desired - break; - default: - Validate.fail("Unexpected token type: " + token.type); - } - return true; - } - - private void insertNode(Node node) { - currentElement().appendChild(node); - } - - Element insert(Token.StartTag startTag) { - Tag tag = Tag.valueOf(startTag.name()); - // todo: wonder if for xml parsing, should treat all tags as unknown? because it's not html. - Element el = new Element(tag, baseUri, startTag.attributes); - insertNode(el); - if (startTag.isSelfClosing()) { - tokeniser.acknowledgeSelfClosingFlag(); - if (!tag.isKnownTag()) // unknown tag, remember this is self closing for output. see above. - tag.setSelfClosing(); - } else { - stack.add(el); - } - return el; - } - - void insert(Token.Comment commentToken) { - Comment comment = new Comment(commentToken.getData(), baseUri); - Node insert = comment; - if (commentToken.bogus) { // xml declarations are emitted as bogus comments (which is right for html, but not xml) - String data = comment.getData(); - if (data.length() > 1 && (data.startsWith("!") || data.startsWith("?"))) { - String declaration = data.substring(1); - insert = new XmlDeclaration(declaration, comment.baseUri(), data.startsWith("!")); - } - } - insertNode(insert); - } - - void insert(Token.Character characterToken) { - Node node = new TextNode(characterToken.getData(), baseUri); - insertNode(node); - } - - void insert(Token.Doctype d) { - DocumentType doctypeNode = new DocumentType(d.getName(), d.getPublicIdentifier(), d.getSystemIdentifier(), baseUri); - insertNode(doctypeNode); - } - - /** - * If the stack contains an element with this tag's name, pop up the stack to remove the first occurrence. If not - * found, skips. - * - * @param endTag - */ - private void popStackToClose(Token.EndTag endTag) { - String elName = endTag.name(); - Element firstFound = null; - - Iterator it = stack.descendingIterator(); - while (it.hasNext()) { - Element next = it.next(); - if (next.nodeName().equals(elName)) { - firstFound = next; - break; - } - } - if (firstFound == null) - return; // not found, skip - - it = stack.descendingIterator(); - while (it.hasNext()) { - Element next = it.next(); - if (next == firstFound) { - it.remove(); - break; - } else { - it.remove(); - } - } - } - - List parseFragment(String inputFragment, String baseUri, ParseErrorList errors) { - initialiseParse(inputFragment, baseUri, errors); - runParser(); - return doc.childNodes(); - } -} diff --git a/src/org/jsoup/parser/package-info.java b/src/org/jsoup/parser/package-info.java deleted file mode 100644 index 168fdf4..0000000 --- a/src/org/jsoup/parser/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - Contains the HTML parser, tag specifications, and HTML tokeniser. - */ -package org.jsoup.parser; diff --git a/src/org/jsoup/safety/Cleaner.java b/src/org/jsoup/safety/Cleaner.java deleted file mode 100644 index e6ed700..0000000 --- a/src/org/jsoup/safety/Cleaner.java +++ /dev/null @@ -1,151 +0,0 @@ -package org.jsoup.safety; - -import org.jsoup.helper.Validate; -import org.jsoup.nodes.*; -import org.jsoup.parser.Tag; -import org.jsoup.select.NodeTraversor; -import org.jsoup.select.NodeVisitor; - - -/** - The whitelist based HTML cleaner. Use to ensure that end-user provided HTML contains only the elements and attributes - that you are expecting; no junk, and no cross-site scripting attacks! -

- The HTML cleaner parses the input as HTML and then runs it through a white-list, so the output HTML can only contain - HTML that is allowed by the whitelist. -

- It is assumed that the input HTML is a body fragment; the clean methods only pull from the source's body, and the - canned white-lists only allow body contained tags. -

- Rather than interacting directly with a Cleaner object, generally see the {@code clean} methods in {@link org.jsoup.Jsoup}. - */ -public class Cleaner { - private Whitelist whitelist; - - /** - Create a new cleaner, that sanitizes documents using the supplied whitelist. - @param whitelist white-list to clean with - */ - public Cleaner(Whitelist whitelist) { - Validate.notNull(whitelist); - this.whitelist = whitelist; - } - - /** - Creates a new, clean document, from the original dirty document, containing only elements allowed by the whitelist. - The original document is not modified. Only elements from the dirt document's body are used. - @param dirtyDocument Untrusted base document to clean. - @return cleaned document. - */ - public Document clean(Document dirtyDocument) { - Validate.notNull(dirtyDocument); - - Document clean = Document.createShell(dirtyDocument.baseUri()); - if (dirtyDocument.body() != null) // frameset documents won't have a body. the clean doc will have empty body. - copySafeNodes(dirtyDocument.body(), clean.body()); - - return clean; - } - - /** - Determines if the input document is valid, against the whitelist. It is considered valid if all the tags and attributes - in the input HTML are allowed by the whitelist. -

- This method can be used as a validator for user input forms. An invalid document will still be cleaned successfully - using the {@link #clean(Document)} document. If using as a validator, it is recommended to still clean the document - to ensure enforced attributes are set correctly, and that the output is tidied. - @param dirtyDocument document to test - @return true if no tags or attributes need to be removed; false if they do - */ - public boolean isValid(Document dirtyDocument) { - Validate.notNull(dirtyDocument); - - Document clean = Document.createShell(dirtyDocument.baseUri()); - int numDiscarded = copySafeNodes(dirtyDocument.body(), clean.body()); - return numDiscarded == 0; - } - - /** - Iterates the input and copies trusted nodes (tags, attributes, text) into the destination. - */ - private final class CleaningVisitor implements NodeVisitor { - private int numDiscarded = 0; - private final Element root; - private Element destination; // current element to append nodes to - - private CleaningVisitor(Element root, Element destination) { - this.root = root; - this.destination = destination; - } - - public void head(Node source, int depth) { - if (source instanceof Element) { - Element sourceEl = (Element) source; - - if (whitelist.isSafeTag(sourceEl.tagName())) { // safe, clone and copy safe attrs - ElementMeta meta = createSafeElement(sourceEl); - Element destChild = meta.el; - destination.appendChild(destChild); - - numDiscarded += meta.numAttribsDiscarded; - destination = destChild; - } else if (source != root) { // not a safe tag, so don't add. don't count root against discarded. - numDiscarded++; - } - } else if (source instanceof TextNode) { - TextNode sourceText = (TextNode) source; - TextNode destText = new TextNode(sourceText.getWholeText(), source.baseUri()); - destination.appendChild(destText); - } else if (source instanceof DataNode && whitelist.isSafeTag(source.parent().nodeName())) { - DataNode sourceData = (DataNode) source; - DataNode destData = new DataNode(sourceData.getWholeData(), source.baseUri()); - destination.appendChild(destData); - } else { // else, we don't care about comments, xml proc instructions, etc - numDiscarded++; - } - } - - public void tail(Node source, int depth) { - if (source instanceof Element && whitelist.isSafeTag(source.nodeName())) { - destination = destination.parent(); // would have descended, so pop destination stack - } - } - } - - private int copySafeNodes(Element source, Element dest) { - CleaningVisitor cleaningVisitor = new CleaningVisitor(source, dest); - NodeTraversor traversor = new NodeTraversor(cleaningVisitor); - traversor.traverse(source); - return cleaningVisitor.numDiscarded; - } - - private ElementMeta createSafeElement(Element sourceEl) { - String sourceTag = sourceEl.tagName(); - Attributes destAttrs = new Attributes(); - Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs); - int numDiscarded = 0; - - Attributes sourceAttrs = sourceEl.attributes(); - for (Attribute sourceAttr : sourceAttrs) { - if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr)) - destAttrs.put(sourceAttr); - else - numDiscarded++; - } - Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag); - destAttrs.addAll(enforcedAttrs); - - return new ElementMeta(dest, numDiscarded); - } - - private static class ElementMeta { - Element el; - int numAttribsDiscarded; - - ElementMeta(Element el, int numAttribsDiscarded) { - this.el = el; - this.numAttribsDiscarded = numAttribsDiscarded; - } - } - -} diff --git a/src/org/jsoup/safety/Whitelist.java b/src/org/jsoup/safety/Whitelist.java deleted file mode 100644 index c144520..0000000 --- a/src/org/jsoup/safety/Whitelist.java +++ /dev/null @@ -1,615 +0,0 @@ -package org.jsoup.safety; - -/* - Thank you to Ryan Grove (wonko.com) for the Ruby HTML cleaner http://github.com/rgrove/sanitize/, which inspired - this whitelist configuration, and the initial defaults. - */ - -import org.jsoup.helper.Validate; -import org.jsoup.nodes.Attribute; -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - - -/** - Whitelists define what HTML (elements and attributes) to allow through the cleaner. Everything else is removed. -

- Start with one of the defaults: -

    -
  • {@link #none} -
  • {@link #simpleText} -
  • {@link #basic} -
  • {@link #basicWithImages} -
  • {@link #relaxed} -
-

- If you need to allow more through (please be careful!), tweak a base whitelist with: -

    -
  • {@link #addTags} -
  • {@link #addAttributes} -
  • {@link #addEnforcedAttribute} -
  • {@link #addProtocols} -
-

- You can remove any setting from an existing whitelist with: -

    -
  • {@link #removeTags} -
  • {@link #removeAttributes} -
  • {@link #removeEnforcedAttribute} -
  • {@link #removeProtocols} -
-

- The cleaner and these whitelists assume that you want to clean a body fragment of HTML (to add user - supplied HTML into a templated page), and not to clean a full HTML document. If the latter is the case, either wrap the - document HTML around the cleaned body HTML, or create a whitelist that allows html and head - elements as appropriate. -

- If you are going to extend a whitelist, please be very careful. Make sure you understand what attributes may lead to - XSS attack vectors. URL attributes are particularly vulnerable and require careful validation. See - http://ha.ckers.org/xss.html for some XSS attack examples. - - @author Jonathan Hedley - */ -public class Whitelist { - private Set tagNames; // tags allowed, lower case. e.g. [p, br, span] - private Map> attributes; // tag -> attribute[]. allowed attributes [href] for a tag. - private Map> enforcedAttributes; // always set these attribute values - private Map>> protocols; // allowed URL protocols for attributes - private boolean preserveRelativeLinks; // option to preserve relative links - - /** - This whitelist allows only text nodes: all HTML will be stripped. - - @return whitelist - */ - public static Whitelist none() { - return new Whitelist(); - } - - /** - This whitelist allows only simple text formatting: b, em, i, strong, u. All other HTML (tags and - attributes) will be removed. - - @return whitelist - */ - public static Whitelist simpleText() { - return new Whitelist() - .addTags("b", "em", "i", "strong", "u") - ; - } - - /** - This whitelist allows a fuller range of text nodes: a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, - ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul, and appropriate attributes. -

- Links (a elements) can point to http, https, ftp, mailto, and have an enforced - rel=nofollow attribute. -

- Does not allow images. - - @return whitelist - */ - public static Whitelist basic() { - return new Whitelist() - .addTags( - "a", "b", "blockquote", "br", "cite", "code", "dd", "dl", "dt", "em", - "i", "li", "ol", "p", "pre", "q", "small", "span", "strike", "strong", "sub", - "sup", "u", "ul") - - .addAttributes("a", "href") - .addAttributes("blockquote", "cite") - .addAttributes("q", "cite") - - .addProtocols("a", "href", "ftp", "http", "https", "mailto") - .addProtocols("blockquote", "cite", "http", "https") - .addProtocols("cite", "cite", "http", "https") - - .addEnforcedAttribute("a", "rel", "nofollow") - ; - - } - - /** - This whitelist allows the same text tags as {@link #basic}, and also allows img tags, with appropriate - attributes, with src pointing to http or https. - - @return whitelist - */ - public static Whitelist basicWithImages() { - return basic() - .addTags("img") - .addAttributes("img", "align", "alt", "height", "src", "title", "width") - .addProtocols("img", "src", "http", "https") - ; - } - - /** - This whitelist allows a full range of text and structural body HTML: a, b, blockquote, br, caption, cite, - code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub, - sup, table, tbody, td, tfoot, th, thead, tr, u, ul -

- Links do not have an enforced rel=nofollow attribute, but you can add that if desired. - - @return whitelist - */ - public static Whitelist relaxed() { - return new Whitelist() - .addTags( - "a", "b", "blockquote", "br", "caption", "cite", "code", "col", - "colgroup", "dd", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", - "i", "img", "li", "ol", "p", "pre", "q", "small", "span", "strike", "strong", - "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u", - "ul") - - .addAttributes("a", "href", "title") - .addAttributes("blockquote", "cite") - .addAttributes("col", "span", "width") - .addAttributes("colgroup", "span", "width") - .addAttributes("img", "align", "alt", "height", "src", "title", "width") - .addAttributes("ol", "start", "type") - .addAttributes("q", "cite") - .addAttributes("table", "summary", "width") - .addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width") - .addAttributes( - "th", "abbr", "axis", "colspan", "rowspan", "scope", - "width") - .addAttributes("ul", "type") - - .addProtocols("a", "href", "ftp", "http", "https", "mailto") - .addProtocols("blockquote", "cite", "http", "https") - .addProtocols("cite", "cite", "http", "https") - .addProtocols("img", "src", "http", "https") - .addProtocols("q", "cite", "http", "https") - ; - } - - /** - Create a new, empty whitelist. Generally it will be better to start with a default prepared whitelist instead. - - @see #basic() - @see #basicWithImages() - @see #simpleText() - @see #relaxed() - */ - public Whitelist() { - tagNames = new HashSet(); - attributes = new HashMap>(); - enforcedAttributes = new HashMap>(); - protocols = new HashMap>>(); - preserveRelativeLinks = false; - } - - /** - Add a list of allowed elements to a whitelist. (If a tag is not allowed, it will be removed from the HTML.) - - @param tags tag names to allow - @return this (for chaining) - */ - public Whitelist addTags(String... tags) { - Validate.notNull(tags); - - for (String tagName : tags) { - Validate.notEmpty(tagName); - tagNames.add(TagName.valueOf(tagName)); - } - return this; - } - - /** - Remove a list of allowed elements from a whitelist. (If a tag is not allowed, it will be removed from the HTML.) - - @param tags tag names to disallow - @return this (for chaining) - */ - public Whitelist removeTags(String... tags) { - Validate.notNull(tags); - - for(String tag: tags) { - Validate.notEmpty(tag); - TagName tagName = TagName.valueOf(tag); - - if(tagNames.remove(tagName)) { // Only look in sub-maps if tag was allowed - attributes.remove(tagName); - enforcedAttributes.remove(tagName); - protocols.remove(tagName); - } - } - return this; - } - - /** - Add a list of allowed attributes to a tag. (If an attribute is not allowed on an element, it will be removed.) -

- E.g.: addAttributes("a", "href", "class") allows href and class attributes - on a tags. -

- To make an attribute valid for all tags, use the pseudo tag :all, e.g. - addAttributes(":all", "class"). - - @param tag The tag the attributes are for. The tag will be added to the allowed tag list if necessary. - @param keys List of valid attributes for the tag - @return this (for chaining) - */ - public Whitelist addAttributes(String tag, String... keys) { - Validate.notEmpty(tag); - Validate.notNull(keys); - Validate.isTrue(keys.length > 0, "No attributes supplied."); - - TagName tagName = TagName.valueOf(tag); - if (!tagNames.contains(tagName)) - tagNames.add(tagName); - Set attributeSet = new HashSet(); - for (String key : keys) { - Validate.notEmpty(key); - attributeSet.add(AttributeKey.valueOf(key)); - } - if (attributes.containsKey(tagName)) { - Set currentSet = attributes.get(tagName); - currentSet.addAll(attributeSet); - } else { - attributes.put(tagName, attributeSet); - } - return this; - } - - /** - Remove a list of allowed attributes from a tag. (If an attribute is not allowed on an element, it will be removed.) -

- E.g.: removeAttributes("a", "href", "class") disallows href and class - attributes on a tags. -

- To make an attribute invalid for all tags, use the pseudo tag :all, e.g. - removeAttributes(":all", "class"). - - @param tag The tag the attributes are for. - @param keys List of invalid attributes for the tag - @return this (for chaining) - */ - public Whitelist removeAttributes(String tag, String... keys) { - Validate.notEmpty(tag); - Validate.notNull(keys); - Validate.isTrue(keys.length > 0, "No attributes supplied."); - - TagName tagName = TagName.valueOf(tag); - Set attributeSet = new HashSet(); - for (String key : keys) { - Validate.notEmpty(key); - attributeSet.add(AttributeKey.valueOf(key)); - } - if(tagNames.contains(tagName) && attributes.containsKey(tagName)) { // Only look in sub-maps if tag was allowed - Set currentSet = attributes.get(tagName); - currentSet.removeAll(attributeSet); - - if(currentSet.isEmpty()) // Remove tag from attribute map if no attributes are allowed for tag - attributes.remove(tagName); - } - if(tag.equals(":all")) // Attribute needs to be removed from all individually set tags - for(TagName name: attributes.keySet()) { - Set currentSet = attributes.get(name); - currentSet.removeAll(attributeSet); - - if(currentSet.isEmpty()) // Remove tag from attribute map if no attributes are allowed for tag - attributes.remove(name); - } - return this; - } - - /** - Add an enforced attribute to a tag. An enforced attribute will always be added to the element. If the element - already has the attribute set, it will be overridden. -

- E.g.: addEnforcedAttribute("a", "rel", "nofollow") will make all a tags output as - <a href="..." rel="nofollow"> - - @param tag The tag the enforced attribute is for. The tag will be added to the allowed tag list if necessary. - @param key The attribute key - @param value The enforced attribute value - @return this (for chaining) - */ - public Whitelist addEnforcedAttribute(String tag, String key, String value) { - Validate.notEmpty(tag); - Validate.notEmpty(key); - Validate.notEmpty(value); - - TagName tagName = TagName.valueOf(tag); - if (!tagNames.contains(tagName)) - tagNames.add(tagName); - AttributeKey attrKey = AttributeKey.valueOf(key); - AttributeValue attrVal = AttributeValue.valueOf(value); - - if (enforcedAttributes.containsKey(tagName)) { - enforcedAttributes.get(tagName).put(attrKey, attrVal); - } else { - Map attrMap = new HashMap(); - attrMap.put(attrKey, attrVal); - enforcedAttributes.put(tagName, attrMap); - } - return this; - } - - /** - Remove a previously configured enforced attribute from a tag. - - @param tag The tag the enforced attribute is for. - @param key The attribute key - @return this (for chaining) - */ - public Whitelist removeEnforcedAttribute(String tag, String key) { - Validate.notEmpty(tag); - Validate.notEmpty(key); - - TagName tagName = TagName.valueOf(tag); - if(tagNames.contains(tagName) && enforcedAttributes.containsKey(tagName)) { - AttributeKey attrKey = AttributeKey.valueOf(key); - Map attrMap = enforcedAttributes.get(tagName); - attrMap.remove(attrKey); - - if(attrMap.isEmpty()) // Remove tag from enforced attribute map if no enforced attributes are present - enforcedAttributes.remove(tagName); - } - return this; - } - - /** - * Configure this Whitelist to preserve relative links in an element's URL attribute, or convert them to absolute - * links. By default, this is false: URLs will be made absolute (e.g. start with an allowed protocol, like - * e.g. {@code http://}. - *

- * Note that when handling relative links, the input document must have an appropriate {@code base URI} set when - * parsing, so that the link's protocol can be confirmed. Regardless of the setting of the {@code preserve relative - * links} option, the link must be resolvable against the base URI to an allowed protocol; otherwise the attribute - * will be removed. - * - * @param preserve {@code true} to allow relative links, {@code false} (default) to deny - * @return this Whitelist, for chaining. - * @see #addProtocols - */ - public Whitelist preserveRelativeLinks(boolean preserve) { - preserveRelativeLinks = preserve; - return this; - } - - /** - Add allowed URL protocols for an element's URL attribute. This restricts the possible values of the attribute to - URLs with the defined protocol. -

- E.g.: addProtocols("a", "href", "ftp", "http", "https") -

- To allow a link to an in-page URL anchor (i.e. <a href="#anchor">, add a #:
- E.g.: addProtocols("a", "href", "#") - - @param tag Tag the URL protocol is for - @param key Attribute key - @param protocols List of valid protocols - @return this, for chaining - */ - public Whitelist addProtocols(String tag, String key, String... protocols) { - Validate.notEmpty(tag); - Validate.notEmpty(key); - Validate.notNull(protocols); - - TagName tagName = TagName.valueOf(tag); - AttributeKey attrKey = AttributeKey.valueOf(key); - Map> attrMap; - Set protSet; - - if (this.protocols.containsKey(tagName)) { - attrMap = this.protocols.get(tagName); - } else { - attrMap = new HashMap>(); - this.protocols.put(tagName, attrMap); - } - if (attrMap.containsKey(attrKey)) { - protSet = attrMap.get(attrKey); - } else { - protSet = new HashSet(); - attrMap.put(attrKey, protSet); - } - for (String protocol : protocols) { - Validate.notEmpty(protocol); - Protocol prot = Protocol.valueOf(protocol); - protSet.add(prot); - } - return this; - } - - /** - Remove allowed URL protocols for an element's URL attribute. -

- E.g.: removeProtocols("a", "href", "ftp") - - @param tag Tag the URL protocol is for - @param key Attribute key - @param protocols List of invalid protocols - @return this, for chaining - */ - public Whitelist removeProtocols(String tag, String key, String... protocols) { - Validate.notEmpty(tag); - Validate.notEmpty(key); - Validate.notNull(protocols); - - TagName tagName = TagName.valueOf(tag); - AttributeKey attrKey = AttributeKey.valueOf(key); - - if(this.protocols.containsKey(tagName)) { - Map> attrMap = this.protocols.get(tagName); - if(attrMap.containsKey(attrKey)) { - Set protSet = attrMap.get(attrKey); - for (String protocol : protocols) { - Validate.notEmpty(protocol); - Protocol prot = Protocol.valueOf(protocol); - protSet.remove(prot); - } - - if(protSet.isEmpty()) { // Remove protocol set if empty - attrMap.remove(attrKey); - if(attrMap.isEmpty()) // Remove entry for tag if empty - this.protocols.remove(tagName); - } - } - } - return this; - } - - /** - * Test if the supplied tag is allowed by this whitelist - * @param tag test tag - * @return true if allowed - */ - protected boolean isSafeTag(String tag) { - return tagNames.contains(TagName.valueOf(tag)); - } - - /** - * Test if the supplied attribute is allowed by this whitelist for this tag - * @param tagName tag to consider allowing the attribute in - * @param el element under test, to confirm protocol - * @param attr attribute under test - * @return true if allowed - */ - protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) { - TagName tag = TagName.valueOf(tagName); - AttributeKey key = AttributeKey.valueOf(attr.getKey()); - - if (attributes.containsKey(tag)) { - if (attributes.get(tag).contains(key)) { - if (protocols.containsKey(tag)) { - Map> attrProts = protocols.get(tag); - // ok if not defined protocol; otherwise test - return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key)); - } else { // attribute found, no protocols defined, so OK - return true; - } - } - } - // no attributes defined for tag, try :all tag - return !tagName.equals(":all") && isSafeAttribute(":all", el, attr); - } - - private boolean testValidProtocol(Element el, Attribute attr, Set protocols) { - // try to resolve relative urls to abs, and optionally update the attribute so output html has abs. - // rels without a baseuri get removed - String value = el.absUrl(attr.getKey()); - if (value.length() == 0) - value = attr.getValue(); // if it could not be made abs, run as-is to allow custom unknown protocols - if (!preserveRelativeLinks) - attr.setValue(value); - - for (Protocol protocol : protocols) { - String prot = protocol.toString(); - - if (prot.equals("#")) { // allows anchor links - if (isValidAnchor(value)) { - return true; - } else { - continue; - } - } - - prot += ":"; - - if (value.toLowerCase().startsWith(prot)) { - return true; - } - } - return false; - } - - private boolean isValidAnchor(String value) { - return value.startsWith("#") && !value.matches(".*\\s.*"); - } - - Attributes getEnforcedAttributes(String tagName) { - Attributes attrs = new Attributes(); - TagName tag = TagName.valueOf(tagName); - if (enforcedAttributes.containsKey(tag)) { - Map keyVals = enforcedAttributes.get(tag); - for (Map.Entry entry : keyVals.entrySet()) { - attrs.put(entry.getKey().toString(), entry.getValue().toString()); - } - } - return attrs; - } - - // named types for config. All just hold strings, but here for my sanity. - - static class TagName extends TypedValue { - TagName(String value) { - super(value); - } - - static TagName valueOf(String value) { - return new TagName(value); - } - } - - static class AttributeKey extends TypedValue { - AttributeKey(String value) { - super(value); - } - - static AttributeKey valueOf(String value) { - return new AttributeKey(value); - } - } - - static class AttributeValue extends TypedValue { - AttributeValue(String value) { - super(value); - } - - static AttributeValue valueOf(String value) { - return new AttributeValue(value); - } - } - - static class Protocol extends TypedValue { - Protocol(String value) { - super(value); - } - - static Protocol valueOf(String value) { - return new Protocol(value); - } - } - - abstract static class TypedValue { - private String value; - - TypedValue(String value) { - Validate.notNull(value); - this.value = value; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - TypedValue other = (TypedValue) obj; - if (value == null) { - if (other.value != null) return false; - } else if (!value.equals(other.value)) return false; - return true; - } - - @Override - public String toString() { - return value; - } - } -} - diff --git a/src/org/jsoup/safety/package-info.java b/src/org/jsoup/safety/package-info.java deleted file mode 100644 index ac890f0..0000000 --- a/src/org/jsoup/safety/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - Contains the jsoup HTML cleaner, and whitelist definitions. - */ -package org.jsoup.safety; diff --git a/src/org/jsoup/select/Collector.java b/src/org/jsoup/select/Collector.java deleted file mode 100644 index 8f01045..0000000 --- a/src/org/jsoup/select/Collector.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.nodes.Element; -import org.jsoup.nodes.Node; - -/** - * Collects a list of elements that match the supplied criteria. - * - * @author Jonathan Hedley - */ -public class Collector { - - private Collector() { - } - - /** - Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator. - @param eval Evaluator to test elements against - @param root root of tree to descend - @return list of matches; empty if none - */ - public static Elements collect (Evaluator eval, Element root) { - Elements elements = new Elements(); - new NodeTraversor(new Accumulator(root, elements, eval)).traverse(root); - return elements; - } - - private static class Accumulator implements NodeVisitor { - private final Element root; - private final Elements elements; - private final Evaluator eval; - - Accumulator(Element root, Elements elements, Evaluator eval) { - this.root = root; - this.elements = elements; - this.eval = eval; - } - - public void head(Node node, int depth) { - if (node instanceof Element) { - Element el = (Element) node; - if (eval.matches(root, el)) - elements.add(el); - } - } - - public void tail(Node node, int depth) { - // void - } - } -} diff --git a/src/org/jsoup/select/CombiningEvaluator.java b/src/org/jsoup/select/CombiningEvaluator.java deleted file mode 100644 index e25668e..0000000 --- a/src/org/jsoup/select/CombiningEvaluator.java +++ /dev/null @@ -1,104 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.helper.StringUtil; -import org.jsoup.nodes.Element; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; - -/** - * Base combining (and, or) evaluator. - */ -abstract class CombiningEvaluator extends Evaluator { - final ArrayList evaluators; - int num = 0; - - CombiningEvaluator() { - super(); - evaluators = new ArrayList(); - } - - CombiningEvaluator(Collection evaluators) { - this(); - this.evaluators.addAll(evaluators); - updateNumEvaluators(); - } - - Evaluator rightMostEvaluator() { - return num > 0 ? evaluators.get(num - 1) : null; - } - - void replaceRightMostEvaluator(Evaluator replacement) { - evaluators.set(num - 1, replacement); - } - - void updateNumEvaluators() { - // used so we don't need to bash on size() for every match test - num = evaluators.size(); - } - - static final class And extends CombiningEvaluator { - And(Collection evaluators) { - super(evaluators); - } - - And(Evaluator... evaluators) { - this(Arrays.asList(evaluators)); - } - - @Override - public boolean matches(Element root, Element node) { - for (int i = 0; i < num; i++) { - Evaluator s = evaluators.get(i); - if (!s.matches(root, node)) - return false; - } - return true; - } - - @Override - public String toString() { - return StringUtil.join(evaluators, " "); - } - } - - static final class Or extends CombiningEvaluator { - /** - * Create a new Or evaluator. The initial evaluators are ANDed together and used as the first clause of the OR. - * @param evaluators initial OR clause (these are wrapped into an AND evaluator). - */ - Or(Collection evaluators) { - super(); - if (num > 1) - this.evaluators.add(new And(evaluators)); - else // 0 or 1 - this.evaluators.addAll(evaluators); - updateNumEvaluators(); - } - - Or() { - super(); - } - - public void add(Evaluator e) { - evaluators.add(e); - updateNumEvaluators(); - } - - @Override - public boolean matches(Element root, Element node) { - for (int i = 0; i < num; i++) { - Evaluator s = evaluators.get(i); - if (s.matches(root, node)) - return true; - } - return false; - } - - @Override - public String toString() { - return String.format(":or%s", evaluators); - } - } -} diff --git a/src/org/jsoup/select/Elements.java b/src/org/jsoup/select/Elements.java deleted file mode 100644 index dd4a0fe..0000000 --- a/src/org/jsoup/select/Elements.java +++ /dev/null @@ -1,564 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.helper.Validate; -import org.jsoup.nodes.Element; -import org.jsoup.nodes.FormElement; -import org.jsoup.nodes.Node; - -import java.util.*; - -/** - A list of {@link Element}s, with methods that act on every element in the list. -

- To get an {@code Elements} object, use the {@link Element#select(String)} method. - - @author Jonathan Hedley, jonathan@hedley.net */ -public class Elements implements List, Cloneable { - private List contents; - - public Elements() { - contents = new ArrayList(); - } - - public Elements(int initialCapacity) { - contents = new ArrayList(initialCapacity); - } - - public Elements(Collection elements) { - contents = new ArrayList(elements); - } - - public Elements(List elements) { - contents = elements; - } - - public Elements(Element... elements) { - this(Arrays.asList(elements)); - } - - /** - * Creates a deep copy of these elements. - * @return a deep copy - */ - @Override - public Elements clone() { - Elements clone; - try { - clone = (Elements) super.clone(); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - List elements = new ArrayList(); - clone.contents = elements; - - for(Element e : contents) - elements.add(e.clone()); - - - return clone; - } - - // attribute methods - /** - Get an attribute value from the first matched element that has the attribute. - @param attributeKey The attribute key. - @return The attribute value from the first matched element that has the attribute.. If no elements were matched (isEmpty() == true), - or if the no elements have the attribute, returns empty string. - @see #hasAttr(String) - */ - public String attr(String attributeKey) { - for (Element element : contents) { - if (element.hasAttr(attributeKey)) - return element.attr(attributeKey); - } - return ""; - } - - /** - Checks if any of the matched elements have this attribute set. - @param attributeKey attribute key - @return true if any of the elements have the attribute; false if none do. - */ - public boolean hasAttr(String attributeKey) { - for (Element element : contents) { - if (element.hasAttr(attributeKey)) - return true; - } - return false; - } - - /** - * Set an attribute on all matched elements. - * @param attributeKey attribute key - * @param attributeValue attribute value - * @return this - */ - public Elements attr(String attributeKey, String attributeValue) { - for (Element element : contents) { - element.attr(attributeKey, attributeValue); - } - return this; - } - - /** - * Remove an attribute from every matched element. - * @param attributeKey The attribute to remove. - * @return this (for chaining) - */ - public Elements removeAttr(String attributeKey) { - for (Element element : contents) { - element.removeAttr(attributeKey); - } - return this; - } - - /** - Add the class name to every matched element's {@code class} attribute. - @param className class name to add - @return this - */ - public Elements addClass(String className) { - for (Element element : contents) { - element.addClass(className); - } - return this; - } - - /** - Remove the class name from every matched element's {@code class} attribute, if present. - @param className class name to remove - @return this - */ - public Elements removeClass(String className) { - for (Element element : contents) { - element.removeClass(className); - } - return this; - } - - /** - Toggle the class name on every matched element's {@code class} attribute. - @param className class name to add if missing, or remove if present, from every element. - @return this - */ - public Elements toggleClass(String className) { - for (Element element : contents) { - element.toggleClass(className); - } - return this; - } - - /** - Determine if any of the matched elements have this class name set in their {@code class} attribute. - @param className class name to check for - @return true if any do, false if none do - */ - public boolean hasClass(String className) { - for (Element element : contents) { - if (element.hasClass(className)) - return true; - } - return false; - } - - /** - * Get the form element's value of the first matched element. - * @return The form element's value, or empty if not set. - * @see Element#val() - */ - public String val() { - if (size() > 0) - return first().val(); - else - return ""; - } - - /** - * Set the form element's value in each of the matched elements. - * @param value The value to set into each matched element - * @return this (for chaining) - */ - public Elements val(String value) { - for (Element element : contents) - element.val(value); - return this; - } - - /** - * Get the combined text of all the matched elements. - *

- * Note that it is possible to get repeats if the matched elements contain both parent elements and their own - * children, as the Element.text() method returns the combined text of a parent and all its children. - * @return string of all text: unescaped and no HTML. - * @see Element#text() - */ - public String text() { - StringBuilder sb = new StringBuilder(); - for (Element element : contents) { - if (sb.length() != 0) - sb.append(" "); - sb.append(element.text()); - } - return sb.toString(); - } - - public boolean hasText() { - for (Element element: contents) { - if (element.hasText()) - return true; - } - return false; - } - - /** - * Get the combined inner HTML of all matched elements. - * @return string of all element's inner HTML. - * @see #text() - * @see #outerHtml() - */ - public String html() { - StringBuilder sb = new StringBuilder(); - for (Element element : contents) { - if (sb.length() != 0) - sb.append("\n"); - sb.append(element.html()); - } - return sb.toString(); - } - - /** - * Get the combined outer HTML of all matched elements. - * @return string of all element's outer HTML. - * @see #text() - * @see #html() - */ - public String outerHtml() { - StringBuilder sb = new StringBuilder(); - for (Element element : contents) { - if (sb.length() != 0) - sb.append("\n"); - sb.append(element.outerHtml()); - } - return sb.toString(); - } - - /** - * Get the combined outer HTML of all matched elements. Alias of {@link #outerHtml()}. - * @return string of all element's outer HTML. - * @see #text() - * @see #html() - */ - @Override - public String toString() { - return outerHtml(); - } - - /** - * Update the tag name of each matched element. For example, to change each {@code } to a {@code }, do - * {@code doc.select("i").tagName("em");} - * @param tagName the new tag name - * @return this, for chaining - * @see Element#tagName(String) - */ - public Elements tagName(String tagName) { - for (Element element : contents) { - element.tagName(tagName); - } - return this; - } - - /** - * Set the inner HTML of each matched element. - * @param html HTML to parse and set into each matched element. - * @return this, for chaining - * @see Element#html(String) - */ - public Elements html(String html) { - for (Element element : contents) { - element.html(html); - } - return this; - } - - /** - * Add the supplied HTML to the start of each matched element's inner HTML. - * @param html HTML to add inside each element, before the existing HTML - * @return this, for chaining - * @see Element#prepend(String) - */ - public Elements prepend(String html) { - for (Element element : contents) { - element.prepend(html); - } - return this; - } - - /** - * Add the supplied HTML to the end of each matched element's inner HTML. - * @param html HTML to add inside each element, after the existing HTML - * @return this, for chaining - * @see Element#append(String) - */ - public Elements append(String html) { - for (Element element : contents) { - element.append(html); - } - return this; - } - - /** - * Insert the supplied HTML before each matched element's outer HTML. - * @param html HTML to insert before each element - * @return this, for chaining - * @see Element#before(String) - */ - public Elements before(String html) { - for (Element element : contents) { - element.before(html); - } - return this; - } - - /** - * Insert the supplied HTML after each matched element's outer HTML. - * @param html HTML to insert after each element - * @return this, for chaining - * @see Element#after(String) - */ - public Elements after(String html) { - for (Element element : contents) { - element.after(html); - } - return this; - } - - /** - Wrap the supplied HTML around each matched elements. For example, with HTML - {@code

This is Jsoup

}, - doc.select("b").wrap("<i></i>"); - becomes {@code

This is jsoup

} - @param html HTML to wrap around each element, e.g. {@code
}. Can be arbitrarily deep. - @return this (for chaining) - @see Element#wrap - */ - public Elements wrap(String html) { - Validate.notEmpty(html); - for (Element element : contents) { - element.wrap(html); - } - return this; - } - - /** - * Removes the matched elements from the DOM, and moves their children up into their parents. This has the effect of - * dropping the elements but keeping their children. - *

- * This is useful for e.g removing unwanted formatting elements but keeping their contents. - *

- * E.g. with HTML: {@code

One Two
}
- * {@code doc.select("font").unwrap();}
- * HTML = {@code
One Two
} - * - * @return this (for chaining) - * @see Node#unwrap - */ - public Elements unwrap() { - for (Element element : contents) { - element.unwrap(); - } - return this; - } - - /** - * Empty (remove all child nodes from) each matched element. This is similar to setting the inner HTML of each - * element to nothing. - *

- * E.g. HTML: {@code

Hello there

now

}
- * doc.select("p").empty();
- * HTML = {@code

} - * @return this, for chaining - * @see Element#empty() - * @see #remove() - */ - public Elements empty() { - for (Element element : contents) { - element.empty(); - } - return this; - } - - /** - * Remove each matched element from the DOM. This is similar to setting the outer HTML of each element to nothing. - *

- * E.g. HTML: {@code

Hello

there

}
- * doc.select("p").remove();
- * HTML = {@code
} - *

- * Note that this method should not be used to clean user-submitted HTML; rather, use {@link org.jsoup.safety.Cleaner} to clean HTML. - * @return this, for chaining - * @see Element#empty() - * @see #empty() - */ - public Elements remove() { - for (Element element : contents) { - element.remove(); - } - return this; - } - - // filters - - /** - * Find matching elements within this element list. - * @param query A {@link Selector} query - * @return the filtered list of elements, or an empty list if none match. - */ - public Elements select(String query) { - return Selector.select(query, this); - } - - /** - * Remove elements from this list that match the {@link Selector} query. - *

- * E.g. HTML: {@code

Two
}
- * Elements divs = doc.select("div").not("#logo");
- * Result: {@code divs: [
Two
]} - *

- * @param query the selector query whose results should be removed from these elements - * @return a new elements list that contains only the filtered results - */ - public Elements not(String query) { - Elements out = Selector.select(query, this); - return Selector.filterOut(this, out); - } - - /** - * Get the nth matched element as an Elements object. - *

- * See also {@link #get(int)} to retrieve an Element. - * @param index the (zero-based) index of the element in the list to retain - * @return Elements containing only the specified element, or, if that element did not exist, an empty list. - */ - public Elements eq(int index) { - return contents.size() > index ? new Elements(get(index)) : new Elements(); - } - - /** - * Test if any of the matched elements match the supplied query. - * @param query A selector - * @return true if at least one element in the list matches the query. - */ - public boolean is(String query) { - Elements children = select(query); - return !children.isEmpty(); - } - - /** - * Get all of the parents and ancestor elements of the matched elements. - * @return all of the parents and ancestor elements of the matched elements - */ - public Elements parents() { - HashSet combo = new LinkedHashSet(); - for (Element e: contents) { - combo.addAll(e.parents()); - } - return new Elements(combo); - } - - // list-like methods - /** - Get the first matched element. - @return The first matched element, or null if contents is empty. - */ - public Element first() { - return contents.isEmpty() ? null : contents.get(0); - } - - /** - Get the last matched element. - @return The last matched element, or null if contents is empty. - */ - public Element last() { - return contents.isEmpty() ? null : contents.get(contents.size() - 1); - } - - /** - * Perform a depth-first traversal on each of the selected elements. - * @param nodeVisitor the visitor callbacks to perform on each node - * @return this, for chaining - */ - public Elements traverse(NodeVisitor nodeVisitor) { - Validate.notNull(nodeVisitor); - NodeTraversor traversor = new NodeTraversor(nodeVisitor); - for (Element el: contents) { - traversor.traverse(el); - } - return this; - } - - /** - * Get the {@link FormElement} forms from the selected elements, if any. - * @return a list of {@link FormElement}s pulled from the matched elements. The list will be empty if the elements contain - * no forms. - */ - public List forms() { - ArrayList forms = new ArrayList(); - for (Element el: contents) - if (el instanceof FormElement) - forms.add((FormElement) el); - return forms; - } - - // implements List delegates: - public int size() {return contents.size();} - - public boolean isEmpty() {return contents.isEmpty();} - - public boolean contains(Object o) {return contents.contains(o);} - - public Iterator iterator() {return contents.iterator();} - - public Object[] toArray() {return contents.toArray();} - - public T[] toArray(T[] a) {return contents.toArray(a);} - - public boolean add(Element element) {return contents.add(element);} - - public boolean remove(Object o) {return contents.remove(o);} - - public boolean containsAll(Collection c) {return contents.containsAll(c);} - - public boolean addAll(Collection c) {return contents.addAll(c);} - - public boolean addAll(int index, Collection c) {return contents.addAll(index, c);} - - public boolean removeAll(Collection c) {return contents.removeAll(c);} - - public boolean retainAll(Collection c) {return contents.retainAll(c);} - - public void clear() {contents.clear();} - - @Override - public boolean equals(Object o) {return contents.equals(o);} - - @Override - public int hashCode() {return contents.hashCode();} - - public Element get(int index) {return contents.get(index);} - - public Element set(int index, Element element) {return contents.set(index, element);} - - public void add(int index, Element element) {contents.add(index, element);} - - public Element remove(int index) {return contents.remove(index);} - - public int indexOf(Object o) {return contents.indexOf(o);} - - public int lastIndexOf(Object o) {return contents.lastIndexOf(o);} - - public ListIterator listIterator() {return contents.listIterator();} - - public ListIterator listIterator(int index) {return contents.listIterator(index);} - - public List subList(int fromIndex, int toIndex) {return contents.subList(fromIndex, toIndex);} -} diff --git a/src/org/jsoup/select/Evaluator.java b/src/org/jsoup/select/Evaluator.java deleted file mode 100644 index e18922e..0000000 --- a/src/org/jsoup/select/Evaluator.java +++ /dev/null @@ -1,704 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.helper.Validate; -import org.jsoup.nodes.Comment; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.DocumentType; -import org.jsoup.nodes.Element; -import org.jsoup.nodes.Node; -import org.jsoup.nodes.XmlDeclaration; - -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - - -/** - * Evaluates that an element matches the selector. - */ -public abstract class Evaluator { - protected Evaluator() { - } - - /** - * Test if the element meets the evaluator's requirements. - * - * @param root Root of the matching subtree - * @param element tested element - */ - public abstract boolean matches(Element root, Element element); - - /** - * Evaluator for tag name - */ - public static final class Tag extends Evaluator { - private String tagName; - - public Tag(String tagName) { - this.tagName = tagName; - } - - @Override - public boolean matches(Element root, Element element) { - return (element.tagName().equals(tagName)); - } - - @Override - public String toString() { - return String.format("%s", tagName); - } - } - - /** - * Evaluator for element id - */ - public static final class Id extends Evaluator { - private String id; - - public Id(String id) { - this.id = id; - } - - @Override - public boolean matches(Element root, Element element) { - return (id.equals(element.id())); - } - - @Override - public String toString() { - return String.format("#%s", id); - } - - } - - /** - * Evaluator for element class - */ - public static final class Class extends Evaluator { - private String className; - - public Class(String className) { - this.className = className; - } - - @Override - public boolean matches(Element root, Element element) { - return (element.hasClass(className)); - } - - @Override - public String toString() { - return String.format(".%s", className); - } - - } - - /** - * Evaluator for attribute name matching - */ - public static final class Attribute extends Evaluator { - private String key; - - public Attribute(String key) { - this.key = key; - } - - @Override - public boolean matches(Element root, Element element) { - return element.hasAttr(key); - } - - @Override - public String toString() { - return String.format("[%s]", key); - } - - } - - /** - * Evaluator for attribute name prefix matching - */ - public static final class AttributeStarting extends Evaluator { - private String keyPrefix; - - public AttributeStarting(String keyPrefix) { - this.keyPrefix = keyPrefix; - } - - @Override - public boolean matches(Element root, Element element) { - List values = element.attributes().asList(); - for (org.jsoup.nodes.Attribute attribute : values) { - if (attribute.getKey().startsWith(keyPrefix)) - return true; - } - return false; - } - - @Override - public String toString() { - return String.format("[^%s]", keyPrefix); - } - - } - - /** - * Evaluator for attribute name/value matching - */ - public static final class AttributeWithValue extends AttributeKeyPair { - public AttributeWithValue(String key, String value) { - super(key, value); - } - - @Override - public boolean matches(Element root, Element element) { - return element.hasAttr(key) && value.equalsIgnoreCase(element.attr(key)); - } - - @Override - public String toString() { - return String.format("[%s=%s]", key, value); - } - - } - - /** - * Evaluator for attribute name != value matching - */ - public static final class AttributeWithValueNot extends AttributeKeyPair { - public AttributeWithValueNot(String key, String value) { - super(key, value); - } - - @Override - public boolean matches(Element root, Element element) { - return !value.equalsIgnoreCase(element.attr(key)); - } - - @Override - public String toString() { - return String.format("[%s!=%s]", key, value); - } - - } - - /** - * Evaluator for attribute name/value matching (value prefix) - */ - public static final class AttributeWithValueStarting extends AttributeKeyPair { - public AttributeWithValueStarting(String key, String value) { - super(key, value); - } - - @Override - public boolean matches(Element root, Element element) { - return element.hasAttr(key) && element.attr(key).toLowerCase().startsWith(value); // value is lower case already - } - - @Override - public String toString() { - return String.format("[%s^=%s]", key, value); - } - - } - - /** - * Evaluator for attribute name/value matching (value ending) - */ - public static final class AttributeWithValueEnding extends AttributeKeyPair { - public AttributeWithValueEnding(String key, String value) { - super(key, value); - } - - @Override - public boolean matches(Element root, Element element) { - return element.hasAttr(key) && element.attr(key).toLowerCase().endsWith(value); // value is lower case - } - - @Override - public String toString() { - return String.format("[%s$=%s]", key, value); - } - - } - - /** - * Evaluator for attribute name/value matching (value containing) - */ - public static final class AttributeWithValueContaining extends AttributeKeyPair { - public AttributeWithValueContaining(String key, String value) { - super(key, value); - } - - @Override - public boolean matches(Element root, Element element) { - return element.hasAttr(key) && element.attr(key).toLowerCase().contains(value); // value is lower case - } - - @Override - public String toString() { - return String.format("[%s*=%s]", key, value); - } - - } - - /** - * Evaluator for attribute name/value matching (value regex matching) - */ - public static final class AttributeWithValueMatching extends Evaluator { - String key; - Pattern pattern; - - public AttributeWithValueMatching(String key, Pattern pattern) { - this.key = key.trim().toLowerCase(); - this.pattern = pattern; - } - - @Override - public boolean matches(Element root, Element element) { - return element.hasAttr(key) && pattern.matcher(element.attr(key)).find(); - } - - @Override - public String toString() { - return String.format("[%s~=%s]", key, pattern.toString()); - } - - } - - /** - * Abstract evaluator for attribute name/value matching - */ - public abstract static class AttributeKeyPair extends Evaluator { - String key; - String value; - - public AttributeKeyPair(String key, String value) { - Validate.notEmpty(key); - Validate.notEmpty(value); - - this.key = key.trim().toLowerCase(); - if (value.startsWith("\"") && value.endsWith("\"")) { - value = value.substring(1, value.length()-1); - } - this.value = value.trim().toLowerCase(); - } - } - - /** - * Evaluator for any / all element matching - */ - public static final class AllElements extends Evaluator { - - @Override - public boolean matches(Element root, Element element) { - return true; - } - - @Override - public String toString() { - return "*"; - } - } - - /** - * Evaluator for matching by sibling index number (e < idx) - */ - public static final class IndexLessThan extends IndexEvaluator { - public IndexLessThan(int index) { - super(index); - } - - @Override - public boolean matches(Element root, Element element) { - return element.elementSiblingIndex() < index; - } - - @Override - public String toString() { - return String.format(":lt(%d)", index); - } - - } - - /** - * Evaluator for matching by sibling index number (e > idx) - */ - public static final class IndexGreaterThan extends IndexEvaluator { - public IndexGreaterThan(int index) { - super(index); - } - - @Override - public boolean matches(Element root, Element element) { - return element.elementSiblingIndex() > index; - } - - @Override - public String toString() { - return String.format(":gt(%d)", index); - } - - } - - /** - * Evaluator for matching by sibling index number (e = idx) - */ - public static final class IndexEquals extends IndexEvaluator { - public IndexEquals(int index) { - super(index); - } - - @Override - public boolean matches(Element root, Element element) { - return element.elementSiblingIndex() == index; - } - - @Override - public String toString() { - return String.format(":eq(%d)", index); - } - - } - - /** - * Evaluator for matching the last sibling (css :last-child) - */ - public static final class IsLastChild extends Evaluator { - @Override - public boolean matches(Element root, Element element) { - final Element p = element.parent(); - return p != null && !(p instanceof Document) && element.elementSiblingIndex() == p.children().size()-1; - } - - @Override - public String toString() { - return ":last-child"; - } - } - - public static final class IsFirstOfType extends IsNthOfType { - public IsFirstOfType() { - super(0,1); - } - @Override - public String toString() { - return ":first-of-type"; - } - } - - public static final class IsLastOfType extends IsNthLastOfType { - public IsLastOfType() { - super(0,1); - } - @Override - public String toString() { - return ":last-of-type"; - } - } - - - public static abstract class CssNthEvaluator extends Evaluator { - protected final int a, b; - - public CssNthEvaluator(int a, int b) { - this.a = a; - this.b = b; - } - public CssNthEvaluator(int b) { - this(0,b); - } - - @Override - public boolean matches(Element root, Element element) { - final Element p = element.parent(); - if (p == null || (p instanceof Document)) return false; - - final int pos = calculatePosition(root, element); - if (a == 0) return pos == b; - - return (pos-b)*a >= 0 && (pos-b)%a==0; - } - - @Override - public String toString() { - if (a == 0) - return String.format(":%s(%d)",getPseudoClass(), b); - if (b == 0) - return String.format(":%s(%dn)",getPseudoClass(), a); - return String.format(":%s(%dn%+d)", getPseudoClass(),a, b); - } - - protected abstract String getPseudoClass(); - protected abstract int calculatePosition(Element root, Element element); - } - - - /** - * css-compatible Evaluator for :eq (css :nth-child) - * - * @see IndexEquals - */ - public static final class IsNthChild extends CssNthEvaluator { - - public IsNthChild(int a, int b) { - super(a,b); - } - - protected int calculatePosition(Element root, Element element) { - return element.elementSiblingIndex()+1; - } - - - protected String getPseudoClass() { - return "nth-child"; - } - } - - /** - * css pseudo class :nth-last-child) - * - * @see IndexEquals - */ - public static final class IsNthLastChild extends CssNthEvaluator { - public IsNthLastChild(int a, int b) { - super(a,b); - } - - @Override - protected int calculatePosition(Element root, Element element) { - return element.parent().children().size() - element.elementSiblingIndex(); - } - - @Override - protected String getPseudoClass() { - return "nth-last-child"; - } - } - - /** - * css pseudo class nth-of-type - * - */ - public static class IsNthOfType extends CssNthEvaluator { - public IsNthOfType(int a, int b) { - super(a,b); - } - - protected int calculatePosition(Element root, Element element) { - int pos = 0; - Elements family = element.parent().children(); - for (int i = 0; i < family.size(); i++) { - if (family.get(i).tag().equals(element.tag())) pos++; - if (family.get(i) == element) break; - } - return pos; - } - - @Override - protected String getPseudoClass() { - return "nth-of-type"; - } - } - - public static class IsNthLastOfType extends CssNthEvaluator { - - public IsNthLastOfType(int a, int b) { - super(a, b); - } - - @Override - protected int calculatePosition(Element root, Element element) { - int pos = 0; - Elements family = element.parent().children(); - for (int i = element.elementSiblingIndex(); i < family.size(); i++) { - if (family.get(i).tag().equals(element.tag())) pos++; - } - return pos; - } - - @Override - protected String getPseudoClass() { - return "nth-last-of-type"; - } - } - - /** - * Evaluator for matching the first sibling (css :first-child) - */ - public static final class IsFirstChild extends Evaluator { - @Override - public boolean matches(Element root, Element element) { - final Element p = element.parent(); - return p != null && !(p instanceof Document) && element.elementSiblingIndex() == 0; - } - - @Override - public String toString() { - return ":first-child"; - } - } - - /** - * css3 pseudo-class :root - * @see :root selector - * - */ - public static final class IsRoot extends Evaluator { - @Override - public boolean matches(Element root, Element element) { - final Element r = root instanceof Document?root.child(0):root; - return element == r; - } - @Override - public String toString() { - return ":root"; - } - } - - public static final class IsOnlyChild extends Evaluator { - @Override - public boolean matches(Element root, Element element) { - final Element p = element.parent(); - return p!=null && !(p instanceof Document) && element.siblingElements().size() == 0; - } - @Override - public String toString() { - return ":only-child"; - } - } - - public static final class IsOnlyOfType extends Evaluator { - @Override - public boolean matches(Element root, Element element) { - final Element p = element.parent(); - if (p==null || p instanceof Document) return false; - - int pos = 0; - Elements family = p.children(); - for (int i = 0; i < family.size(); i++) { - if (family.get(i).tag().equals(element.tag())) pos++; - } - return pos == 1; - } - @Override - public String toString() { - return ":only-of-type"; - } - } - - public static final class IsEmpty extends Evaluator { - @Override - public boolean matches(Element root, Element element) { - List family = element.childNodes(); - for (int i = 0; i < family.size(); i++) { - Node n = family.get(i); - if (!(n instanceof Comment || n instanceof XmlDeclaration || n instanceof DocumentType)) return false; - } - return true; - } - @Override - public String toString() { - return ":empty"; - } - } - - /** - * Abstract evaluator for sibling index matching - * - * @author ant - */ - public abstract static class IndexEvaluator extends Evaluator { - int index; - - public IndexEvaluator(int index) { - this.index = index; - } - } - - /** - * Evaluator for matching Element (and its descendants) text - */ - public static final class ContainsText extends Evaluator { - private String searchText; - - public ContainsText(String searchText) { - this.searchText = searchText.toLowerCase(); - } - - @Override - public boolean matches(Element root, Element element) { - return (element.text().toLowerCase().contains(searchText)); - } - - @Override - public String toString() { - return String.format(":contains(%s", searchText); - } - } - - /** - * Evaluator for matching Element's own text - */ - public static final class ContainsOwnText extends Evaluator { - private String searchText; - - public ContainsOwnText(String searchText) { - this.searchText = searchText.toLowerCase(); - } - - @Override - public boolean matches(Element root, Element element) { - return (element.ownText().toLowerCase().contains(searchText)); - } - - @Override - public String toString() { - return String.format(":containsOwn(%s", searchText); - } - } - - /** - * Evaluator for matching Element (and its descendants) text with regex - */ - public static final class Matches extends Evaluator { - private Pattern pattern; - - public Matches(Pattern pattern) { - this.pattern = pattern; - } - - @Override - public boolean matches(Element root, Element element) { - Matcher m = pattern.matcher(element.text()); - return m.find(); - } - - @Override - public String toString() { - return String.format(":matches(%s", pattern); - } - } - - /** - * Evaluator for matching Element's own text with regex - */ - public static final class MatchesOwn extends Evaluator { - private Pattern pattern; - - public MatchesOwn(Pattern pattern) { - this.pattern = pattern; - } - - @Override - public boolean matches(Element root, Element element) { - Matcher m = pattern.matcher(element.ownText()); - return m.find(); - } - - @Override - public String toString() { - return String.format(":matchesOwn(%s", pattern); - } - } -} diff --git a/src/org/jsoup/select/NodeTraversor.java b/src/org/jsoup/select/NodeTraversor.java deleted file mode 100644 index f45241f..0000000 --- a/src/org/jsoup/select/NodeTraversor.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.nodes.Node; - -/** - * Depth-first node traversor. Use to iterate through all nodes under and including the specified root node. - *

- * This implementation does not use recursion, so a deep DOM does not risk blowing the stack. - */ -public class NodeTraversor { - private NodeVisitor visitor; - - /** - * Create a new traversor. - * @param visitor a class implementing the {@link NodeVisitor} interface, to be called when visiting each node. - */ - public NodeTraversor(NodeVisitor visitor) { - this.visitor = visitor; - } - - /** - * Start a depth-first traverse of the root and all of its descendants. - * @param root the root node point to traverse. - */ - public void traverse(Node root) { - Node node = root; - int depth = 0; - - while (node != null) { - visitor.head(node, depth); - if (node.childNodeSize() > 0) { - node = node.childNode(0); - depth++; - } else { - while (node.nextSibling() == null && depth > 0) { - visitor.tail(node, depth); - node = node.parentNode(); - depth--; - } - visitor.tail(node, depth); - if (node == root) - break; - node = node.nextSibling(); - } - } - } -} diff --git a/src/org/jsoup/select/NodeVisitor.java b/src/org/jsoup/select/NodeVisitor.java deleted file mode 100644 index 20112e8..0000000 --- a/src/org/jsoup/select/NodeVisitor.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.nodes.Node; - -/** - * Node visitor interface. Provide an implementing class to {@link NodeTraversor} to iterate through nodes. - *

- * This interface provides two methods, {@code head} and {@code tail}. The head method is called when the node is first - * seen, and the tail method when all of the node's children have been visited. As an example, head can be used to - * create a start tag for a node, and tail to create the end tag. - */ -public interface NodeVisitor { - /** - * Callback for when a node is first visited. - * - * @param node the node being visited. - * @param depth the depth of the node, relative to the root node. E.g., the root node has depth 0, and a child node - * of that will have depth 1. - */ - public void head(Node node, int depth); - - /** - * Callback for when a node is last visited, after all of its descendants have been visited. - * - * @param node the node being visited. - * @param depth the depth of the node, relative to the root node. E.g., the root node has depth 0, and a child node - * of that will have depth 1. - */ - public void tail(Node node, int depth); -} diff --git a/src/org/jsoup/select/QueryParser.java b/src/org/jsoup/select/QueryParser.java deleted file mode 100644 index 66a899c..0000000 --- a/src/org/jsoup/select/QueryParser.java +++ /dev/null @@ -1,356 +0,0 @@ -package org.jsoup.select; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.jsoup.helper.StringUtil; -import org.jsoup.helper.Validate; -import org.jsoup.parser.TokenQueue; - -/** - * Parses a CSS selector into an Evaluator tree. - */ -class QueryParser { - private final static String[] combinators = {",", ">", "+", "~", " "}; - private static final String[] AttributeEvals = new String[]{"=", "!=", "^=", "$=", "*=", "~="}; - - private TokenQueue tq; - private String query; - private List evals = new ArrayList(); - - /** - * Create a new QueryParser. - * @param query CSS query - */ - private QueryParser(String query) { - this.query = query; - this.tq = new TokenQueue(query); - } - - /** - * Parse a CSS query into an Evaluator. - * @param query CSS query - * @return Evaluator - */ - public static Evaluator parse(String query) { - QueryParser p = new QueryParser(query); - return p.parse(); - } - - /** - * Parse the query - * @return Evaluator - */ - Evaluator parse() { - tq.consumeWhitespace(); - - if (tq.matchesAny(combinators)) { // if starts with a combinator, use root as elements - evals.add(new StructuralEvaluator.Root()); - combinator(tq.consume()); - } else { - findElements(); - } - - while (!tq.isEmpty()) { - // hierarchy and extras - boolean seenWhite = tq.consumeWhitespace(); - - if (tq.matchesAny(combinators)) { - combinator(tq.consume()); - } else if (seenWhite) { - combinator(' '); - } else { // E.class, E#id, E[attr] etc. AND - findElements(); // take next el, #. etc off queue - } - } - - if (evals.size() == 1) - return evals.get(0); - - return new CombiningEvaluator.And(evals); - } - - private void combinator(char combinator) { - tq.consumeWhitespace(); - String subQuery = consumeSubQuery(); // support multi > childs - - Evaluator rootEval; // the new topmost evaluator - Evaluator currentEval; // the evaluator the new eval will be combined to. could be root, or rightmost or. - Evaluator newEval = parse(subQuery); // the evaluator to add into target evaluator - boolean replaceRightMost = false; - - if (evals.size() == 1) { - rootEval = currentEval = evals.get(0); - // make sure OR (,) has precedence: - if (rootEval instanceof CombiningEvaluator.Or && combinator != ',') { - currentEval = ((CombiningEvaluator.Or) currentEval).rightMostEvaluator(); - replaceRightMost = true; - } - } - else { - rootEval = currentEval = new CombiningEvaluator.And(evals); - } - evals.clear(); - - // for most combinators: change the current eval into an AND of the current eval and the new eval - if (combinator == '>') - currentEval = new CombiningEvaluator.And(newEval, new StructuralEvaluator.ImmediateParent(currentEval)); - else if (combinator == ' ') - currentEval = new CombiningEvaluator.And(newEval, new StructuralEvaluator.Parent(currentEval)); - else if (combinator == '+') - currentEval = new CombiningEvaluator.And(newEval, new StructuralEvaluator.ImmediatePreviousSibling(currentEval)); - else if (combinator == '~') - currentEval = new CombiningEvaluator.And(newEval, new StructuralEvaluator.PreviousSibling(currentEval)); - else if (combinator == ',') { // group or. - CombiningEvaluator.Or or; - if (currentEval instanceof CombiningEvaluator.Or) { - or = (CombiningEvaluator.Or) currentEval; - or.add(newEval); - } else { - or = new CombiningEvaluator.Or(); - or.add(currentEval); - or.add(newEval); - } - currentEval = or; - } - else - throw new Selector.SelectorParseException("Unknown combinator: " + combinator); - - if (replaceRightMost) - ((CombiningEvaluator.Or) rootEval).replaceRightMostEvaluator(currentEval); - else rootEval = currentEval; - evals.add(rootEval); - } - - private String consumeSubQuery() { - StringBuilder sq = new StringBuilder(); - while (!tq.isEmpty()) { - if (tq.matches("(")) - sq.append("(").append(tq.chompBalanced('(', ')')).append(")"); - else if (tq.matches("[")) - sq.append("[").append(tq.chompBalanced('[', ']')).append("]"); - else if (tq.matchesAny(combinators)) - break; - else - sq.append(tq.consume()); - } - return sq.toString(); - } - - private void findElements() { - if (tq.matchChomp("#")) - byId(); - else if (tq.matchChomp(".")) - byClass(); - else if (tq.matchesWord()) - byTag(); - else if (tq.matches("[")) - byAttribute(); - else if (tq.matchChomp("*")) - allElements(); - else if (tq.matchChomp(":lt(")) - indexLessThan(); - else if (tq.matchChomp(":gt(")) - indexGreaterThan(); - else if (tq.matchChomp(":eq(")) - indexEquals(); - else if (tq.matches(":has(")) - has(); - else if (tq.matches(":contains(")) - contains(false); - else if (tq.matches(":containsOwn(")) - contains(true); - else if (tq.matches(":matches(")) - matches(false); - else if (tq.matches(":matchesOwn(")) - matches(true); - else if (tq.matches(":not(")) - not(); - else if (tq.matchChomp(":nth-child(")) - cssNthChild(false, false); - else if (tq.matchChomp(":nth-last-child(")) - cssNthChild(true, false); - else if (tq.matchChomp(":nth-of-type(")) - cssNthChild(false, true); - else if (tq.matchChomp(":nth-last-of-type(")) - cssNthChild(true, true); - else if (tq.matchChomp(":first-child")) - evals.add(new Evaluator.IsFirstChild()); - else if (tq.matchChomp(":last-child")) - evals.add(new Evaluator.IsLastChild()); - else if (tq.matchChomp(":first-of-type")) - evals.add(new Evaluator.IsFirstOfType()); - else if (tq.matchChomp(":last-of-type")) - evals.add(new Evaluator.IsLastOfType()); - else if (tq.matchChomp(":only-child")) - evals.add(new Evaluator.IsOnlyChild()); - else if (tq.matchChomp(":only-of-type")) - evals.add(new Evaluator.IsOnlyOfType()); - else if (tq.matchChomp(":empty")) - evals.add(new Evaluator.IsEmpty()); - else if (tq.matchChomp(":root")) - evals.add(new Evaluator.IsRoot()); - else // unhandled - throw new Selector.SelectorParseException("Could not parse query '%s': unexpected token at '%s'", query, tq.remainder()); - - } - - private void byId() { - String id = tq.consumeCssIdentifier(); - Validate.notEmpty(id); - evals.add(new Evaluator.Id(id)); - } - - private void byClass() { - String className = tq.consumeCssIdentifier(); - Validate.notEmpty(className); - evals.add(new Evaluator.Class(className.trim().toLowerCase())); - } - - private void byTag() { - String tagName = tq.consumeElementSelector(); - Validate.notEmpty(tagName); - - // namespaces: if element name is "abc:def", selector must be "abc|def", so flip: - if (tagName.contains("|")) - tagName = tagName.replace("|", ":"); - - evals.add(new Evaluator.Tag(tagName.trim().toLowerCase())); - } - - private void byAttribute() { - TokenQueue cq = new TokenQueue(tq.chompBalanced('[', ']')); // content queue - String key = cq.consumeToAny(AttributeEvals); // eq, not, start, end, contain, match, (no val) - Validate.notEmpty(key); - cq.consumeWhitespace(); - - if (cq.isEmpty()) { - if (key.startsWith("^")) - evals.add(new Evaluator.AttributeStarting(key.substring(1))); - else - evals.add(new Evaluator.Attribute(key)); - } else { - if (cq.matchChomp("=")) - evals.add(new Evaluator.AttributeWithValue(key, cq.remainder())); - - else if (cq.matchChomp("!=")) - evals.add(new Evaluator.AttributeWithValueNot(key, cq.remainder())); - - else if (cq.matchChomp("^=")) - evals.add(new Evaluator.AttributeWithValueStarting(key, cq.remainder())); - - else if (cq.matchChomp("$=")) - evals.add(new Evaluator.AttributeWithValueEnding(key, cq.remainder())); - - else if (cq.matchChomp("*=")) - evals.add(new Evaluator.AttributeWithValueContaining(key, cq.remainder())); - - else if (cq.matchChomp("~=")) - evals.add(new Evaluator.AttributeWithValueMatching(key, Pattern.compile(cq.remainder()))); - else - throw new Selector.SelectorParseException("Could not parse attribute query '%s': unexpected token at '%s'", query, cq.remainder()); - } - } - - private void allElements() { - evals.add(new Evaluator.AllElements()); - } - - // pseudo selectors :lt, :gt, :eq - private void indexLessThan() { - evals.add(new Evaluator.IndexLessThan(consumeIndex())); - } - - private void indexGreaterThan() { - evals.add(new Evaluator.IndexGreaterThan(consumeIndex())); - } - - private void indexEquals() { - evals.add(new Evaluator.IndexEquals(consumeIndex())); - } - - //pseudo selectors :first-child, :last-child, :nth-child, ... - private static final Pattern NTH_AB = Pattern.compile("((\\+|-)?(\\d+)?)n(\\s*(\\+|-)?\\s*\\d+)?", Pattern.CASE_INSENSITIVE); - private static final Pattern NTH_B = Pattern.compile("(\\+|-)?(\\d+)"); - - private void cssNthChild(boolean backwards, boolean ofType) { - String argS = tq.chompTo(")").trim().toLowerCase(); - Matcher mAB = NTH_AB.matcher(argS); - Matcher mB = NTH_B.matcher(argS); - final int a, b; - if ("odd".equals(argS)) { - a = 2; - b = 1; - } else if ("even".equals(argS)) { - a = 2; - b = 0; - } else if (mAB.matches()) { - a = mAB.group(3) != null ? Integer.parseInt(mAB.group(1).replaceFirst("^\\+", "")) : 1; - b = mAB.group(4) != null ? Integer.parseInt(mAB.group(4).replaceFirst("^\\+", "")) : 0; - } else if (mB.matches()) { - a = 0; - b = Integer.parseInt(mB.group().replaceFirst("^\\+", "")); - } else { - throw new Selector.SelectorParseException("Could not parse nth-index '%s': unexpected format", argS); - } - if (ofType) - if (backwards) - evals.add(new Evaluator.IsNthLastOfType(a, b)); - else - evals.add(new Evaluator.IsNthOfType(a, b)); - else { - if (backwards) - evals.add(new Evaluator.IsNthLastChild(a, b)); - else - evals.add(new Evaluator.IsNthChild(a, b)); - } - } - - private int consumeIndex() { - String indexS = tq.chompTo(")").trim(); - Validate.isTrue(StringUtil.isNumeric(indexS), "Index must be numeric"); - return Integer.parseInt(indexS); - } - - // pseudo selector :has(el) - private void has() { - tq.consume(":has"); - String subQuery = tq.chompBalanced('(', ')'); - Validate.notEmpty(subQuery, ":has(el) subselect must not be empty"); - evals.add(new StructuralEvaluator.Has(parse(subQuery))); - } - - // pseudo selector :contains(text), containsOwn(text) - private void contains(boolean own) { - tq.consume(own ? ":containsOwn" : ":contains"); - String searchText = TokenQueue.unescape(tq.chompBalanced('(', ')')); - Validate.notEmpty(searchText, ":contains(text) query must not be empty"); - if (own) - evals.add(new Evaluator.ContainsOwnText(searchText)); - else - evals.add(new Evaluator.ContainsText(searchText)); - } - - // :matches(regex), matchesOwn(regex) - private void matches(boolean own) { - tq.consume(own ? ":matchesOwn" : ":matches"); - String regex = tq.chompBalanced('(', ')'); // don't unescape, as regex bits will be escaped - Validate.notEmpty(regex, ":matches(regex) query must not be empty"); - - if (own) - evals.add(new Evaluator.MatchesOwn(Pattern.compile(regex))); - else - evals.add(new Evaluator.Matches(Pattern.compile(regex))); - } - - // :not(selector) - private void not() { - tq.consume(":not"); - String subQuery = tq.chompBalanced('(', ')'); - Validate.notEmpty(subQuery, ":not(selector) subselect must not be empty"); - - evals.add(new StructuralEvaluator.Not(parse(subQuery))); - } -} diff --git a/src/org/jsoup/select/Selector.java b/src/org/jsoup/select/Selector.java deleted file mode 100644 index ccfe1fc..0000000 --- a/src/org/jsoup/select/Selector.java +++ /dev/null @@ -1,141 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.helper.Validate; -import org.jsoup.nodes.Element; - -import java.util.Collection; -import java.util.LinkedHashSet; - -/** - * CSS-like element selector, that finds elements matching a query. - *

- *

Selector syntax

- * A selector is a chain of simple selectors, separated by combinators. Selectors are case insensitive (including against - * elements, attributes, and attribute values). - *

- * The universal selector (*) is implicit when no element selector is supplied (i.e. {@code *.header} and {@code .header} - * is equivalent). - *

- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
PatternMatchesExample
*any element*
tagelements with the given tag namediv
ns|Eelements of type E in the namespace nsfb|name finds <fb:name> elements
#idelements with attribute ID of "id"div#wrap, #logo
.classelements with a class name of "class"div.left, .result
[attr]elements with an attribute named "attr" (with any value)a[href], [title]
[^attrPrefix]elements with an attribute name starting with "attrPrefix". Use to find elements with HTML5 datasets[^data-], div[^data-]
[attr=val]elements with an attribute named "attr", and value equal to "val"img[width=500], a[rel=nofollow]
[attr="val"]elements with an attribute named "attr", and value equal to "val"span[hello="Cleveland"][goodbye="Columbus"], a[rel="nofollow"]
[attr^=valPrefix]elements with an attribute named "attr", and value starting with "valPrefix"a[href^=http:]
[attr$=valSuffix]elements with an attribute named "attr", and value ending with "valSuffix"img[src$=.png]
[attr*=valContaining]elements with an attribute named "attr", and value containing "valContaining"a[href*=/search/]
[attr~=regex]elements with an attribute named "attr", and value matching the regular expressionimg[src~=(?i)\\.(png|jpe?g)]
The above may be combined in any orderdiv.header[title]

Combinators

E Fan F element descended from an E elementdiv a, .logo h1
E > Fan F direct child of Eol > li
E + Fan F element immediately preceded by sibling Eli + li, div.head + div
E ~ Fan F element preceded by sibling Eh1 ~ p
E, F, Gall matching elements E, F, or Ga[href], div, h3

Pseudo selectors

:lt(n)elements whose sibling index is less than ntd:lt(3) finds the first 2 cells of each row
:gt(n)elements whose sibling index is greater than ntd:gt(1) finds cells after skipping the first two
:eq(n)elements whose sibling index is equal to ntd:eq(0) finds the first cell of each row
:has(selector)elements that contains at least one element matching the selectordiv:has(p) finds divs that contain p elements
:not(selector)elements that do not match the selector. See also {@link Elements#not(String)}div:not(.logo) finds all divs that do not have the "logo" class.
div:not(:has(div)) finds divs that do not contain divs.
:contains(text)elements that contains the specified text. The search is case insensitive. The text may appear in the found element, or any of its descendants.p:contains(jsoup) finds p elements containing the text "jsoup".
:matches(regex)elements whose text matches the specified regular expression. The text may appear in the found element, or any of its descendants.td:matches(\\d+) finds table cells containing digits. div:matches((?i)login) finds divs containing the text, case insensitively.
:containsOwn(text)elements that directly contain the specified text. The search is case insensitive. The text must appear in the found element, not any of its descendants.p:containsOwn(jsoup) finds p elements with own text "jsoup".
:matchesOwn(regex)elements whose own text matches the specified regular expression. The text must appear in the found element, not any of its descendants.td:matchesOwn(\\d+) finds table cells directly containing digits. div:matchesOwn((?i)login) finds divs containing the text, case insensitively.
The above may be combined in any order and with other selectors.light:contains(name):eq(0)

Structural pseudo selectors

:rootThe element that is the root of the document. In HTML, this is the html element:root
:nth-child(an+b)

elements that have an+b-1 siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element. For values of a and b greater than zero, this effectively divides the element's children into groups of a elements (the last group taking the remainder), and selecting the bth element of each group. For example, this allows the selectors to address every other row in a table, and could be used to alternate the color of paragraph text in a cycle of four. The a and b values must be integers (positive, negative, or zero). The index of the first child of an element is 1.

- * In addition to this, :nth-child() can take odd and even as arguments instead. odd has the same signification as 2n+1, and even has the same signification as 2n.
tr:nth-child(2n+1) finds every odd row of a table. :nth-child(10n-1) the 9th, 19th, 29th, etc, element. li:nth-child(5) the 5h li
:nth-last-child(an+b)elements that have an+b-1 siblings after it in the document tree. Otherwise like :nth-child()tr:nth-last-child(-n+2) the last two rows of a table
:nth-of-type(an+b)pseudo-class notation represents an element that has an+b-1 siblings with the same expanded element name before it in the document tree, for any zero or positive integer value of n, and has a parent elementimg:nth-of-type(2n+1)
:nth-last-of-type(an+b)pseudo-class notation represents an element that has an+b-1 siblings with the same expanded element name after it in the document tree, for any zero or positive integer value of n, and has a parent elementimg:nth-last-of-type(2n+1)
:first-childelements that are the first child of some other element.div > p:first-child
:last-childelements that are the last child of some other element.ol > li:last-child
:first-of-typeelements that are the first sibling of its type in the list of children of its parent elementdl dt:first-of-type
:last-of-typeelements that are the last sibling of its type in the list of children of its parent elementtr > td:last-of-type
:only-childelements that have a parent element and whose parent element hasve no other element children
:only-of-type an element that has a parent element and whose parent element has no other element children with the same expanded element name
:emptyelements that have no children at all
- * - * @author Jonathan Hedley, jonathan@hedley.net - * @see Element#select(String) - */ -public class Selector { - private final Evaluator evaluator; - private final Element root; - - private Selector(String query, Element root) { - Validate.notNull(query); - query = query.trim(); - Validate.notEmpty(query); - Validate.notNull(root); - - this.evaluator = QueryParser.parse(query); - - this.root = root; - } - - /** - * Find elements matching selector. - * - * @param query CSS selector - * @param root root element to descend into - * @return matching elements, empty if not - */ - public static Elements select(String query, Element root) { - return new Selector(query, root).select(); - } - - /** - * Find elements matching selector. - * - * @param query CSS selector - * @param roots root elements to descend into - * @return matching elements, empty if not - */ - public static Elements select(String query, Iterable roots) { - Validate.notEmpty(query); - Validate.notNull(roots); - LinkedHashSet elements = new LinkedHashSet(); - - for (Element root : roots) { - elements.addAll(select(query, root)); - } - return new Elements(elements); - } - - private Elements select() { - return Collector.collect(evaluator, root); - } - - // exclude set. package open so that Elements can implement .not() selector. - static Elements filterOut(Collection elements, Collection outs) { - Elements output = new Elements(); - for (Element el : elements) { - boolean found = false; - for (Element out : outs) { - if (el.equals(out)) { - found = true; - break; - } - } - if (!found) - output.add(el); - } - return output; - } - - public static class SelectorParseException extends IllegalStateException { - public SelectorParseException(String msg, Object... params) { - super(String.format(msg, params)); - } - } -} diff --git a/src/org/jsoup/select/StructuralEvaluator.java b/src/org/jsoup/select/StructuralEvaluator.java deleted file mode 100644 index d00c1d3..0000000 --- a/src/org/jsoup/select/StructuralEvaluator.java +++ /dev/null @@ -1,138 +0,0 @@ -package org.jsoup.select; - -import org.jsoup.nodes.Element; - -/** - * Base structural evaluator. - */ -abstract class StructuralEvaluator extends Evaluator { - Evaluator evaluator; - - static class Root extends Evaluator { - public boolean matches(Element root, Element element) { - return root == element; - } - } - - static class Has extends StructuralEvaluator { - public Has(Evaluator evaluator) { - this.evaluator = evaluator; - } - - public boolean matches(Element root, Element element) { - for (Element e : element.getAllElements()) { - if (e != element && evaluator.matches(root, e)) - return true; - } - return false; - } - - @Override - public String toString() { - return String.format(":has(%s)", evaluator); - } - } - - static class Not extends StructuralEvaluator { - public Not(Evaluator evaluator) { - this.evaluator = evaluator; - } - - public boolean matches(Element root, Element node) { - return !evaluator.matches(root, node); - } - - @Override - public String toString() { - return String.format(":not%s", evaluator); - } - } - - static class Parent extends StructuralEvaluator { - public Parent(Evaluator evaluator) { - this.evaluator = evaluator; - } - - public boolean matches(Element root, Element element) { - if (root == element) - return false; - - Element parent = element.parent(); - while (parent != root) { - if (evaluator.matches(root, parent)) - return true; - parent = parent.parent(); - } - return false; - } - - @Override - public String toString() { - return String.format(":parent%s", evaluator); - } - } - - static class ImmediateParent extends StructuralEvaluator { - public ImmediateParent(Evaluator evaluator) { - this.evaluator = evaluator; - } - - public boolean matches(Element root, Element element) { - if (root == element) - return false; - - Element parent = element.parent(); - return parent != null && evaluator.matches(root, parent); - } - - @Override - public String toString() { - return String.format(":ImmediateParent%s", evaluator); - } - } - - static class PreviousSibling extends StructuralEvaluator { - public PreviousSibling(Evaluator evaluator) { - this.evaluator = evaluator; - } - - public boolean matches(Element root, Element element) { - if (root == element) - return false; - - Element prev = element.previousElementSibling(); - - while (prev != null) { - if (evaluator.matches(root, prev)) - return true; - - prev = prev.previousElementSibling(); - } - return false; - } - - @Override - public String toString() { - return String.format(":prev*%s", evaluator); - } - } - - static class ImmediatePreviousSibling extends StructuralEvaluator { - public ImmediatePreviousSibling(Evaluator evaluator) { - this.evaluator = evaluator; - } - - public boolean matches(Element root, Element element) { - if (root == element) - return false; - - Element prev = element.previousElementSibling(); - return prev != null && evaluator.matches(root, prev); - } - - @Override - public String toString() { - return String.format(":prev%s", evaluator); - } - } -} diff --git a/src/org/jsoup/select/package-info.java b/src/org/jsoup/select/package-info.java deleted file mode 100644 index a6e6a2f..0000000 --- a/src/org/jsoup/select/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - Packages to support the CSS-style element selector. - */ -package org.jsoup.select; \ No newline at end of file