You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by tk...@apache.org on 2001/01/23 23:18:28 UTC

cvs commit: xml-batik/sources/org/apache/batik/script/test ScriptDOMTest.java

tkormann    01/01/23 14:18:28

  Added:       sources/org/apache/batik/script ConcreteInterpreterPool.java
  Removed:     sources/org/apache/batik/script/test ScriptDOMTest.java
  Log:
  clean
  
  Revision  Changes    Path
  1.1                  xml-batik/sources/org/apache/batik/script/ConcreteInterpreterPool.java
  
  Index: ConcreteInterpreterPool.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.script;
  
  import org.apache.batik.script.*;
  import org.w3c.dom.Document;
  import java.util.HashMap;
  
  /**
   * The reference implementation of the <code>InterpreterPool</code> interface.
   * It is able to create <code>Interpreter</code> instances for ECMAScript,
   * Python and Tcl scripting languages if you provide the right jar files in
   * your CLASSPATH (i.e. Rhino, JPython and Jacl jar files).
   * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
   * @version $Id: ConcreteInterpreterPool.java,v 1.1 2001/01/23 22:18:24 tkormann Exp $
   */
  public class ConcreteInterpreterPool implements InterpreterPool {
      private Document document;
      private HashMap factories = new HashMap(3);
      private HashMap interpreters = new HashMap(1);
  
      private static final String RHINO =
          "org.apache.batik.script.rhino.RhinoInterpreterFactory";
  
      private static final String JPYTHON =
          "org.apache.batik.script.jpython.JPythonInterpreterFactory";
  
      private static final String JACL =
          "org.apache.batik.script.jacl.JaclInterpreterFactory";
  
      /**
       * Builds an instance of <code>ConcreteInterpreterPool</code>.
       */
      public ConcreteInterpreterPool() {
      }
  
      /**
       * Builds an instance of <code>ConcreteInterpreterPool</code> that
       * will create <code>Interpreter</code> instances for the the given
       * <code>Document</code>.
       */
      public ConcreteInterpreterPool(Document doc) {
          document = doc;
          InterpreterFactory factory = null;
          try {
              factory =
                  (InterpreterFactory)Class.forName(RHINO).newInstance();
              putInterpreterFactory("text/ecmascript",
                                    factory);
          } catch (Throwable t1) {
              // may append if the class is not in the CLASSPATH
          }
          try {
              factory =
                  (InterpreterFactory)Class.forName(JPYTHON).newInstance();
              putInterpreterFactory("text/python",
                                    factory);
          } catch (Throwable t2) {
              // may append if the class is not in the CLASSPATH
          }
          try {
              factory =
                  (InterpreterFactory)Class.forName(JACL).newInstance();
              putInterpreterFactory("text/tcl",
                                    factory);
          } catch (Throwable t3) {
              // may append if the class is not in the CLASSPATH
          }
      }
  
      /**
       * Returns a unique instance of an implementation of
       * <code>Interpreter</code> interface that match the given language.
       * It returns <code>null</code> if the interpreter cannot be build (for
       * example the language is not recognized by this
       * <code>InterpreterPool</code>). The variable "document" in the returned
       * interpreter will reference the instance of <code>Document</code>
       * to which the <code>InterpreterPool</code> is linked.
       * @param language a mimeType like string describing the language to use
       * (i.e. "text/ecmascript" for ECMAScript interpreter, "text/tcl" for Tcl
       * interpreter...).
       */
      public Interpreter getInterpreter(String language) {
          Interpreter interpreter = (Interpreter)interpreters.get(language);
          if (interpreter == null) {
              InterpreterFactory factory = (InterpreterFactory)factories.
                  get(language);
              if (factory != null)
                  try {
                      interpreter = factory.createInterpreter();
                      if (document != null) {
                          interpreter.bindObject("document", document);
                      }
                      interpreters.put(language, interpreter);
                  } catch (Throwable t) {
                      // may append if the class is here but
                      // not the scripting engine jar
                  }
          }
          return interpreter;
      }
  
      /**
       * Registers an <code>InterpreterFactory</code> for the given
       * language. This allows you to add other languages to the default
       * ones or to replace the <code>InterpreterFactory</code> used to create
       * an <code>Interpreter</code> instance for a particular language to
       * be able to have your own interpreter.
       * @param language the language for which the factory is registered.
       * @parma factory the <code>InterpreterFactory</code> that will allow to
       * create a interpreter for the language.
       */
      public void putInterpreterFactory(String language,
                                        InterpreterFactory factory) {
          factories.put(language, factory);
      }
  
      /**
       * Unregisters the <code>InterpreterFactory</code> of the given
       * language.
       * @param language the language for which the factory should
       * be unregistered.
       */
      public void removeInterpreterFactory(String language) {
          factories.remove(language);
      }
  }