|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.Ostermiller.Syntax.ToHTML
public class ToHTML
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 |
---|
protected static ResourceBundle labels
Constructor Detail |
---|
public ToHTML()
Method Detail |
---|
public static void register(String lexer, String[] mimeTypes, String[] 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".
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)public void writeHTMLFragment() throws IOException, InvocationTargetException
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.
IOException
- if an I/O error occurs.
InvocationTargetException
- if a lexer class is specified which can't be instantiated.public void writeFullHTML() throws IOException, InvocationTargetException, com.Ostermiller.bte.CompileException
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.
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.public void setTemplate(String bteSuper)
bteSuper
- url of the bte template file.public void setFileExt(String fileExt)
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.
fileExt
- the file extension of the document or null to clear.public void setDocName(String docName)
This name will be reported to html template.
docName
- Name of the document.public void setLexerType(String lexerType)
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.
lexerType
- full java name of the lexer to use.public void setMimeType(String mimeType)
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.
mimeType
- the mimeType of the document or null to clear.public String getMimeType()
public void setOutput(Writer out)
out
- new output stream.public void setOutput(PrintWriter out)
out
- new output stream.public void setOutput(File f) throws IOException
f
- file to which to write.
IOException
- if the file cannot be opened.public void setInput(File f) throws IOException
f
- file from which to read.
IOException
- if the file cannot be opened.public void setInput(Reader in)
in
- new input stream.public void setStyleSheet(String styleSheet)
styleSheet
- link stylesheet URL to use, or null to none.public void setTitle(String title)
title
- document title, or null to use none.public String getStyleSheet()
public String getTitle()
public boolean addIgnoreStyle(String style)
If styles have been translated, then the translated style should be the one to be ignored.
style
- the name of the style to be ignored.
public boolean removeIgnoreStyle(String style)
style
- the name of the style to no longer be ignored.
public String translateStyle(String canonicalStyle, String style)
canonicalStyle
- the canonical (default) name of the style.style
- the new name of the style.
public String removeTranslation(String canonicalStyle)
canonicalStyle
- the canonical (default) name of the style.
public static void main(String[] args)
public void setDocNameFromFileName(String inputFileName)
inputFileName
- file name from which to extract the document name.public void setExtFromFileName(String inputFileName)
inputFileName
- file name from which to extract the extension.public static void htmlifyJava(Reader in, PrintWriter out) throws IOException
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.
in
- Data stream that needs to be formatted in html.out
- html document gets written here.
IOException
- if an I/O error occurs.public static void htmlifyC(Reader in, PrintWriter out) throws IOException
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.
in
- Data stream that needs to be formatted in html.out
- html document gets written here.
IOException
- if an I/O error occurs.public static void htmlifySimpleHTML(Reader in, PrintWriter out) throws IOException
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.
in
- Data stream that needs to be formatted in html.out
- html document gets written here.
IOException
- if an I/O error occurs.public static void htmlifyComplexHTML(Reader in, PrintWriter out) throws IOException
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.
in
- Data stream that needs to be formatted in html.out
- html document gets written here.
IOException
- if an I/O error occurs.public static void htmlifyPlain(Reader in, PrintWriter out) throws IOException
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.
in
- Data stream that needs to be formatted in html.out
- html document gets written here.
IOException
- if an I/O error occurs.public static void htmlify(Reader in, PrintWriter out, String mimeType) throws IOException
Recognized mime-types include:
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.
in
- Data stream that needs to be formatted in htmlout
- html document gets written heremimeType
- the mime type of the document to be used for syntax highlighting purposes.
IOException
- if an I/O error occurs.public static void htmlify(Reader in, PrintWriter out, String title, String mimeType) throws IOException
This is a convenience method that creates an new ToHTML object.
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.
IOException
- if an I/O error occurs.public static void htmlify(InputStream in, PrintStream out, String mimeType) throws IOException
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.
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.
IOException
- if an I/O error occurs.public static void htmlify(Lexer lexer, PrintWriter out) throws IOException
This is a convenience method that creates an new ToHTML object.
lexer
- contains the tokens that need to be html escaped.out
- html gets written here.
IOException
- if an I/O error occurs.public static void htmlify(Lexer lexer, PrintStream out) throws IOException
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.
lexer
- contains the tokens that need to be html escaped.out
- html gets written here.
IOException
- if an I/O error occurs.public static void htmlify(Lexer lexer, PrintWriter out, String title) throws IOException
This is a convenience method that creates an new ToHTML object.
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.
IOException
- if an I/O error occurs.public static void htmlify(Lexer lexer, PrintStream out, String title) throws IOException
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.
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.
IOException
- if an I/O error occurs.public static void writeEscapedHTML(String text, PrintWriter out)
text
- The string to be escaped and writtenout
- output gets written herepublic static void writeEscapedHTML(String text, PrintStream out)
Conversions between characters and bytes will be done using the default character set for the system.
text
- The string to be escaped and writtenout
- output gets written here
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |