Skip to content

Commit 6f8291d

Browse files
committed
Improved javadoc for StreamParser
1 parent 76b391e commit 6f8291d

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

src/main/java/org/jsoup/helper/DataUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static Document load(Path path, @Nullable String charsetName, String base
128128
* @param charset (optional) character set of input; specify {@code null} to attempt to autodetect from metadata.
129129
* A BOM in the file will always override this setting.
130130
* @param baseUri base URI of document, to resolve relative links against
131-
* @param parser alternate {@link Parser#xmlParser() parser} to use.
131+
* @param parser underlying HTML or XML parser to use.
132132
133133
* @return Document
134134
* @throws IOException on IO error

src/main/java/org/jsoup/nodes/Element.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public List<DataNode> dataNodes() {
472472
* @param cssQuery a {@link Selector} CSS-like query
473473
* @return an {@link Elements} list containing elements that match the query (empty if none match)
474474
* @see Selector selector query syntax
475-
* @see QueryParser#parse(String)
475+
* @see #select(Evaluator)
476476
* @throws Selector.SelectorParseException (unchecked) on an invalid CSS query.
477477
*/
478478
public Elements select(String cssQuery) {
@@ -485,6 +485,7 @@ public Elements select(String cssQuery) {
485485
* repeatedly parsing the CSS query.
486486
* @param evaluator an element evaluator
487487
* @return an {@link Elements} list containing elements that match the query (empty if none match)
488+
* @see QueryParser#parse(String)
488489
*/
489490
public Elements select(Evaluator evaluator) {
490491
return Selector.select(evaluator, this);

src/main/java/org/jsoup/parser/StreamParser.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
A StreamParser provides a progressive parse of its input. As each Element is completed, it is emitted via a Stream or
3030
Iterator interface. Elements returned will be complete with all their children, and an (empty) next sibling, if
3131
applicable.
32-
<p>Elements (or their children) may be removed from the DOM during the parse, for e.g. to conserve memory, providing a
33-
mechanism to parse an input document that would otherwise be too large to fit into memory, yet still providing a DOM
34-
interface to the document and its elements.</p>
32+
<p>To conserve memory, you can {@link Node#remove() remove()} Elements (or their children) from the DOM during the
33+
parse. This provides a mechanism to parse an input document that would otherwise be too large to fit into memory, yet
34+
still providing a DOM interface to the document and its elements.</p>
3535
<p>
3636
Additionally, the parser provides a {@link #selectFirst(String query)} / {@link #selectNext(String query)}, which will
3737
run the parser until a hit is found, at which point the parse is suspended. It can be resumed via another
@@ -45,6 +45,8 @@ interface to the document and its elements.</p>
4545
New parsers should be used in each thread.</p>
4646
<p>If created via {@link Connection.Response#streamParser()}, or another Reader that is I/O backed, the iterator and
4747
stream consumers will throw an {@link java.io.UncheckedIOException} if the underlying Reader errors during read.</p>
48+
<p>For examples, see the jsoup
49+
<a href="https://jsoup.org/cookbook/input/streamparser-dom-sax">StreamParser cookbook.</a></p>
4850
@since 1.18.1
4951
*/
5052
public class StreamParser implements Closeable {
@@ -204,6 +206,7 @@ public List<Node> completeFragment() throws IOException {
204206
@param query the {@link org.jsoup.select.Selector} query.
205207
@return the first matching {@link Element}, or {@code null} if there's no match
206208
@throws IOException if an I/O error occurs
209+
@see #selectFirst(Evaluator)
207210
*/
208211
public @Nullable Element selectFirst(String query) throws IOException {
209212
return selectFirst(QueryParser.parse(query));
@@ -228,9 +231,12 @@ public Element expectFirst(String query) throws IOException {
228231
/**
229232
Finds the first Element that matches the provided query. If the parsed Document does not already have a match, the
230233
input will be parsed until the first match is found, or the input is completely read.
234+
<p>By providing a compiled evaluator vs a CSS selector, this method may be more efficient when executing the same
235+
query against multiple documents.</p>
231236
@param eval the {@link org.jsoup.select.Selector} evaluator.
232237
@return the first matching {@link Element}, or {@code null} if there's no match
233238
@throws IOException if an I/O error occurs
239+
@see QueryParser#parse(String)
234240
*/
235241
public @Nullable Element selectFirst(Evaluator eval) throws IOException {
236242
final Document doc = document();
@@ -248,6 +254,7 @@ public Element expectFirst(String query) throws IOException {
248254
@param query the {@link org.jsoup.select.Selector} query.
249255
@return the next matching {@link Element}, or {@code null} if there's no match
250256
@throws IOException if an I/O error occurs
257+
@see #selectNext(Evaluator)
251258
*/
252259
public @Nullable Element selectNext(String query) throws IOException {
253260
return selectNext(QueryParser.parse(query));
@@ -272,9 +279,12 @@ public Element expectNext(String query) throws IOException {
272279
/**
273280
Finds the next Element that matches the provided query. The input will be parsed until the next match is found, or
274281
the input is completely read.
282+
<p>By providing a compiled evaluator vs a CSS selector, this method may be more efficient when executing the same
283+
query against multiple documents.</p>
275284
@param eval the {@link org.jsoup.select.Selector} evaluator.
276285
@return the next matching {@link Element}, or {@code null} if there's no match
277286
@throws IOException if an I/O error occurs
287+
@see QueryParser#parse(String)
278288
*/
279289
public @Nullable Element selectNext(Evaluator eval) throws IOException {
280290
try {

src/main/java/org/jsoup/select/Evaluator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323

2424
/**
25-
* Evaluates that an element matches the selector.
25+
An Evaluator tests if an element meets the selector's requirements. Obtain an evaluator for a given CSS selector
26+
with {@link QueryParser#parse}. If you are executing the same selector on many elements (or documents), it
27+
can be more efficient to compile and reuse an Evaluator than to reparse the selector on each invocation of select().
2628
*/
2729
public abstract class Evaluator {
2830
protected Evaluator() {

src/main/java/org/jsoup/select/QueryParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ private QueryParser(String query) {
3535
}
3636

3737
/**
38-
* Parse a CSS query into an Evaluator.
38+
* Parse a CSS query into an Evaluator. If you are evaluating the same query repeatedly, it may be more efficient to
39+
* parse it once and reuse the Evaluator.
3940
* @param query CSS query
4041
* @return Evaluator
4142
* @see Selector selector query syntax

0 commit comments

Comments
 (0)