-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathRenjinScriptContext.java
123 lines (101 loc) · 3.1 KB
/
RenjinScriptContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Renjin : JVM-based interpreter for the R language for the statistical analysis
* Copyright © 2010-2016 BeDataDriven Groep B.V. and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.gnu.org/licenses/gpl-2.0.txt
*/
package org.renjin.script;
import org.renjin.eval.Context;
import javax.script.Bindings;
import javax.script.ScriptContext;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class RenjinScriptContext implements ScriptContext {
private Context context;
private Map<String,Object> attributes = new TreeMap<>();
RenjinScriptContext(Context context) {
this.context = context;
}
public Context getContext() {
return context;
}
@Override
public Object getAttribute(String arg0) {
return attributes.get(arg0);
}
@Override
public Object getAttribute(String arg0, int arg1) {
return attributes.get(arg0);
}
@Override
public int getAttributesScope(String arg0) {
return ScriptContext.ENGINE_SCOPE;
}
@Override
public Bindings getBindings(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Writer getErrorWriter() {
return context.getSession().getConnectionTable().getStderr().getPrintWriter();
}
@Override
public Reader getReader() {
return context.getSession().getStdIn();
}
@Override
public List<Integer> getScopes() {
return Collections.singletonList(ScriptContext.ENGINE_SCOPE);
}
@Override
public Writer getWriter() {
return context.getSession().getStdOut();
}
@Override
public Object removeAttribute(String arg0, int arg1) {
return attributes.remove(arg0);
}
@Override
public void setAttribute(String name, Object value, int scope) {
if (scope == ScriptContext.ENGINE_SCOPE) {
attributes.put(name,value);
} else {
throw new UnsupportedOperationException(
String.format("setting attribute in scope (%d) not supported", scope));
}
}
@Override
public void setBindings(Bindings arg0, int arg1) {
throw new UnsupportedOperationException("nyi");
}
@Override
public void setErrorWriter(Writer errorWriter) {
context.getSession().setStdErr(new PrintWriter(errorWriter));
}
@Override
public void setReader(Reader reader) {
context.getSession().setStdIn(reader);
}
@Override
public void setWriter(Writer writer) {
context.getSession().setStdOut(new PrintWriter(writer));
}
}