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
package Torello.HTML.Tools.JavaDoc;

import Torello.HTML.*;
import Torello.HTML.NodeSearch.*;
import Torello.Java.*;

import Torello.Java.Shell.C;

import java.util.*;
import java.io.*;

import java.util.function.Consumer;

/**
 * <CODE>CleanPackageSummaries - Documentation.</CODE><BR /><BR />
 * <EMBED CLASS="external-html" DATA-FILE-ID="CLEANPKSUMM">
 */
@StaticFunctional(Excused="sw", Excuses=Excuse.LOGGING)
public class CleanPackageSummaries
{
    private CleanPackageSummaries() { }

    private static final String[] searches = { ".", "<br/>", "\n" };

    private static StorageWriter sw = null;

    /**
     * Implents a cleaner.  This will remove extraneous notes that are inserted by Java Doc
     * into the {@code Class, Interface, Enum, Exception} and {@code Error} lists at the top
     * of a {@code 'package-summary.html'} page.
     * 
     * @param cietSummary The actual HTML that has been extracted from a <B>{@code Class
     * Summary}</B>
     */
    public static final void defaultCleaner(Vector<HTMLNode> cietSummary)
    {
        // System.out.println("INSIDE MY LAMBDA:\n" + Util.pageToString(cietSummary));

        int i=0, nodePos=-1;

        if (sw != null)
            sw.println("cietSummary: " + C.BGREEN + Util.pageToString(cietSummary) + C.RESET);

        while ((nodePos == -1) && (i < searches.length))
            nodePos = TextNodeFind.first(cietSummary, TextComparitor.CN_CI, searches[i++]);

        if (nodePos == -1)
        {
            sw.println("No String Markers Found!  Exiting...");
            return;
        }

        String  s   = cietSummary.elementAt(nodePos).str;
        int     pos = StrIndexOf.first_CI(s, searches[--i]);

        if (sw != null) sw.println("s: " + s);

        s = s.substring(0, pos);

        if (sw != null) sw.println("s.substring: " + s);

        if (s.length() != 0)
            cietSummary.setElementAt(new TextNode(s), nodePos++);

        if (nodePos < cietSummary.size())
            Util.removeRange(cietSummary, nodePos, cietSummary.size());

        Util.removeAllTagNodes(cietSummary);

        if (sw != null) sw.println(
            C.BCYAN + Util.pageToString(cietSummary) + C.RESET +
            "\n************************************************************\n"
        );
    }

    /**
     * This method will clean any potentially skewed or damaged package summary pages.  It
     * simply iterates through each {@code class, interface, enum, exception} and {@code error}
     * found on the summary page, and then invokes the cleaner on the HTML that is present in
     * that summary.  The default cleaner simply removes everything after the first sentence
     * in the page.
     * 
     * @param jdPage A vectorized Java-Doc Generated HTML Package-Summary Web-Page.
     * @param summaryCleaner This is the {@code Consumer} that should clean the summary.
     * @param sw The log output writer.
     */
    public static void run(
            Vector<HTMLNode> jdPage,
            Consumer<Vector<HTMLNode>> summaryCleaner,
            StorageWriter sw
        )
    {
        if (sw != null) sw.println("CleanPackageSummaries.run(...):");

        CleanPackageSummaries.sw = sw;
        int count = 0;

        HNLIInclusive iter = InnerTagInclusiveIterator.get
            (jdPage, "tr", "class", TextComparitor.C, "altColor", "rowColor");

        while (iter.hasNext())
        {
            DotPair dp      = iter.nextDotPair();

            DotPair cLast = InnerTagFindInclusive.first
                (jdPage, dp.start, dp.end, "td", "class", TextComparitor.C, "colLast");

            if (cLast == null) continue;

            DotPair div = InnerTagFindInclusive.first
                (jdPage, cLast.start, cLast.end, "div", "class", TextComparitor.C, "block");

            if (div == null) continue;
            if (div.size() < 3) continue;

            DotPair divContentsDP = new DotPair(div.start+1, div.end-1);

            Vector<HTMLNode> divContents = Util.cloneRange(jdPage, divContentsDP);
            summaryCleaner.accept(divContents);
            iter.replaceRange(divContentsDP, divContents);

            count++;
        }

        if (sw != null) sw.println(
            "\tCleaned " + C.BBLUE +  StringParse.zeroPad(count) + C.RESET +
            " '" + C.BCYAN + "package-summary.html" + C.RESET + "' CIETEE-Summary Dividers."
        );
    }
}