* @since 1.0
*/
public void highlight(String name, InputStream in, OutputStream out, String encoding, boolean fragment)
throws IOException
{
ExplicitStateHighlighter highlighter = getHighlighter();
Reader isr;
Writer osw;
if (null == encoding)
{
isr = new InputStreamReader(in);
osw = new OutputStreamWriter(out);
}
else
{
isr = new InputStreamReader(in, encoding);
osw = new OutputStreamWriter(out, encoding);
}
BufferedReader r = new BufferedReader(isr);
BufferedWriter w = new BufferedWriter(osw);
if (fragment)
{
// w.write(getXhtmlHeaderFragment(name));
}
else
{
w.write(getXhtmlHeader(name));
}
String line;
String token;
int length;
int style;
String css_class;
int previous_style = 0;
boolean newline = false;
StringBuffer buf = new StringBuffer();
int c = 0;
while ((c = r.read()) != -1) {
buf.append((char) c);
}
String allLines = buf.toString();
String[] lines = allLines.split("\n");
// for (int jj=0; jj < lines.length; jj++) {
// System.out.println("Line: " + lines[jj]);
// }
// We get a new instance of this class each time we parse a programlisting so we can
// can use an instance variable to detect if it's the first time we've been called.
// This will allow us to put <br/> before each line so that the callout extension will work correctly.
for (int i=0; i < lines.length; i ++) {
line = lines[i];
lineNo++;
line = StringUtils.convertTabsToSpaces(line, 4);
// should be optimized by reusing a custom LineReader class
Reader lineReader = new StringReader(line);
highlighter.setReader(lineReader);
int index = 0;
while (index < line.length())
{
style = highlighter.getNextToken();
length = highlighter.getTokenLength();
token = line.substring(index, index + length);
if (style != previous_style || newline) // assume we have a new style if there is a newline
{
css_class = getCssClass(style);