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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Torello.HTML.Tools.NewsSite;

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

import Torello.HTML.HTMLNode;

/**
 * An exception that is thrown from inside the <CODE><B>lambda-methods</B></CODE> created by the 
 * class <CODE><B>ArticleGet</B></CODE> methods - <I>if an error occurs while retrieving a news
 * article body from inside a news web-page</I>.
 * 
 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=ARTICLE_GET_EX>
 */
public class ArticleGetException extends Exception
{
    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUIDEX>  */
    public static final long serialVersionUID = 1;

    /**
     * If the code that has generated this exception has decided to pass the HTML 
     * page-{@code Vector} as a parameter to the constructor it has used, when this exception is
     * thrown, that page will be retained in this {@code public, final} parameter.  Not all four
     * of the provided constructor's for this exception require the {@code page} and {@code url}
     * be preserved.  <I>If the throwing code has declined to provide this reference to the
     * constructor, then the value of this {@code 'page'} field will be null.</I>
     */
    public final Vector<HTMLNode> page;

    /**
     * If the code that has generated this exception has decided to pass the HTML page-{@code URL}
     * as a parameter to the constructor it has used, when this exception is thrown, that page will
     * be retained in this {@code public, final} parameter. Not all four of the provided
     * constructor's for this exception require the {@code 'page'} and {@code 'URL'} be preserved.
     * <I>If the throwing code has declined to provide this reference to the constructor, then the
     * value of this {@code 'URL'} field will be null.</I>
     */
    public final URL url;

    /**
     * Creates a new {@code ArticleGetException} that may be thrown.
     *
     * @param message An explanation of the problem encountered when using the {@code ArticleGet}
     * consumer.
     */
    public ArticleGetException(String message)
    { this(message, null, null); }

    /**
     * Creates a new {@code ArticleGetException} that may be thrown, having the given message,
     * and a "previously thrown exception" (cause) - either from another thread, or caught and
     * re-branded into an {@code ArticleGetException}
     *
     * @param message The explanation of the problem encountered when parsing an HTML page using
     * the a user-defined, or factory-created {@code ArticleGet} instance.
     *
     * @param cause This may be a "cause" exception.  It can be caused, and facilitates a
     * programmer catching a problem, and then re-naming it to {@code ArticleGetException}
     */
    public ArticleGetException(String message, Throwable cause)
    { this(message, cause, null, null); }

    /**
     * When this constructor is used, the {@code URL} and page-{@code Vector} will be preserved
     * as {@code final} (constant) fields within this class instance for future reference and
     * debugging.
     *
     * @param message The explanation of the problem encountered when parsing an HTML page using
     * the a user-defined, or factory-created {@code ArticleGet} instance.
     *
     * @param url The {@code URL} from which the original HTML page-{@code Vector} was retrieved
     *
     * @param page The HTML page (as a {@code Vector}) that could not be resolved by the
     * {@code ArticleGet} instance which threw this exception.
     */
    public ArticleGetException(String message, URL url, Vector<HTMLNode> page)
    {
        super(message);
        this.url = url;
        this.page = page;
    }

    /**
     * When this constructor is used, the {@code URL} and page-{@code Vector} will be preserved
     * as {@code final} (constant) fields within this class instance for future reference and 
     * debugging.  If there was an underlying exception that caused the article-body retrieval to
     * fail, that will be stored in the Java {@code getCause();} method available.
     *
     * @param message The explanation of the problem encountered when parsing an HTML page using
     * the a user-defined, or factory-created {@code ArticleGet} instance.
     *
     * @param cause This may be a "cause" exception.  It can be caused, and facilitates a
     * programmer catching a problem, and then re-naming it to {@code ArticleGetException}
     *
     * @param url The {@code URL} from which the original HTML page-{@code Vector} was retrieved
     *
     * @param page The HTML page (as a {@code Vector}) that could not be resolved by the
     * {@code ArticleGet} instance which threw this exception.
     */
    public ArticleGetException(String message, Throwable cause, URL url, Vector<HTMLNode> page)
    {
        super(message, cause);
        this.url = url;
        this.page = page;
    }

    /**
     * When this constructor is used, the {@code URL} and page-{@code Vector} will be preserved
     * as {@code final} (constant) fields within this class instance for future reference and
     * debugging.
     *
     * @param url The {@code URL} from which the original HTML page-{@code Vector} was retrieved
     *
     * @param page The HTML page (as a {@code Vector}) that could not be resolved by the
     * {@code ArticleGet} instance which threw this exception.
     */
    public static void check(URL url, Vector<HTMLNode> page) throws ArticleGetException
    {
        if (url == null) throw new ArticleGetException
            ("ArticleGet.apply(...) has been called with a null URL.", url, page);

        if (page == null) throw new ArticleGetException
            ("ArticleGet.apply(...) has been called with a null page-vector.", url, page);

        if (page.size() == 0) throw new ArticleGetException
            ("ArticleGet.apply(...) has been called with a zero-length page-vector.", url, page);
    }

    static final int RET_NULL = 1;
    static final int RET_EMPTY_VECTOR = 2;
    static final int GOT_EXCEPTION = 3;

    // Constructor #1
    ArticleGetException(int FLAG, String functionNameStr, Exception e)
    { this(MESSAGE(FLAG, functionNameStr), e); }

    // Constructor #2
    ArticleGetException(int FLAG, String functionNameStr)
    { this(MESSAGE(FLAG, functionNameStr)); }

    private static String MESSAGE(int FLAG, String functionNameStr)
    {
        StringBuilder sb = new StringBuilder
            ("The article or page whose content-body you have tried to retrieve using your ArticleGet ");

        switch (FLAG)
        {
            case RET_NULL:          sb.append("produced null.");
                                    break;

            case RET_EMPTY_VECTOR:  sb.append("produced a ZERO-LENGTH vector.");
                                    break;

            case GOT_EXCEPTION:     sb.append("threw an exception while parsing.  (see e.getCause().)");
                                    break;

            default:                sb.append("failed.  Unknown cause.");
                                    break;
        }

        sb.append("\nFactory-generated ArticleGet had called: " + functionNameStr);

        return sb.toString();
    }
}