KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > util > Variable


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.util;
31
32 import java.lang.reflect.Array JavaDoc;
33 import java.math.BigDecimal JavaDoc;
34 import java.text.ParseException JavaDoc;
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.Date JavaDoc;
37
38 import com.genimen.djeneric.language.Messages;
39 import com.genimen.djeneric.repository.DjList;
40 import com.genimen.djeneric.repository.DjObject;
41 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
42 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
43
44 public class Variable
45 {
46   SimpleDateFormat JavaDoc _sdf = new SimpleDateFormat JavaDoc("dd-MM-yyyy");
47
48   Object JavaDoc _obj;
49   String JavaDoc _name;
50   String JavaDoc _typeName = String JavaDoc.class.getName();
51   boolean _isList = false;
52
53   public Variable(String JavaDoc name, Object JavaDoc obj, String JavaDoc typeName)
54   {
55     _name = name;
56     setObject(obj);
57     _typeName = typeName;
58   }
59
60   public Variable(String JavaDoc name, Object JavaDoc obj)
61   {
62     _name = name;
63     setObject(obj);
64   }
65
66   public String JavaDoc toString()
67   {
68     return getName() + "=" + getObject();
69   }
70
71   public String JavaDoc getName()
72   {
73     return _name;
74   }
75
76   public Object JavaDoc getObject()
77   {
78     return _obj;
79   }
80
81   public Object JavaDoc getObject(int idx) throws DjScriptExecutionException
82   {
83     assertList();
84
85     if (_obj instanceof DjList) return ((DjList) _obj).get(idx);
86
87     return Array.get(_obj, idx);
88   }
89
90   public void setObject(Object JavaDoc obj)
91   {
92     if (obj != null) _typeName = obj.getClass().getName();
93     _obj = obj;
94     _isList = obj instanceof DjList;
95     if (_isList)
96     {
97       _typeName = ((DjList) _obj).getStoredType().getName();
98     }
99
100   }
101
102   public void assign(int idx, Object JavaDoc value, SimpleNode context) throws DjScriptExecutionException
103   {
104     assertList();
105
106     if (_obj instanceof DjList) ((DjList) _obj).set(idx, coerse(value, context));
107     else Array.set(_obj, idx, value);
108   }
109
110   private void assertList() throws DjScriptExecutionException
111   {
112     boolean l = _obj != null && (_obj instanceof DjList || _obj.getClass().isArray());
113     if (!l) throw new DjScriptExecutionException(Messages.getString("Variable.NotListOrArray", getName()));
114   }
115
116   public void assign(Object JavaDoc value, SimpleNode context) throws DjScriptExecutionException
117   {
118     _obj = coerse(value, context);
119   }
120
121   private Object JavaDoc coerse(Object JavaDoc value, SimpleNode context) throws DjScriptExecutionException
122   {
123     Object JavaDoc result = null;
124
125     if (value == null)
126     {
127       result = value;
128     }
129     else if (_typeName.equals(value.getClass().getName()))
130     {
131       result = value;
132     }
133     else if (_typeName.equals(Long JavaDoc.class.getName()))
134     {
135       result = new Long JavaDoc(value.toString());
136     }
137     else if (_typeName.equals(BigDecimal JavaDoc.class.getName()))
138     {
139       result = new BigDecimal JavaDoc(value.toString());
140     }
141     else if (_typeName.equals(Boolean JavaDoc.class.getName()))
142     {
143       result = new Boolean JavaDoc(value.toString());
144     }
145     else if (_typeName.equals(String JavaDoc.class.getName()))
146     {
147       result = value.toString();
148     }
149     else if (_typeName.equals(Date JavaDoc.class.getName()))
150     {
151       if (value instanceof Long JavaDoc)
152       {
153         result = new Date JavaDoc(((Long JavaDoc) value).longValue());
154       }
155       else
156       {
157         try
158         {
159           result = _sdf.parse(value.toString());
160         }
161         catch (ParseException JavaDoc e)
162         {
163           throw new DjScriptExecutionException(e.getMessage(), context);
164         }
165       }
166     }
167     else if (value instanceof DjObject)
168     {
169       DjObject v = (DjObject) value;
170       try
171       {
172         if (v.isInstanceOf(_typeName)) result = value;
173         else throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", v.getExtent()
174             .getObjectType(), _typeName), context);
175       }
176       catch (ObjectNotDefinedException e)
177       {
178         throw new DjScriptExecutionException(Messages.getString("Variable.UndefinedType", _typeName), context);
179       }
180     }
181     else if (value instanceof DjList)
182     {
183       DjList lst = (DjList) value;
184       if (_isList)
185       {
186         if (lst.getStoredType() != null && lst.getStoredType().isInstanceof(_typeName)) result = value;
187         else
188         {
189           String JavaDoc typeName = "[untyped]";
190           if (lst.getStoredType() != null) typeName = lst.getStoredType().getObjectType();
191
192           throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", typeName, _typeName),
193               context);
194         }
195       }
196       else
197       {
198         if (lst.size() == 0) result = null;
199         else if (lst.size() == 1) result = lst.getDjenericObjectAt(0);
200         else throw new DjScriptExecutionException(Messages.getString("Variable.NotOneElement", getName()), context);
201       }
202     }
203     else
204     {
205       String JavaDoc val = "null";
206       if (value != null) val = value.getClass().getName();
207
208       throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", val, _typeName), context);
209     }
210
211     return result;
212
213   }
214
215   public String JavaDoc getTypeName()
216   {
217     return _typeName;
218   }
219
220   public boolean isList()
221   {
222     return _isList;
223   }
224 }
Popular Tags