KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.tools.generator.core.nodes;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Hashtable JavaDoc;
5
6 import com.genimen.djeneric.repository.DjObject;
7 import com.genimen.djeneric.repository.DjPersistenceManager;
8 import com.genimen.djeneric.repository.DjSession;
9 import com.genimen.djeneric.repository.exceptions.DjenericException;
10 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
11 import com.genimen.djeneric.tools.generator.core.ParseException;
12 import com.genimen.djeneric.tools.generator.core.SimpleNode;
13 import com.genimen.djeneric.tools.generator.core.util.ParseContext;
14
15 public class TemplateNode extends SimpleNode
16 {
17   boolean _isEvaluated = false;
18   String JavaDoc _fileName = null;
19
20   Hashtable JavaDoc _globalConstants = new Hashtable JavaDoc();
21
22   public TemplateNode(int i)
23   {
24     super(i);
25   }
26
27   public TemplateNode(DjentelParserEngine p, int i)
28   {
29     super(p, i);
30   }
31
32   public String JavaDoc getName()
33   {
34     return "template";
35   }
36
37   public String JavaDoc toString()
38   {
39     return "";
40   }
41
42   public void setGlobalConstants(Hashtable JavaDoc constants)
43   {
44     _globalConstants = constants;
45   }
46
47   public boolean isRootBased()
48   {
49     ModuleNode modNode = (ModuleNode) getChild(0);
50     return modNode.isRootBased();
51   }
52
53   public String JavaDoc evaluate(DjPersistenceManager mgr) throws ParseException, DjenericException
54   {
55     return evaluate(mgr, null);
56   }
57
58   public String JavaDoc evaluate(DjPersistenceManager mgr, long objectId) throws ParseException, DjenericException
59   {
60     return evaluate(mgr, new Long JavaDoc(objectId));
61   }
62
63   private String JavaDoc smartTrim(String JavaDoc src)
64   {
65     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(src);
66     String JavaDoc whitespace = " \t\r\n";
67     int i = 0;
68     while (i < sb.length())
69     {
70       if (whitespace.indexOf(sb.charAt(i)) == -1) break;
71       i++;
72     }
73     if (i > 0) i--;
74     while (i > 0 && sb.charAt(i) != '\n')
75       i--;
76     if (sb.charAt(i) == '\n') i++;
77     if (i != 0) sb.delete(0, i);
78     while (whitespace.indexOf(sb.charAt(sb.length() - 1)) != -1)
79       sb.deleteCharAt(sb.length() - 1);
80     sb.append("\n");
81     return sb.toString();
82   }
83
84   public String JavaDoc getModuleName()
85   {
86     ModuleNode modNode = (ModuleNode) getChild(0);
87     return modNode.getModuleName();
88   }
89
90   public String JavaDoc getObjectType()
91   {
92     ModuleNode modNode = (ModuleNode) getChild(0);
93     return modNode.getObjectType();
94   }
95
96   private String JavaDoc evaluate(DjPersistenceManager mgr, Long JavaDoc objectId) throws ParseException, DjenericException
97   {
98     ModuleNode modNode = (ModuleNode) getChild(0);
99
100     BodyNode body = (BodyNode) getChild(1);
101
102     DjSession session = mgr.createSession();
103
104     ParseContext context = new ParseContext(mgr);
105
106     addGlobalConstantsToContext(context);
107     addRootTocontext(objectId, mgr, modNode, session, context);
108     addConstToContext(this, context);
109
110     ValueExpression cen = (ValueExpression) modNode.getChild(0);
111
112     _fileName = null;
113     Object JavaDoc o = cen.getValue(context);
114     if (o != null) _fileName = o.toString();
115
116     _isEvaluated = true;
117
118     context = new ParseContext(mgr);
119
120     try
121     {
122       addGlobalConstantsToContext(context);
123       addRootTocontext(objectId, mgr, modNode, session, context);
124       return smartTrim(body.evaluate(context));
125     }
126     finally
127     {
128       session.close();
129     }
130   }
131
132   public String JavaDoc getFileName() throws ParseException
133   {
134     if (!_isEvaluated) throw new ParseException("Template not evaluated yet", 0, 0);
135     return _fileName;
136   }
137
138   private void addConstToContext(SimpleNode root, ParseContext context) throws ParseException, DjenericException
139   {
140     if (root == null) return;
141
142     for (int i = 0; i < root.getChildCount(); i++)
143     {
144       if (root.getChild(i) instanceof ConstNode)
145       {
146         ConstNode cn = (ConstNode) root.getChild(i);
147         cn.evaluate(context);
148       }
149       addConstToContext(root.getChild(i), context);
150     }
151   }
152
153   private void addRootTocontext(Long JavaDoc objectId, DjPersistenceManager mgr, ModuleNode modNode, DjSession session,
154                                 ParseContext context) throws ParseException, DjenericException
155   {
156     if (isRootBased() && objectId == null)
157     {
158       throw new ParseException("This module requires a root object of type " + modNode.getObjectType(), beginLine,
159           beginColumn);
160     }
161
162     if (isRootBased())
163     {
164       DjObject obj = session.getObject(mgr.getExtentByObjectType(modNode.getObjectType()), objectId.longValue());
165       if (!obj.isInstanceOf(modNode.getObjectType()))
166       {
167         throw new ParseException("Object with ID = " + objectId.longValue() + " is of type "
168                                  + obj.getExtent().getObjectType() + " but the module is based on "
169                                  + modNode.getObjectType(), 0, 0);
170       }
171       context.pushObject(modNode.getObjectName(), obj);
172     }
173   }
174
175   private void addGlobalConstantsToContext(ParseContext context)
176   {
177     Enumeration JavaDoc keys = _globalConstants.keys();
178     while (keys.hasMoreElements())
179     {
180       String JavaDoc key = keys.nextElement().toString();
181       context.pushConst(key, _globalConstants.get(key));
182     }
183   }
184
185 }
Popular Tags