1 23 24 package org.enhydra.xml.xmlc.html.parsers.tidy; 25 26 import gnu.regexp.RE; 27 import gnu.regexp.REException; 28 import gnu.regexp.REMatch; 29 import gnu.regexp.RESyntax; 30 31 import java.io.PrintWriter ; 32 33 import org.enhydra.xml.io.ErrorReporter; 34 import org.enhydra.xml.xmlc.XMLCError; 35 import org.enhydra.xml.xmlc.misc.LineNumberMap; 36 37 38 45 class TidyErrorHandler extends PrintWriter { 46 49 private ErrorReporter fReporter; 50 51 54 private String fFileName; 55 56 60 private LineNumberMap fLineNumberMap; 61 62 65 private StringBuffer fOutbuf = new StringBuffer (); 66 67 77 private static final String WARN_ERROR_REGEXP = 78 "^line ([0-9,]+) column [0-9,]+ - (Warning:|Error:|) (.*)$"; 79 80 83 private static final String WARN_MSG = "Warning:"; 84 85 88 private static final RE fWarnErrorRE; 89 90 93 private static final String [] DISCARD_REGEXPS = { 94 ".*Tidy \\(vers.*\\) Parsing.*", 95 ".*\\\".*\\\" appears to be.*", 96 ".*warnings/errors were found.*", 97 ".*warnings or errors were found.*", 98 ".*This document has errors that must be fixed before.*", 99 ".*Document content looks like.*" 100 }; 101 102 105 private static final RE[] fDiscardREs; 106 107 static { 108 try { 110 fWarnErrorRE = new RE(WARN_ERROR_REGEXP, 0, 111 RESyntax.RE_SYNTAX_POSIX_EXTENDED); 112 113 fDiscardREs = new RE[DISCARD_REGEXPS.length]; 114 for (int idx = 0; idx < DISCARD_REGEXPS.length; idx++) { 115 fDiscardREs[idx] = new RE(DISCARD_REGEXPS[idx], 116 RE.REG_MULTILINE, 117 RESyntax.RE_SYNTAX_POSIX_EXTENDED); 118 } 119 } catch (REException except) { 120 throw new XMLCError(except); 121 } 122 } 123 124 127 public TidyErrorHandler(ErrorReporter reporter, 128 String fileName, 129 LineNumberMap lineNumberMap) { 130 super(System.err); 132 fReporter = reporter; 133 fFileName = fileName; 134 fLineNumberMap = lineNumberMap; 135 } 136 137 141 private int convertTidyInt(String intStr) { 142 char[] strBuf = new char[intStr.length()]; 144 int cIdx = 0; 145 for (int sIdx = 0; sIdx < intStr.length(); sIdx++) { 146 char c = intStr.charAt(sIdx); 147 if (c != ',') { 148 strBuf[cIdx++] = c; 149 } 150 } 151 return Integer.valueOf(new String (strBuf, 0 ,cIdx)).intValue(); 152 } 153 154 157 private void processWarnError(REMatch match) { 158 String fileName = fFileName; 159 int lineNum = convertTidyInt(match.toString(1)); 160 if (fLineNumberMap != null) { 161 LineNumberMap.Line line = fLineNumberMap.getLineFromLineNum(lineNum); 163 fileName = line.getFileName(); 164 lineNum = line.getLineNum(); 165 } 166 167 String which = match.toString(2); 168 String msg = match.toString(3); 169 170 if (which.equals(WARN_MSG) || (which.length() == 0)) { 172 fReporter.warning(msg, fileName, lineNum); 173 } else { 174 fReporter.error(msg, fileName, lineNum); 175 } 176 } 177 178 181 private void processLine() { 182 REMatch match = fWarnErrorRE.getMatch(fOutbuf); 183 if (match != null) { 184 processWarnError(match); 185 } else { 186 int idx; 188 for (idx = 0; idx < fDiscardREs.length; idx++) { 189 if (fDiscardREs[idx].isMatch(fOutbuf)) { 190 break; 191 } 192 } 193 if (idx == fDiscardREs.length) { 194 fReporter.warning("XMLC did not detect this Tidy message: " 196 + fOutbuf); 197 } 198 } 199 200 fOutbuf.setLength(0); 201 } 202 203 206 public void flush() { 207 } 208 209 212 public void close() { 213 } 214 215 218 public boolean checkError() { 219 return false; 220 } 221 222 225 public void write(int c) { 226 fOutbuf.append(c); 227 } 228 229 232 public void write(char buf[], int off, int len) { 233 fOutbuf.append(buf, off, len); 234 } 235 236 239 public void write(String s, int off, int len) { 240 fOutbuf.append(s.substring(off, off+len)); 241 } 242 243 246 public void write(String s) { 247 fOutbuf.append(s); 248 } 249 250 253 public void println() { 254 processLine(); 255 } 256 257 260 public void println(boolean x) { 261 fOutbuf.append(x); 262 processLine(); 263 } 264 265 268 public void println(char x) { 269 fOutbuf.append(x); 270 processLine(); 271 } 272 273 276 public void println(int x) { 277 fOutbuf.append(x); 278 processLine(); 279 } 280 281 284 public void println(long x) { 285 fOutbuf.append(x); 286 processLine(); 287 } 288 289 292 public void println(float x) { 293 fOutbuf.append(x); 294 processLine(); 295 } 296 297 300 public void println(double x) { 301 fOutbuf.append(x); 302 processLine(); 303 } 304 305 308 public void println(char x[]) { 309 fOutbuf.append(x); 310 processLine(); 311 } 312 313 316 public void println(String x) { 317 fOutbuf.append(x); 318 processLine(); 319 } 320 321 324 public void println(Object x) { 325 fOutbuf.append(x); 326 processLine(); 327 } 328 } 329 | Popular Tags |