Class JavaCodeTransform

  • All Implemented Interfaces:
    java.io.Serializable

    public class JavaCodeTransform
    extends java.lang.Object
    implements java.io.Serializable
    Syntax highlights java by turning it into html. A codeviewer object is created and then keeps state as lines are passed in. Each line passed in as java test, is returned as syntax highlighted html text. Users of the class can set how the java code will be highlighted with setter methods. Only valid java lines should be passed in since the object maintains state and may not handle illegal code gracefully. The actual system is implemented as a series of filters that deal with specific portions of the java code. The filters are as follows:
      htmlFilter
        |__
          ongoingMultiLineCommentFilter -> uriFilter
            |__
              inlineCommentFilter
                |__
                  beginMultiLineCommentFilter -> ongoingMultiLineCommentFilter
                    |__
                      stringFilter
                        |__
                          keywordFilter
                            |__
                              uriFilter
                                |__
                                  jxrFilter
                                    |__
                                      importFilter
     
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String COMMENT_END
      end comment delimiter
      private static java.lang.String COMMENT_START
      start comment delimiter
      private java.nio.file.Path currentFilename
      File name that is currently being processed.
      private FileManager fileManager
      current file manager
      private boolean inJavadocComment
      Flag set to true when a javadoc comment is started.
      private boolean inMultiLineComment
      Flag set to true when a multi-line comment is started.
      private static java.lang.String JAVADOC_COMMENT_END
      end javadoc comment delimiter
      private static java.lang.String JAVADOC_COMMENT_START
      start javadoc comment delimiter
      private java.nio.file.Path javadocLinkDir
      Relative path to javadocs, suitable for hyperlinking.
      private static boolean LINE_NUMBERS
      show line numbers
      private java.util.Locale locale
      The wanted locale
      private java.lang.String outputEncoding
      The output encoding
      private PackageManager packageManager
      Package Manager for this project.
      private static java.lang.String RESERVED_WORD_END
      end reserved word delimiter
      private static java.lang.String RESERVED_WORD_START
      start reserved word delimiter
      private java.util.Map<java.lang.String,​java.lang.String> reservedWords
      HashTable containing java reserved words
      private java.lang.String revision
      Revision of the currently transformed document.
      private static java.lang.String STRING_END
      end String delimiter
      private static java.lang.String STRING_START
      start String delimiter
      private static java.lang.String STYLESHEET_FILENAME
      stylesheet file name
      private static char[] VALID_URI_CHARS
      Specify the only characters that are allowed in a URI besides alpha and numeric characters.
      private static java.lang.String[] VALID_URI_SCHEMES  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void appendFooter​(java.io.PrintWriter out, java.lang.String bottom)
      Gets the footer attribute of the JavaCodeTransform object
      private void appendHeader​(java.io.PrintWriter out)
      Gets the header attribute of the JavaCodeTransform object
      private java.lang.String beginMultiLineCommentFilter​(java.lang.String line)
      Detect and handle the start of multiLine comments.
      private java.nio.file.Path getCurrentFilename()
      Gets the current file name.
      private java.lang.String getFileOverview()
      Gets an overview header for this file.
      private java.lang.String getHREF​(java.lang.String dest)
      Based on the destination package, get the HREF.
      private java.lang.String getHREF​(java.lang.String dest, ClassType jc)
      Given the current package, get an HREF to the package and class given
      private java.lang.String getLineWidth​(int linenumber)
      Handles line width which may need to change depending on which line number you are on.
      private int getPackageCount​(java.lang.String packageName)
      Given the name of a package...
      private java.lang.String getPackageRoot()
      From the current file, determine the package root based on the current path.
      private java.io.Reader getReader​(java.nio.file.Path sourcefile, java.lang.String inputEncoding)  
      java.lang.String getRevision()
      The current revision of the module.
      private java.io.Writer getWriter​(java.nio.file.Path destfile, java.lang.String outputEncoding)  
      private java.lang.String htmlFilter​(java.lang.String line)
      Filter html tags into more benign text.
      private java.lang.String importFilter​(java.lang.String line)
      Parse out the current link and look for package/import statements and then create HREFs for them
      private java.lang.String inlineCommentFilter​(java.lang.String line)
      Filter inline comments from a line and formats them properly.
      private boolean isInsideString​(java.lang.String line, int position)
      Checks to see if some position in a line is between String start and ending characters.
      private boolean isInvalidURICharacter​(char c)
      if the given char is not one of the following in VALID_URI_CHARS then return true
      private java.lang.String jxrFilter​(java.lang.String line)
      Handles finding classes based on the current filename and then makes HREFs for you to link to them with.
      private java.lang.String keywordFilter​(java.lang.String line)
      Filters keywords from a line of text and formats them properly.
      private java.lang.String ongoingMultiLineCommentFilter​(java.lang.String line)
      Handle ongoing multi-line comments, detecting ends if present.
      State is maintained in private boolean members, one each for javadoc and (normal) multi-line comments.
      (package private) void readObject​(java.io.ObjectInputStream ois)  
      private void setCurrentFilename​(java.nio.file.Path filename)
      Sets the current file name.
      private java.lang.String stringFilter​(java.lang.String line)
      Filters strings from a line of text and formats them properly.
      private java.lang.String syntaxHighlight​(java.lang.String line)
      Now different method of seeing if at end of input stream, closes inputs stream at end.
      private void transform​(java.io.Reader sourceReader, java.io.Writer destWriter, java.util.Locale locale, java.lang.String outputEncoding, java.nio.file.Path javadocLinkDir, java.lang.String revision, java.lang.String bottom)
      This is the public method for doing all transforms of code.
      void transform​(java.nio.file.Path sourcefile, java.nio.file.Path destfile, java.util.Locale locale, java.lang.String inputEncoding, java.lang.String outputEncoding, java.nio.file.Path javadocLinkDir, java.lang.String revision, java.lang.String bottom)
      This is the public method for doing all transforms of code.
      private java.lang.String uriFilter​(java.lang.String line)
      Given a line of text, search for URIs and make href's out of them.
      (package private) void writeObject​(java.io.ObjectOutputStream oos)  
      private java.lang.String xrLine​(java.lang.String line, java.lang.String packageName, ClassType classType)
      Cross Reference the given line with JXR returning the new content.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LINE_NUMBERS

        private static final boolean LINE_NUMBERS
        show line numbers
        See Also:
        Constant Field Values
      • COMMENT_START

        private static final java.lang.String COMMENT_START
        start comment delimiter
        See Also:
        Constant Field Values
      • COMMENT_END

        private static final java.lang.String COMMENT_END
        end comment delimiter
        See Also:
        Constant Field Values
      • JAVADOC_COMMENT_START

        private static final java.lang.String JAVADOC_COMMENT_START
        start javadoc comment delimiter
        See Also:
        Constant Field Values
      • JAVADOC_COMMENT_END

        private static final java.lang.String JAVADOC_COMMENT_END
        end javadoc comment delimiter
        See Also:
        Constant Field Values
      • STRING_START

        private static final java.lang.String STRING_START
        start String delimiter
        See Also:
        Constant Field Values
      • STRING_END

        private static final java.lang.String STRING_END
        end String delimiter
        See Also:
        Constant Field Values
      • RESERVED_WORD_START

        private static final java.lang.String RESERVED_WORD_START
        start reserved word delimiter
        See Also:
        Constant Field Values
      • RESERVED_WORD_END

        private static final java.lang.String RESERVED_WORD_END
        end reserved word delimiter
        See Also:
        Constant Field Values
      • STYLESHEET_FILENAME

        private static final java.lang.String STYLESHEET_FILENAME
        stylesheet file name
        See Also:
        Constant Field Values
      • VALID_URI_SCHEMES

        private static final java.lang.String[] VALID_URI_SCHEMES
      • VALID_URI_CHARS

        private static final char[] VALID_URI_CHARS
        Specify the only characters that are allowed in a URI besides alpha and numeric characters. Refer RFC2396 - http://www.ietf.org/rfc/rfc2396.txt
      • reservedWords

        private java.util.Map<java.lang.String,​java.lang.String> reservedWords
        HashTable containing java reserved words
      • inMultiLineComment

        private boolean inMultiLineComment
        Flag set to true when a multi-line comment is started.
      • inJavadocComment

        private boolean inJavadocComment
        Flag set to true when a javadoc comment is started.
      • currentFilename

        private java.nio.file.Path currentFilename
        File name that is currently being processed.
      • revision

        private java.lang.String revision
        Revision of the currently transformed document.
      • outputEncoding

        private java.lang.String outputEncoding
        The output encoding
      • locale

        private java.util.Locale locale
        The wanted locale
      • javadocLinkDir

        private java.nio.file.Path javadocLinkDir
        Relative path to javadocs, suitable for hyperlinking.
      • packageManager

        private final PackageManager packageManager
        Package Manager for this project.
      • fileManager

        private final FileManager fileManager
        current file manager
    • Method Detail

      • syntaxHighlight

        private java.lang.String syntaxHighlight​(java.lang.String line)
        Now different method of seeing if at end of input stream, closes inputs stream at end.
        Parameters:
        line - String
        Returns:
        filtered line of code
      • appendHeader

        private void appendHeader​(java.io.PrintWriter out)
        Gets the header attribute of the JavaCodeTransform object
        Parameters:
        out - the writer where the header is appended to
      • appendFooter

        private void appendFooter​(java.io.PrintWriter out,
                                  java.lang.String bottom)
        Gets the footer attribute of the JavaCodeTransform object
        Parameters:
        out - the writer where the header is appended to
        bottom - the bottom text
      • transform

        private void transform​(java.io.Reader sourceReader,
                               java.io.Writer destWriter,
                               java.util.Locale locale,
                               java.lang.String outputEncoding,
                               java.nio.file.Path javadocLinkDir,
                               java.lang.String revision,
                               java.lang.String bottom)
                        throws java.io.IOException
        This is the public method for doing all transforms of code.
        Parameters:
        sourceReader - Reader
        destWriter - Writer
        locale - String
        outputEncoding - String
        javadocLinkDir - String
        revision - String
        bottom - string
        Throws:
        java.io.IOException
      • transform

        public final void transform​(java.nio.file.Path sourcefile,
                                    java.nio.file.Path destfile,
                                    java.util.Locale locale,
                                    java.lang.String inputEncoding,
                                    java.lang.String outputEncoding,
                                    java.nio.file.Path javadocLinkDir,
                                    java.lang.String revision,
                                    java.lang.String bottom)
                             throws java.io.IOException
        This is the public method for doing all transforms of code.
        Parameters:
        sourcefile - source file
        destfile - destination file
        locale - locale
        inputEncoding - input encoding
        outputEncoding - output encoding
        javadocLinkDir - relative path to javadocs
        revision - revision of the module
        bottom - bottom text
        Throws:
        java.io.IOException - in I/O failures in reading/writing files
      • getWriter

        private java.io.Writer getWriter​(java.nio.file.Path destfile,
                                         java.lang.String outputEncoding)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getReader

        private java.io.Reader getReader​(java.nio.file.Path sourcefile,
                                         java.lang.String inputEncoding)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getCurrentFilename

        private java.nio.file.Path getCurrentFilename()
        Gets the current file name.
        Returns:
        path of file
      • setCurrentFilename

        private void setCurrentFilename​(java.nio.file.Path filename)
        Sets the current file name.
        Parameters:
        filename - file name
      • getPackageRoot

        private java.lang.String getPackageRoot()
        From the current file, determine the package root based on the current path.
        Returns:
        package root
      • uriFilter

        private java.lang.String uriFilter​(java.lang.String line)
        Given a line of text, search for URIs and make href's out of them.
        Parameters:
        line - String
        Returns:
        href
      • getRevision

        public final java.lang.String getRevision()
        The current revision of the module.
        Returns:
        revision
      • xrLine

        private java.lang.String xrLine​(java.lang.String line,
                                        java.lang.String packageName,
                                        ClassType classType)
        Cross Reference the given line with JXR returning the new content.
        Parameters:
        line - line
        packageName - String
        classType - ClassType
        Returns:
        cross-referenced line
      • htmlFilter

        private java.lang.String htmlFilter​(java.lang.String line)
        Filter html tags into more benign text.
        Parameters:
        line - String
        Returns:
        html encoded line
      • ongoingMultiLineCommentFilter

        private java.lang.String ongoingMultiLineCommentFilter​(java.lang.String line)
        Handle ongoing multi-line comments, detecting ends if present.
        State is maintained in private boolean members, one each for javadoc and (normal) multi-line comments.
        Parameters:
        line - line
        Returns:
        processed line
      • inlineCommentFilter

        private java.lang.String inlineCommentFilter​(java.lang.String line)
        Filter inline comments from a line and formats them properly. One problem we'll have to solve here: comments contained in a string should be ignored... this is also true of the multi-line comments. So, we could either ignore the problem, or implement a function called something like isInsideString(line, index) where index points to some point in the line that we need to check... started doing this function below.
        Parameters:
        line - line
        Returns:
        processed line
      • beginMultiLineCommentFilter

        private java.lang.String beginMultiLineCommentFilter​(java.lang.String line)
        Detect and handle the start of multiLine comments. State is maintained in private boolean members one each for javadoc and (normal) multiline comments.
        Parameters:
        line - line
        Returns:
        processed line
      • stringFilter

        private java.lang.String stringFilter​(java.lang.String line)
        Filters strings from a line of text and formats them properly.
        Parameters:
        line - line
        Returns:
        processed line
      • keywordFilter

        private java.lang.String keywordFilter​(java.lang.String line)
        Filters keywords from a line of text and formats them properly.
        Parameters:
        line - line
        Returns:
        processed line
      • isInsideString

        private boolean isInsideString​(java.lang.String line,
                                       int position)
        Checks to see if some position in a line is between String start and ending characters. Not yet used in code or fully working :)
        Parameters:
        line - String
        position - int
        Returns:
        boolean
      • writeObject

        final void writeObject​(java.io.ObjectOutputStream oos)
                        throws java.io.IOException
        Parameters:
        oos - ObjectOutputStream
        Throws:
        java.io.IOException - on I/O error during write
      • readObject

        final void readObject​(java.io.ObjectInputStream ois)
                       throws java.lang.ClassNotFoundException,
                              java.io.IOException
        Parameters:
        ois - object input stream
        Throws:
        java.lang.ClassNotFoundException - if the class of a serialized object could not be found.
        java.io.IOException - on I/O error during read
      • getFileOverview

        private java.lang.String getFileOverview()
        Gets an overview header for this file.
        Returns:
        overview header
      • getLineWidth

        private java.lang.String getLineWidth​(int linenumber)
        Handles line width which may need to change depending on which line number you are on.
        Parameters:
        linenumber - int
        Returns:
        blanks
      • jxrFilter

        private java.lang.String jxrFilter​(java.lang.String line)
        Handles finding classes based on the current filename and then makes HREFs for you to link to them with.
        Parameters:
        line - line
        Returns:
        processed line
      • getHREF

        private java.lang.String getHREF​(java.lang.String dest,
                                         ClassType jc)
        Given the current package, get an HREF to the package and class given
        Parameters:
        dest - destination
        jc - class type
        Returns:
        href
      • getHREF

        private java.lang.String getHREF​(java.lang.String dest)
        Based on the destination package, get the HREF.
        Parameters:
        dest - destination
        Returns:
        href
      • getPackageCount

        private int getPackageCount​(java.lang.String packageName)

        Given the name of a package... get the number of subdirectories/subpackages there would be.

        EX: org.apache.maven == 3

        Parameters:
        packageName - String
        Returns:
        int
      • importFilter

        private java.lang.String importFilter​(java.lang.String line)
        Parse out the current link and look for package/import statements and then create HREFs for them
        Parameters:
        line - line
        Returns:
        processed line
      • isInvalidURICharacter

        private boolean isInvalidURICharacter​(char c)
        if the given char is not one of the following in VALID_URI_CHARS then return true
        Parameters:
        c - char to check against VALID_URI_CHARS list
        Returns:
        true if c is a valid URI char