Package openbook.tools.converter
Class HTMLTokenRenderer
- java.lang.Object
-
- openbook.tools.converter.HTMLTokenRenderer
-
- All Implemented Interfaces:
TokenRenderer
public class HTMLTokenRenderer extends java.lang.Object implements TokenRenderer
Renders Java source tokens as HTML tags. This renderer renders the parsed Java tokens with HTML styles. The styles of the tokens are determined by their types such as Java keywords, identifiers, comments etc. Moreover, an identifier can be a custom type if it matched a given list of identifiers. The actual HTML text is enclosed in <span id="style">token<\span> to apply the style. The styles are defined in a Cascaded Style Sheet (CSS). The cascaded style sheet by default is namedjava.css.
The rendering takes care of line breaks and white space in the following way to work around some limitations of Swing based HTML editor kit's usage of CSS attributes.- Line Breaks: Swing Editor seems to require an explicit carriage return-line feed character to render in separate line. While a normal browser works with <br> tag alone.
- White space: The CSS property
white-spaceis sufficient for browsers to preserve white space within <span> tags. But Swing Editor seems to require explicitnbsp;for white spaces within <span> tags.
Two boolean properties are provided to control these two properties.
- Line Numbering: A boolean property controls whether line numbers will be printed. Line numbers are printed in 4 digits with leading zeros, by default.
- Line Number Anchoring: An anchor can be specified at every line. The anchor is
line.nnnwherennnis the actual line number without any leading zero.- JavaDoc comment : The JavaDoc comments can use characters that if reproduced exactly in HTML output can confuse the rendering process. On the other hand, the JavaDoc tags that define an anchor in the source code or creates a hyperlink should be preserved in the HTML output. The capacity and limitation of processing HTML tages inside JavaDoc comments are described in here.
- Author:
- Pinaki Poddar
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCSS_ANNOTATIONstatic java.lang.StringCSS_COMMENTstatic java.lang.StringCSS_CUSTOMThe CSS named styles.static java.lang.StringCSS_DECIMALstatic java.lang.StringCSS_ENUMstatic java.lang.StringCSS_KEYWORDstatic java.lang.StringCSS_LINE_NOstatic java.lang.StringCSS_LITERALstatic java.lang.StringHTML_BR_TAGstatic java.lang.StringHTML_SPACEstatic java.lang.StringNEW_LINE
-
Constructor Summary
Constructors Constructor Description HTMLTokenRenderer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringendLine(int line)Gets a end-of-line string: a HTML <br> tag followed by carriage return and line feed.booleangetAddExplicitSpace()Affirms if explicitwill be added at the HTML output.booleangetAddLineBreak()Affirms if explicit line break (carriage return and line feed) will be added at the HTML output.booleangetAnchorLineNumber()Affirms if an anchor will be created on every line.java.lang.StringgetEpilogue()Gets the closing <BODY> and <HTML> tagsjava.lang.StringgetLineNumberFormat()Gets the format string to format line number such as"%%0%4d"for a 4-digit number with leading zeros.java.lang.StringgetPrologue()Gets the opening <BODY> and <HTML> tags and the <link type="stylesheet"> clause.booleangetShowLineNumber()Affirms if a line number will be added in HTML output.java.lang.StringgetStylesheet()Gets the stylesheet to be linked at HTML output.java.lang.StringnewLine(int line)Gets a string for beginning of a new line.java.lang.Stringrender(int decision, org.antlr.runtime.Token token)Produce a string representation of the given token.voidsetAddExplicitSpace(boolean addSpace)Sets if explicitwill be added at the HTML output.voidsetAddLineBreak(boolean addLineBreak)Sets if explicit line break (carriage return and line feed) will be added at the HTML output.voidsetAnchorLineNumber(boolean anchorLineNumber)Sets if an anchor will be created on every line.voidsetLineNumberFormat(java.lang.String lineNumberFormat)Sets the format string to format line number such as"%%0%4d"for a 4-digit number with leading zeros.voidsetShowLineNumber(boolean showLineNumber)Sets if a line number will be added in HTML output.voidsetStylesheet(java.lang.String stylesheet)Sets the stylesheet to be linked at the HTML output.
-
-
-
Field Detail
-
CSS_CUSTOM
public static final java.lang.String CSS_CUSTOM
The CSS named styles.- See Also:
- Constant Field Values
-
CSS_KEYWORD
public static final java.lang.String CSS_KEYWORD
- See Also:
- Constant Field Values
-
CSS_ANNOTATION
public static final java.lang.String CSS_ANNOTATION
- See Also:
- Constant Field Values
-
CSS_ENUM
public static final java.lang.String CSS_ENUM
- See Also:
- Constant Field Values
-
CSS_COMMENT
public static final java.lang.String CSS_COMMENT
- See Also:
- Constant Field Values
-
CSS_LITERAL
public static final java.lang.String CSS_LITERAL
- See Also:
- Constant Field Values
-
CSS_DECIMAL
public static final java.lang.String CSS_DECIMAL
- See Also:
- Constant Field Values
-
CSS_LINE_NO
public static final java.lang.String CSS_LINE_NO
- See Also:
- Constant Field Values
-
NEW_LINE
public static final java.lang.String NEW_LINE
- See Also:
- Constant Field Values
-
HTML_BR_TAG
public static final java.lang.String HTML_BR_TAG
- See Also:
- Constant Field Values
-
HTML_SPACE
public static final java.lang.String HTML_SPACE
- See Also:
- Constant Field Values
-
-
Method Detail
-
endLine
public java.lang.String endLine(int line)
Gets a end-of-line string: a HTML <br> tag followed by carriage return and line feed.- Specified by:
endLinein interfaceTokenRenderer- Parameters:
line- the line being ended- Returns:
- a String can be a blank
-
newLine
public java.lang.String newLine(int line)
Gets a string for beginning of a new line.- Specified by:
newLinein interfaceTokenRenderer- Parameters:
line- the current line number- Returns:
- a String can be a blank
-
render
public java.lang.String render(int decision, org.antlr.runtime.Token token)Description copied from interface:TokenRendererProduce a string representation of the given token.- Specified by:
renderin interfaceTokenRenderer- Parameters:
decision- the index of the decision (or the context) in which the current token is being processed. The index refers to ANTLRDebugEventListener.enterDecision(int).token- the token to be rendered. Can be a hidden token as well.- Returns:
- a string representation of the given token.
-
getPrologue
public java.lang.String getPrologue()
Gets the opening <BODY> and <HTML> tags and the <link type="stylesheet"> clause.- Specified by:
getProloguein interfaceTokenRenderer
-
getEpilogue
public java.lang.String getEpilogue()
Gets the closing <BODY> and <HTML> tags- Specified by:
getEpiloguein interfaceTokenRenderer
-
getStylesheet
public java.lang.String getStylesheet()
Gets the stylesheet to be linked at HTML output.
-
setStylesheet
public void setStylesheet(java.lang.String stylesheet)
Sets the stylesheet to be linked at the HTML output.
-
getShowLineNumber
public boolean getShowLineNumber()
Affirms if a line number will be added in HTML output.
-
setShowLineNumber
public void setShowLineNumber(boolean showLineNumber)
Sets if a line number will be added in HTML output.
-
getAnchorLineNumber
public boolean getAnchorLineNumber()
Affirms if an anchor will be created on every line.
-
setAnchorLineNumber
public void setAnchorLineNumber(boolean anchorLineNumber)
Sets if an anchor will be created on every line.
-
getAddLineBreak
public boolean getAddLineBreak()
Affirms if explicit line break (carriage return and line feed) will be added at the HTML output.- See Also:
endLine(int)
-
setAddLineBreak
public void setAddLineBreak(boolean addLineBreak)
Sets if explicit line break (carriage return and line feed) will be added at the HTML output.- See Also:
endLine(int)
-
getAddExplicitSpace
public boolean getAddExplicitSpace()
Affirms if explicitwill be added at the HTML output.
-
setAddExplicitSpace
public void setAddExplicitSpace(boolean addSpace)
Sets if explicitwill be added at the HTML output.
-
getLineNumberFormat
public java.lang.String getLineNumberFormat()
Gets the format string to format line number such as"%%0%4d"for a 4-digit number with leading zeros.
-
setLineNumberFormat
public void setLineNumberFormat(java.lang.String lineNumberFormat)
Sets the format string to format line number such as"%%0%4d"for a 4-digit number with leading zeros.
-
-