KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
33
34 import com.genimen.djeneric.language.Messages;
35 import com.genimen.djeneric.repository.DjExtent;
36 import com.genimen.djeneric.repository.DjList;
37 import com.genimen.djeneric.repository.DjObject;
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 SetNode extends SimpleNode
47 {
48   String JavaDoc _setName = "";
49
50   public SetNode(int i)
51   {
52     super(i);
53   }
54
55   public SetNode(DjScriptParserEngine p, int i)
56   {
57     super(p, i);
58   }
59
60   public String JavaDoc getName()
61   {
62     return "set";
63   }
64
65   public String JavaDoc toString()
66   {
67     return "";
68   }
69
70   public void execute(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
71   {
72     ValueExpression val = (ValueExpression) getChild(0);
73     Object JavaDoc o = val.getValue(context);
74     if (!(o instanceof DjList)) throw new DjScriptExecutionException(Messages.getString("SetNode.NotASet"), this);
75     DjList lst = (DjList) o;
76
77     FilterNode filter = null;
78     OrderByNode orderBy = null;
79
80     for (int i = 1; i < getChildCount(); i++)
81     {
82       if (getChild(i) instanceof FilterNode) filter = (FilterNode) getChild(i);
83       if (getChild(i) instanceof OrderByNode) orderBy = (OrderByNode) getChild(i);
84     }
85
86     lst = processFilter(lst, context, filter);
87     processOrderBy(lst, orderBy);
88
89     context.getVariableStack().push(getSetName(), lst);
90   }
91
92   public String JavaDoc getSetName()
93   {
94     return _setName;
95   }
96
97   public void setSetName(String JavaDoc n)
98   {
99     _setName = n;
100   }
101
102   public DjList processFilter(DjList lst, DjScriptExecutionTimeScope context, FilterNode filter)
103       throws DjScriptExecutionException
104   {
105     // Is it filtered?
106
if (filter != null)
107     {
108       DjList filteredSet = new DjList();
109       context.mark();
110       Variable co = new Variable(getSetName(), null);
111       context.pushVariable(co);
112       for (int i = 0; i < lst.size(); i++)
113       {
114         co.setObject(lst.getDjenericObjectAt(i));
115         if (filter.isTrue(context)) filteredSet.add(lst.getDjenericObjectAt(i));
116       }
117       lst = filteredSet;
118
119       context.releaseToMark();
120     }
121     return lst;
122   }
123
124   public void processOrderBy(DjList lst, OrderByNode orderBy) throws DjScriptExecutionException
125   {
126     if (orderBy != null)
127     {
128       try
129       {
130         String JavaDoc[] props = orderBy.getProperties();
131         int[] idxs = new int[props.length];
132         DjObject dob = lst.getDjenericObjectAt(0);
133
134         for (int i = 0; i < props.length; i++)
135         {
136           idxs[i] = dob.getPropertyIndex(props[i]);
137         }
138         lst.sort(idxs);
139       }
140       catch (ObjectNotDefinedException onde)
141       {
142         throw new DjScriptExecutionException(Messages.getString("SetNode.OrderBy", onde.getMessage()), orderBy);
143       }
144     }
145   }
146
147   public String JavaDoc getValidatedTypeName(DjScriptCompileTimeScope context) throws DjScriptExecutionException
148   {
149     ValueExpression val = (ValueExpression) getChild(0);
150
151     String JavaDoc typeName = val.getValidatedTypeName(context);
152     try
153     {
154       DjExtent ext = context.getPersistenceManager().getExtentByObjectType(typeName);
155
156       Variable variable = new Variable(getSetName(), new DjList(ext));
157       context.pushVariable(variable);
158
159       return typeName;
160     }
161     catch (ObjectNotDefinedException onde)
162     {
163       throw new DjScriptExecutionException(onde, this);
164     }
165   }
166
167   public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
168   {
169     getValidatedTypeName(ctxt);
170   }
171
172   public void collectVariables(DjScriptCompileTimeScope ctxt, HashMap JavaDoc variables, int stopAtLine)
173       throws DjScriptExecutionException
174   {
175     if (stopAtLine >= 0 && getLine() > stopAtLine) return;
176     variables.put(getSetName(), getValidatedTypeName(ctxt));
177   }
178
179 }
Popular Tags