001: /*===============================================================================
002: * Copyright Texas Instruments 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.swing.console;
020:
021: import java.io.*;
022:
023: /**
024: * The LogInputHandler class logs everything written to a console to an output
025: * stream.
026: */
027: public class LogInputHandler extends InputAdapter {
028: private Writer out;
029:
030: /**
031: * Class constructor.
032: *
033: * @param console the console view to which this input handler
034: * attaches to.
035: * @param out the output stream to log to
036: */
037: public LogInputHandler(Console console, Writer out) {
038: super (console.getInputHandler());
039: this .out = out;
040: log("<html> <body bgcolor=\"#000000\" text=\"#ffffff\"><br><pre>");
041: }
042:
043: /**
044: * Class constructor.
045: *
046: * @param console the console view to which this input handler
047: * attaches to.
048: * @param out the output stream to log to
049: */
050: public LogInputHandler(Console console, OutputStream out)
051: throws IOException {
052: this (console, new OutputStreamWriter(out));
053: }
054:
055: /**
056: * Append characters to the end of the character stream.
057: *
058: * @param cbuf the character buffer
059: * @param off the offset into cbuf to first character to append
060: * @param len the number of characters to append
061: */
062: public void append(char[] cbuf, int off, int len) {
063: synchronized (getBufferLock()) {
064: processBufferData(cbuf, off, len);
065: //log( new String( cbuf, off, len ) );
066: super .append(cbuf, off, len);
067: }
068: }
069:
070: /**
071: * Delete characters from end of character stream.
072: *
073: * @param num the number of characters to delete
074: */
075: public void zap(int num) {
076: synchronized (getBufferLock()) {
077: for (int i = 0; i < num; i++)
078: log("^?");
079: super .zap(num);
080: }
081: }
082:
083: int FG_COLOR_ATTR = 1;
084: int BG_COLOR_ATTR = 2;
085: int DATA_ATTR = 3;
086: int NULL_ATTR = -1;
087:
088: /*string buffer holding the html stream info for the regions parsed */
089: java.io.StringWriter htmlStream = new java.io.StringWriter();
090:
091: /* indicates the depth of the currently opened color region
092: * If 0 then indicates that there are no color regions open
093: * or all opened color regions are closed.
094: */
095: int openColorAttrCount = 0;
096:
097: /* Indicates whether processing region color-info or region data */
098: int currAttrTye = DATA_ATTR;
099:
100: /* holds color type 9in string format) of a region */
101: String colorVal = "";
102:
103: /* keeps track of the color index ( r, g, b ) for the current open region */
104: int colorIndex = 0;
105:
106: boolean receivedEscChar = false;
107:
108: /**
109: * Writes the string to a htmlStream buffer
110: *
111: * @param buf string buffer holding the html format string.
112: */
113: private void writeToHtmlStream(String buf) {
114: htmlStream.write(buf);
115: }
116:
117: /**
118: * Handles the color info for the currently open region being parsed.
119: *
120: * @param c character from the region's stream buffer
121: */
122: private void handleColorInfo(char c) {
123: colorVal += (c > 0) ? java.lang.Integer.toHexString((int) c)
124: : "00";
125: colorIndex++;
126: if (colorIndex >= 3) {
127: //got all r,g,b values so form a string for the rgb value.
128: String colorTag = "color: #" + colorVal;
129: if (currAttrTye == BG_COLOR_ATTR) {
130: colorTag = "background-" + colorTag;
131: } else {
132: String fontTag = "<font color=\"#" + colorVal + "\">";
133: writeToHtmlStream(fontTag);
134: }
135:
136: String styleTag = "<span style=\"" + colorTag + ";\">";
137: writeToHtmlStream(styleTag);
138:
139: colorVal = "";
140: currAttrTye = DATA_ATTR;
141: colorIndex = 0;
142: }
143: }
144:
145: /**
146: * flushes the html stream buffer to disk file.
147: *
148: */
149: private void flushHtmlInfo() {
150: String logStr = htmlStream.toString();
151:
152: if (logStr.length() > 0) {
153: log(logStr);
154: htmlStream = new java.io.StringWriter();
155: }
156: }
157:
158: /**
159: * process's the passed on buffer and converts it into html format strings.
160: * scans each char in the buffer and processes it.If a color region start is found
161: * then the color info is extracted, converted to html tags and streamed to a html string buffer.
162: * If the char is part of display text then it is just streamed to the html string buffer.
163: *
164: * @param cbuf the character buffer
165: * @param off the offset into cbuf to first character to append
166: * @param len the number of characters to append
167: */
168: private void processBufferData(char[] cbuf, int off, int len) {
169: for (int index = 0; index < len; index++) {
170: char c = cbuf[index + off];
171: switch (c) {
172: case ColorInputHandler.ESCAPE_CHAR:
173: receivedEscChar = true;
174: break;
175:
176: case ColorInputHandler.HYPERLINK_ATTR_OPEN:
177: case ColorInputHandler.HYPERLINK_ATTR_CLOSE:
178: //index++;
179: break;
180:
181: case ColorInputHandler.BGCOLOR_ATTR_OPEN:
182: case ColorInputHandler.FGCOLOR_ATTR_OPEN:
183: if (receivedEscChar) {
184: currAttrTye = (c == ColorInputHandler.BGCOLOR_ATTR_OPEN) ? BG_COLOR_ATTR
185: : FG_COLOR_ATTR;
186: openColorAttrCount++;
187: }
188: break;
189:
190: case ColorInputHandler.FGCOLOR_ATTR_CLOSE:
191: case ColorInputHandler.BGCOLOR_ATTR_CLOSE:
192: receivedEscChar = false;
193: currAttrTye = DATA_ATTR;
194: if (openColorAttrCount > 0) {
195: openColorAttrCount--;
196: writeToHtmlStream("</span>");
197:
198: if (c == ColorInputHandler.FGCOLOR_ATTR_CLOSE)
199: writeToHtmlStream("</font>");
200:
201: if (openColorAttrCount == 0)
202: flushHtmlInfo();
203: }
204: break;
205:
206: default: {
207: if (currAttrTye == DATA_ATTR)
208: writeToHtmlStream(String.valueOf(c));
209: else
210: handleColorInfo(c);
211: break;
212: }
213: }
214: }
215:
216: if (openColorAttrCount == 0) {
217: flushHtmlInfo();
218: }
219: return;
220: }
221:
222: private void log(String str) {
223: if (out != null) {
224: try {
225: out.write(str);
226: out.flush();
227: } catch (IOException e) {
228: out = null;
229: throw new ti.exceptions.ProgrammingErrorException(e);
230: }
231: }
232: }
233:
234: public void close() {
235: if (out != null) {
236: try {
237: log("</pre></body></html>");
238: out.close();
239: out = null;
240: } catch (IOException e) {
241: throw new ti.exceptions.ProgrammingErrorException(
242: "unhandled exception: " + e);
243: }
244: }
245: }
246:
247: }
248:
249: /*
250: * Local Variables:
251: * tab-width: 2
252: * indent-tabs-mode: nil
253: * mode: java
254: * c-indentation-style: java
255: * c-basic-offset: 2
256: * eval: (c-set-offset 'substatement-open '0)
257: * eval: (c-set-offset 'case-label '+)
258: * eval: (c-set-offset 'inclass '+)
259: * eval: (c-set-offset 'inline-open '0)
260: * End:
261: */
|