KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > Rule


1 package org.sapia.validator;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Locale JavaDoc;
6 import org.sapia.util.xml.confix.ConfigurationException;
7 import org.sapia.util.xml.confix.ObjectCreationCallback;
8
9 /**
10  * This class models a validation rule.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public abstract class Rule implements Validatable, ObjectCreationCallback{
20   private String JavaDoc _id;
21   private ErrorMessages _msgs = new ErrorMessages();
22   private List JavaDoc _tmp = new ArrayList JavaDoc();
23   private String JavaDoc _prefix, _localName;
24   
25   /**
26    * Constructor for Rule.
27    */

28   public Rule() {
29   }
30
31   /**
32    * Inits rule with the specified information.
33    *
34    * @param this rule's namespace prefix.
35    * @param this rule's local name.
36    */

37   public void initName(String JavaDoc prefix, String JavaDoc localName){
38     _prefix = prefix;
39     _localName = localName;
40   }
41   
42   /**
43    * @see #getId()
44    */

45   public void setId(String JavaDoc id) {
46     _id = id;
47   }
48
49   /**
50    * @see Validatable#getId()
51    */

52   public String JavaDoc getId() {
53     return _id;
54   }
55   
56   /**
57    * Adds an error message to this instance and returns it.
58    *
59    * @return the created <code>ErrorMessage</code>.
60    */

61   public ErrorMessage createMessage() {
62     ErrorMessage msg = new ErrorMessage();
63     _tmp.add(msg);
64     return msg;
65   }
66
67   /**
68    * Sets this instance's error messages.
69    *
70    * @param msgs some <code>ErrorMessages</code>.
71    */

72   public void setErrorMessages(ErrorMessages msgs){
73     _msgs = msgs;
74   }
75   
76   /**
77    * @return this instance's <code>ErrorMessages</code>.
78    */

79   public ErrorMessages getErrorMessages(){
80     return _msgs;
81   }
82
83   /**
84    * Returns the error message corresponding to the
85    * given <code>Locale</code>.
86    *
87    * @param a <code>Locale</code>.
88    * @return an error message, or <code>null</code> if no error message could
89    * be found for the given <code>Locale</code>.
90    */

91   public String JavaDoc getErrorMessageFor(Locale JavaDoc loc) {
92     ErrorMessage msg = _msgs.getErrorMessageFor(loc);
93
94     if (msg != null) {
95       return msg.getValue();
96     } else {
97       return null;
98     }
99   }
100
101   /**
102    * @see Validatable#validate(ValidationContext)
103    */

104   public abstract void validate(ValidationContext context);
105   
106   /**
107    * Returns the local name of this rule.
108    *
109    * @return this instance's local name.
110    */

111   protected String JavaDoc localName(){
112     return _localName;
113   }
114   
115   /**
116    * Returns the prefix of the namespace to which this rule belongs.
117    *
118    * @return this instance's namespace prefix.
119    */

120   protected String JavaDoc prefix(){
121     return _prefix;
122   }
123   
124   /**
125    * Returns this rule's qualified name (prefix() + ':' + localName()).
126    *
127    * @see #prefix()
128    * @see #localName()
129    *
130    * @return the qualified name of this rule.
131    */

132   protected String JavaDoc qualifiedName(){
133     if(_prefix != null && _localName != null){
134       return _prefix + ':' + _localName;
135     }
136     else{
137       return getClass().getName();
138     }
139   }
140   
141   public Object JavaDoc onCreate() throws ConfigurationException{
142     for(int i = 0; i < _tmp.size(); i++){
143       _msgs.addErrorMessage((ErrorMessage)_tmp.get(i));
144     }
145     _tmp = null;
146     return this;
147   }
148 }
149
Popular Tags