Highlighting fragments of the source

Warning - java2web is now obsolete. Please use its successor: code2web

back to index

Sometimes you might want to highlight a specific area of the source code like this:

0166    
// We have several possibilites. See which one we have imported
0167 for (String qualifiedClassName : possibilities) { 0168 int dot = qualifiedClassName.lastIndexOf("."); 0169 String packageName = qualifiedClassName.substring(0,dot);
0170 for (String imp : imports) { 0171 if (imp.equals(packageName)) return qualifiedClassName; 0172 } 0173 } 0174 0175
// We have several possibilities, but none of them was explicitly imported. In such case
0176
// test if one of the possibilities is java.lang - which does not need an explicit import
0177 for (String qualifiedClassName : possibilities) { 0178 if (qualifiedClassName.equals("java.lang."+className)) 0179 return "java.lang."+className; 0180 }

You can do this using the following methods

HTMLConverter

void highlight( int startLine, int endLine )
void clearHighlights()
void highlight( String zones )

The last method takes a sequence of comma-separated ranges using the syntax: "start line..end line, start line .. end line, ...". For example:

conv.highlight("2..5,88,109..125");

will highlight lines from 2 through 5 inclusive, line 88 and lines 109 through 125 inclusive.

Java2Web

If you are using Java2Web, you define the zones to highlight using the --highlight option and the compact syntax described above:

java2web ... --highlight="2..5,88,109..125" ...

JSP Tag

If you are using the JSP tag, you use the highlight attribute with the same syntax.

<j2w:convert ... highlight="2..5,88,109..125" ... >

 

Style

As always, you can configure the style applied to the highlighted region. This is controlled by the highlightStyle property, with a default value of "highlight"

HTMLConverter   String getHighlightStyle()
void setHighlightStyle( String style )
Java2Web   java2web ... --highlightStyle = xxxxx ...
<j2w:convert>   <j2w:convert ... highlightStyle="style" ... >