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

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

import Torello.Java.Additional.Ret2;
import Torello.Java.StorageWriter;
import Torello.Java.StringParse;
import Torello.Java.Shell.C;

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

/**
 * <B STYLE='color:darkred;'>Process Java Doc Web-Page:</B>
 * 
 * Remove the auto-generated summaries for each Method, Field, Constructor, Enum-Constant, and
 * Annotation-Element in the Summary Section.
 * 
 * <EMBED CLASS="external-html" DATA-FILE-ID=PKG_PRIVATE_MSG>
 * <EMBED CLASS="external-html" DATA-FILE-ID=CLEAN_SUMM>
 */
@StaticFunctional
public class CleanSummaries
{
    private CleanSummaries() { }

    @SuppressWarnings({"unchecked", "rawtypes"})
    static int removeAllDescriptionsFromSummaries(Iterator<SummaryTableHTML> summaryTables)
    {
        Messager.ifVPrintln
            ("SummaryDescriptions.removeAllDescriptionsFromSummaries(...) ");

        int remCount = 0;

        while (summaryTables.hasNext())
        {
            SummaryTableHTML table = summaryTables.next();

            int LAST_COL = (
                   (table.tableType == Entity.ENUM_CONSTANT)
                || (table.tableType == Entity.CONSTRUCTOR)
            )
                ? 2     // Enum-Constants only have 2 rows: name and description
                : 3;    // Everything else: type/modifiers, signature, description

            // REMEMBER: Some SummaryTableHTML instances are for "Inherited Entity" *ONLY*
            //           tables.  If there are no methods/fields/constructors defined inside of
            //           a type, then there will (obviously) be no <TABLE> ... </TABLE> with
            //           the method/field/constructor summary-rows.  In such cases, the 'headerRow'
            //           will be null.

            if (table.headerRow != null)
                remCount += TagNodeRemoveInclusive.nth(table.headerRow, LAST_COL, "td", "th");

            Iterator<Vector<HTMLNode>> rowIter = table.tableRowIterator();

            while (rowIter.hasNext())
                remCount += TagNodeRemoveInclusive.nth(rowIter.next(), LAST_COL, "td", "th");
        }

        if (Messager.isVerbose()) Messager.println(
            "\tRemoved  " + C.BBLUE + StringParse.zeroPad(remCount, 5) + C.RESET + 
            " Summary Descriptions HTMLNode's"
        );

        return remCount;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    static int typesRemovePackageName(Iterator<SummaryTableHTML> summaryTablesIterator)
    {
        int replaceCount = 0;

        while (summaryTablesIterator.hasNext())
        {
            SummaryTableHTML summaryTable = summaryTablesIterator.next();

            if (summaryTable.tableType == Entity.ENUM_CONSTANT) continue;
            if (summaryTable.tableType == Entity.INNER_CLASS)   continue;

            TextNode txn;

            Iterator<Vector<HTMLNode>> rowIterator = summaryTable.tableRowIterator();

            while (rowIterator.hasNext())
            {
                Vector<HTMLNode> row = rowIterator.next();

                for (int i=0; i < row.size(); i++)

                    if ((txn = row.elementAt(i).ifTextNode()) != null)
                    {
                        String oldStr = txn.str;
                        String newStr = removePackageNames(oldStr);

                        if (newStr.length() != oldStr.length())
                        {
                            row.setElementAt(new TextNode(newStr), i);
                            replaceCount++;
                        }
                    }
            }
        }

        return replaceCount;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Efficient-Replace Type-Package-Name
    // ********************************************************************************************
    // ********************************************************************************************


    private static final String[] removeStrs =
    {
        /* 00 */ "java.util.regex.",
        /* 01 */ "java.util.stream.",
        /* 02 */ "java.util.function.",
        /* 03 */ "java.util.concurrent.",
        /* 04 */ "java.util.",
        /* 05 */ "java.lang.reflect.",
        /* 06 */ "java.lang.", 
        /* 07 */ "java.net.",
        /* 08 */ "java.io.", 
        /* 09 */ "java.math.",

        /* 10 */ "com.github.javaparser.ast.body.",
        /* 11 */ "com.github.javaparser.ast.",
    };

    private static final int REM_STRS_1_START   = 0;
    private static final int REM_STRS_1_END     = 9;    // Loop-Conditional, Uses '<='
    private static final int REM_STRS_2_START   = 10;
    private static final int REM_STRS_2_END     = 11;   // Loop-Conditional, Uses '<='


    private static final String[] removeStrs2 =
    {
    };

    // This one day needs to be entered & included into "class Upgrade"
    private static void addAnotherRemoveStrs(String[] removeStrs, Predicate<String> quickTest)
    {
        for (int i=0; i < removeStrs.length; i++)
        
            if (! P1.test(removeStrs[i])) throw new IllegalArgumentException(
                "The " + i + StringParse.ordinalIndicator(i) + " of this array is not a valid " +
                "Package String."
            );

        // CleanSummaries.removeStrs = removeStrs;
    }

    private static final Predicate<String> P1 =
        Pattern.compile("^\\w[\\w\\d&]*\\.(\\w[\\w\\d&]*\\.)*$").asPredicate();

    static String removePackageNames(String s)
    {
        // Java Stream's shall keep records of where the matches occurred
        IntStream.Builder   where   = IntStream.builder();
        IntStream.Builder   which   = IntStream.builder();
        int                 delta   = 0;
        final int           END     = s.length() - 10; // ("java.io.XX".length())
        String              TEMP    = null;

        // This part of the code finds the locations of all the matches in the input string.
        // It does not build the new String, but rather, finds indexes first.  This way a
        // char[] array can be built, and then populated with the updated sub-strings.

        TOP:
        for (int i=0; i < END; i++)

            if  (   (s.charAt(i)    == 'j')
                &&  (s.charAt(i+1)  == 'a')
                &&  (s.charAt(i+2)  == 'v')
                &&  (s.charAt(i+3)  == 'a')
                &&  (s.charAt(i+4)  == '.')
            )
            {
                for (int j=REM_STRS_1_START; j <= REM_STRS_1_END; j++)

                    if (s.regionMatches(i, TEMP = removeStrs[j], 0, TEMP.length()))
                    {
                        where.accept(i);        // WHERE a match has occured
                        which.accept(j);        // WHICH match has occured
                        delta -= TEMP.length(); // num-characters Size-Reduction
                        i += TEMP.length() - 1; // Advance the index pointer

                        continue TOP;
                    }
            }

            else if (   (s.charAt(i)    == 'c')
                    &&  (s.charAt(i+1)  == 'o')
                    &&  (s.charAt(i+2)  == 'm')
                    &&  (s.charAt(i+3)  == '.')
                    &&  (s.charAt(i+4)  == 'g')
            )
                for (int j=REM_STRS_2_START; j <= REM_STRS_2_END; j++)
                {
                    if (s.regionMatches(i, TEMP = removeStrs[j], 0, TEMP.length()))
                    {
                        where.accept(i);        // WHERE a match has occured
                        which.accept(j);        // WHICH match has occured
                        delta -= TEMP.length(); // num-characters Size-Reduction
                        i += TEMP.length() - 1; // Advance the index pointer

                        continue TOP;
                    }
                }

        // List of indices into the input-String for WHERE matches occurred.
        int[] wherePosArr = where.build().toArray();

        // List of indices into the match-array for WHICH matches occurred.
        int[] whichStrIndices = which.build().toArray();

        // The new "Char Array" which will be built into a String.  The "change in size" was
        // computed earlier

        char[] cArr = new char[s.length() + delta];

        // These are some loop-control variables
        int     oldStrPos   = 0;
        int     newStrPos   = 0;
        int     index       = 0;

        // This is a "Pre-Loop Priming Update" or "Priming Read".  It just
        // copies the first non-matching sub-string portion to the cArr[]

        int end = (index < whichStrIndices.length) ? wherePosArr[index] : s.length();

        while (oldStrPos != end) cArr[newStrPos++] = s.charAt(oldStrPos++);

        while (index < whichStrIndices.length)
        {
            oldStrPos += removeStrs[whichStrIndices[index]].length();
            index++;

            // Copy the next non-matching sub-string section from the "Old Input String"
            end = (index < whichStrIndices.length) ? wherePosArr[index] : s.length();

            s.getChars(oldStrPos, end, cArr, newStrPos);

            // Advance the Pointer
            newStrPos += (end - oldStrPos);
            oldStrPos = end;
        }

        return new String(cArr);
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // NOTES
    // ********************************************************************************************
    // ********************************************************************************************


    // One-Day, Soon, There is going to be an Upgrade Class called "JavaDocConfiguration" and
    // it will allow a user to set which version of JavaDoc (and which Java, possibly) he is
    // using.  When that happens, these notes are going to b valuable.

    static int removeAll(SummaryTableHTML<?> table)
    {
        throw new Torello.Java.UnreachableError();
            // This method is no longer used, it is being kept around because the notes in
            // the comments explain some things that will be relevant again, soon.

        // NEWER JavaDoc's add a "Description" column to constructors.  Older Javadoc have a
        // single column called "Constructor & Description"  The 'if statement' after this 
        // checks for '3 columns' but constructors only have 2 columns (unlike the others)

        /*
        DotPair constructorTH = InnerTagFindInclusive.first
            (fileVec, dp.start, dp.end, "th", "class", TextComparitor.C, "colLast");
        

        if (constructorTH != null)
            if (TextNodeFind.first
                (fileVec, constructorTH.start, constructorTH.end, s -> s.equals("Description")) != -1)
                    Util.removeRange(fileVec, constructorTH);
        */

        // NEWER JavaDoc's have a "colSecond" (3 columns)
        /*
        if (InnerTagCount.all
            (fileVec, dp.start, dp.end, "th", "class", TextComparitor.C, "colSecond") > 0)
        {
            // This removes the column HEADERS...  It will not be included in the "remove count" 
            // return value.
            InnerTagRemoveInclusive.all
                (fileVec, dp.start, dp.end, "th", "class", TextComparitor.C, "colLast");

            // These are the actual summary-description columns.  Remove them all at the same time.
            // Return as "remove count"
            return InnerTagRemoveInclusive.all
                (fileVec, dp.start, dp.end, "td", "class", TextComparitor.C, "colLast");
        }
        */

        /*
        for (Vector<HTMLNode> row : table.tableRows)
        {
            remCount++;
            TagNodeRemoveInclusive.nth(row, 3, "td");
        }
        */

        /*
        // OLDER JavaDoc's have "colFirst" and "colLast" (2 columns)
        int             remCount    = 0;
        HNLIInclusive   iter        = funcPtr.apply(fileVec);

        while (iter.hasNext())
        {
            Vector<HTMLNode>    summary = iter.next();
            int[]               posArr  = TagNodeFind.all(summary, TC.Both, "div");

            // A summary that had html dividers means a "summary description" was present.
            // The purpose of this method is to remove those.
            if (posArr.length > 0)
            {
                remCount++; 
                Util.removeRange(summary, posArr[0], posArr[posArr.length - 1] + 1);
                iter.set(summary);
            }
        }

        return remCount;
        */
    }
}