Class Lint


  • public class Lint
    extends java.lang.Object
    A very basic utility for ensuring that the Java Doc Comment portion of a source-file does not exceed a maximum line-length.

    The class can help control the line-length of the JavaDoc portion of '.java' source code files. It has a command line interface that will scroll the thee lines of Java Doc Comments, and ask if the linter has chosen appropriate places to break the lines up.

    It has a few rules about when to insert blank comment lines.


    • Method Detail

      • main

        🡅  🡇     🗕  🗗  🗖
        public static void main​(java.lang.String[] argv)
                         throws java.io.IOException
        This class' operations are all performed via the command line. It asks questions of the user from the command line, and lints the Java Doc Comments parts of '.java' files to ensure that no line comment line is longer than 100 characters. This class is probably not too useful - outside of the Java HTML Library! It did help me make all of my source-code files look nice when you click on the "Hi-Lited Source Code" links.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         if (    ((argv.length < 2) || (argv.length > 3))
             ||  StrCmpr.equalsNAND(argv[0], "1", "2", "3")
         )
             System.out.println(man_page);
        
         else if (argv[0].equals("1")) // Java Doc Linter
         {
             if (argv.length == 2) lint(argv[1]);
             else
             {
                 if (! argv[1].equals("INNERCLASS"))
                 {
                     System.out.println(man_page);
                     System.exit(0);
                 }
        
                 LONG_JD_COMMENT = LONG_JD_COMMENT_INNERCLASS;
                 COMMENT_LINE_BEGINNING = "    " + COMMENT_LINE_BEGINNING;
                 lint(argv[2]);
             }
         }
        
         else if (argv[0].equals("2")) // External-HTML File Linter
             externalHTML(argv[1]);
        
         else if (argv[0].equals("3")) // External-HTML File Linter
             lineLengthChecker(argv[1]);
        
         else System.out.println(man_page);
        
      • lint

        🡅  🡇     🗕  🗗  🗖
        public static void lint​(java.lang.String inFileOrDir)
                         throws java.io.IOException
        Performs a 'LINT' on the input Java Source Code File.
        Parameters:
        inFileOrDir - This is any file or directory. If this is a directory, the entire directory will be scanned for '.java' source-files. If this is a file, then it will be the only file that is linted.
        Throws:
        java.io.FileNotFoundException - If this file or directory is not found.
        java.io.IOException
        Code:
        Exact Method Body:
         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(GREEN + question + RESET)) System.exit(0);
             FileRW.writeFile_NO_NEWLINE(javaFile, file);
             System.out.println("Wrote File:\t" + BYELLOW + file + RESET);
         }
        
      • externalHTML

        🡅  🡇     🗕  🗗  🗖
        public static void externalHTML​(java.lang.String directoryName)
                                 throws java.io.IOException
        This can be a really great tool for transitioning a Source-File to use External-HTML Files. This method merely scans an HTML-File for items that need to be escaped or converted to constructs that are usable in '.html' rather than '.java' files.
        Parameters:
        directoryName - The name of a directory containing '.html' files.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         Iterator<String> externalHTMLFiles = FileNode
             .createRoot(directoryName)
             .loadTree(-1, (File dir, String fileName) -> fileName.endsWith(".html"), null)
             .flattenJustFiles(RTC.FULLPATH_ITERATOR());
        
         while (externalHTMLFiles.hasNext())
         {
             String  fileName    = externalHTMLFiles.next();
             String  fileAsStr   = FileRW.loadFileToString(fileName);
             String  newFileStr  = fileAsStr;
             Matcher codes       = CODES.matcher(fileAsStr);
        
             System.out.println("Linting File: " + BYELLOW + fileName + RESET);
        
             if (! Q.YN("Shall We Lint?"))
             {
                 if (! Q.YN("Continue to Next File? (say 'no' to Exit)")) System.exit(0);
                 else continue;
             }
        
        
        
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
             // Replace the {@code ...}
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
             while (codes.find())
             {
                 String s = codes.group();
        
                 String newS = 
                     "<CODE>" +
                     s.substring(7, s.length() - 1).replace("<", "&lt;").replace(">", "&gt;") +
                     "</CODE>";
        
                 System.out.println(
                     BYELLOW + s.replace("\n", "\\n") +  RESET +
                     BRED + "\n    ===>\n" + RESET +
                     BGREEN + newS + RESET
                 );
        
                 if (Q.YN("Replace this one?"))
                 {
                     newFileStr = codes.replaceFirst(newS);
                     codes.reset(newFileStr);
                 }
             }
        
        
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
             // Replace the {@link ...}  
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
             //
             // **NOTE: The replacements are JUST HELPERS, THEY ARE NOT COMPLETE.
             //         This does about 95% of the typing for you, but it is not 100%
             //         The relative-path string (dot-dots) has not been added.
        
             Matcher links = LINKS.matcher(newFileStr);
        
             while (links.find())
             {
                 String s = links.group();
        
                 String file = links.group(1);
                 String rel = links.group(2);
        
                 String href = 
                     ((file != null) ? (file + ".html") : "") +
                     ((rel != null) ? ("#" + rel) : "");
        
                 String linkText =
                     ((file != null) ? (file) : "") +
                     ((rel != null)
                         ? (((file != null) ? "." : "") + rel)
                         : "");
        
                 String newS =
                     "<B><CODE><A HREF='" + href + "'>" + linkText + "</CODE></B></A>";
        
                 System.out.println(
                     BYELLOW + s.replace("\n", "\\n") +  RESET +
                     BRED + "\n    ===>\n" + RESET +
                     BGREEN + newS + RESET
                 );
        
                 if (Q.YN("Replace this one?"))
                 {
                     newFileStr = links.replaceFirst("\n\n" + newS + "\n\n");
                     links.reset(newFileStr);
                 }
             }
        
        
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
             // Replace the leading  "     * "
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
             newFileStr = JDSTARS.matcher(newFileStr).replaceAll("");
        
        
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
             // Save the file
             // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
             System.out.println(
                 newFileStr +
                 "\n************************************************************\n"
             );
        
             if (Q.YN("Save this File?"))
             {
                 FileRW.writeFile(newFileStr, fileName);
                 System.out.println("Saved File: " + BYELLOW + fileName + RESET);
             }           
         }
        
      • lineLengthChecker

        🡅     🗕  🗗  🗖
        public static void lineLengthChecker​(java.lang.String directoryName)
                                      throws java.io.IOException
        This method will scan an entire directory for '.java' files, and then report if there are any lines in each of those files whose length is greater than 100.

        While not exactly a great tool for all developers, during the development of Java HTML, this has been (on occasion) extremely useful.
        Parameters:
        directoryName - The name of the directory to be scanned for '.java' files.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         Iterator<String> javaFiles = FileNode
             .createRoot(directoryName)
             .loadTree(-1, (File dir, String fileName) -> fileName.endsWith(".java"), null)
             .flattenJustFiles(RTC.FULLPATH_ITERATOR());
        
         while (javaFiles.hasNext())
         {
             String              fileName    = javaFiles.next();
             Vector<String>      lines       = FileRW.loadFileToVector(fileName, true);
             boolean             hadMatch    = false;
             IntFunction<String> zeroPad     = (lines.size() < 999)
                                                 ? StringParse::zeroPad
                                                 : StringParse::zeroPad10e4;
        
             System.out.println("Checking File: " + BYELLOW + fileName + RESET);
        
             for (int i=0; i < lines.size(); i++)
        
                 if (lines.elementAt(i).length() > 100)
                 {
                     String l = lines.elementAt(i);
        
                     System.out.print(
                         "Line-Number: " +
        
                         // NOTE: add one to the line number, when printing it.  The lines
                         //       vector index starts at zero, not one.
        
                         '[' + BGREEN + zeroPad.apply(i + 1) + RESET + ", " +
                         "Has Length: " + BRED + l.length() + RESET + '\n' +
        
                         // Print the line
                         l
                     );
        
                     hadMatch = true;
                 }
        
             if (hadMatch) if (! Q.YN("continue?")) System.exit(0);
         }