SimpleScriptContext.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Decompiled with CFR 0.152.
  3. */
  4. package com.script;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.io.Reader;
  8. import java.io.Writer;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.List;
  12. public class SimpleScriptContext
  13. implements ScriptContext {
  14. private static List<Integer> scopes = new ArrayList<Integer>(2);
  15. protected Bindings engineScope = new SimpleBindings();
  16. protected Writer errorWriter = new PrintWriter(System.err, true);
  17. protected Bindings globalScope = null;
  18. protected Reader reader = new InputStreamReader(System.in);
  19. protected Writer writer = new PrintWriter(System.out, true);
  20. @Override
  21. public void setBindings(Bindings bindings, int scope) {
  22. switch (scope) {
  23. case 100: {
  24. if (bindings == null) {
  25. throw new NullPointerException("Engine scope cannot be null.");
  26. }
  27. this.engineScope = bindings;
  28. return;
  29. }
  30. case 200: {
  31. this.globalScope = bindings;
  32. return;
  33. }
  34. }
  35. throw new IllegalArgumentException("Invalid scope value.");
  36. }
  37. @Override
  38. public Object getAttribute(String name) {
  39. if (this.engineScope.containsKey(name)) {
  40. return this.getAttribute(name, 100);
  41. }
  42. if (this.globalScope == null || !this.globalScope.containsKey(name)) {
  43. return null;
  44. }
  45. return this.getAttribute(name, 200);
  46. }
  47. @Override
  48. public Object getAttribute(String name, int scope) {
  49. switch (scope) {
  50. case 100: {
  51. return this.engineScope.get(name);
  52. }
  53. case 200: {
  54. if (this.globalScope != null) {
  55. return this.globalScope.get(name);
  56. }
  57. return null;
  58. }
  59. }
  60. throw new IllegalArgumentException("Illegal scope value.");
  61. }
  62. @Override
  63. public Object removeAttribute(String name, int scope) {
  64. switch (scope) {
  65. case 100: {
  66. if (this.getBindings(100) != null) {
  67. return this.getBindings(100).remove(name);
  68. }
  69. return null;
  70. }
  71. case 200: {
  72. if (this.getBindings(200) != null) {
  73. return this.getBindings(200).remove(name);
  74. }
  75. return null;
  76. }
  77. }
  78. throw new IllegalArgumentException("Illegal scope value.");
  79. }
  80. @Override
  81. public void setAttribute(String name, Object value, int scope) {
  82. switch (scope) {
  83. case 100: {
  84. this.engineScope.put(name, value);
  85. return;
  86. }
  87. case 200: {
  88. if (this.globalScope != null) {
  89. this.globalScope.put(name, value);
  90. return;
  91. }
  92. return;
  93. }
  94. }
  95. throw new IllegalArgumentException("Illegal scope value.");
  96. }
  97. @Override
  98. public Writer getWriter() {
  99. return this.writer;
  100. }
  101. @Override
  102. public Reader getReader() {
  103. return this.reader;
  104. }
  105. @Override
  106. public void setReader(Reader reader2) {
  107. this.reader = reader2;
  108. }
  109. @Override
  110. public void setWriter(Writer writer2) {
  111. this.writer = writer2;
  112. }
  113. @Override
  114. public Writer getErrorWriter() {
  115. return this.errorWriter;
  116. }
  117. @Override
  118. public void setErrorWriter(Writer writer2) {
  119. this.errorWriter = writer2;
  120. }
  121. @Override
  122. public int getAttributesScope(String name) {
  123. if (this.engineScope.containsKey(name)) {
  124. return 100;
  125. }
  126. if (this.globalScope == null || !this.globalScope.containsKey(name)) {
  127. return -1;
  128. }
  129. return 200;
  130. }
  131. @Override
  132. public Bindings getBindings(int scope) {
  133. if (scope == 100) {
  134. return this.engineScope;
  135. }
  136. if (scope == 200) {
  137. return this.globalScope;
  138. }
  139. throw new IllegalArgumentException("Illegal scope value.");
  140. }
  141. @Override
  142. public List<Integer> getScopes() {
  143. return scopes;
  144. }
  145. static {
  146. scopes.add(100);
  147. scopes.add(200);
  148. scopes = Collections.unmodifiableList(scopes);
  149. }
  150. }