1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Torello.HTML.Tools.NewsSite;

import Torello.HTML.HTMLNode;
import Torello.HTML.TextNode;
import Torello.HTML.Util;

import java.util.Vector;
import java.net.URL;
import java.util.regex.Pattern;

class Usual_pattern 
{
    public static ArticleGet generate(
            final Pattern startPattern,
            final Pattern endPattern
        )
    {
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // FAIL-FAST: Check user-input for possible errors BEFORE building the Lambda-Function.
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        if (startPattern == null) throw new NullPointerException
            ("Null has been passed to parameter 'startPattern', but this is not allowed here.");

        if (endPattern == null) throw new NullPointerException
            ("Null has been passed to parameter 'endPattern', but this is not allowed here.");


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Build the instance, using a lambda-expression
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        return (URL url, Vector<HTMLNode> page) ->
        {
            // This exception-check is done on every invokation of this Lambda-Function.
            // It is merely checking that these inputs are not-null, and page is of non-zero size.
            // ArticleGetException is a compile-time, checked exception.  It is important to halt
            // News-Site Scrape Progress when "Empty News-Page Data" is being passed here.
            // NOTE: This would imply an internal-error with class Download has occured.

            ArticleGetException.check(url, page);
            int         start   = -1;
            int         end     = -1;
            HTMLNode    n       = null;

            while (start++ < page.size())
                if ((n = page.elementAt(start)) instanceof TextNode)
                    if (startPattern.matcher(n.str).find())
                        break;

            while (end++ < page.size())
                if ((n = page.elementAt(end)) instanceof TextNode)
                    if (endPattern.matcher(n.str).find())
                        break;

            // These error-checks are used to deduce whether the "Article Get" was successful.
            // When this exception is thrown, it means that the user-specified means of "Retrieving
            // an Article Body" FAILED.  In this case it is because the start or end regex failed to
            // match.

            if (start == page.size()) throw new ArticleGetException(
                "Start Pattern [" + startPattern.toString() + "], was not found on the HTML " +
                "page."
            );

            if (end == page.size()) throw new ArticleGetException
                ("End Pattern [" + endPattern.toString() + "], was not found on the HTML page.");

            return Util.cloneRange(page, start, end + 1);
        };
    }

}