001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.page;
031:
032: import com.caucho.quercus.Location;
033: import com.caucho.quercus.Quercus;
034: import com.caucho.quercus.env.Env;
035: import com.caucho.quercus.env.NullValue;
036: import com.caucho.quercus.env.QuercusLanguageException;
037: import com.caucho.quercus.env.Value;
038: import com.caucho.quercus.program.AbstractFunction;
039: import com.caucho.quercus.program.ClassDef;
040: import com.caucho.util.L10N;
041: import com.caucho.vfs.Path;
042:
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: /**
047: * Represents a compiled PHP program.
048: */
049: abstract public class QuercusPage {
050: private static final L10N L = new L10N(QuercusPage.class);
051:
052: private HashMap<String, AbstractFunction> _funMap = new HashMap<String, AbstractFunction>();
053:
054: private HashMap<String, AbstractFunction> _funMapLowerCase = new HashMap<String, AbstractFunction>();
055:
056: private HashMap<String, ClassDef> _classMap = new HashMap<String, ClassDef>();
057:
058: /**
059: * Returns true if the page is modified.
060: */
061: public boolean isModified(Env env) {
062: return false;
063: }
064:
065: /**
066: * Returns the page's path.
067: */
068: abstract public Path getSelfPath(Env env);
069:
070: /**
071: * Finds a function.
072: */
073: public AbstractFunction findFunction(String name) {
074: AbstractFunction fun = _funMap.get(name);
075:
076: if (fun != null)
077: return fun;
078:
079: fun = _funMapLowerCase.get(name.toLowerCase());
080:
081: return fun;
082: }
083:
084: /**
085: * Finds a function.
086: */
087: public ClassDef findClass(String name) {
088: return _classMap.get(name);
089: }
090:
091: /**
092: * Returns the class map.
093: */
094: public HashMap<String, ClassDef> getClassMap() {
095: return _classMap;
096: }
097:
098: /**
099: * Execute the program as top-level, i.e. not included.
100: *
101: * @param env the calling environment
102: */
103: public Value executeTop(Env env) {
104: Path oldPwd = env.getPwd();
105:
106: Path pwd = getPwd(env);
107:
108: env.setPwd(pwd);
109: try {
110: return execute(env);
111: } catch (QuercusLanguageException e) {
112:
113: if (env.getExceptionHandler() != null) {
114: try {
115: env.getExceptionHandler().call(env, e.getValue());
116: } catch (QuercusLanguageException e2) {
117: uncaughtExceptionError(env, e2);
118: }
119: } else {
120: uncaughtExceptionError(env, e);
121: }
122:
123: return NullValue.NULL;
124:
125: } finally {
126: env.setPwd(oldPwd);
127: }
128: }
129:
130: /*
131: * Throws an error for this uncaught exception.
132: */
133: private void uncaughtExceptionError(Env env,
134: QuercusLanguageException e) {
135: Location location = e.getLocation(env);
136: String type = e.getValue().getClassName();
137: String message = e.getMessage(env);
138:
139: env.error(location, L.l(
140: "Uncaught exception of type '{0}' with message '{1}'",
141: type, message));
142: }
143:
144: /**
145: * Returns the pwd according to the source page.
146: */
147: public Path getPwd(Env env) {
148: return getSelfPath(env).getParent();
149: }
150:
151: /**
152: * Execute the program
153: *
154: * @param env the calling environment
155: */
156: abstract public Value execute(Env env);
157:
158: /**
159: * Initialize the program
160: *
161: * @param quercus the owning engine
162: */
163: public void init(Quercus quercus) {
164: }
165:
166: /**
167: * Initialize the environment
168: *
169: * @param quercus the owning engine
170: */
171: public void init(Env env) {
172: }
173:
174: /**
175: * Imports the page definitions.
176: */
177: public void importDefinitions(Env env) {
178: for (Map.Entry<String, AbstractFunction> entry : _funMap
179: .entrySet()) {
180: AbstractFunction fun = entry.getValue();
181:
182: if (fun.isGlobal())
183: env.addFunction(entry.getKey(), entry.getValue());
184: }
185:
186: for (Map.Entry<String, ClassDef> entry : _classMap.entrySet()) {
187: env.addClassDef(entry.getKey(), entry.getValue());
188: }
189: }
190:
191: /**
192: * Adds a function.
193: */
194: protected void addFunction(String name, AbstractFunction fun) {
195: _funMap.put(name, fun);
196: _funMapLowerCase.put(name.toLowerCase(), fun);
197: }
198:
199: /**
200: * Adds a class.
201: */
202: protected void addClass(String name, ClassDef cl) {
203: _classMap.put(name, cl);
204: }
205:
206: public String toString() {
207: return getClass().getSimpleName() + "[]";
208: }
209: }
|