1 package org.lsst.ccs.config;
2
3 import groovy.util.Eval;
4 import org.apache.commons.beanutils.MethodUtils;
5 import org.lsst.gruth.types.IncompatibleTypeException;
6 import org.lsst.gruth.jutils.SStructParm;
7 import org.lsst.gruth.types.GArray;
8 import org.lsst.gruth.types.GStruct;
9
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.List;
12 import java.util.Map;
13
14
15
16
17
18
19
20 public class Constraints {
21
22
23
24
25 public static interface Controler {
26
27
28
29
30
31 public Object control(String value);
32 }
33
34 public static Object check (String type, String value , String constraints) {
35 Object realValue = null ;
36 type = type.trim() ;
37
38 value = value.trim() ;
39 if(constraints != null) {
40 constraints = constraints.trim() ;
41 if(! "".equals(constraints)) {
42 return checkConstraints(type, value, constraints) ;
43 }
44 }
45 Class thatClass = null;
46 try {
47 thatClass = Class.forName(type);
48 if(thatClass.isArray()) {
49 realValue = GArray.valueOf(value, thatClass.getName()) ;
50 } else if (null!= SStructParm.structClassUsesList(thatClass)) {
51 realValue = GStruct.valueOf(thatClass, value) ;
52 } else {
53 realValue = MethodUtils.invokeStaticMethod(thatClass, "valueOf", value);
54 }
55 } catch (ClassNotFoundException e) {
56 throw new IllegalArgumentException(e);
57 } catch (NoSuchMethodException e) {
58 if( Map.class.isAssignableFrom(thatClass)||
59 List.class.isAssignableFrom(thatClass)){
60 try {
61 realValue = Eval.me(value) ;
62 } catch (Exception exc) {
63 throw new IllegalArgumentException(exc);
64 }
65 Class realClass = realValue.getClass();
66 if(! thatClass.isAssignableFrom(realClass)) {
67 throw new IncompatibleTypeException(thatClass, realClass);
68 }
69 } else {
70 throw new IllegalArgumentException(e);
71 }
72 } catch (IllegalAccessException e) {
73 throw new IllegalArgumentException(e);
74 } catch (InvocationTargetException e) {
75 throw new IllegalArgumentException(e);
76 } catch (Exception exc) {
77 return null ;
78 }
79 return realValue ;
80
81 }
82
83 public static Object checkConstraints(String type, String value, String constraints) {
84
85
86
87
88 String[] split = constraints.split("\\.\\.");
89 if (split.length == 2) {
90 return checkRange(type, value, split[0], split[1]);
91 }
92
93 try {
94
95 Controler controler = (Controler) Class.forName(constraints).newInstance();
96 return checkWithControler(value, controler);
97 } catch (Exception exc) {
98 throw new IllegalArgumentException("constraint :" + constraints + " " + exc);
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111 public static Object checkRange(String type, String value, String lower, String upper) {
112
113 Class thatClass = null;
114 try {
115
116 if("java.lang.Character".equals(type)) {
117 type = "java.lang.String" ;
118 }
119 thatClass = Class.forName(type);
120 } catch (Exception exc) {
121 throw new IllegalArgumentException(exc);
122 }
123 return checkRange(thatClass, value, lower, upper) ;
124
125 }
126
127 public static Object checkRange(Class thatClass, String value, String lower, String upper) {
128 Object realValue = null ;
129 try {
130 realValue = MethodUtils.invokeStaticMethod(thatClass, "valueOf", value);
131 Object realUpper = MethodUtils.invokeStaticMethod(thatClass, "valueOf", upper);
132 Object realLower = MethodUtils.invokeStaticMethod(thatClass, "valueOf", lower);
133 Comparable compValue = (Comparable) realValue ;
134 if((compValue.compareTo(realLower) >= 0) && (compValue.compareTo(realUpper) <= 0)) {
135 return realValue ;
136 } else {
137 throw new IllegalArgumentException(realValue + "not in range [" +realLower + ".." + realUpper+"]") ;
138 }
139 } catch (Exception exc) {
140 throw new IllegalArgumentException("range " + lower + ".." + upper +
141 " and value :" + value + " raises :" + exc);
142 }
143
144 }
145
146 public static Object checkWithControler(String value, Controler controler) {
147 return controler.control(value);
148 }
149 }