/jdk/src/share/classes/javax/script/SimpleScriptContext.java

https://github.com/Morriar/ProxyJDK · Java · 322 lines · 129 code · 42 blank · 151 comment · 31 complexity · 42a4b330dbdc24995eeef953c22e634c MD5 · raw file

  1. /*
  2. * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package javax.script;
  26. import java.util.*;
  27. import java.io.*;
  28. /**
  29. * Simple implementation of ScriptContext.
  30. *
  31. * @author Mike Grogan
  32. * @since 1.6
  33. */
  34. public class SimpleScriptContext implements ScriptContext {
  35. /**
  36. * This is the writer to be used to output from scripts.
  37. * By default, a <code>PrintWriter</code> based on <code>System.out</code>
  38. * is used. Accessor methods getWriter, setWriter are used to manage
  39. * this field.
  40. * @see java.lang.System#out
  41. * @see java.io.PrintWriter
  42. */
  43. protected Writer writer;
  44. /**
  45. * This is the writer to be used to output errors from scripts.
  46. * By default, a <code>PrintWriter</code> based on <code>System.err</code> is
  47. * used. Accessor methods getErrorWriter, setErrorWriter are used to manage
  48. * this field.
  49. * @see java.lang.System#err
  50. * @see java.io.PrintWriter
  51. */
  52. protected Writer errorWriter;
  53. /**
  54. * This is the reader to be used for input from scripts.
  55. * By default, a <code>InputStreamReader</code> based on <code>System.in</code>
  56. * is used and default charset is used by this reader. Accessor methods
  57. * getReader, setReader are used to manage this field.
  58. * @see java.lang.System#in
  59. * @see java.io.InputStreamReader
  60. */
  61. protected Reader reader;
  62. /**
  63. * This is the engine scope bindings.
  64. * By default, a <code>SimpleBindings</code> is used. Accessor
  65. * methods setBindings, getBindings are used to manage this field.
  66. * @see SimpleBindings
  67. */
  68. protected Bindings engineScope;
  69. /**
  70. * This is the global scope bindings.
  71. * By default, a null value (which means no global scope) is used. Accessor
  72. * methods setBindings, getBindings are used to manage this field.
  73. */
  74. protected Bindings globalScope;
  75. public SimpleScriptContext() {
  76. engineScope = new SimpleBindings();
  77. globalScope = null;
  78. reader = new InputStreamReader(System.in);
  79. writer = new PrintWriter(System.out , true);
  80. errorWriter = new PrintWriter(System.err, true);
  81. }
  82. /**
  83. * Sets a <code>Bindings</code> of attributes for the given scope. If the value
  84. * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
  85. * <code>engineScope</code> field. If the value
  86. * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
  87. * <code>globalScope</code> field.
  88. *
  89. * @param bindings The <code>Bindings</code> of attributes to set.
  90. * @param scope The value of the scope in which the attributes are set.
  91. *
  92. * @throws IllegalArgumentException if scope is invalid.
  93. * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
  94. * the specified <code>Bindings</code> is null.
  95. */
  96. public void setBindings(Bindings bindings, int scope) {
  97. switch (scope) {
  98. case ENGINE_SCOPE:
  99. if (bindings == null) {
  100. throw new NullPointerException("Engine scope cannot be null.");
  101. }
  102. engineScope = bindings;
  103. break;
  104. case GLOBAL_SCOPE:
  105. globalScope = bindings;
  106. break;
  107. default:
  108. throw new IllegalArgumentException("Invalid scope value.");
  109. }
  110. }
  111. /**
  112. * Retrieves the value of the attribute with the given name in
  113. * the scope occurring earliest in the search order. The order
  114. * is determined by the numeric value of the scope parameter (lowest
  115. * scope values first.)
  116. *
  117. * @param name The name of the the attribute to retrieve.
  118. * @return The value of the attribute in the lowest scope for
  119. * which an attribute with the given name is defined. Returns
  120. * null if no attribute with the name exists in any scope.
  121. * @throws NullPointerException if the name is null.
  122. * @throws IllegalArgumentException if the name is empty.
  123. */
  124. public Object getAttribute(String name) {
  125. if (engineScope.containsKey(name)) {
  126. return getAttribute(name, ENGINE_SCOPE);
  127. } else if (globalScope != null && globalScope.containsKey(name)) {
  128. return getAttribute(name, GLOBAL_SCOPE);
  129. }
  130. return null;
  131. }
  132. /**
  133. * Gets the value of an attribute in a given scope.
  134. *
  135. * @param name The name of the attribute to retrieve.
  136. * @param scope The scope in which to retrieve the attribute.
  137. * @return The value of the attribute. Returns <code>null</code> is the name
  138. * does not exist in the given scope.
  139. *
  140. * @throws IllegalArgumentException
  141. * if the name is empty or if the value of scope is invalid.
  142. * @throws NullPointerException if the name is null.
  143. */
  144. public Object getAttribute(String name, int scope) {
  145. switch (scope) {
  146. case ENGINE_SCOPE:
  147. return engineScope.get(name);
  148. case GLOBAL_SCOPE:
  149. if (globalScope != null) {
  150. return globalScope.get(name);
  151. }
  152. return null;
  153. default:
  154. throw new IllegalArgumentException("Illegal scope value.");
  155. }
  156. }
  157. /**
  158. * Remove an attribute in a given scope.
  159. *
  160. * @param name The name of the attribute to remove
  161. * @param scope The scope in which to remove the attribute
  162. *
  163. * @return The removed value.
  164. * @throws IllegalArgumentException
  165. * if the name is empty or if the scope is invalid.
  166. * @throws NullPointerException if the name is null.
  167. */
  168. public Object removeAttribute(String name, int scope) {
  169. switch (scope) {
  170. case ENGINE_SCOPE:
  171. if (getBindings(ENGINE_SCOPE) != null) {
  172. return getBindings(ENGINE_SCOPE).remove(name);
  173. }
  174. return null;
  175. case GLOBAL_SCOPE:
  176. if (getBindings(GLOBAL_SCOPE) != null) {
  177. return getBindings(GLOBAL_SCOPE).remove(name);
  178. }
  179. return null;
  180. default:
  181. throw new IllegalArgumentException("Illegal scope value.");
  182. }
  183. }
  184. /**
  185. * Sets the value of an attribute in a given scope.
  186. *
  187. * @param name The name of the attribute to set
  188. * @param value The value of the attribute
  189. * @param scope The scope in which to set the attribute
  190. *
  191. * @throws IllegalArgumentException
  192. * if the name is empty or if the scope is invalid.
  193. * @throws NullPointerException if the name is null.
  194. */
  195. public void setAttribute(String name, Object value, int scope) {
  196. switch (scope) {
  197. case ENGINE_SCOPE:
  198. engineScope.put(name, value);
  199. return;
  200. case GLOBAL_SCOPE:
  201. if (globalScope != null) {
  202. globalScope.put(name, value);
  203. }
  204. return;
  205. default:
  206. throw new IllegalArgumentException("Illegal scope value.");
  207. }
  208. }
  209. /** {@inheritDoc} */
  210. public Writer getWriter() {
  211. return writer;
  212. }
  213. /** {@inheritDoc} */
  214. public Reader getReader() {
  215. return reader;
  216. }
  217. /** {@inheritDoc} */
  218. public void setReader(Reader reader) {
  219. this.reader = reader;
  220. }
  221. /** {@inheritDoc} */
  222. public void setWriter(Writer writer) {
  223. this.writer = writer;
  224. }
  225. /** {@inheritDoc} */
  226. public Writer getErrorWriter() {
  227. return errorWriter;
  228. }
  229. /** {@inheritDoc} */
  230. public void setErrorWriter(Writer writer) {
  231. this.errorWriter = writer;
  232. }
  233. /**
  234. * Get the lowest scope in which an attribute is defined.
  235. * @param name Name of the attribute
  236. * .
  237. * @return The lowest scope. Returns -1 if no attribute with the given
  238. * name is defined in any scope.
  239. * @throws NullPointerException if name is null.
  240. * @throws IllegalArgumentException if name is empty.
  241. */
  242. public int getAttributesScope(String name) {
  243. if (engineScope.containsKey(name)) {
  244. return ENGINE_SCOPE;
  245. } else if (globalScope != null && globalScope.containsKey(name)) {
  246. return GLOBAL_SCOPE;
  247. } else {
  248. return -1;
  249. }
  250. }
  251. /**
  252. * Returns the value of the <code>engineScope</code> field if specified scope is
  253. * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is
  254. * <code>GLOBAL_SCOPE</code>.
  255. *
  256. * @param scope The specified scope
  257. * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field.
  258. * @throws IllegalArgumentException if the value of scope is invalid.
  259. */
  260. public Bindings getBindings(int scope) {
  261. if (scope == ENGINE_SCOPE) {
  262. return engineScope;
  263. } else if (scope == GLOBAL_SCOPE) {
  264. return globalScope;
  265. } else {
  266. throw new IllegalArgumentException("Illegal scope value.");
  267. }
  268. }
  269. /** {@inheritDoc} */
  270. public List<Integer> getScopes() {
  271. return scopes;
  272. }
  273. private static List<Integer> scopes;
  274. static {
  275. scopes = new ArrayList<Integer>(2);
  276. scopes.add(ENGINE_SCOPE);
  277. scopes.add(GLOBAL_SCOPE);
  278. scopes = Collections.unmodifiableList(scopes);
  279. }
  280. }