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

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

import java.util.function.*;

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

import Torello.Java.Shell.C;

/**
 * <CODE>HiLiteConstructors - Documentation.</CODE><BR /><BR />
 * <EMBED CLASS="external-html" DATA-FILE-ID="HLCONST">
 */
@StaticFunctional
public class HiLiteConstructors
{
    private HiLiteConstructors() { }

    private static final HTMLNode[] SURROUNDING_MESSAGE = HTMLPage
        .getPageTokens(
            "<DT><span class='" + Colorize.CSS_CLASS_FOR_CODE_METHOD_LABEL + "'>" +
            "Code:</span></dt>\n" +
            "<DD CLASS='" + Colorize.CSS_CLASS_FOR_CODE_METHOD_DEFINITION + "'>" +
            "<B>Exact Constructor Body:</B><BR />\n",
            false
        )
        .stream()
        .toArray(HTMLNode[]::new);

    /**
     * This method will hilite all {@code Constructor's} that are scraped from a Java Doc Generated
     * HTML Documentation Page for a Class, Enumerated Type, or Interface.  Hilited source-code for
     * constructor-bodies (as {@code String's}) are inserted into the Java-Doc HTML Web-Page in the
     * <B>{@code 'Constructor Details'}</B> section for any / all {@code Constructor's} found on
     * the page..
     * 
     * @param pr This contains all of the needed parameters for this method, encapsulated into a
     * single record-class.  The list is somewhat lengthy, so this makes the code "look cleaner"
     * 
     * @return This will return a count on exactly how many {@code Constructor's} have had their 
     * code-hilited {@code Constructor}-bodies inserted into the JavaDoc Documentation File for
     * this class.
     */
    public static int run(CommonParamRecord pr)
    {
        if (pr.sw != null) pr.sw.println
            ("HiLiteConstructors.run(...) " + C.BRED + "[REQUESTED] " + C.RESET);

        int countNumConstructorsHiLited = 0;
        int numConstructorsSkipped      = 0;

        // ********** Check whether the fileVec has any Constructors in the first place  **********
        if (Details.hasConstructorDetails(pr.fileVec) == null)
        {
            if (pr.sw != null) pr.sw.println(
                "\tJavaDoc File: " + C.BYELLOW + pr.jdFileName + C.RESET + " does not have a " +
                "Constructor-Details-Section, Skipping..."
            );
            return 0; // no constructors to hilite, skip this file
        }
        // ********** Check whether the fileVec has any Constructors in the first place  **********


        // ********** Iterate through every method found in the '.html' file  *****************
        HNLIInclusive   constructorsIter    = Details.constructorDetailsIterator(pr.fileVec);
        boolean         firstIteration      = true;
        int             cur                 = 0;

        while (constructorsIter.hasNext())
        {
            // Retrieves the next constructor-details section defined in the HTML file using the
            // "Constructor Details Iterator"
            DotPair constructorDP = constructorsIter.nextDotPair();

            // The instance of JavaDocHTMLFile (jdhf) was built using the same '.html' file, and
            // the same iterator.  Using the 'getConstructor(counter)' should retrieve the parsed
            // version of this HTML-defined java constructor from the javadoc documentation HTML
            // page.
            Constructor fromHTMLFileConstructor = pr.jdhf.getConstructor(cur++);

            // Now we need to search through all of the constructors in the JavaSourceCodeFile for
            // a constructor with the EXACT SAME PARAMETER LIST.
            Constructor fromSrcFileConstructor = (fromHTMLFileConstructor != null)
                ? pr.jscf.findConstructor(fromHTMLFileConstructor)
                : null;

            // Since the instance of "Constructor" that is retrieved from the Source-Code '.java'
            // file is guaranteed to have the "body text" filled out (as long as their was a body
            // defined for that constructor, i.e. - it wasn't 'abstract') - we can get that string
            // and save it and hilite it.
            String  constructorBodyStr = (fromSrcFileConstructor != null)
                ? fromSrcFileConstructor.body
                : null;

            // Output a tab or else it will look "lopsided"
            if (pr.sw != null) if (firstIteration) { firstIteration = false; pr.sw.print('\t'); }

            // If there was no constructor body found ... skip to the next constructor.
            if (constructorBodyStr == null)
            {
                if (pr.sw != null)
                    pr.sw.print
                        ("[NOT-FOUND]: " + C.BRED + fromHTMLFileConstructor.name + C.RESET + "  ");

                numConstructorsSkipped++;
                continue;
            }

            // PRINT OUT THE CONSTRUCTOR
            if (pr.sw != null) pr.sw.print(C.BGREEN + fromHTMLFileConstructor.name + C.RESET + "  ");


            // ********** Compute the Insertion Point *****************
            // The "<DL>  <DT></DT><DD></DD>  </DL>" lists are for the throws, see-also, returns,
            // etc....  The Code HiLited Constructor-Body HTML needs to be inserted into its own:
            // <DT>Code:</DT>\n
            // <DD> Hi-Lited Constructor-Body DIV created by HiLite.ME Server</DD>
            //
            // The Insertion point shall be right before the closing </DL> HTML Element.  This
            // means HiLited Code will *always* be the last 'thing' (for lack of a technical 
            // term) that you can see in a Constructor-Details Section.
            //
            // ALSO: There are some Constructor for which no Javadoc Comments have been written,
            //       therefore, there is no HTML "Definition List" already present in that
            //       particular constructor's "Constructor Details" Section.  In this case, an HTML
            //       <DL> ... </DL> surrounding element needs to be created.
            // ********** Compute the Insertion Point *****************

            int insertionPoint = TagNodeFind.last
                (pr.fileVec, constructorDP.start, constructorDP.end, TC.ClosingTags, "dl");

            boolean needToBuildDefList  = insertionPoint == -1;

            // ********** Compute the Insertion Point *****************
            // If we need to build the <DL> (Definition List), then we will also have to find a
            // new insertion point.  The constructor-details section ends with a closing '</LI>' 
            // element, and then a closing '</UL>' Element.  Right before the '</LI>' is where 
            // the insertion should occur.
            // ********** Compute the Insertion Point *****************

            if (needToBuildDefList) insertionPoint = TagNodeFind.last
                (pr.fileVec, constructorDP.start, constructorDP.end, TC.ClosingTags, "li");

            if (insertionPoint == -1)
                // This case should never happen, but keep it is here, just in case.
                // (Kind of the point of exceptions).  Guarantees no "Index Out of Bounds" 
                // Exceptions, and a friendly message.
            {
                if (pr.sw != null) pr.sw.println(
                    C.BRED + "Could not determine a place to put the hilited code in the JD Page. " +
                    "NOT INSERTING CODE." + C.RESET
                );

                continue;
            }

            // ********** Call the Code Hilite Server *****************
            constructorBodyStr = StrIndent.chompCallableBraces(constructorBodyStr);

            try
            { 
                constructorsIter.insertAt(
                    hiLiteConstructorBody(constructorBodyStr, needToBuildDefList, pr.hiLiter),
                    insertionPoint
                );
            }
            catch (IOException | HiLiteException e)
            {
                throw new HiLiteError(
                    "Failed to HiLite Constructor-Body (first three lines): " +
                    "[\n" + StringParse.firstNLines(constructorBodyStr, 3) + "\n].\n" +
                    "Currently Processing Java Source Code File: [" + pr.srcCodeFileName + "].\n" +
                    "Currently Processing JavaDoc HTML Documentation Page: [" + pr.jdFileName + "].\n" +
                    "Please see getCause() throwable for details.\n" + EXCC.toString(e),
                    e
                );
            }
            // ********** Call the Code Hilite Server *****************

            // Loop to the next constructor.
            countNumConstructorsHiLited++;
        }

        if ((countNumConstructorsHiLited > 0) || (numConstructorsSkipped > 0)) 
            if (pr.sw != null) pr.sw.println();

        if (numConstructorsSkipped > 0) 
            if (pr.sw != null) pr.sw.println(
                "\t" + C.BRED + "Skipped  " + C.RESET + 
                C.BBLUE + StringParse.zeroPad(numConstructorsSkipped, 5) + C.RESET + " Constructors."
            );

        if (pr.sw != null) pr.sw.println(
            "\tHilited  " + C.BBLUE + StringParse.zeroPad(countNumConstructorsHiLited, 5) + C.RESET +
            " Constructors."
        );

        return countNumConstructorsHiLited;
    }

    /**
     * This method is used to hilited the code of a Java <B>{@code Constructor Body}</B>
     * 
     * @param constructorBody This is the Java Source Code <B>{@code Constructor Body}</B>
     * Definition that was retrieved using the <B>{@code 'JavaParser Library'}</B>
     * 
     * @param needToBuildDefList This happens when there is no {@code <DL> DD/DT ... </DL>} list
     * already available on the page. In this case, and extra {@code '<DL>'} and {@code '</DL>'}
     * must attached to the HTML-{@code Vector} that is produced here.
     * 
     * @param hiLiter The {@code Functional Interface} module that performs <B>Code Hiliting</B>.
     * 
     * @return This will return the HiLited Source Code.  It is vectorized into a
     * {@code Vector<HTMLNode>}.
     * 
     * @throws IOException If there are I/O errors when calling the {@code HiLiter}
     * @throws HiLiteException If there are other errors when invoking the {@code HiLiter}
     * 
     * @see HiLiter#hiLite(String, String, boolean)     *
     * @see TagNodePeek
     * @see TagNodeIndex
     * @see HTMLTags#hasTag(String, TC)
     */
    private static Vector<HTMLNode> hiLiteConstructorBody
        (String constructorBody, boolean needToBuildDefList, HiLiter hiLiter)
        throws IOException, HiLiteException
    {
        // Call the "Pretty Print HiLiter"
        Vector<HTMLNode> newSubSection = hiLiter.hiLite(constructorBody, "java", true);

        for (int j = (SURROUNDING_MESSAGE.length - 1); j >= 0; j--) 
            newSubSection.add(0, SURROUNDING_MESSAGE[j]);

        // ***  The SURROUNDING_MESSAGE array has the opening <DT><SPAN ...>Code:</SPAN><DD>
        //      nodes that are needed before the hilited code.  HOWEVER, the "Closing </DD>" 
        //      HTML Element needs to be added *AFTER* (at the end of) the hilited code.

        newSubSection.add(HTMLTags.hasTag("dd", TC.ClosingTags));
            // adds </DD> to the end of this vector

        // *** If we have to create the surrounding <DL>...</DL> list, because the actual
        //     constructor-details section did not have one in the first place, add an opening
        //     <DL> and also a closing </DL> element at the beginning, and the end, respectively.

        if (needToBuildDefList)
        {
            newSubSection.add(0, HTMLTags.hasTag("dl", TC.OpeningTags));
                // adds <DL> the beginning of this vector

            newSubSection.add(HTMLTags.hasTag("dl", TC.ClosingTags));
                // adds </DL> to the end of this vector.
        }

        // *** Make all of them scrollable
        TagNodeIndex tni = TagNodePeek.first(newSubSection, TC.OpeningTags, "div");

        newSubSection.setElementAt(
            tni.n.appendToAV
                ("style", " max-height: 20em; overflow: auto; ", true, SD.SingleQuotes),
            tni.index
        );

        return newSubSection;
    }
}