All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.caucho.quercus.script.QuercusScriptEngineFactory Maven / Gradle / Ivy

There is a newer version: 4.0.66
Show newest version
/*
 * Copyright (c) 1998-2012 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.quercus.script;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.SimpleBindings;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Script engine factory
 */
public class QuercusScriptEngineFactory implements ScriptEngineFactory
{
  private Bindings _globalBindings = new SimpleBindings();

  /**
   * Returns the full name of the ScriptEngine.
   */
  public String getEngineName()
  {
    return "Caucho Quercus Script Engine";
  }

  /**
   * Returns the version of the ScriptEngine.
   */
  public String getEngineVersion()
  {
    try {
      Class cl = Class.forName("com.caucho.Version");
      Field version = cl.getField("VERSION");

      return (String) version.get(null);
    }
    catch (Exception e) {
    }

    return "Resin/4.x";
  }

  /**
   * Returns an array of filename extensions normally used by this
   * language.
   */
  public List getExtensions()
  {
    ArrayList ext = new ArrayList();
    ext.add("php");
    return ext;
  }

  /**
   * Returns the mime-types for scripts for the engine.
   */
  public List getMimeTypes()
  {
    return new ArrayList();
  }

  /**
   * Returns the short names for the scripts for the engine,
   * e.g. {"javascript", "rhino"}
   */
  public List getNames()
  {
    ArrayList names = new ArrayList();
    names.add("quercus");
    names.add("php");
    return names;
  }

  /**
   * Returns the name of the supported language.
   */
  public String getLanguageName()
  {
    return "php";
  }

  /**
   * Returns the version of the scripting language.
   */
  public String getLanguageVersion()
  {
    return "5.4.0";
  }

  /**
   * Returns engine-specific properties.
   *
   * Predefined keys include:
   * 
    *
  • THREADING *
*/ public Object getParameter(String key) { if ("THREADING".equals(key)) return "THREAD-ISOLATED"; else if (ScriptEngine.ENGINE.equals(key)) return getEngineName(); else if (ScriptEngine.ENGINE_VERSION.equals(key)) return getEngineVersion(); else if (ScriptEngine.NAME.equals(key)) return getEngineName(); else if (ScriptEngine.LANGUAGE.equals(key)) return getLanguageName(); else if (ScriptEngine.LANGUAGE_VERSION.equals(key)) return getLanguageVersion(); else return null; } /** * Returns a string which could invoke a method of a Java object. */ public String getMethodCallSyntax(String obj, String m, String []args) { StringBuilder sb = new StringBuilder(); sb.append("$"); sb.append(obj); sb.append("->"); sb.append(m); sb.append("("); for (int i = 0; i < args.length; i++) { if (i != 0) sb.append(", "); sb.append("$"); sb.append(args[i]); } sb.append(");"); return sb.toString(); } /** * Returns a string which generates an output statement. */ public String getOutputStatement(String toDisplay) { return "echo(\'" + toDisplay.replace("\'", "\\\'") + "\');"; } /** * Returns a string which generates a valid program. */ public String getProgram(String []statements) { StringBuilder sb = new StringBuilder(); sb.append("\n"); return sb.toString(); } /** * Returns a ScriptEngine instance. */ public ScriptEngine getScriptEngine() { return new QuercusScriptEngine(this); } public String toString() { return getClass().getSimpleName() + "[]"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy