KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > AttrInt


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 /**
27     Class which contains Meta data for all types of attributes which is present in Validation Descriptor
28  * XML File
29  *
30  * Sample
31  * <attribute name=<Name> type="address" />
32  * <attribute name=<Name> type="integer" range="low,high" />
33  * <attribute name=<Name> type="string" max-length="length" />
34     
35     @author Srinivas Krishnan
36     @version 2.0
37 */

38
39 /* Class for attribute type Integer */
40
41 public class AttrInt extends AttrType {
42     
43     public static final int IGNORE_LOW = -2147483648;
44     public static final int IGNORE_HIGH = 2147483638;
45     int highRange;
46     int lowRange;
47     
48     public AttrInt(String JavaDoc name, String JavaDoc type, boolean optional) {
49         super(name,type, optional);
50         this.highRange = IGNORE_HIGH;
51         this.lowRange = IGNORE_LOW;
52     }
53     
54     public int getHighRange() {
55         return highRange;
56     }
57     
58     public int getLowRange() {
59         return lowRange;
60     }
61     
62     public void setHighRange(int high) {
63         highRange = high;
64     }
65     
66     public void setLowRange(int low) {
67         lowRange = low;
68     }
69     
70     public void validate(Object JavaDoc value, ValidationContext valCtx) {
71         super.validate(value, valCtx); // call to common validator first
72
int tmp=0;
73         boolean success=true;
74         if(value == null || value.equals(""))
75             return;
76         if(valCtx.isDELETE())
77             return;
78         try {
79              tmp = Integer.parseInt(value.toString());
80         } catch(NumberFormatException JavaDoc n) {
81             valCtx.result.failed(valCtx.smh.getLocalString(getClass().getName() + ".invalidInteger",
82                 "Attribute({0}={1}) : {2} Invalid integer", new Object JavaDoc[] {valCtx.attrName, value, value}));
83             success=false;
84         }
85         if(success) {
86             if( (lowRange != IGNORE_LOW && tmp < lowRange) || (highRange != IGNORE_HIGH && tmp > highRange) ) {
87                 if(lowRange == 0 && highRange == IGNORE_HIGH) {
88                     valCtx.result.failed(valCtx.smh.getLocalString(getClass().getName() + ".invalidIntegerNegative",
89                     "Attribute({0}={1}) : {2} Invalid Value, Cannot be a negative number", new Object JavaDoc[] {valCtx.attrName, String.valueOf(tmp), String.valueOf(tmp)}));
90                     return;
91                 }
92                 reportAttributeError(valCtx, "invalidIntegerRange",
93                     "Attribute({0}={1}) : {2} Invalid Value, Valid Range {3},{4}",
94                     new Object JavaDoc[] {valCtx.attrName, String.valueOf(tmp), String.valueOf(tmp), String.valueOf(lowRange), String.valueOf(highRange)});
95             }
96             String JavaDoc compValue = getValueForAttribute((String JavaDoc)getRuleValue("le-than"), valCtx);
97             if(compValue!=null && !compValue.trim().equals("0"))
98             {
99                if(compareIntWithStr(tmp, compValue)>0)
100                    reportAttributeError(valCtx, "not-le-than",
101                            "Value ({0}) should be less or equal than to value of attribute {1} ({2})",
102                            new Object JavaDoc[]{value, getRuleValue("le-than"), compValue});
103             }
104             compValue = getValueForAttribute((String JavaDoc)getRuleValue("ge-than"), valCtx);
105             if(compValue!=null && !compValue.trim().equals("0"))
106             {
107                if(compareIntWithStr(tmp, compValue)<0)
108                    reportAttributeError(valCtx, "not-ge-then",
109                            "Value ({0}) should be more or equal to value of attribute {1} ({2})",
110                            new Object JavaDoc[]{value, getRuleValue("ge-than"), compValue});
111             }
112                 
113             compValue = getValueForAttribute((String JavaDoc)getRuleValue("gt-than"), valCtx);
114             if(compValue!=null && !compValue.trim().equals("0"))
115             {
116                if(compareIntWithStr(tmp, compValue)<=0)
117                    reportAttributeError(valCtx, "not-gt-then",
118                            "Value ({0}) should be more then value of attribute {1} ({2})",
119                            new Object JavaDoc[]{value, getRuleValue("gt-than"), compValue});
120             }
121             compValue = getValueForAttribute((String JavaDoc)getRuleValue("ls-than"), valCtx);
122             if(compValue!=null && !compValue.trim().equals("0"))
123             {
124                if(compareIntWithStr(tmp, compValue)>=0)
125                    reportAttributeError(valCtx, "not-ls-then",
126                            "Value ({0}) should be less than value of attribute {1} ({2})",
127                            new Object JavaDoc[]{value, getRuleValue("ls-than"), compValue});
128             }
129         }
130     }
131     
132     int compareIntWithStr(int iVal, String JavaDoc strVal)
133     {
134         return (iVal-Integer.parseInt(strVal));
135     }
136 }
137
Popular Tags