KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.tools.generator.core.nodes;
2
3 import com.genimen.djeneric.repository.DjList;
4 import com.genimen.djeneric.repository.DjObject;
5 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
6 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
7 import com.genimen.djeneric.tools.generator.core.ParseException;
8 import com.genimen.djeneric.tools.generator.core.SimpleNode;
9 import com.genimen.djeneric.tools.generator.core.util.ContextObject;
10 import com.genimen.djeneric.tools.generator.core.util.ParseContext;
11
12 public class SetNode extends SimpleNode
13 {
14   String JavaDoc _setName = "";
15
16   public SetNode(int i)
17   {
18     super(i);
19   }
20
21   public SetNode(DjentelParserEngine p, int i)
22   {
23     super(p, i);
24   }
25
26   public String JavaDoc getName()
27   {
28     return "set";
29   }
30
31   public String JavaDoc toString()
32   {
33     return "";
34   }
35
36   public String JavaDoc evaluate(ParseContext context) throws ParseException
37   {
38     ValueExpression val = (ValueExpression) getChild(0);
39     Object JavaDoc o = val.getValue(context);
40     if (!(o instanceof DjList)) throw new ParseException("Not a set expression", beginLine, beginColumn);
41     DjList lst = (DjList) o;
42
43     FilterNode filter = null;
44     OrderByNode orderBy = null;
45
46     for (int i = 1; i < getChildCount(); i++)
47     {
48       if (getChild(i) instanceof FilterNode) filter = (FilterNode) getChild(i);
49       if (getChild(i) instanceof OrderByNode) orderBy = (OrderByNode) getChild(i);
50     }
51
52     lst = processFilter(lst, context, filter);
53     processOrderBy(lst, orderBy);
54
55     context.getObjectStack().push(getSetName(), lst);
56     return "";
57   }
58
59   public String JavaDoc getSetName()
60   {
61     return _setName;
62   }
63
64   public void setSetName(String JavaDoc n)
65   {
66     _setName = n;
67   }
68
69   public DjList processFilter(DjList lst, ParseContext context, FilterNode filter) throws ParseException
70   {
71     // Is it filtered?
72
if (filter != null)
73     {
74       DjList filteredSet = new DjList();
75       context.mark();
76       ContextObject co = new ContextObject(getSetName(), null);
77       context.pushObject(co);
78       for (int i = 0; i < lst.size(); i++)
79       {
80         co.setObject(lst.getDjenericObjectAt(i));
81         if (filter.isTrue(context)) filteredSet.add(lst.getDjenericObjectAt(i));
82       }
83       lst = filteredSet;
84
85       context.releaseToMark();
86     }
87     return lst;
88   }
89
90   public void processOrderBy(DjList lst, OrderByNode orderBy) throws ParseException
91   {
92     if (orderBy != null)
93     {
94       try
95       {
96         String JavaDoc[] props = orderBy.getProperties();
97         int[] idxs = new int[props.length];
98         DjObject dob = lst.getDjenericObjectAt(0);
99
100         for (int i = 0; i < props.length; i++)
101         {
102           idxs[i] = dob.getPropertyIndex(props[i]);
103         }
104         lst.sort(idxs);
105       }
106       catch (ObjectNotDefinedException onde)
107       {
108         throw new ParseException("Order by: " + onde.getMessage(), orderBy.beginLine, orderBy.beginColumn);
109       }
110     }
111   }
112
113 }
Popular Tags