Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/shell/Environment.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /*
8 Environment.java
9
10 Wraps java.lang.System properties.
11
12 by Patrick C. Beard <[email protected]>
13 */
14
15 package org.mozilla.javascript.tools.shell;
16
17 import org.mozilla.javascript.Scriptable;
18 import org.mozilla.javascript.ScriptRuntime;
19 import org.mozilla.javascript.ScriptableObject;
20
21 import java.util.Map;
22
23 /**
24 * Environment, intended to be instantiated at global scope, provides
25 * a natural way to access System properties from JavaScript.
26 *
27 * @author Patrick C. Beard
28 */
29 public class Environment extends ScriptableObject
30 {
31 static final long serialVersionUID = -430727378460177065L;
32
33 private Environment thePrototypeInstance = null;
34
35 public static void defineClass(ScriptableObject scope) {
36 try {
37 ScriptableObject.defineClass(scope, Environment.class);
38 } catch (Exception e) {
39 throw new Error(e.getMessage());
40 }
41 }
42
43 @Override
44 public String getClassName() {
45 return "Environment";
46 }
47
48 public Environment() {
49 if (thePrototypeInstance == null)
50 thePrototypeInstance = this;
51 }
52
53 public Environment(ScriptableObject scope) {
54 setParentScope(scope);
55 Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
56 if (ctor != null && ctor instanceof Scriptable) {
57 Scriptable s = (Scriptable) ctor;
58 setPrototype((Scriptable) s.get("prototype", s));
59 }
60 }
61
62 @Override
63 public boolean has(String name, Scriptable start) {
64 if (this == thePrototypeInstance)
65 return super.has(name, start);
66
67 return (System.getProperty(name) != null);
68 }
69
70 @Override
71 public Object get(String name, Scriptable start) {
72 if (this == thePrototypeInstance)
73 return super.get(name, start);
74
75 String result = System.getProperty(name);
76 if (result != null)
77 return ScriptRuntime.toObject(getParentScope(), result);
78 else
79 return Scriptable.NOT_FOUND;
80 }
81
82 @Override
83 public void put(String name, Scriptable start, Object value) {
84 if (this == thePrototypeInstance)
85 super.put(name, start, value);
86 else
87 System.getProperties().put(name, ScriptRuntime.toString(value));
88 }
89
90 private Object[] collectIds() {
91 Map<Object,Object> props = System.getProperties();
92 return props.keySet().toArray();
93 }
94
95 @Override
96 public Object[] getIds() {
97 if (this == thePrototypeInstance)
98 return super.getIds();
99 return collectIds();
100 }
101
102 @Override
103 public Object[] getAllIds() {
104 if (this == thePrototypeInstance)
105 return super.getAllIds();
106 return collectIds();
107 }
108 }