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
package Torello.HTML.HelperPackages.parse;

import Torello.HTML.TagNode;
import Torello.HTML.HTMLTags;
import Torello.HTML.NodeSearch.ARGCHECK;

import java.util.*;
import java.util.function.Predicate;

public final class HTMLTagCounter
{
    // This will store the "HTML Tag Count"  This is a positive / negative count which is decremented
    // by the surrounding "search for-loop" in class "Surrounding."  It keeps a list of html-tags that
    // are requested an encountered, and decrements the counter as the for-loop scans (backwards, towards
    // the beginning of the vector) and find "closing-tags."
    private final TreeMap<String, Integer> countTable = new TreeMap<String, Integer>();

    private final boolean firstOrAll;

    public static final boolean EXCEPT  = true;
    public static final boolean NORMAL  = false;

    public static final boolean FIRST   = false;
    public static final boolean ALL     = true;

    @SuppressWarnings("unchecked")
    public HTMLTagCounter(String[] htmlTags, boolean exceptOrNormal, boolean firstOrAll)
    {
        this.firstOrAll = firstOrAll;
    
        if (htmlTags.length > 0) htmlTags = ARGCHECK.htmlTags(htmlTags);
    
        if ((htmlTags.length > 0) && (exceptOrNormal == NORMAL))

            for (String htmlTag : htmlTags)
                countTable.put(htmlTag, 0);

        else

            for ( Iterator<String> iter = HTMLTags.iterator(); iter.hasNext(); )
                countTable.put(iter.next(), 0);

        if (exceptOrNormal == EXCEPT)

            for (String htmlTag : htmlTags) countTable.remove(htmlTag);
    
    }

    public boolean allBanned() { return countTable.size() == 0; }

    public void reportFailed(String tok) { countTable.remove(tok); }

    public boolean check(TagNode tn)
    {
        Integer curValI = countTable.get(tn.tok);
        // System.out.print
        //     ("curValI = [" + ((curValI == null) ? "null" : curValI.toString()) + "]");

        if (curValI == null) return false;

        int curVal = curValI.intValue() + (tn.isClosing ? -1 : 1);
        // System.out.print(", curVal = [" + curVal + "]");
    
        boolean ret = (curVal >= 1);
        // System.out.print(", ret = [" + ret + "]");

        if (ret && (firstOrAll == FIRST))   countTable.remove(tn.tok);
        else                                countTable.put(tn.tok, Integer.valueOf(curVal));

        return ret;
    }
}