001package Torello.HTML.Tools.JavaDoc;
002
003import Torello.HTML.*;
004import Torello.HTML.NodeSearch.*;
005import Torello.Java.*;
006
007import Torello.Java.Shell.C;
008
009import java.util.*;
010import java.io.*;
011
012import java.util.function.Consumer;
013
014/**
015 * <CODE>CleanPackageSummaries - Documentation.</CODE><BR /><BR />
016 * <EMBED CLASS="external-html" DATA-FILE-ID="CLEANPKSUMM">
017 */
018@StaticFunctional(Excused="sw", Excuses=Excuse.LOGGING)
019public class CleanPackageSummaries
020{
021    private CleanPackageSummaries() { }
022
023    private static final String[] searches = { ".", "<br/>", "\n" };
024
025    private static StorageWriter sw = null;
026
027    /**
028     * Implents a cleaner.  This will remove extraneous notes that are inserted by Java Doc
029     * into the {@code Class, Interface, Enum, Exception} and {@code Error} lists at the top
030     * of a {@code 'package-summary.html'} page.
031     * 
032     * @param cietSummary The actual HTML that has been extracted from a <B>{@code Class
033     * Summary}</B>
034     */
035    public static final void defaultCleaner(Vector<HTMLNode> cietSummary)
036    {
037        // System.out.println("INSIDE MY LAMBDA:\n" + Util.pageToString(cietSummary));
038
039        int i=0, nodePos=-1;
040
041        if (sw != null)
042            sw.println("cietSummary: " + C.BGREEN + Util.pageToString(cietSummary) + C.RESET);
043
044        while ((nodePos == -1) && (i < searches.length))
045            nodePos = TextNodeFind.first(cietSummary, TextComparitor.CN_CI, searches[i++]);
046
047        if (nodePos == -1)
048        {
049            sw.println("No String Markers Found!  Exiting...");
050            return;
051        }
052
053        String  s   = cietSummary.elementAt(nodePos).str;
054        int     pos = StrIndexOf.first_CI(s, searches[--i]);
055
056        if (sw != null) sw.println("s: " + s);
057
058        s = s.substring(0, pos);
059
060        if (sw != null) sw.println("s.substring: " + s);
061
062        if (s.length() != 0)
063            cietSummary.setElementAt(new TextNode(s), nodePos++);
064
065        if (nodePos < cietSummary.size())
066            Util.removeRange(cietSummary, nodePos, cietSummary.size());
067
068        Util.removeAllTagNodes(cietSummary);
069
070        if (sw != null) sw.println(
071            C.BCYAN + Util.pageToString(cietSummary) + C.RESET +
072            "\n************************************************************\n"
073        );
074    }
075
076    /**
077     * This method will clean any potentially skewed or damaged package summary pages.  It
078     * simply iterates through each {@code class, interface, enum, exception} and {@code error}
079     * found on the summary page, and then invokes the cleaner on the HTML that is present in
080     * that summary.  The default cleaner simply removes everything after the first sentence
081     * in the page.
082     * 
083     * @param jdPage A vectorized Java-Doc Generated HTML Package-Summary Web-Page.
084     * @param summaryCleaner This is the {@code Consumer} that should clean the summary.
085     * @param sw The log output writer.
086     */
087    public static void run(
088            Vector<HTMLNode> jdPage,
089            Consumer<Vector<HTMLNode>> summaryCleaner,
090            StorageWriter sw
091        )
092    {
093        if (sw != null) sw.println("CleanPackageSummaries.run(...):");
094
095        CleanPackageSummaries.sw = sw;
096        int count = 0;
097
098        HNLIInclusive iter = InnerTagInclusiveIterator.get
099            (jdPage, "tr", "class", TextComparitor.C, "altColor", "rowColor");
100
101        while (iter.hasNext())
102        {
103            DotPair dp      = iter.nextDotPair();
104
105            DotPair cLast = InnerTagFindInclusive.first
106                (jdPage, dp.start, dp.end, "td", "class", TextComparitor.C, "colLast");
107
108            if (cLast == null) continue;
109
110            DotPair div = InnerTagFindInclusive.first
111                (jdPage, cLast.start, cLast.end, "div", "class", TextComparitor.C, "block");
112
113            if (div == null) continue;
114            if (div.size() < 3) continue;
115
116            DotPair divContentsDP = new DotPair(div.start+1, div.end-1);
117
118            Vector<HTMLNode> divContents = Util.cloneRange(jdPage, divContentsDP);
119            summaryCleaner.accept(divContents);
120            iter.replaceRange(divContentsDP, divContents);
121
122            count++;
123        }
124
125        if (sw != null) sw.println(
126            "\tCleaned " + C.BBLUE +  StringParse.zeroPad(count) + C.RESET +
127            " '" + C.BCYAN + "package-summary.html" + C.RESET + "' CIETEE-Summary Dividers."
128        );
129    }
130}