Java2xhtml - A Java Source code highlighter

Java2xhtml is a java source code hightlighter. The program will read Java source code and produce an XHTML document that marks (highlights) the stucture of the language.

Here is a sample

The program works by marking up the structure of the source code. The structures that are deliniated are: keywords, methods, variables, single line comments, traditional comments, javadoc comments, javadoc tags, import names, package names, primitive types, non-primitive types, constructors, constants, double quoted text, single quoted text, numerical literals, and primitive literals.
In addition to marking up the structure of the source code the program can also include line numbering, block background marking, modulus line marking, a legend, a footer (complete with WW3 verification icons for XHTML and CSS as well as a last modified date of the document). The generated XHTML documnet can be either XHTML 1.1 of XHTML 1.0 Strict, or if desired the reusulting output can be a code snippet for inclusion into another XHTML document.

NOTE The program assumes that the Java naming conventions are followed: Classes and Interfaces start with capital letters, constants are in all capital letters and everything else starts with a lowercase letter.


The design of the source code was based on a state machine. As the program parses the java source code it enters into one of nine states: TEXT, IMPORT_NAME, PARAM_VARIABLE, JAVADOC, PACKAGE_NAME, DOUBLE_QUOTE, SINGLE_QUOTE, TRADITIOAL_COMMENT, or LINE_COMMENT. Based on the state of the parsing and the java naming conventions the code is able to markup the structure of the code.


Since the design of the code is based on a state machine and java naming conventions are assumed to be used, the engineering of the program was rather straight forward. I used a singleton class to represent the states, HashSets to hold the keywords, primitive types, primitive literals, and javadoc tags, and property files to hold the varius user options of the program. To increase speed a StringBuffers were used for input and output of the information. The input is read one character at a time, with identifiers allowed to be fully quantified.

Certification Reports (Testing)

Testing was done by running the program on many different java programs and verifying that the code was pased correctly. The program was run on itself and verified. The program was a good test on itseld since the program uses all the different states and has all the source code structures that can be parsed.

Software Documentation

Here is the output of the program on any bad input

Java2xhtml Version 0.9 (C) 2003 Shayne Steele
    Produces an XHTML file of Java source code with syntax highlighting,
    includes additional options (line numbering, tab spacing, etc.)
    License and warranty are under the terms of the GNU General Public License
        (GPL) Version 2.0
    See for license and warranty details.
    NOTE: Common java naming structure is assumed

java  [java options]  Java2xhtml  [options file]  [output file]

  - java is the name of the Java interpreter
  - [java options] are the optional options of the Java interpreter
  - Java2xhtml is the name of this application
  - source is a file or the directory to the Java source file(s)
  - [options file] is the optional path of a file with
    a structure like this:
        externalStyleSheetName=file_name (default style.css)
        tabSize=integer  (default value is 4)
        extraIndentation=integer  (default value is 0)
        lineModulus=integer (default value 5)
        isCodeSnippet=boolean (default false)
        isXHTML_1_1=boolean (default true)
        hasInternalStyleSheet=boolean (default true)
        hasExternalStyleSheet=boolean (default true)
        hasTitle=boolean (default false)
        hasLegend=boolean (default false)
        hasAllBoldSourceCode=boolean (default false)
        hasLineNumbers=boolean (default false)
        hasLineModulusDrawnLines=boolean (default false)
        hasLineModulusCodeBlocks=boolean (default false)
        hasFooter=boolean (default false)
        hasFooterIcons=boolean (default false)
        hasFooterDate=boolean (default true)
    NOTE: filename must end with '.prop'
    Default [options file] is options.prop
  - [output file] is name of the XHTML file that is produced
    Default [output file] is source_java.html

Output: --> [output file]
    Default Output is --> source_java.html

Examples of calling the program:
 process one file (say
    java  Java2xhtml
 process one directory (say C:\HOME):
    java  Java2xhtml  C:\HOME
 process one directory (say C:\HOME with a given options file (options.prop)):
    java  Java2xhtml  C:\HOME options.prop


Using this style sheet style.css, the default output looks like Java2xhtml_java.html.

Using the same style sheet as above and this properties file options.prop (this is the default options file name) and running the program like so
java Java2xhtml Java2xhtml2.html
the output file is Java2xhtml2.html

Using the internal style sheet and this properties file options2.prop (this is the default options file name) and running the program like so
java Java2xhtml options2.prop Java2xhtml3.html
the output file is Java2xhtml3.html

Using this style sheet style2.css, and this properties file options3.prop (this is the default options file name) and running the program like so
java Java2xhtml options3.prop Java2xhtml4.html
the output file is Java2xhtml4.html