KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > program > QuercusProgram


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.program;
31
32 import com.caucho.quercus.Quercus;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.LongValue;
35 import com.caucho.quercus.env.Value;
36 import com.caucho.quercus.page.QuercusPage;
37 import com.caucho.vfs.BasicDependencyContainer;
38 import com.caucho.vfs.Depend;
39 import com.caucho.vfs.Path;
40 import com.caucho.vfs.PersistentDependency;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * Represents a compiled Quercus program.
49  */

50 public class QuercusProgram {
51   private Quercus _quercus;
52
53   private QuercusPage _compiledPage;
54   
55   private Path _sourceFile;
56
57   private HashMap JavaDoc<String JavaDoc,Function> _functionMap;
58   private HashMap JavaDoc<String JavaDoc,Function> _functionMapLowerCase
59     = new HashMap JavaDoc<String JavaDoc,Function>();
60
61   private HashMap JavaDoc<String JavaDoc,InterpretedClassDef> _classMap;
62
63   private FunctionInfo _functionInfo;
64   private Statement _statement;
65
66   private ArrayList JavaDoc<PersistentDependency> _dependList
67     = new ArrayList JavaDoc<PersistentDependency>();
68
69   private BasicDependencyContainer _depend
70     = new BasicDependencyContainer();
71
72   /**
73    * Creates a new quercus program
74    *
75    * @param quercus the owning quercus engine
76    * @param sourceFile the path to the source file
77    * @param statement the top-level statement
78    */

79   public QuercusProgram(Quercus quercus, Path sourceFile,
80             HashMap JavaDoc<String JavaDoc, Function> functionMap,
81             HashMap JavaDoc<String JavaDoc, InterpretedClassDef> classMap,
82             FunctionInfo functionInfo,
83             Statement statement)
84   {
85     _quercus = quercus;
86
87     _sourceFile = sourceFile;
88     if (sourceFile != null)
89       addDepend(sourceFile);
90
91     _functionMap = functionMap;
92
93     for (Map.Entry JavaDoc<String JavaDoc,Function> entry : functionMap.entrySet()) {
94       _functionMapLowerCase.put(entry.getKey().toLowerCase(),
95                 entry.getValue());
96     }
97
98     _classMap = classMap;
99
100     _functionInfo = functionInfo;
101     _statement = statement;
102   }
103
104   /**
105    * Returns the engine.
106    */

107   public Quercus getPhp()
108   {
109     return _quercus;
110   }
111
112   /**
113    * Returns the source path.
114    */

115   public Path getSourcePath()
116   {
117     return _sourceFile;
118   }
119
120   public FunctionInfo getFunctionInfo()
121   {
122     return _functionInfo;
123   }
124
125   public Statement getStatement()
126   {
127     return _statement;
128   }
129
130   /**
131    * Adds a dependency.
132    */

133   public void addDepend(Path path)
134   {
135     Depend depend = new Depend(path);
136     
137     _dependList.add(depend);
138     _depend.add(depend);
139   }
140
141   public ArrayList JavaDoc<PersistentDependency> getDependencyList()
142   {
143     return _dependList;
144   }
145
146   /**
147    * Returns true if the function is modified.
148    */

149   public boolean isModified()
150   {
151     return _depend.isModified();
152   }
153
154   /**
155    * Returns the compiled page.
156    */

157   public QuercusPage getCompiledPage()
158   {
159     return _compiledPage;
160   }
161
162   /**
163    * Sets the compiled page.
164    */

165   public void setCompiledPage(QuercusPage page)
166   {
167     _compiledPage = page;
168   }
169
170   /**
171    * Finds a function.
172    */

173   public AbstractFunction findFunction(String JavaDoc name)
174   {
175     AbstractFunction fun = _functionMap.get(name);
176
177     if (fun != null)
178       return fun;
179
180     if (! _quercus.isStrict())
181       fun = _functionMapLowerCase.get(name.toLowerCase());
182     
183     return fun;
184   }
185
186   /**
187    * Returns the functions.
188    */

189   public Collection JavaDoc<Function> getFunctions()
190   {
191     return _functionMap.values();
192   }
193
194   /**
195    * Returns the functions.
196    */

197   public Collection JavaDoc<InterpretedClassDef> getClasses()
198   {
199     return _classMap.values();
200   }
201
202   /**
203    * Creates a return for the final expr.
204    */

205   public QuercusProgram createExprReturn()
206   {
207     // quercus/1515 - used to convert an call string to return a value
208

209     if (_statement instanceof ExprStatement) {
210       ExprStatement exprStmt = (ExprStatement) _statement;
211
212       _statement = new ReturnStatement(exprStmt.getExpr());
213     }
214     else if (_statement instanceof BlockStatement) {
215       BlockStatement blockStmt = (BlockStatement) _statement;
216
217       Statement []statements = blockStmt.getStatements();
218
219       if (statements.length > 0 &&
220       statements[0] instanceof ExprStatement) {
221     ExprStatement exprStmt
222       = (ExprStatement) statements[0];
223
224     _statement = new ReturnStatement(exprStmt.getExpr());
225       }
226     }
227     
228     return this;
229   }
230
231   /**
232    * Execute the program
233    *
234    * @param env the calling environment
235    *
236    */

237   public Value execute(Env env)
238   {
239     Value value = _statement.execute(env);
240
241     if (value != null)
242       return value;
243     else
244       return LongValue.ONE;
245   }
246
247   /**
248    * Imports the page definitions.
249    */

250   public void init(Env env)
251   {
252     /*
253     for (Map.Entry<String,InterpretedClassDef> entry : _classMap.entrySet()) {
254       entry.getValue().init(env);
255     }
256     */

257   }
258
259   /**
260    * Imports the page definitions.
261    */

262   public void importDefinitions(Env env)
263   {
264     for (Map.Entry JavaDoc<String JavaDoc,Function> entry : _functionMap.entrySet()) {
265       Function fun = entry.getValue();
266
267       if (fun.isGlobal())
268     env.addFunction(entry.getKey(), fun);
269     }
270     
271     for (Map.Entry JavaDoc<String JavaDoc,InterpretedClassDef> entry : _classMap.entrySet()) {
272       env.addClassDef(entry.getKey(), entry.getValue());
273     }
274   }
275
276   //
277
// Java generation code follows
278
//
279

280   public String JavaDoc toString()
281   {
282     return "QuercusProgram[" + _sourceFile + "]";
283   }
284 }
285
286
Popular Tags