1 7 package com.jofti.parser; 8 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 13 import com.jofti.exception.JoftiException; 14 import com.jofti.introspect.ClassIntrospector; 15 import com.jofti.introspect.ClassUtils; 16 import com.jofti.util.ReflectionUtil; 17 18 24 public class AttributeValueParser { 25 26 29 30 31 32 private ClassUtils utils = new ClassUtils(); 33 34 public static final String VALUE ="VALUE"; 36 37 public AttributeValueParser() { 38 39 } 40 41 42 43 public Comparable [] constructArrayValue(Object unParsedValues, ClassIntrospector introspector, Class name, String attribute, Map parameters) throws JoftiException 44 { 45 46 boolean fromObject=false; 48 Collection values =null; 49 50 if (unParsedValues.getClass() == String .class ){ 51 if (isParam((String )unParsedValues)){ 52 53 Object obj = constructFromParameters((String ) unParsedValues, parameters); 54 55 if (obj == null){ 56 57 throw new JoftiException("Value NULL is not valid for use with IN operator "+ parameters); 58 } 59 60 if (! (obj instanceof Collection )){ 61 throw new JoftiException ("Parameter "+ unParsedValues + " must be an instance of a Collection"); 62 } 63 values = (Collection )obj; 64 65 fromObject =true; 66 } 67 }else{ 68 values = (Collection )unParsedValues; 69 } 70 Comparable [] returnValues =null; 72 try { 73 if (values != null && values.size() != 0){ 74 Class clazz =introspector.getClassForAttribute(name, attribute); 75 int size = values.size(); 76 returnValues = new Comparable [size]; 77 if (clazz.isArray()){ 78 clazz = clazz.getComponentType(); 79 } 80 Iterator it = values.iterator(); 81 for (int i=0;i<size;i++){ 82 Object obj = it.next(); 83 if (fromObject){ 84 if (checkType(clazz, obj)){ 85 returnValues[i]= (Comparable )obj; 86 }else{ 87 throw new JoftiException("Type for parameter "+ obj + " does not match required type " + clazz + " in parameter " + unParsedValues); 88 } 89 }else{ 90 returnValues[i]= changeValueType(clazz, (String )obj); 91 92 } 93 } 94 95 }else{ 96 returnValues = new Comparable [1]; 98 } 99 100 } catch (Exception e){ 101 throw new JoftiException(e); 102 } 103 return returnValues; 104 } 105 106 public Comparable changeValueType(Class className, String value ) throws JoftiException{ 107 108 109 return (Comparable ) ReflectionUtil.constructObjectValue(className,value); 110 } 111 112 public Comparable constructForIsNot(String unParsedValue, Map namedParameters) throws JoftiException 113 { 114 Object value = (Comparable )constructFromParameters(unParsedValue, namedParameters); 116 try { 118 if (value == null ||( "null".equalsIgnoreCase((String )value))){ 119 return null; 120 } 121 } catch (ClassCastException e){ 122 } 124 throw new JoftiException("only NULL can be used with operator IS or NOT"); 125 126 127 } 128 public Comparable constructValue(String unParsedValue, ClassIntrospector introspector, Class name, String attribute, Map namedParameters) throws JoftiException 129 { 130 Object value =null; 132 Class clazz =null; 133 try { 134 if (unParsedValue != null){ 135 136 138 if (isParam(unParsedValue) ){ 139 value = constructFromParameters(unParsedValue, namedParameters); 140 if (value ==null){ 141 throw new JoftiException("Value null is only allowed with IS or NOT operators "+ namedParameters); 142 } 143 clazz =introspector.getClassForAttribute(name, attribute); 144 if (!checkType(clazz, value)){ 145 throw new JoftiException("Parameter type "+ value.getClass() + " is not valid for parameter '"+ unParsedValue + "' requires assignable type of "+ clazz); 146 } 147 148 }else { 149 clazz =introspector.getClassForAttribute(name, attribute); 151 if (clazz.isArray()){ 152 value = changeValueType(clazz.getComponentType(), unParsedValue); 153 }else{ 154 value = changeValueType(clazz, unParsedValue); 155 } 156 } 157 158 159 } 160 } catch (Exception e){ 161 throw new JoftiException(e); 162 } 163 return (Comparable )value; 164 } 165 166 public Object constructFromParameters(String unParsedValue, Map parameters) throws JoftiException{ 167 168 Object value =null; 169 char tempChar = unParsedValue.charAt(0); 171 172 if (tempChar== ':' || tempChar == '?' ){ 174 value = parameters.get(unParsedValue.substring(1)); 175 if (value == null){ 176 if (parameters.containsKey(unParsedValue.substring(1))){ 177 return null; 178 } 179 throw new JoftiException("Parameter "+ unParsedValue + " is not in parameter set "+ parameters); 180 }else{ 181 183 value =utils.wrapObject(value); 184 } 185 } 186 189 return value; 190 } 191 192 protected boolean isParam(String value){ 193 if (value ==null) 194 { 195 return false; 196 }else { 197 char tempChar = value.charAt(0); 198 if (tempChar== ':' || tempChar == '?' ){ 199 return true; 200 }else{ 201 return false; 202 } 203 } 204 } 205 206 protected boolean checkType(Class clazz, Object value){ 207 return clazz.isAssignableFrom(value.getClass()) && Comparable .class.isAssignableFrom(clazz); 208 } 209 210 211 public Comparable [] processVals(String val, Map namedParameters) throws JoftiException{ 212 String value = (String )constructFromParameters(val, namedParameters); 213 214 if (value == null){ 215 value = val; 216 } 217 Comparable [] comp = new Comparable [2]; 218 comp[0] = value.substring(0,value.length()-1); 219 comp[1] = ((String )comp[0]) + Character.MAX_VALUE; 220 return comp; 221 222 } 223 } 224 | Popular Tags |