1 16 17 package org.apache.commons.latka.validators; 18 19 import org.apache.commons.latka.Validator; 20 import org.apache.commons.latka.ValidationException; 21 22 import org.apache.commons.latka.http.Response; 23 24 import org.apache.regexp.RE; 25 import org.apache.regexp.RESyntaxException; 26 27 40 public class RegexpValidator extends BaseConditionalValidator implements Validator { 41 42 44 protected String _pattern = null; 45 protected boolean _ignoreCase = false; 46 47 protected static final String BARE_EXCEPTION_MESSAGE = " TO MATCH PATTERN: "; 48 49 51 public RegexpValidator() { 52 this(null,null,true,false); 53 } 54 55 public RegexpValidator(String label) { 56 this(label,null,true,false); 57 } 58 59 public RegexpValidator(String label, String pattern, boolean cond, boolean ignoreCase) { 60 super(label, cond); 61 _pattern = pattern; 62 _ignoreCase = ignoreCase; 63 } 64 65 67 public void setPattern(String pattern) { 68 _pattern = pattern; 69 } 70 71 public void setIgnoreCase(boolean ignoreCase) { 72 _ignoreCase = ignoreCase; 73 } 74 75 public boolean assertTrue(Response response) 76 throws ValidationException { 77 78 String responseBody = response.getResource(); 79 if (responseBody == null) { 80 fail("HTTP RESPONSE BODY WAS NULL"); 81 } 82 83 RE r = null; 84 try { 85 r = new RE(_pattern); } catch (RESyntaxException e) { 87 fail(e.toString()); 88 } 89 90 if (_ignoreCase == true) { 91 r.setMatchFlags(RE.MATCH_CASEINDEPENDENT); 92 } 93 94 boolean matched = 95 r.match(response.getResource()); 97 return matched; 98 99 } 100 101 public String generateBareExceptionMessage() { 102 return BARE_EXCEPTION_MESSAGE + _pattern; 103 } 104 105 } 106 | Popular Tags |