KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > nodes > DeclarationNode


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.nodes;
31
32 import java.math.BigDecimal JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 import com.genimen.djeneric.language.Messages;
37 import com.genimen.djeneric.repository.DjExtent;
38 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
39 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine;
40 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
41 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope;
42 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
43 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope;
44 import com.genimen.djeneric.tools.scriptengine.core.util.Variable;
45
46 public class DeclarationNode extends SimpleNode
47 {
48
49   String JavaDoc _typeName;
50   String JavaDoc _variableName;
51
52   public DeclarationNode(int i)
53   {
54     super(i);
55   }
56
57   public DeclarationNode(DjScriptParserEngine p, int i)
58   {
59     super(p, i);
60   }
61
62   public String JavaDoc getName()
63   {
64     return _typeName + " " + _variableName;
65   }
66
67   public String JavaDoc toString()
68   {
69     return getName();
70   }
71
72   public String JavaDoc getTypeName()
73   {
74     return _typeName;
75   }
76
77   public String JavaDoc getVariableName()
78   {
79     return _variableName;
80   }
81
82   public void setTypeName(String JavaDoc string)
83   {
84     _typeName = string;
85   }
86
87   public void setVariableName(String JavaDoc string)
88   {
89     _variableName = string;
90   }
91
92   public void execute(DjScriptExecutionTimeScope ctxt) throws DjScriptExecutionException
93   {
94     ValueExpression valueExpression = null;
95     if (getChildCount() > 0) valueExpression = (ValueExpression) getChild(0);
96
97     Variable variable = createVariable(ctxt);
98
99     if (valueExpression != null) variable.assign(valueExpression.getValue(ctxt), this);
100
101     ctxt.pushVariable(variable);
102   }
103
104   private Variable createVariable(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
105   {
106     Variable variable;
107
108     if (getTypeName().equals("int") || getTypeName().equals("Integer") || getTypeName().equals("long")
109         || getTypeName().equals("Long"))
110     {
111       variable = new Variable(getVariableName(), new Long JavaDoc(0));
112     }
113     else if (getTypeName().equals("BigDecimal") || getTypeName().equals("double") || getTypeName().equals("float"))
114     {
115       variable = new Variable(getVariableName(), new BigDecimal JavaDoc(0));
116     }
117     else if (getTypeName().equals("String"))
118     {
119       variable = new Variable(getVariableName(), "");
120     }
121     else if (getTypeName().equals("Boolean"))
122     {
123       variable = new Variable(getVariableName(), new Boolean JavaDoc(false));
124     }
125     else if (getTypeName().equals("Date") || getTypeName().equals("Timestamp"))
126     {
127       variable = new Variable(getVariableName(), new Date JavaDoc());
128     }
129     else
130     //////////// Must be a type from the model then..
131
{
132       try
133       {
134         DjExtent extent = ctxt.getPersistenceManager().getExtentByObjectType(getTypeName());
135         variable = new Variable(getVariableName(), null, extent.getName());
136       }
137       catch (ObjectNotDefinedException e)
138       {
139         throw new DjScriptExecutionException("Type " + getTypeName() + " is not defined", this);
140       }
141     }
142     return variable;
143   }
144
145   public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
146   {
147     if (getTypeName().equals("int") || getTypeName().equals("Integer") || getTypeName().equals("long")
148         || getTypeName().equals("Long") || getTypeName().equals("BigDecimal") || getTypeName().equals("double")
149         || getTypeName().equals("float") || getTypeName().equals("String") || getTypeName().equals("Boolean")
150         || getTypeName().equals("Date") || getTypeName().equals("Timestamp")) return;
151     try
152     {
153       ctxt.getPersistenceManager().getExtentByObjectType(getTypeName());
154     }
155     catch (ObjectNotDefinedException e)
156     {
157       throw new DjScriptExecutionException(Messages.getString("DeclarationNode.Type", getTypeName()), this);
158     }
159     Variable variable = createVariable(ctxt);
160     ctxt.pushVariable(variable);
161   }
162
163   public void collectVariables(DjScriptCompileTimeScope ctxt, HashMap JavaDoc variables, int stopAtLine)
164       throws DjScriptExecutionException
165   {
166     if (stopAtLine >= 0 && getLine() > stopAtLine) return;
167
168     variables.put(_variableName, _typeName);
169   }
170
171 }
Popular Tags