KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > page > QuercusPage


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.page;
31
32 import com.caucho.quercus.Quercus;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.NullValue;
35 import com.caucho.quercus.env.QuercusLanguageException;
36 import com.caucho.quercus.env.Value;
37 import com.caucho.quercus.program.AbstractFunction;
38 import com.caucho.quercus.program.ClassDef;
39 import com.caucho.vfs.Path;
40
41 import java.util.HashMap JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * Represents a compiled PHP program.
46  */

47 abstract public class QuercusPage
48 {
49   private HashMap JavaDoc<String JavaDoc,AbstractFunction> _funMap
50     = new HashMap JavaDoc<String JavaDoc,AbstractFunction>();
51   
52   private HashMap JavaDoc<String JavaDoc,AbstractFunction> _funMapLowerCase
53     = new HashMap JavaDoc<String JavaDoc,AbstractFunction>();
54   
55   private HashMap JavaDoc<String JavaDoc,ClassDef> _classMap
56     = new HashMap JavaDoc<String JavaDoc,ClassDef>();
57
58   /**
59    * Returns true if the page is modified.
60    */

61   public boolean isModified()
62   {
63     return false;
64   }
65
66   /**
67    * Returns the page's path.
68    */

69   abstract public Path getSelfPath(Env env);
70   
71   /**
72    * Finds a function.
73    */

74   public AbstractFunction findFunction(String JavaDoc name)
75   {
76     AbstractFunction fun = _funMap.get(name);
77
78     if (fun != null)
79       return fun;
80
81     fun = _funMapLowerCase.get(name.toLowerCase());
82
83     return fun;
84   }
85
86   /**
87    * Finds a function.
88    */

89   public ClassDef findClass(String JavaDoc name)
90   {
91     return _classMap.get(name);
92   }
93
94   /**
95    * Returns the class map.
96    */

97   public HashMap JavaDoc<String JavaDoc,ClassDef> getClassMap()
98   {
99     return _classMap;
100   }
101
102   /**
103    * Execute the program as top-level, i.e. not included.
104    *
105    * @param env the calling environment
106    */

107   public Value executeTop(Env env)
108   {
109     Path oldPwd = env.getPwd();
110
111     Path pwd = getPwd(env);
112
113     env.setPwd(pwd);
114     try {
115       return execute(env);
116     } catch (QuercusLanguageException e) {
117       if (env.getExceptionHandler() != null)
118     env.getExceptionHandler().call(env, e.getValue());
119
120       return NullValue.NULL;
121     } finally {
122       env.setPwd(oldPwd);
123     }
124   }
125
126   /**
127    * Returns the pwd according to the source page.
128    */

129   public Path getPwd(Env env)
130   {
131     return getSelfPath(env).getParent();
132   }
133
134
135   /**
136    * Execute the program
137    *
138    * @param env the calling environment
139    */

140   abstract public Value execute(Env env);
141
142   /**
143    * Initialize the program
144    *
145    * @param quercus the owning engine
146    */

147   public void init(Quercus quercus)
148   {
149   }
150
151   /**
152    * Initialize the environment
153    *
154    * @param quercus the owning engine
155    */

156   public void init(Env env)
157   {
158   }
159
160   /**
161    * Imports the page definitions.
162    */

163   public void importDefinitions(Env env)
164   {
165     for (Map.Entry JavaDoc<String JavaDoc,AbstractFunction> entry : _funMap.entrySet()) {
166       AbstractFunction fun = entry.getValue();
167
168       if (fun.isGlobal())
169     env.addFunction(entry.getKey(), entry.getValue());
170     }
171     
172     for (Map.Entry JavaDoc<String JavaDoc,ClassDef> entry : _classMap.entrySet()) {
173       env.addClassDef(entry.getKey(), entry.getValue());
174     }
175   }
176
177   /**
178    * Adds a function.
179    */

180   protected void addFunction(String JavaDoc name, AbstractFunction fun)
181   {
182     _funMap.put(name, fun);
183     _funMapLowerCase.put(name.toLowerCase(), fun);
184   }
185
186   /**
187    * Adds a class.
188    */

189   protected void addClass(String JavaDoc name, ClassDef cl)
190   {
191     _classMap.put(name, cl);
192   }
193   
194   public String JavaDoc toString()
195   {
196     return "QuercusPage[]";
197   }
198 }
199
200
Popular Tags