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-space
is 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.nnn
wherennn
is 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.String
CSS_ANNOTATION
static java.lang.String
CSS_COMMENT
static java.lang.String
CSS_CUSTOM
The CSS named styles.static java.lang.String
CSS_DECIMAL
static java.lang.String
CSS_ENUM
static java.lang.String
CSS_KEYWORD
static java.lang.String
CSS_LINE_NO
static java.lang.String
CSS_LITERAL
static java.lang.String
HTML_BR_TAG
static java.lang.String
HTML_SPACE
static java.lang.String
NEW_LINE
-
Constructor Summary
Constructors Constructor Description HTMLTokenRenderer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
endLine(int line)
Gets a end-of-line string: a HTML <br> tag followed by carriage return and line feed.boolean
getAddExplicitSpace()
Affirms if explicitboolean
getAddLineBreak()
Affirms if explicit line break (carriage return and line feed) will be added at the HTML output.boolean
getAnchorLineNumber()
Affirms if an anchor will be created on every line.java.lang.String
getEpilogue()
Gets the closing <BODY> and <HTML> tagsjava.lang.String
getLineNumberFormat()
Gets the format string to format line number such as"%%0%4d"
for a 4-digit number with leading zeros.java.lang.String
getPrologue()
Gets the opening <BODY> and <HTML> tags and the <link type="stylesheet"> clause.boolean
getShowLineNumber()
Affirms if a line number will be added in HTML output.java.lang.String
getStylesheet()
Gets the stylesheet to be linked at HTML output.java.lang.String
newLine(int line)
Gets a string for beginning of a new line.java.lang.String
render(int decision, org.antlr.runtime.Token token)
Produce a string representation of the given token.void
setAddExplicitSpace(boolean addSpace)
Sets if explicitvoid
setAddLineBreak(boolean addLineBreak)
Sets if explicit line break (carriage return and line feed) will be added at the HTML output.void
setAnchorLineNumber(boolean anchorLineNumber)
Sets if an anchor will be created on every line.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.void
setShowLineNumber(boolean showLineNumber)
Sets if a line number will be added in HTML output.void
setStylesheet(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:
endLine
in 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:
newLine
in 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:TokenRenderer
Produce a string representation of the given token.- Specified by:
render
in 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:
getPrologue
in interfaceTokenRenderer
-
getEpilogue
public java.lang.String getEpilogue()
Gets the closing <BODY> and <HTML> tags- Specified by:
getEpilogue
in 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 explicit
-
setAddExplicitSpace
public void setAddExplicitSpace(boolean addSpace)
Sets if explicit
-
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.
-
-