AttributeName.java

  1. /**
  2.  * **** BEGIN LICENSE BLOCK *****
  3.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Eclipse Public
  6.  * License Version 1.0 (the "License"); you may not use this file
  7.  * except in compliance with the License. You may obtain a copy of
  8.  * the License at http://www.eclipse.org/legal/epl-v10.html
  9.  *
  10.  * Software distributed under the License is distributed on an "AS
  11.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.  * implied. See the License for the specific language governing
  13.  * rights and limitations under the License.
  14.  *
  15.  * Copyright (C) 2009-2012 Yoko Harada <yokolet@gmail.com>
  16.  *
  17.  * Alternatively, the contents of this file may be used under the terms of
  18.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  19.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  20.  * in which case the provisions of the GPL or the LGPL are applicable instead
  21.  * of those above. If you wish to allow use of your version of this file only
  22.  * under the terms of either the GPL or the LGPL, and not to allow others to
  23.  * use your version of this file under the terms of the EPL, indicate your
  24.  * decision by deleting the provisions above and replace them with the notice
  25.  * and other provisions required by the GPL or the LGPL. If you do not delete
  26.  * the provisions above, a recipient may use your version of this file under
  27.  * the terms of any one of the EPL, the GPL or the LGPL.
  28.  * **** END LICENSE BLOCK *****
  29.  */
  30. package org.jruby.embed;

  31. /**
  32.  * Predefined keys for an attribute map that ScriptingContainer has.
  33.  
  34.  * Usage example:
  35.  * <pre>
  36.  *     ScriptingContainer container = new ScriptingContainer();
  37.  *     container.setAttribute(AttributeName.BASE_DIR, System.getProperty("user.dir");</pre>
  38.  *
  39.  * @author Yoko Harada <yokolet@gmail.com>
  40.  */
  41. public enum AttributeName {
  42.     /**
  43.      * A key used in an attribute map to set a reader. This attribute can be
  44.      * set using a System property, org.jruby.embed.reader.
  45.      */
  46.     READER("org.jruby.embed.reader"),

  47.     /**
  48.      * A key used in an attribute map to set a writer. This attribute can be
  49.      * set using a System property, org.jruby.embed.writer.
  50.      */
  51.     WRITER("org.jruby.embed.writer"),

  52.     /**
  53.      * A key used in an attribute map to set an error writer. This attribute can be
  54.      * set using a System property, org.jruby.embed.errorwriter.
  55.      */
  56.     ERROR_WRITER("org.jruby.embed.errorwriter"),

  57.     /**
  58.      * A key used in an attribute map to set a base directory. This attribute can be
  59.      * set using a System property, org.jruby.embed.basedir.
  60.      */
  61.     BASE_DIR("org.jruby.embed.basedir"),

  62.     /**
  63.      * A key used in an attribute map to set a line number in error message.
  64.      * This attribute is for JSR223 only. This attribute can be
  65.      * set using a System property, org.jruby.embed.linenumber.
  66.      */
  67.     LINENUMBER("org.jruby.embed.linenumber"),

  68.     /**
  69.      * A key used in an attribute map to specify that the script to be parsed has
  70.      * unicode escape in it. Default is false. This attribute can be
  71.      * set using a System property, org.jruby.embed.unicode.escpe.
  72.      */
  73.     UNICODE_ESCAPE("org.jruby.embed.unicode.escpe"),

  74.     /**
  75.      * A key used in an attribute map to turn on/off sharing variable feature.
  76.      * Default is true. If false is set, sharing variables goes off, and better
  77.      * performance will be expected. This attribute can be
  78.      * set using a System property, org.jruby.embed.sharing.variables.
  79.      */
  80.     SHARING_VARIABLES("org.jruby.embed.sharing.variables"),

  81.     /**
  82.      * A key used in an attribute map to turn on/off clearing variables.
  83.      * This attribute is for JSR223 only.
  84.      *
  85.      * Default is false, which means JRubyEngine doesn't clear an internal
  86.      * variable table, which ends up in being reused. If true is set, JRubyEngine
  87.      * clears the internal variable table.
  88.      */
  89.     CLEAR_VARAIBLES("org.jruby.embed.clear.variables"),
  90.    
  91.     /**
  92.      * A key used in an attribute map to turn on/off termination. This attribute
  93.      * is for JSR223 only.
  94.      *
  95.      * Default is false, which means JRubyEngine doesn't terminate any state and
  96.      * doesn't execute at_exit blocks. If true is set, JRubyEngine terminates the state
  97.      * as well as executes at_exit blocks. This attribute can be
  98.      * set using a System property, org.jruby.embed.termination.
  99.      */
  100.     TERMINATION("org.jruby.embed.termination"),

  101.     /**
  102.      * A key used in an attribute map to set a receiver object for sharing variables.
  103.      * This attribute is for JSR223 only. This attribute can be
  104.      * set using a System property, org.jruby.embed.receiver.
  105.      */
  106.     RECEIVER("org.jruby.embed.receiver");

  107.     private final String fqpn;

  108.     /**
  109.      * Creates an AttributeName Enum type instance.
  110.      *
  111.      * @param fqan a fully qualified attribute name
  112.      */
  113.     AttributeName(String fqpn) {
  114.         this.fqpn = fqpn;
  115.     }

  116.     /**
  117.      * Returns the fully qualified attribute name of this enum constant.
  118.      *
  119.      * @return a fully qualified attribute name
  120.      */
  121.     @Override
  122.     public String toString() {
  123.         return fqpn;
  124.     }

  125.     /**
  126.      * Returns a fully qualified attribute name that corresponds to a given
  127.      * enumerated type identifier.
  128.      *
  129.      * @param fqan fully qualified attribute name
  130.      * @return a matched enumerated type identifier
  131.      */
  132.     public static AttributeName getType(String fqpn) {
  133.         AttributeName[] names = AttributeName.values();
  134.         for (int i=0; i<names.length; i++) {
  135.             if (fqpn.equals(names[i].toString())) {
  136.                 return names[i];
  137.             }
  138.         }
  139.         return null;
  140.     }
  141. }