com.Ostermiller.Syntax
Class ToHTML

java.lang.Object
  extended by com.Ostermiller.Syntax.ToHTML

public class ToHTML
extends Object

ToHTML will take source and convert it into an html document with syntax highlighting. All the special characters in the html will be replaced by their escape sequences.

A typical use of this class might be:

 try {
     ToHTML toHTML = new ToHTML();
     toHTML.setInput(new FileReader("Source.java"));
     toHTML.setOutput(new FileWriter("Source.java.html"));
     toHTML.setMimeType("text/x-java");
     toHTML.setFileExt("java") 
     toHTML.writeFullHTML();
 } catch (InvocationTargetException x){
     // can only happen if setLexerType(java.lang.String) method is used.
 } catch (CompileException x){
     // can only happen if setTemplate(java.lang.String) method is used.
 } catch (IOException x){
     System.err.println(x.getMessage());
 }
 

A ToHTML instance may be reused, however it is not thread safe and should only be used by one thread at a time without external synchronization.


Field Summary
protected static ResourceBundle labels
          Locale specific strings displayed to the user.
 
Constructor Summary
ToHTML()
           
 
Method Summary
 boolean addIgnoreStyle(String style)
          Adds the specified style to be ignored if it is not already present.
 String getMimeType()
          Get the mime type of the document.
 String getStyleSheet()
          Get the URL of the linked style sheet.
 String getTitle()
          Get the title used for the html document.
static void htmlify(InputStream in, PrintStream out, String mimeType)
          Write the html version of a document using syntax highlighting for the given mime type.
static void htmlify(Lexer lexer, PrintStream out)
          Write the html version of the output from the given lexer.
static void htmlify(Lexer lexer, PrintStream out, String title)
          Write the html version of the output from the given lexer as a stand alone html document with the given title.
static void htmlify(Lexer lexer, PrintWriter out)
          Write the html version of the output from the given lexer.
static void htmlify(Lexer lexer, PrintWriter out, String title)
          Write the html version of the output from the given lexer as a stand alone html document with the given title.
static void htmlify(Reader in, PrintWriter out, String mimeType)
          Write the html encoded version of a document using syntax highlighting for the given mime type.
static void htmlify(Reader in, PrintWriter out, String title, String mimeType)
          Write the html version of the output from the given lexer as a stand alone html document with the given title.
static void htmlifyC(Reader in, PrintWriter out)
          Write the html encoded version of a document using syntax highlighting for C.
static void htmlifyComplexHTML(Reader in, PrintWriter out)
          Write the html encoded version of a document using syntax highlighting for HTML using a complex coloring algorithm.
static void htmlifyJava(Reader in, PrintWriter out)
          Write the html encoded version of a document using syntax highlighting for Java.
static void htmlifyPlain(Reader in, PrintWriter out)
          Write the html encoded version of a document using no syntax highlighting.
static void htmlifySimpleHTML(Reader in, PrintWriter out)
          Write the html encoded version of a document using syntax highlighting for HTML using a simple coloring algorithm.
static void main(String[] args)
          Program to add syntax highlighting to source files.
static void register(String lexer, String[] mimeTypes, String[] fileExtensions)
          Register a lexer to handle the given mime types and fileExtensions.
 boolean removeIgnoreStyle(String style)
          No longer ignore the given style.
 String removeTranslation(String canonicalStyle)
          No longer ignore the given style.
 void setDocName(String docName)
          Set the name of this document (minus any file extension).
 void setDocNameFromFileName(String inputFileName)
          Sets the document name minus the path and extension
 void setExtFromFileName(String inputFileName)
          Sets the file extension to be anything after the last '.' in the given file name.
 void setFileExt(String fileExt)
          Set the file extension to be used for the document.
 void setInput(File f)
          Set the input to the given file.
 void setInput(Reader in)
          Set the input stream.
 void setLexerType(String lexerType)
          Set the lexer to be used for the document.
 void setMimeType(String mimeType)
          Set the mime type of the document.
 void setOutput(File f)
          Set the output to the given file.
 void setOutput(PrintWriter out)
          Set the output stream.
 void setOutput(Writer out)
          Set the output stream.
 void setStyleSheet(String styleSheet)
          Link the html document to the style sheet at the given URL.
 void setTemplate(String bteSuper)
          Set the URL of the BTE template file which should used.
 void setTitle(String title)
          Use the given title for the html document.
 String translateStyle(String canonicalStyle, String style)
          Rename a style.
static void writeEscapedHTML(String text, PrintStream out)
          Write the string after escaping characters that would hinder it from rendering in html.
static void writeEscapedHTML(String text, PrintWriter out)
          Write the string after escaping characters that would hinder it from rendering in html.
 void writeFullHTML()
          Write a full HTML document to the output location that has been set for this class.
 void writeHTMLFragment()
          Write a HTML fragment to the output location that has been set for this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

labels

protected static ResourceBundle labels
Locale specific strings displayed to the user.

Constructor Detail

ToHTML

public ToHTML()
Method Detail

register

public static void register(String lexer,
                            String[] mimeTypes,
                            String[] fileExtensions)
Register a lexer to handle the given mime types and fileExtensions.

If a document has a type "text/plain" it will first match a registered mime type of "text/plain" and then a registered mime type of "text".

Parameters:
lexer - String representing the fully qualified java name of the lexer.
mimeTypes - array of mime types that the lexer can handle.
fileExtensions - array of fileExtensions the lexer can handle. (case insensitive)

writeHTMLFragment

public void writeHTMLFragment()
                       throws IOException,
                              InvocationTargetException
Write a HTML fragment to the output location that has been set for this class.

The type of syntax highlighting done will be determined first by the class name for the lexer, then by the mime type, then by the file extension.

To save space in the resulting output, it is often a good idea to ignore whitespace. If whitespace is ignored, it will be merged with surrounding tags.

Throws:
IOException - if an I/O error occurs.
InvocationTargetException - if a lexer class is specified which can't be instantiated.

writeFullHTML

public void writeFullHTML()
                   throws IOException,
                          InvocationTargetException,
                          com.Ostermiller.bte.CompileException
Write a full HTML document to the output location that has been set for this class. If a template URL is specified, and template libraries are installed, templates will be used.

The type of syntax highlighting done will be determined first by the class name for the lexer, then by the mime type, then by the file extension.

To save space in the resulting output, it is often a good idea to ignore whitespace. If whitespace is ignored, it will be merged with surrounding tags.

If templates are used, the following sections are available to the template:
date
highlightedDocument
fileExtension
fileName (minus the extension)
styleSheet (if specified)
title (if specified)
The default template may be used as a guide.

Throws:
IOException - if an I/O error occurs.
InvocationTargetException - if a lexer class is specified which can't be instantiated.
com.Ostermiller.bte.CompileException - if a bte template is specified which can't be compiled.

setTemplate

public void setTemplate(String bteSuper)
Set the URL of the BTE template file which should used. For more information on BTE templates please see the BTE website.

Parameters:
bteSuper - url of the bte template file.

setFileExt

public void setFileExt(String fileExt)
Set the file extension to be used for the document.

The type of syntax highlighting to use will depend on the lexerType that is set, the mime type that is given, and the file extension that is given. If a lexer is explicitly given, it is used, otherwise if the mime type is recognized, an appropriate style for that mime type is used, otherwise if the file extension is recognized, an appropriate style for that extension is used, otherwise, no style is given.

Parameters:
fileExt - the file extension of the document or null to clear.

setDocName

public void setDocName(String docName)
Set the name of this document (minus any file extension).

This name will be reported to html template.

Parameters:
docName - Name of the document.

setLexerType

public void setLexerType(String lexerType)
Set the lexer to be used for the document.

The type of syntax highlighting to use will depend on the lexerType that is set, the mime type that is given, and the file extension that is given. If a lexer is explicitly given, it is used, otherwise if the mime type is recognized, an appropriate style for that mime type is used, otherwise if the file extension is recognized, an appropriate style for that extension is used, otherwise, no style is given.

Parameters:
lexerType - full java name of the lexer to use.

setMimeType

public void setMimeType(String mimeType)
Set the mime type of the document.

The type of syntax highlighting to use will depend on the lexerType that is set, the mime type that is given, and the file extension that is given. If a lexer is explicitly given, it is used, otherwise if the mime type is recognized, an appropriate style for that mime type is used, otherwise if the file extension is recognized, an appropriate style for that extension is used, otherwise, no style is given.

Parameters:
mimeType - the mimeType of the document or null to clear.

getMimeType

public String getMimeType()
Get the mime type of the document.

Returns:
the mimeType of the document or null if none.

setOutput

public void setOutput(Writer out)
Set the output stream.

Parameters:
out - new output stream.

setOutput

public void setOutput(PrintWriter out)
Set the output stream.

Parameters:
out - new output stream.

setOutput

public void setOutput(File f)
               throws IOException
Set the output to the given file.

Parameters:
f - file to which to write.
Throws:
IOException - if the file cannot be opened.

setInput

public void setInput(File f)
              throws IOException
Set the input to the given file.

Parameters:
f - file from which to read.
Throws:
IOException - if the file cannot be opened.

setInput

public void setInput(Reader in)
Set the input stream.

Parameters:
in - new input stream.

setStyleSheet

public void setStyleSheet(String styleSheet)
Link the html document to the style sheet at the given URL.

Parameters:
styleSheet - link stylesheet URL to use, or null to none.

setTitle

public void setTitle(String title)
Use the given title for the html document.

Parameters:
title - document title, or null to use none.

getStyleSheet

public String getStyleSheet()
Get the URL of the linked style sheet.

Returns:
stylesheet URL to used, or null if none.

getTitle

public String getTitle()
Get the title used for the html document.

Returns:
document title, or null if none.

addIgnoreStyle

public boolean addIgnoreStyle(String style)
Adds the specified style to be ignored if it is not already present. Ignored styles will not have a span associated with them in the output. This can be used to make the resulting files smaller. It is suggested to ignore "whitespace" as it probably won't look any different with an associated style.

If styles have been translated, then the translated style should be the one to be ignored.

Parameters:
style - the name of the style to be ignored.
Returns:
true if the style was not already being ignored.

removeIgnoreStyle

public boolean removeIgnoreStyle(String style)
No longer ignore the given style.

Parameters:
style - the name of the style to no longer be ignored.
Returns:
true if the style was being ignored.

translateStyle

public String translateStyle(String canonicalStyle,
                             String style)
Rename a style. Useful for working with style sheets that expect certain names or for saving space by providing shorter names. The name of the style will only be changed in the html document. You must edit the CSS stylesheet or provide a custom CSS stylesheet with this option as the default stylesheet will use canonical style names.

Parameters:
canonicalStyle - the canonical (default) name of the style.
style - the new name of the style.
Returns:
previous translation of the canonicalStyle.

removeTranslation

public String removeTranslation(String canonicalStyle)
No longer ignore the given style.

Parameters:
canonicalStyle - the canonical (default) name of the style.
Returns:
previous translation of the canonicalStyle.

main

public static void main(String[] args)
Program to add syntax highlighting to source files. Execute java com.Ostermiller.Syntax.ToHTML --help for more details.


setDocNameFromFileName

public void setDocNameFromFileName(String inputFileName)
Sets the document name minus the path and extension

Parameters:
inputFileName - file name from which to extract the document name.

setExtFromFileName

public void setExtFromFileName(String inputFileName)
Sets the file extension to be anything after the last '.' in the given file name.

Parameters:
inputFileName - file name from which to extract the extension.

htmlifyJava

public static void htmlifyJava(Reader in,
                               PrintWriter out)
                        throws IOException
Write the html encoded version of a document using syntax highlighting for Java.

The document produced is not a full html document but rather an html fragment that may be inserted into a full document.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html.
out - html document gets written here.
Throws:
IOException - if an I/O error occurs.

htmlifyC

public static void htmlifyC(Reader in,
                            PrintWriter out)
                     throws IOException
Write the html encoded version of a document using syntax highlighting for C.

The document produced is not a full html document but rather an html fragment that may be inserted into a full document.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html.
out - html document gets written here.
Throws:
IOException - if an I/O error occurs.

htmlifySimpleHTML

public static void htmlifySimpleHTML(Reader in,
                                     PrintWriter out)
                              throws IOException
Write the html encoded version of a document using syntax highlighting for HTML using a simple coloring algorithm.

The document produced is not a full html document but rather an html fragment that may be inserted into a full document.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html.
out - html document gets written here.
Throws:
IOException - if an I/O error occurs.

htmlifyComplexHTML

public static void htmlifyComplexHTML(Reader in,
                                      PrintWriter out)
                               throws IOException
Write the html encoded version of a document using syntax highlighting for HTML using a complex coloring algorithm.

The document produced is not a full html document but rather an html fragment that may be inserted into a full document.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html.
out - html document gets written here.
Throws:
IOException - if an I/O error occurs.

htmlifyPlain

public static void htmlifyPlain(Reader in,
                                PrintWriter out)
                         throws IOException
Write the html encoded version of a document using no syntax highlighting.

The document produced is not a full html document but rather an html fragment that may be inserted into a full document.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html.
out - html document gets written here.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(Reader in,
                           PrintWriter out,
                           String mimeType)
                    throws IOException
Write the html encoded version of a document using syntax highlighting for the given mime type.

Recognized mime-types include:

If a mime type is not recognized, the html is written without syntax highlighting.

The document produced is not a full html document but rather an html fragment that may be inserted into a full document.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html
out - html document gets written here
mimeType - the mime type of the document to be used for syntax highlighting purposes.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(Reader in,
                           PrintWriter out,
                           String title,
                           String mimeType)
                    throws IOException
Write the html version of the output from the given lexer as a stand alone html document with the given title. The html document will use the style sheet syntax.css.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - contains the tokens that need to be html escaped.
out - html gets written here.
title - the title to be put on the html document.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(InputStream in,
                           PrintStream out,
                           String mimeType)
                    throws IOException
Write the html version of a document using syntax highlighting for the given mime type.

Conversions between characters and bytes will be done using the default character set for the system.

This is a convenience method that creates an new ToHTML object.

Parameters:
in - Data stream that needs to be formatted in html.
out - html document gets written here.
mimeType - the mime type of the document to be used for syntax highlighting purposes.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(Lexer lexer,
                           PrintWriter out)
                    throws IOException
Write the html version of the output from the given lexer. The document is written without an html head or footer so that it can be used as part of a larger html document.

This is a convenience method that creates an new ToHTML object.

Parameters:
lexer - contains the tokens that need to be html escaped.
out - html gets written here.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(Lexer lexer,
                           PrintStream out)
                    throws IOException
Write the html version of the output from the given lexer. The document is written without an html head or footer so that it can be used as part of a larger html document.

Conversions between characters and bytes will be done using the default character set for the system.

This is a convenience method that creates an new ToHTML object.

Parameters:
lexer - contains the tokens that need to be html escaped.
out - html gets written here.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(Lexer lexer,
                           PrintWriter out,
                           String title)
                    throws IOException
Write the html version of the output from the given lexer as a stand alone html document with the given title. The html document will use the style sheet syntax.css.

This is a convenience method that creates an new ToHTML object.

Parameters:
lexer - contains the tokens that need to be html escaped.
out - html gets written here.
title - the title to be put on the html document.
Throws:
IOException - if an I/O error occurs.

htmlify

public static void htmlify(Lexer lexer,
                           PrintStream out,
                           String title)
                    throws IOException
Write the html version of the output from the given lexer as a stand alone html document with the given title. The html document will use the style sheet syntax.css.

Conversions between characters and bytes will be done using the default character set for the system.

This is a convenience method that creates an new ToHTML object.

Parameters:
lexer - contains the tokens that need to be html escaped.
out - html gets written here.
title - the title to be put on the html document.
Throws:
IOException - if an I/O error occurs.

writeEscapedHTML

public static void writeEscapedHTML(String text,
                                    PrintWriter out)
Write the string after escaping characters that would hinder it from rendering in html.

Parameters:
text - The string to be escaped and written
out - output gets written here

writeEscapedHTML

public static void writeEscapedHTML(String text,
                                    PrintStream out)
Write the string after escaping characters that would hinder it from rendering in html.

Conversions between characters and bytes will be done using the default character set for the system.

Parameters:
text - The string to be escaped and written
out - output gets written here