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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package Torello.JavaDoc;

import Torello.HTML.HTMLNode;
import Torello.HTML.SubSection;
import Torello.HTML.Replaceable;
import Torello.HTML.Util;

import Torello.Java.StrPrint;
import static Torello.Java.C.*;

import Torello.JDUInternal.Messager.Messager;
import Torello.JDUInternal.Messager.Where.JDUUserAPI;
import Torello.JDUInternal.Messager.Where.Where_Am_I;

import Torello.JDUInternal.Parse.HTML.HeaderFooter.D1_HeaderFooterRec;

import java.util.Vector;

/**
 * Encapsulates the HTML placed both the top and the bottom of a JavaDoc Web-Page, including the
 * Navigation-Bar HTML, the Package-Name, Type-Signature and other Banner-Labesl.
 * 
 * <EMBED CLASS='external-html' DATA-FILE-ID=PROG_MOD_HTML>
 * <EMBED CLASS='external-html' DATA-FILE-ID=HEADER_FOOTER>
 */
@JDHeaderBackgroundImg(EmbedTagFileID="REFLECTION_HTML_CLASS")
public class HeaderFooterHTML
{
    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
    protected static final long serialVersionUID = 1;

    // When the Messager Reports its errors, this class passes this reference to the Messager
    // to facilitate the printing of that information (What class encountered an error or warning
    // that needs to be printed by the Messager).

    private static final Where_Am_I WHERE_AM_I = JDUUserAPI.HeaderFooterHTML;


    // ********************************************************************************************
    // ********************************************************************************************
    // Constructor - Fields are computed in package: Torello.JDUInternal.Parse.HTML.Other
    // ********************************************************************************************
    // ********************************************************************************************


    // This constructor is invoked by JavaDocHTMLFile
    // The work for this constructor has since been moved into a class inside package:
    // 
    // Torello.JDUInternal.Parse.HTML.Other
    // 
    // Keeping HTML-Parsing code in a separate, internal, package allows me to "localize" the
    // complicated parsing code into a single location - far away from the end user.  This code
    // is dependent upon which of the Java-Doc Versions is being used.
    // 
    // Since October of 2024, HTML-Parsing Code has been moved to a single, JDUInternal,
    // suite of directories that are much easier to manage.  This constructor, for instance, just
    // block copies the fields from one of those JDUInternal Data-Record Classes.

    HeaderFooterHTML
        (final Torello.JDUInternal.Parse.HTML.HeaderFooter.D1_HeaderFooterRec internalDataRec)
    {
        this.head               = internalDataRec.head;
        this.topNavBar          = internalDataRec.topNavBar;
        this.packageInfo        = internalDataRec.packageInfo;
        this.title              = internalDataRec.title;
        this.inheritance        = internalDataRec.inheritance;
        this.defListsAboveSig   = internalDataRec.defListsAboveSig;
        this.signature          = internalDataRec.signature;
        this.description        = internalDataRec.description;
        this.defListsBelowDesc  = internalDataRec.defListsBelowDesc;
        this.bottomNavBar       = internalDataRec.bottomNavBar;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Private Replaceable-HTML Fields
    // ********************************************************************************************
    // ********************************************************************************************


    private final SubSection head;
    private final SubSection topNavBar;
    private final SubSection packageInfo;
    private final SubSection title;
    private final SubSection inheritance;
    private final Replaceable defListsAboveSig;
    private final SubSection signature;
    private final Replaceable description;
    private final Replaceable defListsBelowDesc;
    private final SubSection bottomNavBar;


    // ********************************************************************************************
    // ********************************************************************************************
    // Public Accessor Methods
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve the HTML {@code <HEAD>}.
     * 
     * @return An HTML-{@code Vector} containing all elements between the opening-{@code <HEAD>}
     * and closing-{@code </HEAD>} tags.  This method shall never return null, nor will it return
     * an empty-{@code Vector}
     */
    public Vector<HTMLNode> head()
    { return this.head.html; }

    /**
     * Retrieve the Navigation-Bar HTML at the top of a Java Doc Page.
     * 
     * @return An HTML-{@code Vector} containing all elements in the Top Navigation-Bar.  If there
     * is no Navigation-Bar at the top, <I>then this method shall return null.</I>
     */
    public Vector<HTMLNode> topNavBar()
    { return (this.topNavBar != null) ? this.topNavBar.html : null; }

    /**
     * Retrieve the Type's Package-Information, as an HTML-{@code Vector}.
     * 
     * @return An HTML-{@code Vector} containing all elements in the package label at the top of a
     * Java-Doc Page.  This method shall never return null, nor will it return an
     * empty-{@code Vector}
     */
    public Vector<HTMLNode> packageInfo()
    { return this.packageInfo.html; }

    /**
     * Retrieve the title of the page, as an HTML-{@code Vector}
     * 
     * @return An HTML-{@code Vector} containing all elements in the title.  This method shall
     * never return null, nor will it return an empty-{@code Vector}
     */
    public Vector<HTMLNode> title()
    { return this.title.html; }

    /**
     * Get the HTML for the Type's inheritance {@code <UL>'s}-List.
     * 
     * @return An HTML-{@code Vector} containing all elements in the Type Inheritance List.  If
     * the Type is an {@code Interface} that does not have any inheriting-types, or an
     * {@code @Annotation}, <I>then this method shall return null.</I>
     */
    public Vector<HTMLNode> inheritance()
    { return (this.inheritance != null) ? this.inheritance.html : null; }

    /**
     * Get any Definition-Lists that occur above the Type's Signature, which is at the top.
     * 
     * @return An HTML-{@code Vector} containing all elements in the definition {@code <DL>} lists
     * below the Yellow-Box Description.  This method shall <I>never return null</I>.  If there are
     * no such lists, then an empty HTML-{@code Vector} is returned.
     * 
     * <BR /><BR />It is perfectly acceptable to add HTML-Content to an empty-list.  Here, it would
     * place such content directly above the Type-Signature {@code <PRE>} box that is located at
     * the top of the page.
     */
    public Vector<HTMLNode> defListsAboveSig()
    { return this.defListsAboveSig.currentNodes(); }

    /**
     * Retrieve the signature HTML from the top of the page.
     * 
     * @return An HTML-{@code Vector} containing all elements in the Type-Signature {@code <PRE>}
     * Element.  This is located at the top of the class, and is box with a shadow border.  This
     * method shall never return null, nor will it return an empty-{@code Vector}
     */
    public Vector<HTMLNode> signature()
    { return this.signature.html; }

    /**
     * Retrieve the description from the top of the page, as HTML.
     * 
     * @return An HTML-{@code Vector} containing all elements in the description, which is a
     * Yellow box at the top of the page.  This method shall <I>never return null</I>.  If there
     * is no such HTML divider, then an empty HTML-{@code Vector} is returned.
     * 
     * <BR /><BR />It is perfectly acceptable to add HTML-Content to an empty-list.  Here, it would
     * place such content directly below the Yellow-Description box that is located at the top of
     * the page.
     */
    public Vector<HTMLNode> description()
    { return this.description.currentNodes(); }

    /**
     * Get any Definition-Lists that occur below the Yellow-Box Signature.
     * 
     * @return An HTML-{@code Vector} containing all elements in the definition {@code <DL>} lists
     * below the Yellow-Box Description.  This method shall <I>never return null</I>.  If there are
     * no such lists, then an empty HTML-{@code Vector} is returned.
     * 
     * <BR /><BR />It is perfectly acceptable to add HTML-Content to an empty-list.  Here, it would
     * place such content directly below the Yellow-Description box that is located at the top of
     * the page.
     */
    public Vector<HTMLNode> defListsBelowDesc()
    { return this.defListsBelowDesc.currentNodes(); }

    /**
     * Retrieve the Navigation-Bar HTML at the bottom of a Java Doc Page.
     * 
     * @return An HTML-{@code Vector} containing all elements in the Bottom Navigation-Bar.  If
     * there is no Navigation-Bar at the top, <I>then this method shall return null.</I>
     */
    public Vector<HTMLNode> bottomNavBar()
    { return (this.bottomNavBar != null) ? this.bottomNavBar.html : null; }


    // ********************************************************************************************
    // ********************************************************************************************
    // DEBUG-PRINTING
    // ********************************************************************************************
    // ********************************************************************************************


    private static final String STARS =
        BYELLOW + "\n*****************************************\n" + RESET;

    /**
     * Prints an abbreviated-version of the contents of this instance, to a user-provided
     * {@code Appendable}.  If the HTML requires more than four lines of text, only the first four
     * lines are printed.
     * 
     * @param a This may be any Java Appendable.  If an {@code IOException} is thrown while writing
     * to this {@code Appendable}, it will be caught an wrapped in an
     * {@code IllegalArgumentException}, with the {@code IOException} set as the {@code cause}.
     * 
     * @throws IllegalArgumentException If {@code 'a'} throws an {@code IOException}
     * @see StrPrint#firstNLines(String, int)
     * @see Util#pageToString(Vector)
     */
    public void debugPrint(Appendable a)
    {
        try 
        {
            a.append(
                STARS + BCYAN + "this.head" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(head.html), 4) +
                '\n'
            );

            if (topNavBar != null) a.append(
                STARS + BCYAN + "this.topNavBar" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(topNavBar.html), 4) +
                '\n'
            );
            else a.append(STARS + BRED + "this.topNavBar is null" + RESET + STARS);

            a.append(
                STARS + BCYAN + "this.packageInfo" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(packageInfo.html), 4) +
                '\n'
            );

            a.append(
                STARS + BCYAN + "this.title" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(title.html), 4) +
                '\n'
            );

            if (inheritance != null) a.append(
                STARS + BCYAN + "this.inheritance" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(inheritance.html), 4) +
                '\n'
            );
            else a.append(STARS + BRED + "this.inheritance is null" + RESET + STARS);

            if (defListsAboveSig.currentSize() > 0) a.append(
                STARS + BCYAN + "this.defListsAboveSig" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(defListsAboveSig.currentNodes()), 4) +
                '\n'
            );
            else a.append(STARS + BRED + "this.defListsAboveSig is empty" + RESET + STARS);

            a.append(
                STARS + BCYAN + "this.signature" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(signature.html), 4) +
                '\n'
            );

            if (description.currentSize() > 0) a.append(
                STARS + BCYAN + "this.description" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(description.currentNodes()), 4) +
                '\n'
            );
            else a.append(STARS + BRED + "this.description is empty" + RESET + STARS);

            if (defListsBelowDesc.currentSize() > 0) a.append(
                STARS + BCYAN + "this.defListsBelowDesc" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(defListsBelowDesc.currentNodes()), 4) +
                '\n'
            );
            else a.append(STARS + BRED + "this.defListsBelowDesc is empty" + RESET + STARS);

            if (bottomNavBar != null) a.append(
                STARS + BCYAN + "this.bottomNavBar" + RESET + ':' + STARS +
                StrPrint.firstNLines(Util.pageToString(bottomNavBar.html), 4) +
                '\n'
            );
            else a.append(STARS + BRED + "this.bottomNavBar is null" + RESET + STARS);
        }
        catch (java.io.IOException ioe)
        {
            throw new IllegalArgumentException(
                "Your Appendable instance has caused an IOException to throw.  See getCause() " +
                "for details", ioe
            );
        }
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // At the end of the processing of a JavaDoc Web-Page, this gets all the changes that were made
    // ********************************************************************************************
    // ********************************************************************************************


    Vector<Replaceable> allReplaceables()
    {
        Vector<Replaceable> ret = new Vector<>();

        ret.add(head);              // Required, always present

        if (topNavBar != null) ret.add(topNavBar);

        ret.add(packageInfo);       // Required, always present
        ret.add(title);             // Required, always present

        if (inheritance != null) ret.add(inheritance);

        ret.add(defListsAboveSig);  // A "Replaceable" is never null.
        ret.add(signature);         // Required, always present
        ret.add(description);       // A "Replaceable" is never null.
        ret.add(defListsBelowDesc); // A "Replaceable" is never null.

        if (bottomNavBar != null) ret.add(bottomNavBar);

        return ret;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // THE NEW-THING: Garbage-Collector Helper?
    // ********************************************************************************************
    // ********************************************************************************************
    // 
    // Does this help?  Is this "good" for the Garbage-Collect?  Is this going to speed it up,
    // or slow it down?  This is just a "C-Styled" FREE or DESTORY method...
    // It isn't publicly visible anyway...

    void clear()
    {
        // private final SubSection head;
        // Guaranteed: Never Null, or Messager.assertFailHTML

        head.html.clear();
        head.html = null;

        // private final SubSection topNavBar;
        // Maybe they don't have one

        if (topNavBar != null)
        {
            topNavBar.html.clear();
            topNavBar.html = null;
        }

        // private final SubSection packageInfo;
        // Guaranteed: Never Null, or Messager.assertFailHTML
        // NOTE: This may one day change...

        if (packageInfo != null)
        {
            packageInfo.html.clear();
            packageInfo.html = null;
        }

        // private final SubSection title;
        // Guaranteed: Never Null, or Messager.assertFailHTML

        title.html.clear();
        title.html = null;

        // private final SubSection inheritance;
        // Maybe they don't have one

        if (inheritance != null)
        {
            inheritance.html.clear();
            inheritance.html = null;
        }

        // private final Replaceable defListsAboveSig;
        // THIS IS A REPLACEABLE: It won't be null, but it may be empty

        if (SubSection.class.isAssignableFrom(defListsAboveSig.getClass()))
        {
            ((SubSection) defListsAboveSig).html.clear();
            ((SubSection) defListsAboveSig).html = null;
        }
        else if (defListsAboveSig.currentSize() > 0) defListsAboveSig.clearHTML();

        // private final SubSection signature;
        // Guaranteed: Never Null, or Messager.assertFailHTML

        signature.html.clear();
        signature.html = null;

        // private final Replaceable description;
        // THIS IS A REPLACEABLE: It won't be null, but it may be empty

        if (SubSection.class.isAssignableFrom(description.getClass()))
        {
            ((SubSection) description).html.clear();
            ((SubSection) description).html = null;
        }
        else if (description.currentSize() > 0) description.clearHTML();

        // private final Replaceable defListsBelowDesc;
        // THIS IS A REPLACEABLE: It won't be null, but it may be empty

        if (SubSection.class.isAssignableFrom(defListsBelowDesc.getClass()))
        {
            ((SubSection) defListsBelowDesc).html.clear();
            ((SubSection) defListsBelowDesc).html = null;
        }
        else if (defListsBelowDesc.currentSize() > 0) defListsBelowDesc.clearHTML();

        // private final SubSection bottomNavBar;
        // Maybe they don't have one

        if (bottomNavBar != null)
        {
            bottomNavBar.html.clear();
            bottomNavBar.html = null;
        }
    }
}