KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > parser > AttributeValueParser


1 /*
2  * Created on 31-Jan-2006
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.jofti.parser;
8
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
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 /**
19  * @author xenephon
20  *
21  * TODO To change the template for this generated type comment go to
22  * Window - Preferences - Java - Code Style - Code Templates
23  */

24 public class AttributeValueParser {
25
26     /**
27      *
28      */

29     
30     
31     
32     private ClassUtils utils = new ClassUtils();
33     
34     //special value for primitive types
35
public static final String JavaDoc VALUE ="VALUE";
36     
37     public AttributeValueParser() {
38         
39     }
40
41     
42     
43     public Comparable JavaDoc[] constructArrayValue(Object JavaDoc unParsedValues, ClassIntrospector introspector, Class JavaDoc name, String JavaDoc attribute, Map JavaDoc parameters) throws JoftiException
44     {
45         
46         // we can either have a string that is a key to a named attribute or a list
47
boolean fromObject=false;
48         Collection JavaDoc values =null;
49         
50         if (unParsedValues.getClass() == String JavaDoc.class ){
51             if (isParam((String JavaDoc)unParsedValues)){
52                 
53                 Object JavaDoc obj = constructFromParameters((String JavaDoc) 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 JavaDoc)){
61                     throw new JoftiException ("Parameter "+ unParsedValues + " must be an instance of a Collection");
62                 }
63                 values = (Collection JavaDoc)obj;
64                 
65                 fromObject =true;
66             }
67         }else{
68             values = (Collection JavaDoc)unParsedValues;
69         }
70         // construct the value
71
Comparable JavaDoc[] returnValues =null;
72         try {
73             if (values != null && values.size() != 0){
74                 Class JavaDoc clazz =introspector.getClassForAttribute(name, attribute);
75                 int size = values.size();
76                 returnValues = new Comparable JavaDoc[size];
77                 if (clazz.isArray()){
78                     clazz = clazz.getComponentType();
79                 }
80                 Iterator JavaDoc it = values.iterator();
81                 for (int i=0;i<size;i++){
82                     Object JavaDoc obj = it.next();
83                         if (fromObject){
84                             if (checkType(clazz, obj)){
85                                 returnValues[i]= (Comparable JavaDoc)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 JavaDoc)obj);
91                             
92                         }
93                 }
94                 
95             }else{
96                 // return an empyt array if values is empty
97
returnValues = new Comparable JavaDoc[1];
98             }
99             
100         } catch (Exception JavaDoc e){
101             throw new JoftiException(e);
102         }
103         return returnValues;
104     }
105     
106     public Comparable JavaDoc changeValueType(Class JavaDoc className, String JavaDoc value ) throws JoftiException{
107         
108             
109             return (Comparable JavaDoc) ReflectionUtil.constructObjectValue(className,value);
110         }
111     
112      public Comparable JavaDoc constructForIsNot(String JavaDoc unParsedValue, Map JavaDoc namedParameters) throws JoftiException
113      {
114         //this only allows nulls
115
Object JavaDoc value = (Comparable JavaDoc)constructFromParameters(unParsedValue, namedParameters);
116             // check value is null
117
try {
118                 if (value == null ||( "null".equalsIgnoreCase((String JavaDoc)value))){
119                     return null;
120                 }
121             } catch (ClassCastException JavaDoc e){
122                 //we can ignore this here
123
}
124             throw new JoftiException("only NULL can be used with operator IS or NOT");
125             
126      
127      }
128  public Comparable JavaDoc constructValue(String JavaDoc unParsedValue, ClassIntrospector introspector, Class JavaDoc name, String JavaDoc attribute, Map JavaDoc namedParameters) throws JoftiException
129  {
130      // construct the value
131
Object JavaDoc value =null;
132         Class JavaDoc clazz =null;
133         try {
134          if (unParsedValue != null){
135             
136             // see if it is a named or positional query first
137

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                     // string attribute check from unparsed value
150
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 JavaDoc e){
161             throw new JoftiException(e);
162         }
163      return (Comparable JavaDoc)value;
164  }
165
166     public Object JavaDoc constructFromParameters(String JavaDoc unParsedValue, Map JavaDoc parameters) throws JoftiException{
167         
168         Object JavaDoc value =null;
169         // get the char at pos 0
170
char tempChar = unParsedValue.charAt(0);
171         
172         // see if starts with allowed value
173
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                 // check return type compatability here
182

183                 value =utils.wrapObject(value);
184             }
185         }
186         //return null if we do not have matching prefix
187
// or value from map (which could also be null)
188

189         return value;
190     }
191     
192     protected boolean isParam(String JavaDoc 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 JavaDoc clazz, Object JavaDoc value){
207         return clazz.isAssignableFrom(value.getClass()) && Comparable JavaDoc.class.isAssignableFrom(clazz);
208     }
209     
210     
211        public Comparable JavaDoc[] processVals(String JavaDoc val, Map JavaDoc namedParameters) throws JoftiException{
212         String JavaDoc value = (String JavaDoc)constructFromParameters(val, namedParameters);
213        
214         if (value == null){
215             value = val;
216         }
217         Comparable JavaDoc[] comp = new Comparable JavaDoc[2];
218         comp[0] = value.substring(0,value.length()-1);
219         comp[1] = ((String JavaDoc)comp[0]) + Character.MAX_VALUE;
220         return comp;
221         
222     }
223 }
224
Popular Tags