001: package com.meterware.httpunit.parsing;
002:
003: /********************************************************************************************************************
004: * $Id: JTidyPrintWriter.java,v 1.3 2004/10/05 11:29:52 russgold Exp $
005: *
006: * Copyright (c) 2001-2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.util.StringTokenizer;
024: import java.util.Enumeration;
025: import java.io.PrintWriter;
026: import java.net.URL;
027: import java.text.DecimalFormat;
028: import java.text.NumberFormat;
029: import java.text.ParseException;
030:
031: /**
032: * Basic "parser" for the JTidy error output. Will get the line and column number as well
033: * as the message. It assumes that an error or warning is to be logged once println() has been
034: * called or if a string starts with "line"
035: *
036: * @author <a href="mailto:bx@bigfoot.com">Benoit Xhenseval</a>
037: * @author <a href="mailto:proyal@managingpartners.com">Peter Royal</a>
038: **/
039: class JTidyPrintWriter extends PrintWriter {
040: /**
041: * DecimalFormat.getNumberInstance() should provide us with a proper formatter for the default locale. The
042: * integers returned from JTidy contain the appropriate decimal separator for the current locale.
043: */
044: private static final NumberFormat INTEGER_FORMAT = DecimalFormat
045: .getNumberInstance();
046:
047: JTidyPrintWriter(URL pageURL) {
048: super (System.out);
049: _url = pageURL;
050: }
051:
052: public void print(boolean b) {
053: print(String.valueOf(b));
054: }
055:
056: public void print(char c) {
057: print(String.valueOf(c));
058: }
059:
060: public void print(char[] s) {
061: print(String.valueOf(s));
062: }
063:
064: public void print(double d) {
065: print(String.valueOf(d));
066: }
067:
068: public void print(float f) {
069: print(String.valueOf(f));
070: }
071:
072: public void print(int i) {
073: print(String.valueOf(i));
074: }
075:
076: public void print(long l) {
077: print(String.valueOf(l));
078: }
079:
080: public void print(Object obj) {
081: print(obj.toString());
082: }
083:
084: /**
085: * Detects a new log if starting with "line", a warning if message starts with "Warning"
086: * and an error if it starts with "Error"
087: **/
088: public void print(String s) {
089: if (s.startsWith("line")) {
090: if (!_logged && _line > 0 && _msg != null
091: && _msg.length() > 0) {
092: log(); // log previous!!!
093: }
094: _logged = false; // new error....
095: StringTokenizer tok = new StringTokenizer(s);
096: // skip first "line"
097: tok.nextToken();
098: // get line
099: _line = parseInteger(tok.nextToken());
100: // skip second "column"
101: tok.nextToken();
102: // get column
103: _column = parseInteger(tok.nextToken());
104: } else if (s.startsWith("Warning")) {
105: _error = false;
106: _msg = s;
107: } else if (s.startsWith("Error")) {
108: _error = true;
109: _msg = s;
110: } else {
111: // non structured msg
112: _msg += s;
113: }
114: }
115:
116: private int parseInteger(String integer) {
117: try {
118: return INTEGER_FORMAT.parse(integer).intValue();
119: } catch (ParseException e) {
120: throw new NumberFormatException(
121: "Unable to parse integer [int: " + integer
122: + ", error: " + e.getMessage());
123: }
124: }
125:
126: public void println() {
127: if (!_logged) {
128: log();
129: }
130: }
131:
132: public void println(boolean x) {
133: print(String.valueOf(x));
134: println();
135: }
136:
137: public void println(char c) {
138: print(String.valueOf(c));
139: println();
140: }
141:
142: public void println(char[] c) {
143: print(String.valueOf(c));
144: println();
145: }
146:
147: public void println(double d) {
148: print(String.valueOf(d));
149: println();
150: }
151:
152: public void println(float f) {
153: print(String.valueOf(f));
154: println();
155: }
156:
157: public void println(int i) {
158: print(String.valueOf(i));
159: println();
160: }
161:
162: public void println(long l) {
163: print(String.valueOf(l));
164: println();
165: }
166:
167: public void println(Object o) {
168: print(o.toString());
169: println();
170: }
171:
172: public void println(String s) {
173: print(s);
174: println();
175: }
176:
177: //----------------------------------------------- private members ------------------------------------------------------
178:
179: private int _line = -1;
180: private int _column = -1;
181: private String _msg = "";
182: private boolean _error = false;
183: private boolean _logged = false;
184: private URL _url;
185:
186: /**
187: * reports the warning or error and then resets the current error/warning.
188: **/
189: private void log() {
190: //System.out.println("Logging.........................");
191: if (_error) {
192: reportError(_msg, _line, _column);
193: } else {
194: reportWarning(_msg, _line, _column);
195: }
196: _logged = true;
197: _line = -1;
198: _column = -1;
199: _msg = "";
200: }
201:
202: private void reportError(String msg, int line, int column) {
203: Enumeration listeners = HTMLParserFactory
204: .getHTMLParserListeners().elements();
205: while (listeners.hasMoreElements()) {
206: ((HTMLParserListener) listeners.nextElement()).error(_url,
207: msg, line, column);
208: }
209: }
210:
211: private void reportWarning(String msg, int line, int column) {
212: Enumeration listeners = HTMLParserFactory
213: .getHTMLParserListeners().elements();
214: while (listeners.hasMoreElements()) {
215: ((HTMLParserListener) listeners.nextElement()).warning(
216: _url, msg, line, column);
217: }
218: }
219: }
|