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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package Torello.HTML.NodeSearch;

import Torello.HTML.*;
import Torello.Java.NException;

import java.util.function.Predicate;
import java.util.Vector;

/**
 * <CODE>ARGSCHECK - Documentation.</CODE><BR /><BR />
 * This class is used internally to do argument validity checks.  It guarantees consistent
 * exception-message reporting.
 */
@Torello.HTML.Tools.JavaDoc.StaticFunctional
public class ARGCHECK
{
    private ARGCHECK() { }
    /**
     * Internally Used.  Checks for valid values of {@code 'nth'} to the search methods {@code nth}
     * and {@code nthFromEnd}.
     * 
     * @param nth This is a number identify which search-match to return.  If {@code n = 3}, the
     * third match shall be returned.  Remember to ensure that a negative number is not passed, nor
     * a number too large.
     * 
     * @param v This should be the html-{@code Vector} on which a search is being performed.
     * 
     * <EMBED CLASS="external-html" DATA-FILE-ID="RAWTYPES">
     * 
     * @throws NException When a negative value for {@code 'nth'} is passed, or {@code 'nth'} is
     * greater than the size of the {@code Vector}.
     */
    public static int n(int nth, Vector<?> v)
    {
        NException.check(nth, v);
        return nth;
    }

    /**
     * Internally Used.  Checks for valid and invalid HTML Token arguments to all search methods.
     * Calls Java {@code String} method {@code toLowerCase()} on HTML Token and returns it.  All
     * HTML tokens are stored in lower-case form internally, for quicker comparisons.
     * 
     * @param htmlTag Make sure the user is searching for a valid HTMT Element.  If new tags
     * have been added to the java {@code Collection} (internally represented by 
     * {@code class java.util.TreeSet}), make sure the check those too.
     * 
     * @throws HTMLTokException When an invalid HTML Token Element is passed.
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see HTMLTags
     */
    public static String htmlTag(String htmlTag)
    {
        HTMLTokException.check(htmlTag);
        return htmlTag.toLowerCase();
    }

    /**
     * Internally Used.  Checks multiple HTML Token arguments to all search methods for validity.
     * Calls Java {@code String} method {@code String.toLowerCase()} on each HTML Token and returns
     * the array of tokens - <I>all in lower-case</I>.  All HTML tokens are stored in lower-case
     * form internally, for quicker comparisons.
     * 
     * <BR /><BR /><B><SPAN STYLE="color: red;">IMPORTANT NOTE:</B></SPAN> Here, the 
     * {@code varargs} parameter {@code String[] htmlTags} is not modified; but rather, a new
     * instance of the imported {@code String[] array} is created with lower-cased {@code String's}
     * stored in place of the original {@code String's}.  All Java-{@code String's} are immutable,
     * and here new lower-case {@code String's} are being saved.  The original input parameter
     * isn't modified.
     * 
     * @param htmlTags These are a list of html-tags to be checked.
     * 
     * @throws HTMLTokException When <I>any</I> invalid HTML Token Element is passed.
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see Torello.HTML.HTMLTags
     * @see HTMLTokException#check(String[])
     */
    public static String[] htmlTags(String... htmlTags)
    {
        HTMLTokException.check(htmlTags);
        String[] ret = new String[htmlTags.length];
        for (int i=0; i < htmlTags.length; i++) ret[i] = htmlTags[i].toLowerCase();
        return ret;
    }

    /**
     * Internally Used.  Checks validity of inner-tag arguments to search routines.  Returns
     * lower-case version of the inner-tag.
     * 
     * @throws InnerTagKeyException If an inner-tag value is invalid. 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see InnerTagKeyException#check(String[])
     */
    public static String innerTag(String innerTag)
    {
        InnerTagKeyException.check(innerTag);
        return innerTag.toLowerCase();
    }

    public static final Predicate<String> TRUE = (String innerTagValue) -> true;

    /**
     * This just keeps a short list of all the instance-objects of {@code class TextComparitor}
     * that perform the "CSS Class" Checks.  These {@code TextComparitor's}, when used with a 
     * {@code var-args String[] array} input, should have a finer-grain of exception checking and
     * error checking performed on their inputs.  Checking to ensure CSS Class {@code String's} 
     * inside of HTML Elements are <I><B>only checked against valid CSS-Names</I></B> is possible
     * in this argument-checking class.
     */
    protected static final TextComparitor[] CSSTCs =
    { 
        TextComparitor.C, TextComparitor.C_OR, TextComparitor.C_AND, 
        TextComparitor.C_NAND, TextComparitor.C_XOR
    };

    /**
     * Internally Used.  Checks validity of the {@code TextComparitor String's}, and the 
     * {@code TextComparitor}.  Wraps the {@code TextComparitor} and its
     * {@code String... compareStr} arguments into a Java Lambda {@code Predicate<String>}.
     * 
     * @param tc Any valid instance of {@code TextComparitor}
     * 
     * @param compareStr A {@code varargs String[] array} of test-{@code String's}.
     * 
     * @throws TCCompareStrException This is thrown for invalid search parameters.  Review the
     * {@code Exception} class itself for what constitutes invalid parameter data.
     * 
     * @throws CSSStrException If one of the provided comparison {@code String's} is not a valid
     * CSS name, <B><I>and</I></B> the {@code TextComparitor} passed is a CSS Class Test
     * {@code TextComparitor}.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @return This returns a {@code String-Predicate} that uses the provided
     * {@code TextComparitor} and its associated {@code 'compareStr' String[] array} list to test
     * input {@code String's}.
     * 
     * @see TCCompareStrException#check(String[])
     * @see TextComparitor
     * @see TextComparitor#test(String, String[])
     */
    public static final Predicate<String> TC(TextComparitor tc, String... compareStr)
    {
        if (tc == null) throw new NullPointerException(
            "You have passed a null value to the parameter TextComparitor here, " +
            "but this is not allowed."
        );

        TCCompareStrException.check(compareStr);

        for (TextComparitor cssTC : CSSTCs)
            if (cssTC == tc)
            { CSSStrException.check(compareStr); break; }

        // String[] cmpStr = compareStr.clone(); // MADNESS, ISN'T IT?
        return (String innerTagValue) -> tc.test(innerTagValue, compareStr);
    }

    /**
     * Internally Used.  Checks validity of the {@code TextComparitor String's}, and the
     * {@code TextComparitor}.
     * Wraps the {@code TextComparitor} and its {@code String... compareStr} arguments into a
     * Java Lambda {@code Predicate<String>}.
     * 
     * @param tc Any valid instance of {@code TextComparitor}
     * 
     * @param compareStr A {@code varargs String[] array} of test-{@code String's}.
     * 
     * @throws TCCompareStrException This is thrown for invalid search parameters.  Review the
     * {@code Exception} class itself for what constitutes invalid parameter data.
     * 
     * @throws CSSStrException If one of the provided {@code compareStr's} is not a valid CSS name,
     * <B><I>and</I></B> the {@code TextComparitor} passed is a CSS Class Test
     * {@code TextComparitor}.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @return This returns a {@code Predicate<TextNode>} that uses the provided 
     * {@code TextComparitor} and {@code compareStr's} to test input {@code TextNode's} using the
     * {@code 'TextNode.str'} field.
     * 
     * @see TCCompareStrException#check(String[])
     * @see TextComparitor
     * @see TextComparitor#test(String, String[])
     * @see HTMLNode#str
     * @see TextNode#str
     */
    public static final Predicate<TextNode> TC_TXNP(TextComparitor tc, String... compareStr)
    {
        if (tc == null) throw new NullPointerException(
            "You have passed a null value to the parameter TextComparitor here, " +
            "but this is not allowed."
        );

        TCCompareStrException.check(compareStr);

        for (TextComparitor cssTC : CSSTCs)
            if (cssTC == tc)
            { CSSStrException.check(compareStr); break; }

        // String[] cmpStr = compareStr.clone(); // MADNESS, ISN'T IT?
        return (TextNode txn) -> tc.test(txn.str, compareStr);
    }


    /**
     * Internally Used.  Checks validity of the {@code TextComparitor String's}, and the
     * {@code TextComparitor}.  Wraps the {@code TextComparitor} and its
     * {@code String... compareStr} arguments into a Java Lambda {@code Predicate}.
     * 
     * @param tc Any valid instance of {@code TextComparitor}
     * 
     * @param compareStr A {@code varargs String[] array} of test-{@code String's}.
     * 
     * @throws TCCompareStrException This is thrown for invalid search parameters.  Review the
     * {@code Exception} class itself for what constitutes invalid parameter data.
     * 
     * @throws CSSStrException If one of the provided {@code compareStr's} is not a valid CSS name,
     * <B><I>and</I></B> the {@code TextComparitor} passed is a CSS Class Test
     * {@code TextComparitor}.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @return This returns a {@code Predicate<CommentNode>} that uses the provided
     * {@code TextComparitor} and {@code compareStr's} to test input {@code CommentNode's} using
     * the {@code CommentNode.body} field.
     * 
     * @see TCCompareStrException#check(String[])
     * @see TextComparitor
     * @see TextComparitor#test(String, String[])
     * @see CommentNode#body
     */
    public static final Predicate<CommentNode> TC_CNP(TextComparitor tc, String... compareStr)
    {
        if (tc == null) throw new NullPointerException(
            "You have passed a null value to the parameter TextComparitor here, " +
            "but this is not allowed."
        );

        TCCompareStrException.check(compareStr);

        for (TextComparitor cssTC : CSSTCs)
            if (cssTC == tc)
            { CSSStrException.check(compareStr); break; }

        // String[] cmpStr = compareStr.clone(); // MADNESS, ISN'T IT?
        return (CommentNode cn) -> tc.test(cn.body, compareStr);
    }

    /**
     * Internally Used.  Checks that a Java Regex {@code Pattern} is not null.  Wraps the reg-ex
     * {@code Pattern} into a Java Lambda {@code Predicate<String>} Tester Expression.
     * 
     * @param p This may be any valid input regular-expression {@code Pattern}.
     * 
     * @return This shall return a {@code Predicate<String>} that tests input {@code String's} 
     * against the regular-expression.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     */
    public static final Predicate<String> REGEX(java.util.regex.Pattern p)
    {
        if (p == null) throw new NullPointerException(
            "You have passed a null value to a search-method's regular-expression 'Pattern' " +
            "parameter, but this is not allowed."
        );

        return p.asPredicate();

        // return (String innerTagValue) -> p.matcher(innerTagValue).find();
    }

    /**
     * Internally Used.  Checks that a Java Regex {@code Pattern} is not null.  Wraps the reg-ex
     * {@code Pattern} into a Java Lambda {@code Predicate<TextNode>} Tester Expression.
     * 
     * @param p This may be any valid input regular-expression {@code Pattern}.
     * 
     * @return This shall return a {@code Predicate<TextNode>} that tests input {@code TextNode's}
     * (using the {@code TextNode.str} - inherited from {@code HTMLNode.str} field) against the
     * regular-expression.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see HTMLNode#str
     * @see TextNode#str
     */
    public static final Predicate<TextNode> REGEX_TXNP(java.util.regex.Pattern p)
    {
        if (p == null) throw new NullPointerException(
            "You have passed a null value to a search-method's regular-expression 'Pattern' " +
            "parameter, but this is not allowed."
        );

        Predicate<String> pred = p.asPredicate();
        return (TextNode txn) -> pred.test(txn.str);

        // return (TextNode txn) -> p.matcher(txn.str).find();
    }

    /**
     * Internally Used.  Checks that a Java Regex {@code Pattern} is not null.  Wraps the reg-ex
     * {@code Pattern} into a Java Lambda {@code Predicate<CommentNode>} Tester Expression.
     * 
     * @param p This may be any valid input regular-expression pattern.
     * 
     * @return This shall return a {@code CommentNode-Predicate} that tests input
     * {@code CommentNode's} (using the {@code CommentNode.body} field) against the
     * regular-expression.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see CommentNode#body
     */
    public static final Predicate<CommentNode> REGEX_CNP(java.util.regex.Pattern p)
    {
        if (p == null) throw new NullPointerException(
            "You have passed a null value to a search-method's regular-expression 'Pattern' " +
            "parameter, but this is not allowed."
        );

        Predicate<String> pred = p.asPredicate();
        return (CommentNode cn) -> pred.test(cn.body);

        // return (CommentNode cn) -> p.matcher(cn.body).find();
    }


    /**
     * Internally Used.  Checks that a Java Regex {@code Pattern} is not null.  Wraps the reg-ex
     * {@code Pattern} into a Java Lambda {@code Predicate<TextNode>} Tester Expression.
     * 
     * @param p This may be any valid {@code String-Predicate}
     * 
     * @return This shall return a {@code TextNode-Predicate} that "wraps" the input
     * {@code String-Predicate} and uses the field {@code TextNode.str} (inherited from
     * {@code HTMLNode.str}) as a value for the wrapped, "original," {@code String-Predicate} test.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see TextNode#str
     * @see HTMLNode#str
     */
    public static final Predicate<TextNode> SP_TO_TXNP(Predicate<String> p)
    {
        if (p == null) throw new NullPointerException(
            "You have passed a null value to a search-method's String-Predicate 'p' parameter, " +
            "but this is not allowed."
        );

        return (TextNode txn) -> p.test(txn.str);
    }


    /**
     * Internally Used.  Checks that a Java Regex {@code Pattern} is not null.  Wraps the reg-ex
     * {@code Pattern} into a Java Lambda {@code Predicate<CommentNode>} Tester Expression.
     * 
     * @param p This may be any valid {@code String-Predicate}
     * 
     * @return This shall return a {@code CommentNode-Predicate} that "wraps" the input
     * {@code String-Predicate} and uses the field {@code CommentNode.body} as a value for the
     * wrapped, "original," {@code String-Predicate} test.
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * 
     * @see CommentNode#body
     */
    public static final Predicate<CommentNode> SP_TO_CNP(Predicate<String> p)
    {
        if (p == null) throw new NullPointerException(
            "You have passed a null value to a search-method's String-Predicate 'p' parameter, " +
            "but this is not allowed."
        );

        return (CommentNode cn) -> p.test(cn.body);
    }

    /**
     * Internally Used.  Checks to ensure that an index-parameter, when use in conjunction with the
     * {@code class Surrounding} (which is sometimes referred to by the term 'Container') find
     * methods is properly within the bounds of the HTML page.
     * 
     * @param page This may be any valid vectorized-html page or sub-page.  
     * 
     * @param index This is expected to be an index into the 'page' parameter-vector.  If this is
     * not a valid index, then this method shall throw an exception.
     * 
     * @return This method returns the exact-value of input parameter 'index'
     * 
     * @throws NullPointerException If any of the provided input reference parameters are null.
     * @throws IndexOutOfBoundsException If the 'index' parameter is not a valid index into the
     * 'page' parameter, then this exception will be thrown.
     */
    public static int index(Vector<? extends HTMLNode> page, int index)
    {
        if (index < 0) throw new IndexOutOfBoundsException(
            "The index you passed [" + index + "] into vectorized-HTML page was negative, " +
            "this is not allowed here."
        );

        if (index >= page.size()) throw new IndexOutOfBoundsException(
            "The index you passed [" + index + "] into vectorized-HTML page was is greater than " +
            "the vector's size [" + page.size() + "]"
        );

        return index;
    }
}