Package Torello.Java

Class HiLiteMe


  • public class HiLiteMe
    extends java.lang.Object
    HiLiteMe - Documentation.

    This class puts a front-end on the web-server http://HiLite.ME. The site pretty-prints a wide variety (266, to be exact) different versions of software code files. If you wish to write a Java method to hi-light your Java code, quickly, and automatically, then use the methods in this class. HiLite.ME not only pretty-prints Java Code - it can handle most modern programming languages.

    • public Iterator<String> getCodeTypes()
    • public Iterator<String> getStyleTypes()
    • public Iterator<String> getCodeTypeDescriptions()

    The above Iterator<String> will provide a complete list of all parameters that this code-hiliter will accept as a 'Code Type'. Click the link below to see the complete list of available programming-type codes.

    Programming Language Codes

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // This is how to hi-lite an entire Source-Code directory.
    // NOTE: IT ISN'T NECCESSARY To USE GSUTIL (Google Cloud Sever Utility) to HiLite code with
    // HiLite.ME, but Google Cloud Server Storage Buckets are where these files put after being
    // hi-lited, so that they may be viewed on the web.
    
    String FILEROOT_HILITEME = "data/HiLite.ME/";   // Store output html files here!
    
    FileNode root = FileNode
         .createRoot("Torello/");
         .loadTree(-1, (file, name) -> name.endsWith(".java"), null);
    
    HiLiteMe.Params hlmp = new HiLiteMe.Params();
    hlmp.targetDirectory = FILEROOT_HILITEME;
    
    HiLiteMe.prettyPrintRecurseTree(root, hlmp);
    HiLiteMe.createIndex(hlmp.fileIndexList, hlmp.targetDirectory);
    
    // *** These lines copy the HiLited code to my Google Cloud Server Storage Bucket
    // *** But you may move it to wherever you like - you don't need to use Google at all.
    
    String gcsDir = "gs://developer.torello.directory/MY_MANDARIN_LANGUAGE_PROJECT/";
    GSUTIL.RM(gcsDir + "HiLite.ME/**");
    GSUTIL.CP(FILEROOT_HILITEME + "Torello/", gcsDir + "HiLite.ME/");
    GSUTIL.CP(FILEROOT_HILITEME + "index.html", gcsDir + "HiLite.ME/");
    


    Static (Functional) API: The methods in this class are all (100%) defined with the Java Key-Word / Key-Concept 'static'. Furthermore, there is no way to obtain an instance of this class, because there are no public (nor private) constructors. Java's Spring-Boot, MVC feature is *not* utilized because it flies directly in the face of the light-weight data-classes philosophy. This has many advantages over the rather ornate Component Annotations (@Component, @Service, @AutoWired, etc... 'Java Beans') syntax:

    • The methods here use the key-word 'static' which means (by implication) that there is no internal-state. Without any 'internal state' there is no need for constructors in the first place! (This is often the complaint by MVC Programmers).
    • A 'Static' (Functional-Programming) API expects to use fewer data-classes, and light-weight data-classes, making it easier to understand and to program.
    • The Vectorized HTML data-model allows more user-control over HTML parse, search, update & scrape. Also, memory management, memory leakage, and the Java Garbage Collector ought to be intelligible through the 'reuse' of the standard JDK class Vector for storing HTML Web-Page data.

    The power that object-oriented programming extends to a user is (mostly) limited to data-representation. Thinking of "Services" as "Objects" (Spring-MVC, 'Java Beans') is somewhat 'over-applying' the Object Oriented Programming Model. Like most classes in the Java-HTML JAR Library, this class backtracks to a more C-Styled Functional Programming Model (no Objects) - by re-using (quite profusely) the key-word static with all of its methods, and by sticking to Java's well-understood class Vector

    Static Fields: The methods in this class do not create any internal state that is maintained - however there are a few private & static fields defined. These fields are instantiated only once during the Class Loader phase (and only if this class shall be used), and serve as data 'lookup' fields (static constants). View this class' source-code in the link provided below to see internally used data.

    This class has three private static TreeSet and TreeMap fields that are used to hold the complete-list of 'Code Types' and the complete-list of 'Style Types' in memory. There is also a lazy-loading TreeMap that helps the simplifyColorSpans method make the HTML <SPAN> elements more brief.



    • Field Detail

      • INDEX_HEADER_HTML

        public static java.lang.String INDEX_HEADER_HTML
        If you choose to call the method createIndex(TreeSet<String> fileIndexList, String targetDirectory), then this String will be used as the header for that file.

        NOTE: It may be changed, as it has not been declared 'final'.
        Code:
        Exact Field Declaration Expression:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        public static String INDEX_HEADER_HTML	=
             	"<HTML>\n" +
                "<HEAD>\n" +
                "<TITLE>HiLite.ME Index</TITLE>\n" +
                "<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" +
                "<STYLE TYPE=\"text/css\">\n" +
                "A                      { color: black; text-decoration: none;  }\n" +
                "UL LI:Nth-Child(odd)   { background: lightgray;                }\n" +
                "UL LI:Nth-Child(even)  { background: white;                    }\n" +
                "UL LI                  { padding: 5 5 5 5;                     }\n" +
                "UL                     { max-width: 60%;                       }\n" +
                "</STYLE>\n" +
                "</HEAD>\n" +
                "<BODY>\n" +
                "<H2>HiLite.ME Code</H2>\n";
        
    • Method Detail

      • getCodeTypes

        public static java.util.Iterator<java.lang.String> getCodeTypes()
        Each time a piece of code is to be pretty-printed, HiLite.ME expects to receive a "type of software" or "type of code" descriptor String that identifies what type of textual-code it is receiving. There are exactly 266 different types of software files that may be passed to the HiLite.ME server. These String-tag for these 'Code Types' may be viewed here.

        Click the link below to see the complete list of programming-type codes.

        Programming Language Codes
        Returns:
        An Iterator<String> that produces each String-tag that may be passed as a 'Code Type' to http://HiLite.ME
        Code:
        Exact Method Body:
        1
        2
        3
         // NOTE: The 'RemoveUnsupportedIterator' wrapper class prohibits modifications to this
         //       TreeMap
         return new RemoveUnsupportedIterator<String>(codeTypeDescriptions.keySet().iterator());
        
      • getCodeTypeDescriptions

        public static java.util.Iterator<java.lang.String> getCodeTypeDescriptions
                    ()
        
        This will iterate over the full-name descriptions of the software types available for parsing with the HiLite.ME server
        Returns:
        An Iterator<String> that produces a String-description of each software-types available for parsing.
        Code:
        Exact Method Body:
        1
        2
        3
         // NOTE: The 'RemoveUnsupportedIterator' wrapper class prohibits modifications to this
         //       TreeMap
         return new RemoveUnsupportedIterator<String>(codeTypeDescriptions.values().iterator());
        
      • getStyleTypes

        public static java.util.Iterator<java.lang.String> getStyleTypes()
        This will iterate over the "Defining Style-Output Types" available to users of the http://HiLite.ME server.

        Click the link below to see the complete list of 'Style Codes'

        HiLiting Style Codes
        Returns:
        An Iterator<String> over the different available String-tags that may be passed as a 'Style Tag' when performing a "pretty print" operation.
        Code:
        Exact Method Body:
        1
        2
        3
         // NOTE: The 'RemoveUnsupportedIterator' wrapper class prohibits modifications to this
         //       Vector
         return new RemoveUnsupportedIterator<String>(styleTypes.iterator());
        
      • getCodeDescription

        public static java.lang.String getCodeDescription​
                    (java.lang.String codeType)
        
        Returns the description for a specific 'Code Type'
        Returns:
        the long-form of the 'codeType' as a java String
        Code:
        Exact Method Body:
        1
         return codeTypeDescriptions.get(codeType);
        
      • isCodeType

        public static boolean isCodeType​(java.lang.String s)
        Checks whether the passed String-parameter is a recognized 'Code Type'

        Click the link below to see the complete list of programming-type codes.

        Programming Language Codes
        Parameters:
        s - This may be any java.lang.String. It is intended to be one of the listed 'Code Types' available for use with the HiLite.ME server.
        Returns:
        This will return TRUE if the passed String-tag is one of the tags listed with the HiLite.ME server for Software Types, or 'Code Types' - and FALSE otherwise. Use the Iterator to get the complete list of available 'Code Tags' (or click the link, above, to view them in this browser).
        See Also:
        getCodeTypes(), getCodeTypeDescriptions()
        Code:
        Exact Method Body:
        1
         return codeTypeDescriptions.containsKey(s);
        
      • isStyleType

        public static boolean isStyleType​(java.lang.String s)
        Checks whether the passed String-parameter is a recognized 'Style Type'

        Click the link below to see the complete list of 'Style Codes'

        HiLiting Style Codes
        Parameters:
        s - This may be any java.lang.String. It is intended to be one of the listed 'Style Types' available for use with the HiLite.ME server.
        Returns:
        This will return TRUE if the passed String-tag is one of the tags listed with the HiLite.ME server for Style Types, and FALSE otherwise. Use the Iterator to get the complete list of available 'Style Tags' (or click the link, above, to view them in this browser).
        See Also:
        getStyleTypes()
        Code:
        Exact Method Body:
        1
         return styleTypes.contains(s);
        
      • createIndex

        public static void createIndex​
                    (java.util.TreeSet<java.lang.String> fileIndexList,
                     java.lang.String targetDirectory)
                throws java.io.IOException
        
        This will generate an 'index.html' file of all the recently generated '.html' files.
        Parameters:
        targetDirectory - This is simply the save-location for this 'index.html' file
        fileIndexList - This must be a list of file-names that were generated by the class HiLiteMe
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
         StringBuilder out = new StringBuilder();
        
         out.append(INDEX_HEADER_HTML);
         out.append("<UL>");
        
         for (String f : fileIndexList)
             out.append(
                 "<LI><A HREF=\"" + f + (f.endsWith(".txt") ? "" : ".html") +
                 "\" TARGET=\"_blank\">\n" + f +"</A></LI>\n"
             );
        
         out.append("</UL>");
         out.append(Params.DEFAULT_ENDING_HTML);
        
         FileRW.writeFile(out, targetDirectory + "index.html");
        
      • prettyPrintRecurseTree

        public static void prettyPrintRecurseTree​(FileNode node,
                                                  HiLiteMe.Params hlmp)
                                           throws java.io.IOException
        This will take a FileNode tree, and iterate through all of it - calling the http://HiLite.ME/ server for each software file that it finds / discovers and recognizes. Each source-code file that is within the FileNode tree that is passed, after being pretty-printed, will be saved as a '.html' file in the HiLiteMe.Params.targetDirectory

        IMPORTANT NOTE: The provided java class FileNode has some very simple filter operations for making sure that only the code-files that you want to be transmitted will actually be sent. Each and every file in the FileNode tree whose 'Code Type' can be deduced (by it's file-extension, or via the HiLiteMe.Params.customMappings) will be sent to the HiLite.ME server for hiliting.

        It should be easy to call the fileNodeRoot.prune(...) method to make sure you are only transmitting/hiliting the files you want.

        FURTHERMORE: The means by which files are copied from a "Source" source-code directory to a "Target" hilited-HTML directory is simply via the FileRW.copyFile method. Because the copy operations preserves the directory-tree structure of the input source-code file-system tree, it is imperative to use a relative directory-location when loading the source-code FileNode tree.
        Parameters:
        node - This is the root node of a FileNode directory tree. Every operating-system file that is found inside this tree will have it's code-hilited using the HiLite.Me server.
        hlmp - Please review the inner-class Params to configure the extra parameters for storing and saving the results of the code hiliting operation.
        Throws:
        java.io.IOException
        See Also:
        prettyPrintScrape(String, String, String), HiLiteMe.Params
        Code:
        Exact Method Body:
          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
         (new File(hlmp.targetDirectory + node.getFullPathName())).mkdirs();
        
         // This "flattens" the source-code FileNode tree into an Iterator.  The Iterator will not
         // iterate "FileNodes" - but rather String's - where each String is the relative path
         // name of the source code file.
         Iterator<String> files = node.getDirContentsFiles(RetTypeChoice.FULLPATH_ITERATOR);
        
         while (files.hasNext())
         {
             String fileName = files.next();
        
             if (hlmp.verbose) System.out.printf("%1$-80s", fileName);
        
             // Sometimes it helps to have any text-files copied to output / target directory
             if (hlmp.copyTextFiles && fileName.toLowerCase().endsWith(".txt"))
             {
                 // Again, put this in the "Complete List" of files for the "creatIndex" method.
                 // If the user wants an 'index.html' file that lists all of the files that have
                 // been hilited, this list is necessary.
                 if (hlmp.fileIndexList != null) hlmp.fileIndexList.add(fileName);
        
                 if (hlmp.verbose)
                     System.out.println( "\nCOPYING " + C.BCYAN + "TEXT-FILE " + C.RESET);
        
                 // This is why a "relative FileNode" is mandatory.  All that is happening is that
                 // the "Full Path Name" of the source-code file is being appended to the
                 // "Target Directory" name.
                 FileRW.copyFile(fileName, hlmp.targetDirectory + fileName, true);
        
                 // Text files are not actually hilited, just copied.
                 continue;
             }
        
             // This checks the file-extension - '.java', '.js', '.html' - and uses that extension
             // String as a 'Code Type' with the HiLite server.
             //
             // If there are files where that system doesn't work - the mapping of a file-extension to
             // a 'Code Type' should be put into the "Custom Mappings" TreeMap in class HiLiteMe.Params
             // and passed to this method
             String codeTypeParam = deduceCodeType(fileName, hlmp.customMappings);
        
             if (codeTypeParam == null)
             {
                 // Without a 'Code Type' there isn't any way to hilite the file...  Let the user
                 // know and continue 
                 if (hlmp.verbose)
                     System.out.println(
                             "\nUNKNOWN FILE TYPE FOUND: " + C.BCYAN + fileName + C.RESET +
                             C.BYELLOW + "\nNOT CONVERTING FILE..." + C.RESET
                     );
        
                 continue; // next source code file loop iteration
             }
        
             // Loads the source-code file to a String
             String fileText     = FileRW.loadFileToStringBuilder(fileName).toString();
             String hilitedCode  = null;
        
             // It is always faster to use a Cache with big projects...
             // If there is a 'Cache' check that first...
        
             if (hlmp.cache == null)
                 hilitedCode = prettyPrintScrape(fileText, codeTypeParam, hlmp.styleTag);
             else
             {
                 // Check the Cache.  Note that it is pretty efficient, and doesn't do a 
                 // character-by-character check - unless there is an exact hashCode match first.
                 hilitedCode = hlmp.cache.get(fileText, codeTypeParam, hlmp.styleTag);
        
                 // The Cache did not have an exact match for the Source Code File
                 if (hilitedCode == null)
                 {
                     hilitedCode = prettyPrintScrape(fileText, codeTypeParam, hlmp.styleTag);
                     hlmp.cache.checkIn(fileText, hilitedCode, codeTypeParam, hlmp.styleTag);
                 }
             }
        
             // Now just write the hilited code to disk... Make sure to insert the HTML header
             // and tail/footer text.
        
             hilitedCode = hlmp.headerHTML.replace("INSERT HERE", fileName) + 
                 hilitedCode + hlmp.endingHTML;
        
             FileRW.writeFile(hilitedCode, hlmp.targetDirectory + fileName + ".html");
        
             // Add this file-name to the list of files that have been converted/hilited
             // (but only if there is such a list being kep!)  This list can be used by the
             // method createIndex(TreeSet, String) to create an 'index.html' file
             if (hlmp.fileIndexList != null) hlmp.fileIndexList.add(fileName);
        
             // Let the user know this file has been successfully hilited.
             if (hlmp.verbose) System.out.println(C.BRED + "Completed." + C.RESET);
         }
        
         // After having iterated all the files in a directory, use recursion to iterate through the
         // files in any sub directories...
         //
         // The iterator of sub-dirs is an 'Iterator<FileNode>' rather than an 'Iterator<String>'
         // (which was used in the loop above)
        
         Iterator<FileNode> dirs = node.getDirContentsDirs(RetTypeChoice.ITERATOR);
        
         while (dirs.hasNext()) prettyPrintRecurseTree(dirs.next(), hlmp);
        
      • deduceCodeType

        public static java.lang.String deduceCodeType​
                    (java.lang.String fileName,
                     java.util.TreeMap<java.lang.String,​java.lang.String> customMappings)
        
        This attempts to guess the 'Code Type' parameter for the file, based on the input file name. The file is loaded from disk, so be sure to include the full path to the file in the fileName.
        Parameters:
        fileName - The full-path name of a Source-Code / Software file. It will be loaded from disk, and transmitted to HiLite.ME servers and "pretty-printed" into HTML.
        customMappings - This parameter may be null, and if it is, then it is ignored. If this parameter is not null, then the file-extension obtained from the String 'fileName' field will be "looked up" in the TreeMap, and if it matches one of these custom-mappings of file-extension to HiLite.ME code-type parameters, that 'Code Type' parameter will be used in the call to the Pretty Print Server.
        Returns:
        the 'Code Type' that is associated with the file-name extension - or null if there is no file-name extension, or the file-name extension is not found.
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
         int extStartPos = fileName.lastIndexOf('.');
         if (extStartPos == -1) return null;
        
         String ext = fileName.substring(extStartPos + 1);
         if (ext == null) return null;
        
         if ((customMappings != null) && customMappings.containsKey(ext))
             return customMappings.get(ext);
        
         return isCodeType(ext) ? ext : null;
        
      • prettyPrintScrape

        public static java.lang.String prettyPrintScrape​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam)
                throws java.io.IOException
        
        Convenience Method. Invokes prettyPrintScrapeToVector(String, String, String)

        Converts vectorized-output to an HTML-String

        No Cache is expected.
        Code:
        Exact Method Body:
        1
        2
         return Util.pageToString
             (prettyPrintScrapeToVector(codeText, codeTypeParam, styleTypeParam));
        
      • prettyPrintScrape

        public static java.lang.String prettyPrintScrape​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam,
                     HiLiteMe.Cache cache)
                throws java.io.IOException
        
        Convenience Method. Invokes prettyPrintScrapeToVector(String, String, String, Cache)

        Converts vectorized-output to an HTML-String

        Cache Required.
        Code:
        Exact Method Body:
        1
        2
         return Util.pageToString
             (prettyPrintScrapeToVector(codeText, codeTypeParam, styleTypeParam, cache));
        
      • prettyPrintScrapeAndSimplify

        public static java.lang.String prettyPrintScrapeAndSimplify​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam)
                throws java.io.IOException
        
        Convenience Method. Invokes prettyPrintScrapeToVector(String, String, String, Cache)

        Converts vectorized-output to an HTML-String

        No Cache is expected.
        Code:
        Exact Method Body:
        1
        2
        3
        4
        5
         return Util.pageToString(
             simplifyColorSpans(
                 prettyPrintScrapeToVector(codeText, codeTypeParam, styleTypeParam),
                 styleTypeParam
             ));
        
      • prettyPrintScrapeAndSimplify

        public static java.lang.String prettyPrintScrapeAndSimplify​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam,
                     HiLiteMe.Cache cache)
                throws java.io.IOException
        
        Convenience Method. Invokes prettyPrintScrapeToVectorAndSimplify(String, String, String, Cache)

        Converts vectorized-output to an HTML-String

        Cache Required.
        Code:
        Exact Method Body:
        1
        2
        3
         // HTML as a String is returned, Cache is Required
         return Util.pageToString
             (prettyPrintScrapeToVectorAndSimplify(codeText, codeTypeParam, styleTypeParam, cache));
        
      • prettyPrintScrapeToVectorAndSimplify

        public static java.util.Vector<HTMLNodeprettyPrintScrapeToVectorAndSimplify​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam)
                throws java.io.IOException
        
        Convenience Method. Invokes prettyPrintScrapeToVector(String, String, String, Cache)

        Vector<HTMLNode> is returned.

        No Cache is expected.
        Code:
        Exact Method Body:
        1
        2
        3
        4
         return simplifyColorSpans(
                 prettyPrintScrapeToVector(codeText, codeTypeParam, styleTypeParam),
                 styleTypeParam
             );
        
      • prettyPrintScrapeToVector

        public static java.util.Vector<HTMLNodeprettyPrintScrapeToVector​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam)
                throws java.io.IOException
        
        This will take a java.lang.String of code - in almost any coding language - and generate an HTML-"ified" version of that code. This is often called "Pretty-Printing" code. The software-platform/engine that does the transformation of source code to "nice-looking HTML code" is just a site called http://HiLite.me.

        This method takes three parameters, and the first is the code itself (passed as a java.lang.String.) The second is the "descriptor" - which is a short text-String that identifies what type (programming-language) is being passed to HiLite.ME. The complete list of available Code Types can be found in the Raw HTML of HiLite.ME's main page.

        • 'java' => Java Code!
        • 'css' => CSS - Cascading Style Sheets
        • 'js' => Java-Script code
        • The list contains at least 75 languages, select "View Code Source" in Google-Chrome or Internet-Explorer to see complete set of options!


        The third parameter is a 'style' name / type parameter. Again, view the http://HiLite.ME website to see the complete list of available styles. My preferred / defaults are 'vim' and 'native'
        Parameters:
        codeText - This is the software-code (like CSS, Java, SQL, Python, etc..) saved as a single text-String
        codeTypeParam - This variable indicates what type of code this is. Usually, I use 'java'

        NOTE: To view the complete list of Code Types available, just view the returned results of (Iterator<String>) method getCodeTypes()

        You may also click the link below to see the complete list of 'Code Types'

        Programming Language Codes
        styleTypeParam - This variable identifies which of HiLite.ME's styles to choose. Go to the website to see the list of available options.

        NOTE: To view the complete list of 'Style Types'available, just view the returned results of (Iterator<String>) method getStyleTypes()

        You may also click the link below to see the complete list of 'Style Codes'

        HiLiting Style Codes
        Returns:
        The HTML is returned as a Vector<HTMLNode>

        The "HTML with formatting and colors" version of the code. May be saved as an HTML-file, and put on a website to see your own code.

        NOTE: HTML Header and Footer information (for the returned page itself, not the hilited code!) is not included as part of the returned HTML. HTML header elements should be added before writing this String to a '.html' text file!

        SPECIFICALLY: The String that is returned has a single HTML <DIV> element that wraps a large code-hiliting section. The HTML itself, though, does not have any of the usual <HEAD>, <TITLE>, <BODY> tag-elements yet, though.
        Throws:
        java.io.IOException - If there are any problems communicating with the HiLite Server.
        See Also:
        URLs.toProperURLV2(String), HTMLPage.getPageTokens(BufferedReader, boolean), InnerTagGetInclusive, TagNodeRemoveInclusive
        Code:
        Exact Method Body:
         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
         codeText = URLs.toProperURLV2(codeText);
        
         URL             url         = new URL("http://hilite.me");
         URLConnection   connection  = url.openConnection();
        
         connection.setDoOutput(true);  
                
         OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
         out.write(
             "lexer=" + codeTypeParam +
             "&style=" + styleTypeParam + 
             "&linenos=0" +
             "&divstyles=border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;" +
             "&code=" + codeText
         );
         out.flush();   
        
         BufferedReader br = new BufferedReader
             (new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
        
         // Read the HTML String response and Parse the JTML into Vectorized-HTML.
         Vector<HTMLNode> v = HTMLPage.getPageTokens(br, false);
         v = InnerTagGetInclusive.first(v, "div", "id", TextComparitor.EQ_CI, "preview");
        
         // An HTML Validator caught that there were tons of dividers with the same ID - because
         // this method may be called thousands of times for a single file ..  Remove the "ID" !!
         // 
         // If an ID field is needed, add your own.
         v.setElementAt(((TagNode) v.elementAt(0)).remove("id"), 0);
        
         // I don't remember what the first "<P>...</P>" paragraph is about, but it is unnecessary
         TagNodeRemoveInclusive.first(v, "p");
        
         return v;
        
      • prettyPrintScrapeToVector

        public static java.util.Vector<HTMLNodeprettyPrintScrapeToVector​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam,
                     HiLiteMe.Cache cache)
                throws java.io.IOException
        
        This performs an identical operation to method: prettyPrintScrapeToVector(String, String, String), but it checks the provided cache first, before querying the server.
        Parameters:
        codeText - This is the software-code (like CSS, Java, SQL, Python, etc..) saved as a single text-String.
        codeTypeParam - This variable indicates what type of code this is. Usually, I use 'java'

        NOTE: To view the complete list of Code Types available, just view the returned results of (Iterator<String>) method getCodeTypes()

        You may also click the link below to see the complete list of 'Code Types'

        Programming Language Codes
        styleTypeParam - This variable identifies which of HiLite.ME's styles to choose. Go to the website to see the list of available options.

        NOTE: To view the complete list of 'Style Types'available, just view the returned results of (Iterator<String>) method getStyleTypes()

        You may also click the link below to see the complete list of 'Style Codes'

        HiLiting Style Codes
        cache - This is the cache that must be passed. Read about the HiLiteMe.Cache static inner class for an explanation about how caching results from the server works. Save time on the build by caching results that have not changed.
        Returns:
        The HTML is returned as a Vector<HTMLNode>

        The "HTML with formatting and colors" version of the code. May be saved as an HTML-file, and put on a website to see your own code.

        NOTE: HTML Header and Footer information (for the returned page itself, not the hilited code!) is not included as part of the returned HTML. HTML header elements should be added before writing this String to a '.html' text file!

        SPECIFICALLY: The String that is returned has a single HTML <DIV> element that wraps a large code-hiliting section. The HTML itself, though, does not have any of the usual <HEAD>, <TITLE>, <BODY> tag-elements yet, though.
        Throws:
        java.io.IOException - If there are any problems communicating with the HiLite Server.
        See Also:
        prettyPrintScrapeToVector(String, String, String), HTMLPage.getPageTokens(CharSequence, boolean)
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
         // FIRST: Check the Cache, to see if the exact String has been hilited!
         String ret = cache.get(codeText, codeTypeParam, styleTypeParam);
        
         // If there was a Cache hit -> return that String rather than querying the server.
         if (ret != null) return HTMLPage.getPageTokens(ret, false);
        
         // NO? Then query the server.
         Vector<HTMLNode> retVec =
             prettyPrintScrapeToVector(codeText, codeTypeParam, styleTypeParam);
        
         // Make sure to save the response in the Cache for next time
         cache.checkIn(codeText, Util.pageToString(retVec), codeTypeParam, styleTypeParam);
        
         return retVec;
        
      • prettyPrintScrapeToVectorAndSimplify

        public static java.util.Vector<HTMLNodeprettyPrintScrapeToVectorAndSimplify​
                    (java.lang.String codeText,
                     java.lang.String codeTypeParam,
                     java.lang.String styleTypeParam,
                     HiLiteMe.Cache cache)
                throws java.io.IOException
        
        This performs an identical operation to method: prettyPrintScrapeToVector(String, String, String), but it checks the provided cache first, before querying the server. Furthermore, the HTML that is returned has been simplified, using the method simplifyColorSpans(Vector, String).

        Using this version of Pretty Print mandates that the user provide a Cache.
        Parameters:
        codeText - This is the software-code (like CSS, Java, SQL, Python, etc..) saved as a single text-String.
        codeTypeParam - This variable indicates what type of code this is. Usually, I use 'java'

        NOTE: To view the complete list of Code Types available, just view the returned results of (Iterator<String>) method getCodeTypes()

        You may also click the link below to see the complete list of 'Code Types'

        Programming Language Codes
        styleTypeParam - This variable identifies which of HiLite.ME's styles to choose. Go to the website to see the list of available options.

        NOTE: To view the complete list of 'Style Types'available, just view the returned results of (Iterator<String>) method getStyleTypes()

        You may also click the link below to see the complete list of 'Style Codes'

        HiLiting Style Codes
        cache - This is the cache that must be passed. Read about the HiLiteMe.Cache static inner class for an explanation about how caching results from the server works. Save time on the build by caching results that have not changed.
        Returns:
        The HTML is returned as a Vector<HTMLNode>

        The "HTML with formatting and colors" version of the code. May be saved as an HTML-file, and put on a website to see your own code.

        NOTE: HTML Header and Footer information (for the returned page itself, not the hilited code!) is not included as part of the returned HTML. HTML header elements should be added before writing this String to a '.html' text file!

        SPECIFICALLY: The String that is returned has a single HTML <DIV> element that wraps a large code-hiliting section. The HTML itself, though, does not have any of the usual <HEAD>, <TITLE>, <BODY> tag-elements yet, though.
        Throws:
        java.io.IOException - If there are any problems communicating with the HiLite Server.
        See Also:
        prettyPrintScrapeToVector(String, String, String), HTMLPage.getPageTokens(CharSequence, boolean), simplifyColorSpans(Vector, String)
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
         // FIRST: Check the Cache, to see if the exact String has been hilited!
         String ret = cache.get(codeText, codeTypeParam, styleTypeParam);
        
         // If there was a Cache hit -> return that String rather than querying the server.
         if (ret != null) return HTMLPage.getPageTokens(ret, false);
        
         // NO? Then query the server.
         Vector<HTMLNode> retVec =
             prettyPrintScrapeToVector(codeText, codeTypeParam, styleTypeParam);
        
         // Replace all of the STYLE=... attributes to CLASS=... attributes
         simplifyColorSpans(retVec, styleTypeParam);
        
         // Make sure to save the response in the Cache for next time
         cache.checkIn(codeText, Util.pageToString(retVec), codeTypeParam, styleTypeParam);
        
         return retVec;
        
      • styleParamCSSClasses

        public static java.lang.String styleParamCSSClasses​
                    (java.lang.String styleParam)
        
        Will return a java.lang.String containing all of the CSS STYLE definitions for a particular 'Style Type'.

        NOTE: These definitions are only useful if the HiLited Source Code that you have used has been simplified from in-line 'STYLE=...' attributes into a CLASS=... version of the hiliting.
        Parameters:
        styleParam - This may be any of the valid 'Style Tags' available for hiliting source-code. The complete list of 'Style Tags' may be viewed here:

        HiLiting Style Codes
        Returns:
        The CSS Class definitions as a String

        NOTE: The RETURN VALUES (as String's) can actually be viewed right here by clicking the link below. Each 'Style Type' is followed by a list of CSS Definitions that must be included in order for the Code HiLiting to work (if-and-only-if) you have opted to use the "Simplified Color Spans."

        Style Tag CSS Definitions
        Throws:
        java.lang.IllegalArgumentException - If an invalid 'Style Type' has been passed to the 'styleParam' parameter.
        Code:
        Exact Method Body:
        1
        2
        3
        4
        5
        6
        7
        8
         if (! isStyleType(styleParam)) throw new IllegalArgumentException
             ("The Style Type Parameter passed [" + styleParam + "] is not a valid Style Type");
        
         if (styleCSSDefinitions == null)
             styleCSSDefinitions = (TreeMap<String, String>) LFEC.readObjectFromFile_JAR
                 (Torello.Data.DataFileLoader.class, "data20.tmdat", true, TreeMap.class);
        
         return styleCSSDefinitions.get(styleParam);
        
      • main

        public static void main​(java.lang.String[] argv)
        This may be called from the command line. It will print the CSS Definitions for a 'Style Type' parameter. The complete list of valid Style Types may be viewed here:

        Style Tag CSS Definitions
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
         if (argv.length != 1)
             System.out.println("Pass a Style Type as an argument to this main method.");
        
         if (! isStyleType(argv[0]))
             System.out.println(
                 "You have not passed a valid Style Type to this main method.  Please view " +
                 "the documentation to see the list of valid Style Type's."
             );
        
         System.out.println(styleParamCSSClasses(argv[0]));
        
      • simplifyColorSpans

        public static java.util.Vector<HTMLNodesimplifyColorSpans​
                    (java.util.Vector<HTMLNode> page,
                     java.lang.String styleTypeParam)
        
        This method converts the HiLite.ME output to a simplified-version where the inline 'STYLE' attributes are converted to 'CLASS' attributes. The HTML that results looks a lot cleaner and is more simple. Furthermore, the HiLiter itself is not perfect in that the HTML generated by that site has quite a number of "Redundant SPAN" elements. This method will also eliminate redundant HTML <SPAN>'s.

        The conversion that is done here will make one issue more complicated, though. Since the original <SPAN STYLE='color: #...'> [source-code] </SPAN> elements use inline CSS-STYLE declarations (as may be seen in the line directly above), and the output-page from this method uses inline CSS CLASS declarations, it will be necessary to include the CSS CLASSES either in your HTML Page Header somewhere.

        These CSS CLASS definitions may be obtained by calling the method styleParamCSSClasses(String). The return value for this method is a java.lang.String, and it literally will be text that should be placed between two HTML <STYLE> ... </STYLE> tags somewhere in your header.

        The complete list of CSS Definitions Required to use the "Simplified Version" of the code HiLiting may be viewed here:

        Style Tag CSS Definitions

        Below is an example of what the HTML looks like for a piece of Java Source Code that has been hilited - both BEFORE and AFTER this method has been invoked. The first window here is the Java Source Code that is being hilited:


         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        public class SomeClass
        {
            public static void main(String[] argv) throws IOException
            {
                URL url = new URL("https://some.url.com/index.html");
                Vector<HTMLNode> page = HTMLPage.getPageTokens(url, false);
        
                for (HTMLNode n : page)
                    if (n.str.contains("FOO"))
                        System.out.println(n);
            }
        }
        

        The above Java Source Code example, when hilited by this class, will produce (HiLited) HTML that looks as below:

        HTML Elements:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        <!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">SomeClass</span>
            <span style="color: #333333">{</span>
                <span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">main</span><span style="color: #333333">(</span>String<span style="color: #333333">[]</span> argv<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">throws</span> IOException
                <span style="color: #333333">{</span>
                    URL url <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> URL<span style="color: #333333">(</span><span style="background-color: #fff0f0">&quot;https://some.url.com/index.html&quot;</span><span style="color: #333333">);</span>
                    Vector<span style="color: #333333"><</span>HTMLNode<span style="color: #333333">></span> page <span style="color: #333333">=</span> HTMLPage<span style="color: #333333">.</span><span style="color: #0000CC">getPageTokens</span><span style="color: #333333">(</span>url<span style="color: #333333">,</span> <span style="color: #008800; font-weight: bold">false</span><span style="color: #333333">);</span>
            
                    <span style="color: #008800; font-weight: bold">for</span> <span style="color: #333333">(</span>HTMLNode n <span style="color: #333333">:</span> page<span style="color: #333333">)</span>
                        <span style="color: #008800; font-weight: bold">if</span> <span style="color: #333333">(</span>n<span style="color: #333333">.</span><span style="color: #0000CC">str</span><span style="color: #333333">.</span><span style="color: #0000CC">contains</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">&quot;FOO&quot;</span><span style="color: #333333">))</span>
                            System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">println</span><span style="color: #333333">(</span>n<span style="color: #333333">);</span>
                <span style="color: #333333">}</span>
            <span style="color: #333333">}</span>
            </pre></div>    
        

        It is important to note that each of the hilited sections above have an HTML <SPAN STYLE=...> wrapped around key-words inside the source-code. However, after invoking this "Simplify" method, the SPAN elements would look as below:

        HTML Elements:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        <!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><SPAN CLASS=H136>public class</span> <SPAN CLASS=H143>SomeClass</span>
            <SPAN CLASS=H138>{</span>
                <SPAN CLASS=H136>public static</span> <SPAN CLASS=H139>void</span> <SPAN CLASS=H135>main</span><SPAN CLASS=H138>(String[] argv)</span> <SPAN CLASS=H136>throws</span> IOException
                <SPAN CLASS=H138>{
                    URL url =</span> <SPAN CLASS=H136>new</span> URL<SPAN CLASS=H138>(</span><SPAN CLASS=H131>&quot;https://some.url.com/index.html&quot;</span><SPAN CLASS=H138>);
                    Vector<HTMLNode> page = HTMLPage.</span><SPAN CLASS=H132>getPageTokens</span><SPAN CLASS=H138>(url,</span> <SPAN CLASS=H136>false</span><SPAN CLASS=H138>);</span>
            
                    <SPAN CLASS=H136>for</span> <SPAN CLASS=H138>(HTMLNode n : page)</span>
                        <SPAN CLASS=H136>if</span> <SPAN CLASS=H138>(n.</span><SPAN CLASS=H132>str</span><SPAN CLASS=H138>.</span><SPAN CLASS=H132>contains</span><SPAN CLASS=H138>(</span><SPAN CLASS=H131>&quot;FOO&quot;</span><SPAN CLASS=H138>))
                            System.</span><SPAN CLASS=H132>out</span><SPAN CLASS=H138>.</span><SPAN CLASS=H132>println</span><SPAN CLASS=H138>(n);
                }
            }</span>
            </pre></div>
        

        See that the STYLE=... attribute within each SPAN have been replaced by a CLASS=... attribute instead. It is important to remember to retrieve the CSS CLASS definitions for this change, or the hiliting will not be visible!

        IN FACT:

        • A total of 48 STYLE attributes were removed and replaced with CLASS attributes.

        • Of that: 34 <SPAN> and </SPAN> elements were removed completely.

        • The original String-Length of that snippet was 2616 Characters, but the shortened snippet has been cut in half to 1382 Characters
        Parameters:
        page - This should be Vectorized-HTML that was produced by the 'prettyPrintScrapeToVector' method.
        styleTypeParam - This has to be the same value for 'styleTypeParam' that was used to convert the HTML for the 'page' parameter. If a different 'Style Type' is accidentally passed to this method, none of the HTML <SPAN> elements will be replaced.
        Returns:
        The original page Vector<HTMLNode>
        Throws:
        java.lang.IllegalArgumentException - If the passed 'styleTypeParam' is not recognized by the internal list of available 'Style Types'.
        Code:
        Exact Method Body:
         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
         if (! isStyleType(styleTypeParam)) throw new IllegalArgumentException(
             "The passed 'styleTypeParam' value [" + styleTypeParam + "] is not a registered " +
             "Style Type Code known by this class."
         );
        
         // Lazy
         if (allMaps == null)
         {
             allMaps = 
                 (TreeMap<String, TreeMap<String, TagNode>>)
                 LFEC.readObjectFromFile_JAR
                 (Torello.Data.DataFileLoader.class, "data19.tmdat", true, TreeMap.class);
         }
        
         // Retrieves the a TreeMap that maps STYLE-ATTRIBUTE values (retrieved from <SPAN>
         // elements) directly to replacement TagNode's that use a CLASS=... instead.
         TreeMap<String, TagNode> map = allMaps.get(styleTypeParam);
        
         // This should never happen, but if it does, this error is better than NullPointerException
         if (map == null) throw new InternalError(
             "The style parameter you have passed IS VALID, but unfortunately, the map file for " +
             "that style is not loading properly."
         );
        
         // Retrieve all HTML "<SPAN STYLE=...>" elements. Specifically, retrieve all <SPAN>
         // that actually contain a STYLE attribute.  Afterwards, retrieve all of the values of
         // that style-element, and store that in the 'styles' String-Array.
         int[]       spans       = InnerTagFind.all(page, "span", "style");
         String[]    styles      = Attributes.retrieve(page, spans, "style");
         TagNode     replacement = null;
        
         // Minor optimization that's inside the data-file.  The "STYLE='color: #123456;...'
         // eliminates the leading characters "color: #" in the style-attribute.  Here we need
         // to do the EXACT-SAME removal, or else the lookup-table will not find that
         // style-attribute.
         for (int i=0; i < styles.length; i++)
             if (styles[i].startsWith("color: #"))
                 styles[i] = styles[i].substring("color: #".length());
        
         // Replace all of the <SPAN STYLE=...> TagNode's with the simplified TagNode's.
         for (int i=0; i < spans.length; i++)
             if ((replacement = map.get(styles[i])) != null)
                 page.setElementAt(replacement, spans[i]);
        
         removeDuplicateColorSpans(page);
        
         return page;