1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 38 39 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 name, String 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 value, ValidationContext valCtx) { 71 super.validate(value, valCtx); 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 n) { 81 valCtx.result.failed(valCtx.smh.getLocalString(getClass().getName() + ".invalidInteger", 82 "Attribute({0}={1}) : {2} Invalid integer", new Object [] {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 [] {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 [] {valCtx.attrName, String.valueOf(tmp), String.valueOf(tmp), String.valueOf(lowRange), String.valueOf(highRange)}); 95 } 96 String compValue = getValueForAttribute((String )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 []{value, getRuleValue("le-than"), compValue}); 103 } 104 compValue = getValueForAttribute((String )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 []{value, getRuleValue("ge-than"), compValue}); 111 } 112 113 compValue = getValueForAttribute((String )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 []{value, getRuleValue("gt-than"), compValue}); 120 } 121 compValue = getValueForAttribute((String )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 []{value, getRuleValue("ls-than"), compValue}); 128 } 129 } 130 } 131 132 int compareIntWithStr(int iVal, String strVal) 133 { 134 return (iVal-Integer.parseInt(strVal)); 135 } 136 } 137 | Popular Tags |