Class JavaCodeTransform

java.lang.Object
org.apache.maven.jxr.JavaCodeTransform
All Implemented Interfaces:
Serializable

public class JavaCodeTransform extends Object implements 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:
  • Field Details

    • LINE_NUMBERS

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

      private static final String COMMENT_START
      start comment delimiter
      See Also:
    • COMMENT_END

      private static final String COMMENT_END
      end comment delimiter
      See Also:
    • JAVADOC_COMMENT_START

      private static final String JAVADOC_COMMENT_START
      start javadoc comment delimiter
      See Also:
    • JAVADOC_COMMENT_END

      private static final String JAVADOC_COMMENT_END
      end javadoc comment delimiter
      See Also:
    • STRING_START

      private static final String STRING_START
      start String delimiter
      See Also:
    • STRING_END

      private static final String STRING_END
      end String delimiter
      See Also:
    • RESERVED_WORD_START

      private static final String RESERVED_WORD_START
      start reserved word delimiter
      See Also:
    • RESERVED_WORD_END

      private static final String RESERVED_WORD_END
      end reserved word delimiter
      See Also:
    • STYLESHEET_FILENAME

      private static final String STYLESHEET_FILENAME
      stylesheet file name
      See Also:
    • VALID_URI_SCHEMES

      private static final 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 Map<String,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 Path currentFilename
      File name that is currently being processed.
    • revision

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

      private String outputEncoding
      The output encoding
    • locale

      private Locale locale
      The wanted locale
    • javadocLinkDir

      private 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
  • Constructor Details

  • Method Details

    • syntaxHighlight

      private String syntaxHighlight(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(PrintWriter out)
      Gets the header attribute of the JavaCodeTransform object
      Parameters:
      out - the writer where the header is appended to
    • appendFooter

      private void appendFooter(PrintWriter out, 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(Reader sourceReader, Writer destWriter, Locale locale, String outputEncoding, Path javadocLinkDir, String revision, String bottom) throws 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:
      IOException
    • transform

      public final void transform(Path sourcefile, Path destfile, Locale locale, String inputEncoding, String outputEncoding, Path javadocLinkDir, String revision, String bottom) throws 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:
      IOException - in I/O failures in reading/writing files
    • getWriter

      private Writer getWriter(Path destfile, String outputEncoding) throws IOException
      Throws:
      IOException
    • getReader

      private Reader getReader(Path sourcefile, String inputEncoding) throws IOException
      Throws:
      IOException
    • getCurrentFilename

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

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

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

      private String uriFilter(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 String getRevision()
      The current revision of the module.
      Returns:
      revision
    • xrLine

      private String xrLine(String line, 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 String htmlFilter(String line)
      Filter html tags into more benign text.
      Parameters:
      line - String
      Returns:
      html encoded line
    • ongoingMultiLineCommentFilter

      private String ongoingMultiLineCommentFilter(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 String inlineCommentFilter(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 String beginMultiLineCommentFilter(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 String stringFilter(String line)
      Filters strings from a line of text and formats them properly.
      Parameters:
      line - line
      Returns:
      processed line
    • keywordFilter

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

      private boolean isInsideString(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(ObjectOutputStream oos) throws IOException
      Parameters:
      oos - ObjectOutputStream
      Throws:
      IOException - on I/O error during write
    • readObject

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

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

      private 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 String jxrFilter(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 String getHREF(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 String getHREF(String dest)
      Based on the destination package, get the HREF.
      Parameters:
      dest - destination
      Returns:
      href
    • getPackageCount

      private int getPackageCount(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 String importFilter(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