KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > script > QuercusScriptEngineFactory


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.script;
31
32 import javax.script.Bindings;
33 import javax.script.ScriptEngine;
34 import javax.script.ScriptEngineFactory;
35 import javax.script.SimpleBindings;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.lang.reflect.*;
39
40 /**
41  * Script engine factory
42  */

43 public class QuercusScriptEngineFactory implements ScriptEngineFactory {
44   private Bindings _globalBindings = new SimpleBindings();
45   
46   /**
47    * Returns the full name of the ScriptEngine.
48    */

49   public String JavaDoc getEngineName()
50   {
51     return "Caucho Quercus Script Engine";
52   }
53
54   /**
55    * Returns the version of the ScriptEngine.
56    */

57   public String JavaDoc getEngineVersion()
58   {
59     try {
60       //return com.caucho.Version.VERSION;
61

62       Class JavaDoc cl = Class.forName("com.caucho.Version");
63       Field version = cl.getField("VERSION");
64
65       return (String JavaDoc) version.get(null);
66     } catch (Exception JavaDoc e) {
67     }
68
69     return "Resin/3.1.0";
70   }
71
72   /**
73    * Returns an array of filename extensions normally used by this
74    * language.
75    */

76   public List JavaDoc<String JavaDoc> getExtensions()
77   {
78     ArrayList JavaDoc<String JavaDoc> ext = new ArrayList JavaDoc<String JavaDoc>();
79     ext.add("php");
80     return ext;
81   }
82
83   /**
84    * Returns the mime-types for scripts for the engine.
85    */

86   public List JavaDoc<String JavaDoc> getMimeTypes()
87   {
88     return new ArrayList JavaDoc<String JavaDoc>();
89   }
90
91   /**
92    * Returns the short names for the scripts for the engine,
93    * e.g. {"javascript", "rhino"}
94    */

95   public List JavaDoc<String JavaDoc> getNames()
96   {
97     ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
98     names.add("quercus");
99     names.add("php");
100     return names;
101   }
102
103   /**
104    * Returns the name of the supported language.
105    */

106   public String JavaDoc getLanguageName()
107   {
108     return "php";
109   }
110
111   /**
112    * Returns the version of the scripting language.
113    */

114   public String JavaDoc getLanguageVersion()
115   {
116     return "5.0.4";
117   }
118
119   /**
120    * Returns engine-specific properties.
121    *
122    * Predefined keys include:
123    * <ul>
124    * <li>THREADING
125    * </ul>
126    */

127   public Object JavaDoc getParameter(String JavaDoc key)
128   {
129     if ("THREADING".equals(key))
130       return "THREAD-ISOLATED";
131     else if (ScriptEngine.ENGINE.equals(key))
132       return getEngineName();
133     else if (ScriptEngine.ENGINE_VERSION.equals(key))
134       return getEngineVersion();
135     else if (ScriptEngine.NAME.equals(key))
136       return getEngineName();
137     else if (ScriptEngine.LANGUAGE.equals(key))
138       return getLanguageName();
139     else if (ScriptEngine.LANGUAGE_VERSION.equals(key))
140       return getLanguageVersion();
141     else
142       return null;
143   }
144
145   /**
146    * Returns a string which could invoke a method of a Java object.
147    */

148   public String JavaDoc getMethodCallSyntax(String JavaDoc obj, String JavaDoc m, String JavaDoc []args)
149   {
150     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
151
152     sb.append("$");
153     sb.append(obj);
154     sb.append("->");
155     sb.append(m);
156     sb.append("(");
157     for (int i = 0; i < args.length; i++) {
158       if (i != 0)
159     sb.append(", ");
160
161       sb.append("$");
162       sb.append(args[i]);
163     }
164     sb.append(");");
165     
166     return sb.toString();
167   }
168
169   /**
170    * Returns a string which generates an output statement.
171    */

172   public String JavaDoc getOutputStatement(String JavaDoc toDisplay)
173   {
174     return "echo(\'" + toDisplay.replace("\'", "\\\'") + "\');";
175   }
176
177   /**
178    * Returns a string which generates a valid program.
179    */

180   public String JavaDoc getProgram(String JavaDoc []statements)
181   {
182     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
183
184     sb.append("<?php\n");
185
186     for (int i = 0; i < statements.length; i++) {
187       sb.append(statements[i]);
188       sb.append(";\n");
189     }
190     
191     sb.append("?>\n");
192     
193     return sb.toString();
194   }
195   
196   /**
197    * Returns a ScriptEngine instance.
198    */

199   public ScriptEngine getScriptEngine()
200   {
201     return new QuercusScriptEngine(this);
202   }
203
204   public String JavaDoc toString()
205   {
206     return "QuercusScriptEngineFactory[]";
207   }
208 }
209
210
Popular Tags