1 package com.tonbeller.tbutils.httpunit; 2 3 import java.net.URL ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import junit.framework.Assert; 8 9 import com.meterware.httpunit.parsing.HTMLParserFactory; 10 import com.meterware.httpunit.parsing.HTMLParserListener; 11 12 public class HtmlListener { 13 boolean checkWarnings = false; 14 boolean checkErrors = false; 15 16 List errors = new ArrayList (); 17 List warnings = new ArrayList (); 18 19 private static final HtmlListener instance = new HtmlListener(); 20 21 HTMLParserListener htmlParserListener = new HTMLParserListener() { 22 public void warning(URL url, String msg, int line, int column) { 23 if (checkWarnings) 24 warnings.add(makeMessage(url, msg, line, column)); 25 } 26 27 public void error(URL url, String msg, int line, int column) { 28 if (checkErrors) 29 errors.add(makeMessage(url, msg, line, column)); 30 } 31 32 private String makeMessage(URL url, String msg, int line, int column) { 33 return msg + " (" + url + ", line=" + line + ", column=" + column + ")"; 34 } 35 }; 36 37 private HtmlListener() { 38 HTMLParserFactory.setParserWarningsEnabled(true); 39 HTMLParserFactory.addHTMLParserListener(htmlParserListener); 40 } 41 42 public static HtmlListener instance() { 43 return instance; 44 } 45 46 void reset() { 47 checkErrors = false; 48 errors.clear(); 49 checkWarnings = false; 50 warnings.clear(); 51 } 52 53 public void failOnError(String context) { 54 if (errors.size() != 0) { 55 String msg = "HTML Errors in " + context + ": " + errors; 56 Assert.fail(msg); 57 } 58 } 59 60 public boolean isCheckErrors() { 61 return checkErrors; 62 } 63 64 65 public void setCheckErrors(boolean checkErrors) { 66 this.checkErrors = checkErrors; 67 } 68 69 70 public boolean isCheckWarnings() { 71 return checkWarnings; 72 } 73 74 75 public void setCheckWarnings(boolean checkWarnings) { 76 this.checkWarnings = checkWarnings; 77 } 78 79 80 public List getErrors() { 81 return errors; 82 } 83 84 85 public List getWarnings() { 86 return warnings; 87 } 88 89 90 91 } 92 | Popular Tags |