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
package Torello.Java.Build;

import Torello.Java.Q;
import Torello.Java.FileRW;
import Torello.Java.StrCmpr;
import Torello.Java.StringParse;
import Torello.Java.FileNode;
import Torello.Java.RTC;

import static Torello.Java.C.BYELLOW;
import static Torello.Java.C.BCYAN;
import static Torello.Java.C.BGREEN;
import static Torello.Java.C.BRED;
import static Torello.Java.C.RESET;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import java.util.Vector;

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;

class JavaDocLineLengths
{
    // Regular Expression for breaking up long lines of JavaDoc Comments
    private static /* final */ Pattern LONG_JD_COMMENT = Pattern.compile
        ("^(     \\* [^\\n]{0,92})\\s([^\\n]+)\n");

    // Regular Expression for breaking up long lines of JavaDoc Comments
    private static final Pattern LONG_JD_COMMENT_INNERCLASS = Pattern.compile
        ("^(         \\* [^\\n]{0,88})\\s([^\\n]+)\n");

    // Regular Expression for matching lines that have JavaDoc Upgrade HiLite Dividers
    private static final Pattern OPENING_HILITE_DIV = Pattern.compile(
        "<DIV\\s+CLASS=['\\\"](EXAMPLE|SNIP|LOC|SHELL|HTML|REGEX|SHELLBLOCK|COMPLETE)" +
        "(-SCROLL)?['\\\"]\\w*>\\w*\\{",
        Pattern.CASE_INSENSITIVE);

    // Regular Expression for matching lines that have 'closing' HiLite Divider Elements.
    private static final Pattern CLOSING_HILITE_DIV = Pattern.compile
        ("\\}\\w*<\\/DIV>", Pattern.CASE_INSENSITIVE);

    // Long lines that don't require being queried by the user, because they only have text
    private static final Pattern SAFE_ENDING = Pattern.compile
        ("^.*?[\\w\\d\\s,\\-\\.]{20}$");

    // The first 7 characters of a line of text in JavaDoc Comments portions of source files.
    private static /* final */ String COMMENT_LINE_BEGINNING = "     * ";

    static void prepareForInnerClass()
    {
        LONG_JD_COMMENT = LONG_JD_COMMENT_INNERCLASS;

        COMMENT_LINE_BEGINNING =
            "    " + JavaDocLineLengths.COMMENT_LINE_BEGINNING;
    }

    private static final String[] PREV_LINE_MUST_BE_STRS =
    {
        "* @",
        "* <EMBED ",
        "* <BR /><BR />",

        "* <DIV ",
        "* <BR /><DIV ",

        "* <UL ",
        "* <BR /><UL ",

        "* <OL ",
        "* <BR /><OL ",

        "* <TABLE ",
        "* <BR /><TABLE "
    };

    private static final String[] CANNOT_PREPEND_TO_LINE_STRS =
        new String[11 + PREV_LINE_MUST_BE_STRS.length];

    static
    {
        System.arraycopy(
            PREV_LINE_MUST_BE_STRS, 0, CANNOT_PREPEND_TO_LINE_STRS,
            11, PREV_LINE_MUST_BE_STRS.length
        );

        CANNOT_PREPEND_TO_LINE_STRS[0]  = "* </UL>";
        CANNOT_PREPEND_TO_LINE_STRS[1]  = "* </OL>";
        CANNOT_PREPEND_TO_LINE_STRS[2]  = "* </TABLE>";
        CANNOT_PREPEND_TO_LINE_STRS[3]  = "* <LI>";
        CANNOT_PREPEND_TO_LINE_STRS[4]  = "* </LI>";
        CANNOT_PREPEND_TO_LINE_STRS[5]  = "* <TD>";
        CANNOT_PREPEND_TO_LINE_STRS[6]  = "* </TD>";
        CANNOT_PREPEND_TO_LINE_STRS[7]  = "* <TR>";
        CANNOT_PREPEND_TO_LINE_STRS[8]  = "* </TR>";
        CANNOT_PREPEND_TO_LINE_STRS[9]  = "* <TH>";
        CANNOT_PREPEND_TO_LINE_STRS[10] = "* </TH>";
    }

    // Performs a 'LINT' on the input Java Source Code File.
    //  
    // @param inFileOrDir This is any file or directory.  If this is a directory, the entire
    // directory will be scanned for {@code '.java'} source-files.  If this is a file, then it
    // will be the only file that is linted.
    // 
    // @throws FileNotFoundException If this file or directory is not found.

    static void lint(String inFileOrDir) throws IOException
    {
        File            f = new File(inFileOrDir);
        Vector<String>  files;

        if (! f.exists()) throw new FileNotFoundException
            ("A file or directory named [" + inFileOrDir + "], was not found.");

        boolean lintingDirectory = true;

        if (f.isDirectory()) files = FileNode
            .createRoot(inFileOrDir)
            .loadTree(1, (File file, String name) -> name.endsWith(".java"), null)
            .flattenJustFiles(RTC.FULLPATH_VECTOR());
        else 
        {
            files = new Vector<>();
            files.add(inFileOrDir);
            lintingDirectory = false;
        }

        for (int fNum=0; fNum < files.size(); fNum++)
        {
            String file = files.elementAt(fNum);

            System.out.println(
                "Linting File [" + BCYAN + (fNum+1) + " of " + files.size() + RESET + "]\n" +
                "Visiting File: " + BYELLOW + file + RESET + "\n"
            );

            Vector<String>  javaFile        = FileRW.loadFileToVector(file, true);
            boolean         insideHiLiteDIV = false;
            int             linesAdded      = 0;

            for (int i=1; i < (javaFile.size()-1); i++)
            {
                String  line                    = javaFile.elementAt(i);
                String  prevLine                = javaFile.elementAt(i-1);
                String  nextLine                = javaFile.elementAt(i+1);

                String  lineTrimmed             = line.trim();
                String  prevLineTrimmed         = prevLine.trim();
                String  nextLineTrimmed         = nextLine.trim();

                boolean lineIsComment           = line.startsWith(COMMENT_LINE_BEGINNING);
                boolean nextLineIsComment       = nextLine.startsWith(COMMENT_LINE_BEGINNING);
                boolean prevLineWasComment      = prevLine.startsWith(COMMENT_LINE_BEGINNING);

                boolean lineIsTooLong           = line.length() > 100;
                boolean prevLineWasBlankComment = prevLineTrimmed.equals("*");
                boolean nextLineIsEndingComment = nextLineTrimmed.equals("*/");
                boolean nthSeeTagInARow         = lineTrimmed.startsWith("* @see") && 
                                                    prevLineTrimmed.startsWith("* @see");

                boolean hasOpeningHiLiteDIV     = lineIsComment && 
                                                    OPENING_HILITE_DIV.matcher(lineTrimmed).find();

                boolean hasClosingHiLiteDIV     = lineIsComment && 
                                                    CLOSING_HILITE_DIV.matcher(lineTrimmed).find();

                boolean mustBePreceededByBlankCommentLine =
                    lineIsComment &&
                    StrCmpr.startsWithXOR_CI(lineTrimmed, PREV_LINE_MUST_BE_STRS);

                boolean appendToStartOfNextLineIsOK =
                    nextLineIsComment &&
                    (! StrCmpr.startsWithXOR_CI(nextLineTrimmed, CANNOT_PREPEND_TO_LINE_STRS)) &&
                    (! nextLineIsEndingComment);

                // ************************************************************
                // MAIN LOOP PART
                // ************************************************************

                if (hasOpeningHiLiteDIV)
                    insideHiLiteDIV = true;
                else
                {
                    if (hasClosingHiLiteDIV)
                    { insideHiLiteDIV = false; System.out.print(line); continue; }

                    if (insideHiLiteDIV)
                    { System.out.print(line); continue; }
                } // tricky...

                if (    mustBePreceededByBlankCommentLine
                    &&  (! prevLineWasBlankComment)
                    &&  (! nthSeeTagInARow)
                    )
                {
                    linesAdded++;
                    javaFile.add(i, COMMENT_LINE_BEGINNING + '\n');
                    System.out.print(BGREEN + COMMENT_LINE_BEGINNING + RESET + '\n');
                }
                else if (lineIsComment && lineIsTooLong)
                {
                    System.out.print(BRED + line + RESET);
                    Matcher m = LONG_JD_COMMENT.matcher(line);

                    if (! m.find()) throw new IllegalStateException("MESSED UP, WTF?");

                    String shortenedLine    = StringParse.trimRight(m.group(1));
                    String restOfThisLine   = line.substring(m.end(1)).trim();

                    System.out.print(shortenedLine + '\n');
                    javaFile.setElementAt(shortenedLine + '\n', i);

                    if (! SAFE_ENDING.matcher(shortenedLine).find())

                        while (! Q.YN(
                            "Break OK?  (Currently on Line #" + i + " of " +
                            javaFile.size() + ")"
                        ))
                        {
                            int pos = shortenedLine.lastIndexOf(' ');
                            if (pos == -1) System.exit(0);

                            restOfThisLine = shortenedLine.substring(pos + 1).trim() +
                                ' ' + restOfThisLine;
        
                            shortenedLine = StringParse.trimRight(shortenedLine.substring(0, pos));

                            System.out.print(BRED + shortenedLine + RESET + '\n');
                            javaFile.setElementAt(shortenedLine + '\n', i);
                        }

                    if (restOfThisLine.length() == 0) continue;

                    if (appendToStartOfNextLineIsOK)
                    {
                        nextLine = COMMENT_LINE_BEGINNING + restOfThisLine + ' ' + 
                            nextLine.substring(COMMENT_LINE_BEGINNING.length());

                        javaFile.setElementAt(nextLine, i+1);
                    }
                    else
                    {
                        linesAdded++;
                        javaFile.add(i+1, COMMENT_LINE_BEGINNING + restOfThisLine + '\n');
                    }
                }
                else System.out.print(line);
            }

            System.out.println(
                "Finished File:\t" + BYELLOW + file + RESET + '\n' +
                "Added [" + BCYAN + StringParse.zeroPad(linesAdded) + RESET + "] new lines " +
                "to  the file."
            );

            for (int count=5; count > 0; count--) 
                Q.YN(
                    "Press Y/N " + BCYAN + StringParse.zeroPad(count) + RESET +
                    " more times..."
                );

            String question = lintingDirectory
                ? "Save and continue to next file?"
                : "Save file?";

            if (! Q.YN(BGREEN + question + RESET)) System.exit(0);
            FileRW.writeFile_NO_NEWLINE(javaFile, file);
            System.out.println("Wrote File:\t" + BYELLOW + file + RESET);
        }
    }  
}