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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package Torello.JavaDoc;

// The "Mirror Class" since Java's Constructors aren't always so pleasant
import Torello.JDUInternal.Parse.Java.Location.LocJavaCtorSucks;


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Java-HTML Imports
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

import Torello.Java.*;

import static Torello.Java.C.*;
import static Torello.JavaDoc.PF.*;


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Standard-Java Imports
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

import java.util.Optional;
import java.io.IOException;


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// The new Source-Code Parser: com.sun.source.*
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

import com.sun.source.tree.Tree;
import com.sun.source.doctree.DocCommentTree;

/**
 * Location Information storing line-number, column-number and character-number for items inside
 * of a Java Source-Code File (a {@code '.java'} file).
 */
public class Location implements java.io.Serializable, Cloneable
{
    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
    protected static final long serialVersionUID = 1;


    // ********************************************************************************************
    // ********************************************************************************************
    // This class' public fields
    // ********************************************************************************************
    // ********************************************************************************************


    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_SIG_START_POS> */
    public final int signatureStartPos;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_SIG_START_LINE> */
    public final int signatureStartLine;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_SIG_START_COL> */
    public final int signatureStartCol;


    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_SIG_END_POS> */
    public final int signatureEndPos;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_SIG_END_LINE> */
    public final int signatureEndLine;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_SIG_END_COL> */
    public final int signatureEndCol;


    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_JDC_START_POS> */
    public final int jdcStartPos;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_JDC_START_LINE> */
    public final int jdcStartLine;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_JDC_START_COL> */
    public final int jdcStartCol;


    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_JDC_END_POS> */
    public final int jdcEndPos;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_JDC_END_LINE> */
    public final int jdcEndLine;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_JDC_END_COL> */
    public final int jdcEndCol;


    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_BODY_START_POS> */
    public final int bodyStartPos;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_BODY_START_LINE> */
    public final int bodyStartLine;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_BODY_START_COL> */
    public final int bodyStartCol;


    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_BODY_END_POS> */
    public final int bodyEndPos;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_BODY_END_LINE> */
    public final int bodyEndLine;

    /** <EMBED CLASS='external-html' DATA-FILE-ID=LOC_BODY_END_COL> */
    public final int bodyEndCol;


    // ********************************************************************************************
    // ********************************************************************************************
    // Primary-Constructor
    // ********************************************************************************************
    // ********************************************************************************************


    public Location(LocJavaCtorSucks l)
    {
        this.signatureStartPos  = l.signatureStartPos;
        this.signatureStartLine = l.signatureStartLine;
        this.signatureStartCol  = l.signatureStartCol;
    
        this.signatureEndPos    = l.signatureEndPos;
        this.signatureEndLine   = l.signatureEndLine;
        this.signatureEndCol    = l.signatureEndCol;
    
        this.jdcStartPos        = l.jdcStartPos;
        this.jdcStartLine       = l.jdcStartLine;
        this.jdcStartCol        = l.jdcStartCol;
    
        this.jdcEndPos          = l.jdcEndPos;
        this.jdcEndLine         = l.jdcEndLine;
        this.jdcEndCol          = l.jdcEndCol;
    
        this.bodyStartPos       = l.bodyStartPos;
        this.bodyStartLine      = l.bodyStartLine;
        this.bodyStartCol       = l.bodyStartCol;
    
        this.bodyEndPos         = l.bodyEndPos;
        this.bodyEndLine        = l.bodyEndLine;
        this.bodyEndCol         = l.bodyEndCol;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Clone & Clone-Constructor
    // ********************************************************************************************
    // ********************************************************************************************


    private Location(Location l)
    {
        this.signatureStartPos  = l.signatureStartPos;
        this.signatureStartLine = l.signatureStartLine;
        this.signatureStartCol  = l.signatureStartCol;
    
        this.signatureEndPos    = l.signatureEndPos;
        this.signatureEndLine   = l.signatureEndLine;
        this.signatureEndCol    = l.signatureEndCol;
    
        this.jdcStartPos        = l.jdcStartPos;
        this.jdcStartLine       = l.jdcStartLine;
        this.jdcStartCol        = l.jdcStartCol;
    
        this.jdcEndPos          = l.jdcEndPos;
        this.jdcEndLine         = l.jdcEndLine;
        this.jdcEndCol          = l.jdcEndCol;
    
        this.bodyStartPos       = l.bodyStartPos;
        this.bodyStartLine      = l.bodyStartLine;
        this.bodyStartCol       = l.bodyStartCol;
    
        this.bodyEndPos         = l.bodyEndPos;
        this.bodyEndLine        = l.bodyEndLine;
        this.bodyEndCol         = l.bodyEndCol;
    }

    /**
     * Standard Java {@code clone()} method
     * @return Returns a copy of {@code 'this'} instance
     */
    public Object clone()
    { return new Location(this); }


    // ********************************************************************************************
    // ********************************************************************************************
    // java.lang.Object methods
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Standard Java {@code toString()} method
     * @return Returns the contents of this class, as a {@code java.lang.String}
     */
    public String toString()
    {
        return
            "signatureStartPos:  " + signatureStartPos + '\n' +
            "signatureStartLine: " + signatureStartLine + '\n' +
            "signatureStartCol:  " + signatureStartCol + '\n' +
    
            "signatureEndPos:    " + signatureEndPos + '\n' +
            "signatureEndLine:   " + signatureEndLine + '\n' +
            "signatureEndCol:    " + signatureEndCol + '\n' +
    
            "jdcStartPos:        " + jdcStartPos + '\n' +
            "jdcStartLine:       " + jdcStartLine + '\n' +
            "jdcStartCol:        " + jdcStartCol + '\n' +
    
            "jdcEndPos:          " + jdcEndPos + '\n' +
            "jdcEndLine:         " + jdcEndLine + '\n' +
            "jdcEndCol:          " + jdcEndCol + '\n' +
    
            "bodyStartPos:       " + bodyStartPos + '\n' +
            "bodyStartLine:      " + bodyStartLine + '\n' +
            "bodyStartCol:       " + bodyStartCol + '\n' +
    
            "bodyEndPos:         " + bodyEndPos + '\n' +
            "bodyEndLine:        " + bodyEndLine + '\n' +
            "bodyEndCol:         " + bodyEndCol + '\n';
    }

    /**
     * Standard Java {@code equals()} method
     * 
     * @param other This may be any Java Object, but one that extends class {@code 'Location'}
     * has the potential to result in a {@code TRUE} evaluation from this method.
     * 
     * @return {@code TRUE} if and only if the contents of {@code 'this'} instance are identical to
     * the contents of {@code 'other'} (and only if {@code 'other'} is actually an instance of
     * {@code Location}, or extends at least extends it).
     */
    public boolean equals(Object other)
    {
        if (! Location.class.isAssignableFrom(other.getClass())) return false;

        Location l = (Location) other;

        return
                (this.signatureStartPos     == l.signatureStartPos)
            &&  (this.signatureStartLine    == l.signatureStartLine)
            &&  (this.signatureStartCol     == l.signatureStartCol)
    
            &&  (this.signatureEndPos       == l.signatureEndPos)
            &&  (this.signatureEndLine      == l.signatureEndLine)
            &&  (this.signatureEndCol       == l.signatureEndCol)
    
            &&  (this.jdcStartPos           == l.jdcStartPos)
            &&  (this.jdcStartLine          == l.jdcStartLine)
            &&  (this.jdcStartCol           == l.jdcStartCol)
    
            &&  (this.jdcEndPos             == l.jdcEndPos)
            &&  (this.jdcEndLine            == l.jdcEndLine)
            &&  (this.jdcEndCol             == l.jdcEndCol)
    
            &&  (this.bodyStartPos          == l.bodyStartPos)
            &&  (this.bodyStartLine         == l.bodyStartLine)
            &&  (this.bodyStartCol          == l.bodyStartCol)
    
            &&  (this.bodyEndPos            == l.bodyEndPos)
            &&  (this.bodyEndLine           == l.bodyEndLine)
            &&  (this.bodyEndCol            == l.bodyEndCol);
    }

    /**
     * Simply invokes the {@code hashCode()} static-method provided by class {@code java.lang.Integer}.
     * @return a hash code value for this object.
     */
    public int hashCode()
    { return Integer.hashCode(this.signatureStartPos * this.signatureEndPos); }


    // ********************************************************************************************
    // ********************************************************************************************
    // Specialized toString
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Generates a {@code String} - with all information available, including any content requested
     * by the {@code 'flags'} parameter.
     * 
     * @param flags These are the {@code toString(...)} flags from class {@code PF}
     * ("Print Flags"). View available flags listed in class {@link PF}.
     * 
     * @param srcFileAsStr This parameter should contain the complete {@code '.java'} source-code
     * file as a {@code String}.  If you have passed the Print-Flags for requesting that the body
     * or Java-Doc Comment be output to the returned-{@code String}, this parameter must be passed
     * a valid copy of the {@code '.java'} File.
     * 
     * <BR /><BR />This parameter may be null, and if it is it will be ignored.  When null is
     * passed to this parameter, the actual Body, Java-Doc Comment &amp; Signature simply cannot
     * be extracted, and the output of this {@code toString} method shall only print the location
     * numbers, column's and line-numbers instead.
     * 
     * <BR /><BR />If a valid copy of the contents of the {@code '.java'} file that produced
     * {@code 'this'} instance of {@code Location} is passed <B STYLE='color: red;'><I>and</I></B>
     * if the appropriate Print-Flags are passed tot he {@code 'flags'} parameter requesting that
     * the specified-elements (signature, comment and body) be output to the
     * returned-{@code String},  <B STYLE='color: red;'><I>then</I></B> those elements will be 
     * printed to the output-{@code String}.
     * 
     * <BR /><BR /><B>NOTE:</B> If the contents of parameter {@code 'srcFileAsstr'} are not the
     * exact and un-modified contents of the {@code'.java} file that produced {@code 'this'}
     * instance of location, then the {@code String} returned will contain erroneous information.
     * It is even possible that a {@code StringIndexOutOfBoundsException} may be thrown.
     * 
     * @return A printable {@code String} of the locations represented by this instance
     * 
     * @see PF
     * @see StrCSV#toCSV(String[], boolean, boolean, Integer)
     * @see #toString()
     */
    public String toString(int flags, String srcFileAsStr)
    {
        boolean color       = (flags & UNIX_COLORS) > 0;
        boolean src         = srcFileAsStr != null;
        boolean signature   = src;
        boolean bodyShort   = src && ((flags & BODY_SHORT) > 0);
        boolean bodyLong    = (! bodyShort) && src && ((flags & BODY) > 0);
        boolean jdc         = src && ((flags & JAVADOC_COMMENTS) > 0);
        boolean locShort    = (flags & BRIEF_LOCATION) > 0;
        int     M_OVER_2    = MAX_STR_LEN / 2;

        if (locShort) return

            (signature
                ? ("Signature:        " + StrIndent.indentAfter2ndLine
                    (srcFileAsStr.substring(signatureStartPos, signatureEndPos), 4, true, false) +
                    '\n')
                : "") +

            "Signature-Start:  " +
            "pos="  + (color ? BGREEN : "") + signatureStartPos  + (color ? RESET : "") + ", " +
            "line=" + (color ? BGREEN : "") + signatureStartLine + (color ? RESET : "") + ", " +
            "col="  + (color ? BGREEN : "") + signatureStartCol  + (color ? RESET : "") + "\n" +

            "Signature-End:    " +
            "pos="  + (color ? BCYAN : "") + signatureEndPos  + (color ? RESET : "") + ", " +
            "line=" + (color ? BCYAN : "") + signatureEndLine + (color ? RESET : "") + ", " +
            "col="  + (color ? BCYAN : "") + signatureEndCol  + (color ? RESET : "") + "\n" +

            (jdc
                ? ("Java-Doc Comment: " + StrPrint.abbrev(srcFileAsStr.substring
                    (jdcStartPos, jdcEndPos), M_OVER_2, true, null, MAX_STR_LEN) + '\n')
                : "") +

            "Java-Doc-Start:   " +
            "pos="  + (color ? BGREEN : "") + jdcStartPos  + (color ? RESET : "") + ", " +
            "line=" + (color ? BGREEN : "") + jdcStartLine + (color ? RESET : "") + ", " +
            "col="  + (color ? BGREEN : "") + jdcStartCol  + (color ? RESET : "") + "\n" +

            "Java-Doc-End:     " +
            "pos="  + (color ? BCYAN : "") + jdcEndPos  + (color ? RESET : "") + ", " +
            "line=" + (color ? BCYAN : "") + jdcEndLine + (color ? RESET : "") + ", " +
            "col="  + (color ? BCYAN : "") + jdcEndCol  + (color ? RESET : "") + "\n" +

            (bodyShort
                ? ("Body:             " + StrPrint.abbrev(srcFileAsStr.substring
                    (signatureStartPos, signatureEndPos), M_OVER_2, true, null, MAX_STR_LEN))
                : "") +

            (bodyLong
                ? ("Body:             " + StrIndent.indentAfter2ndLine
                    (srcFileAsStr.substring(signatureStartPos, signatureEndPos), 4, true, false))
                : "") +

            "Body-Start:       " +
            "pos="  + (color ? BGREEN : "") + bodyStartPos  + (color ? RESET : "") + ", " +
            "line=" + (color ? BGREEN : "") + bodyStartLine + (color ? RESET : "") + ", " +
            "col="  + (color ? BGREEN : "") + bodyStartCol  + (color ? RESET : "") + "\n" +

            "Body-End:         " +
            "pos="  + (color ? BCYAN : "") + bodyEndPos  + (color ? RESET : "") + ", " +
            "line=" + (color ? BCYAN : "") + bodyEndLine + (color ? RESET : "") + ", " +
            "col="  + (color ? BCYAN : "") + bodyEndCol  + (color ? RESET : "");

        else return

            (signature
                ? ("SIGNATURE: " + StrIndent.indentAfter2ndLine
                    (srcFileAsStr.substring(signatureStartPos, signatureEndPos), 4, true, false))
                : "") +

            "signatureStartPos:  " + (color ? BGREEN : "") + signatureStartPos + '\n' +
                (color ? RESET : "") + 
            "signatureStartLine: " + (color ? BGREEN : "") + signatureStartLine + '\n' +
                (color ? RESET : "") + 
            "signatureStartCol:  " + (color ? BGREEN : "") + signatureStartCol + '\n' +
                (color ? RESET : "") + 
    
            "signatureEndPos:    " + (color ? BCYAN : "") + signatureEndPos + '\n' +
                (color ? RESET : "") + 
            "signatureEndLine:   " + (color ? BCYAN : "") + signatureEndLine + '\n' +
                (color ? RESET : "") + 
            "signatureEndCol:    " + (color ? BCYAN : "") + signatureEndCol + '\n' +
                (color ? RESET : "") + 

            (jdc
                ? ("BODY: " + StrPrint.abbrev(srcFileAsStr.substring
                    (jdcStartPos, jdcEndPos), M_OVER_2, true, null, MAX_STR_LEN))
                : "") +

            "jdcStartPos:        " + (color ? BGREEN : "") + jdcStartPos + '\n' +
                (color ? RESET : "") + 
            "jdcStartLine:       " + (color ? BGREEN : "") + jdcStartLine + '\n' +
                (color ? RESET : "") + 
            "jdcStartCol:        " + (color ? BGREEN : "") + jdcStartCol + '\n' +
                (color ? RESET : "") + 

            "jdcEndPos:          " + (color ? BCYAN : "") + jdcEndPos + '\n' +
                (color ? RESET : "") + 
            "jdcEndLine:         " + (color ? BCYAN : "") + jdcEndLine + '\n' +
                (color ? RESET : "") + 
            "jdcEndCol:          " + (color ? BCYAN : "") + jdcEndCol + '\n' +
                (color ? RESET : "") + 

            (bodyShort
                ? ("BODY: " + StrPrint.abbrev(srcFileAsStr.substring
                    (signatureStartPos, signatureEndPos), M_OVER_2, true, null, MAX_STR_LEN))
                : "") +

            (bodyLong
                ? ("BODY: " + StrIndent.indentAfter2ndLine
                    (srcFileAsStr.substring(signatureStartPos, signatureEndPos), 4, true, false))
                : "") +

            "bodyStartPos:       " + (color ? BGREEN : "") + bodyStartPos +'\n' +
                (color ? RESET : "") + 
            "bodyStartLine:      " + (color ? BGREEN : "") + bodyStartLine + '\n' +
                (color ? RESET : "") +
            "bodyStartCol:       " + (color ? BGREEN : "") + bodyStartCol + '\n' +
                (color ? RESET : "") + 

            "bodyEndPos:         " + (color ? BCYAN : "") + bodyEndPos + '\n' +
                (color ? RESET : "") + 
            "bodyEndLine:        " + (color ? BCYAN : "") + bodyEndLine + '\n' +
                (color ? RESET : "") + 
            "bodyEndCol:         " + (color ? BCYAN : "") + bodyEndCol + '\n' +
                (color ? RESET : "");
    }

    /**
     * Produces a quick-summary, as a {@code String}.
     * @return a very brief summary of the information contained by this class.  Lists only the
     * starting line numbers for the three sections.
     */
    public String quickSummary()
    {
        return "signature-line=" + this.signatureStartLine + ", javadoc-line=" + this.jdcStartLine +
            ", body-line=" + this.bodyStartLine;        
    }
}