View Javadoc

1   /*
2    * Copyright 2002-2004 the original author or authors.
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 net.sf.fridaymvc.metadata.commons;
18  
19  import java.util.regex.Pattern;
20  
21  import net.sf.fridaymvc.Parameters;
22  import net.sf.fridaymvc.RunData;
23  
24  /***
25   * @author <a href="mailto:arto.pastinen@ofw.fi">Arto Pastinen</a>
26   * @version $Id: RegExpValidatorAttribute.java,v 1.1 2004/11/23 20:35:58 artsi Exp $
27   */
28  
29  public class RegExpValidatorAttribute {
30      private String id;
31      private String fieldName;
32      private String regexp;
33      
34      public RegExpValidatorAttribute(String id, String fieldName, String regexp) {
35          setId(id);
36          setFieldName(fieldName);
37          setRegexp(regexp);
38      }
39  
40      /***
41       * @return Returns the fieldName.
42       */
43      public String getFieldName() {
44          return this.fieldName;
45      }
46      /***
47       * @param fieldName The fieldName to set.
48       */
49      public void setFieldName(String fieldName) {
50          this.fieldName = fieldName;
51      }
52      /***
53       * @return Returns the regexp.
54       */
55      public String getRegexp() {
56          return this.regexp;
57      }
58      /***
59       * @param regexp The regexp to set.
60       */
61      public void setRegexp(String regexp) {
62          this.regexp = regexp;
63      }
64      /***
65       * @return Returns the id.
66       */
67      public String getId() {
68          return this.id;
69      }
70      /***
71       * @param id The id to set.
72       */
73      public void setId(String regexpId) {
74          this.id = regexpId;
75      }
76      
77      public boolean passValidation() {
78          Parameters p = RunData.getInstance().getParameters();
79          String tmp = p.getPrefix();
80          p.setPrefix("action.");        
81          try {            
82              return Pattern.matches(getRegexp(), p.get(getFieldName()));
83          }
84          catch(Throwable t) {
85              // ignore
86              return false;
87          }
88          finally {
89              p.setPrefix(tmp);
90          }
91      }
92      
93      /* (non-Javadoc)
94       * @see java.lang.Object#equals(java.lang.Object)
95       */
96      public boolean equals(Object obj) {
97          if(obj instanceof RegExpValidatorAttribute) {
98              if(((RegExpValidatorAttribute) obj).getId().equals(getId())) return true;
99              return false;
100         }
101         return false;
102     }
103     
104     /* (non-Javadoc)
105      * @see java.lang.Object#hashCode()
106      */
107     public int hashCode() {
108         return getId().hashCode();
109     }
110 }