KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > validation > RuntimeValidator


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

18 package net.sf.uitags.tagutil.validation;
19
20 import javax.servlet.jsp.PageContext JavaDoc;
21
22 import net.sf.uitags.tagutil.ScopedIdGenerator;
23 import net.sf.uitags.util.UiString;
24
25 /**
26  * Class that provides tag validations at runtime.
27  *
28  * @author hgani
29  * @version $Id$
30  */

31 public final class RuntimeValidator {
32   /**
33    * Utility classes should not have a public or default constructor.
34    */

35   private RuntimeValidator() {
36     // hide the constructor
37
}
38
39   /**
40    * Makes sure a tag only appears once in a page.
41    *
42    * @param tagId identifier of the tag
43    * @param pageContext page context of the tag
44    * @param tagName name of the tag
45    * @throws RuntimeValidationException if the assertion failed
46    */

47   public static void assertSingleUse(
48       String JavaDoc tagId, String JavaDoc tagName, PageContext JavaDoc pageContext) {
49     long instanceId = ScopedIdGenerator.nextId(
50         PageContext.REQUEST_SCOPE, tagId, pageContext);
51     if (instanceId > 1) {
52       throw new RuntimeValidationException(UiString.simpleConstruct(
53           "'{0}' tag can only be used at most once in a request.",
54           new String JavaDoc[] { tagName }));
55     }
56   }
57
58   /**
59    * Ensures that at most only one of the two attributes are specified.
60    *
61    * @param name1 the first attribute name
62    * @param value1 the first attribute value
63    * @param name2 the second attribute name
64    * @param value2 the second attribute value
65    * @throws DeferredValidationException if the assertion failed
66    */

67   public static void assertAttributeExclusive(
68       String JavaDoc name1, String JavaDoc value1, String JavaDoc name2, String JavaDoc value2) {
69     if ((value1 != null && value2 != null) &&
70         (!value1.equals("") && !value2.equals(""))) {
71       throw new DeferredValidationException("The attribute '" + name1 +
72           "' and '" + name2 + "' are exclusive.");
73     }
74   }
75
76   /**
77    * Ensures that at least one of the two attributes are specified.
78    *
79    * @param name1 the first attribute name
80    * @param value1 the first attribute value
81    * @param name2 the second attribute name
82    * @param value2 the second attribute value
83    * @throws DeferredValidationException if the assertion failed
84    */

85   public static void assertEitherSpecified(
86       String JavaDoc name1, String JavaDoc value1, String JavaDoc name2, String JavaDoc value2) {
87     if ((isStringEmpty(value1) && isStringEmpty(value2))) {
88       throw new DeferredValidationException("One of the attributes '" + name1 +
89           "' and '" + name2 + "' must be specified.");
90     }
91   }
92
93   /**
94    * Checks if a string is empty -- either <code>null</code> or equals to
95    * <code>""</code>.
96    *
97    * @param value the string
98    * @return <code>true</code> if it is empty, <code>false</code> otherwise
99    */

100   private static boolean isStringEmpty(String JavaDoc value) {
101     return value == null || value.equals("");
102   }
103 }
104
Popular Tags