KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > validators > RegexpValidator


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
28  * Perform regular expression matches on the body of
29  * the HTTP response. Setting the "condition" attribute
30  * of the test indicates whether or not a match
31  * is expected. If the server returns a <i>null</i>
32  * (e.g. in the case of an empty HEAD response), the
33  * validator will always fail, regardless of the setting
34  * of the "condition" attribute.
35  *
36  * @author Morgan Delagrange
37  * @author dIon Gillard
38  * @version $Id: RegexpValidator.java 155424 2005-02-26 13:09:29Z dirkv $
39  */

40 public class RegexpValidator extends BaseConditionalValidator implements Validator {
41
42     // --------------------------------------------------------------- Attributes
43

44     protected String JavaDoc _pattern = null;
45     protected boolean _ignoreCase = false;
46
47     protected static final String JavaDoc BARE_EXCEPTION_MESSAGE = " TO MATCH PATTERN: ";
48
49     // ------------------------------------------------------------- Constructors
50

51     public RegexpValidator() {
52         this(null,null,true,false);
53     }
54
55     public RegexpValidator(String JavaDoc label) {
56         this(label,null,true,false);
57     }
58
59     public RegexpValidator(String JavaDoc label, String JavaDoc pattern, boolean cond, boolean ignoreCase) {
60         super(label, cond);
61         _pattern = pattern;
62         _ignoreCase = ignoreCase;
63     }
64
65     // ------------------------------------------------------------------ Methods
66

67     public void setPattern(String JavaDoc 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 JavaDoc 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); // Compile expression
86
} 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()); // Match against expression
96

97         return matched;
98
99     }
100
101     public String JavaDoc generateBareExceptionMessage() {
102         return BARE_EXCEPTION_MESSAGE + _pattern;
103     }
104
105 }
106
Popular Tags