/*===============================================================================
* Copyright Texas Instruments 2002. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package ti.swing.console;
import java.io.*;
/**
* The LogInputHandler class logs everything written to a console to an output
* stream.
*/
public class LogInputHandler extends InputAdapter
{
private Writer out;
/**
* Class constructor.
*
* @param console the console view to which this input handler
* attaches to.
* @param out the output stream to log to
*/
public LogInputHandler( Console console, Writer out )
{
super( console.getInputHandler() );
this.out = out;
log( "<html> <body bgcolor=\"#000000\" text=\"#ffffff\"><br><pre>" );
}
/**
* Class constructor.
*
* @param console the console view to which this input handler
* attaches to.
* @param out the output stream to log to
*/
public LogInputHandler( Console console, OutputStream out )
throws IOException
{
this( console, new OutputStreamWriter(out) );
}
/**
* Append characters to the end of the character stream.
*
* @param cbuf the character buffer
* @param off the offset into cbuf to first character to append
* @param len the number of characters to append
*/
public void append( char[] cbuf, int off, int len )
{
synchronized( getBufferLock() )
{
processBufferData( cbuf, off, len );
//log( new String( cbuf, off, len ) );
super.append( cbuf, off, len );
}
}
/**
* Delete characters from end of character stream.
*
* @param num the number of characters to delete
*/
public void zap( int num )
{
synchronized( getBufferLock() )
{
for( int i=0; i<num; i++ )
log("^?");
super.zap(num);
}
}
int FG_COLOR_ATTR = 1;
int BG_COLOR_ATTR = 2;
int DATA_ATTR = 3;
int NULL_ATTR = -1;
/*string buffer holding the html stream info for the regions parsed */
java.io.StringWriter htmlStream = new java.io.StringWriter();
/* indicates the depth of the currently opened color region
* If 0 then indicates that there are no color regions open
* or all opened color regions are closed.
*/
int openColorAttrCount =0;
/* Indicates whether processing region color-info or region data */
int currAttrTye = DATA_ATTR;
/* holds color type 9in string format) of a region */
String colorVal = "";
/* keeps track of the color index ( r, g, b ) for the current open region */
int colorIndex = 0;
boolean receivedEscChar = false;
/**
* Writes the string to a htmlStream buffer
*
* @param buf string buffer holding the html format string.
*/
private void writeToHtmlStream(String buf)
{
htmlStream.write(buf);
}
/**
* Handles the color info for the currently open region being parsed.
*
* @param c character from the region's stream buffer
*/
private void handleColorInfo( char c )
{
colorVal += (c>0)?java.lang.Integer.toHexString((int)c):"00";
colorIndex++;
if( colorIndex >= 3 )
{
//got all r,g,b values so form a string for the rgb value.
String colorTag = "color: #" + colorVal ;
if( currAttrTye == BG_COLOR_ATTR)
{
colorTag = "background-" + colorTag;
}
else
{
String fontTag = "<font color=\"#" + colorVal + "\">";
writeToHtmlStream(fontTag);
}
String styleTag = "<span style=\"" + colorTag + ";\">";
writeToHtmlStream(styleTag);
colorVal = "";
currAttrTye = DATA_ATTR;
colorIndex = 0;
}
}
/**
* flushes the html stream buffer to disk file.
*
*/
private void flushHtmlInfo()
{
String logStr = htmlStream.toString();
if( logStr.length() > 0)
{
log(logStr);
htmlStream = new java.io.StringWriter();
}
}
/**
* process's the passed on buffer and converts it into html format strings.
* scans each char in the buffer and processes it.If a color region start is found
* then the color info is extracted, converted to html tags and streamed to a html string buffer.
* If the char is part of display text then it is just streamed to the html string buffer.
*
* @param cbuf the character buffer
* @param off the offset into cbuf to first character to append
* @param len the number of characters to append
*/
private void processBufferData(char[] cbuf, int off, int len)
{
for( int index = 0;index < len; index++)
{
char c = cbuf[index+off];
switch(c)
{
case ColorInputHandler.ESCAPE_CHAR:
receivedEscChar = true;
break;
case ColorInputHandler.HYPERLINK_ATTR_OPEN:
case ColorInputHandler.HYPERLINK_ATTR_CLOSE:
//index++;
break;
case ColorInputHandler.BGCOLOR_ATTR_OPEN:
case ColorInputHandler.FGCOLOR_ATTR_OPEN:
if( receivedEscChar )
{
currAttrTye =(c == ColorInputHandler.BGCOLOR_ATTR_OPEN)? BG_COLOR_ATTR:FG_COLOR_ATTR;
openColorAttrCount++;
}
break;
case ColorInputHandler.FGCOLOR_ATTR_CLOSE:
case ColorInputHandler.BGCOLOR_ATTR_CLOSE:
receivedEscChar = false;
currAttrTye = DATA_ATTR;
if( openColorAttrCount > 0 )
{
openColorAttrCount--;
writeToHtmlStream("</span>");
if( c == ColorInputHandler.FGCOLOR_ATTR_CLOSE )
writeToHtmlStream("</font>");
if( openColorAttrCount == 0 )
flushHtmlInfo();
}
break;
default:
{
if( currAttrTye == DATA_ATTR )
writeToHtmlStream(String.valueOf(c));
else
handleColorInfo(c);
break;
}
}
}
if( openColorAttrCount == 0 )
{
flushHtmlInfo();
}
return;
}
private void log( String str )
{
if( out != null )
{
try
{
out.write(str);
out.flush();
}
catch(IOException e)
{
out = null;
throw new ti.exceptions.ProgrammingErrorException(e);
}
}
}
public void close()
{
if( out != null )
{
try
{
log("</pre></body></html>");
out.close();
out = null;
}
catch(IOException e)
{
throw new ti.exceptions.ProgrammingErrorException("unhandled exception: " + e);
}
}
}
}
/*
* Local Variables:
* tab-width: 2
* indent-tabs-mode: nil
* mode: java
* c-indentation-style: java
* c-basic-offset: 2
* eval: (c-set-offset 'substatement-open '0)
* eval: (c-set-offset 'case-label '+)
* eval: (c-set-offset 'inclass '+)
* eval: (c-set-offset 'inline-open '0)
* End:
*/
|