Skip to content

Commit c842420

Browse files
committed
Merge pull request #66 from scijava/ij1-macro
Prepare for a fancy ImageJ 1.x macro interpreter
2 parents 1b9bd3f + 8d2ffaf commit c842420

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/main/java/org/scijava/script/ScriptModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void run() {
146146

147147
// initialize the script engine
148148
engine.put(ScriptEngine.FILENAME, path);
149+
engine.put(ScriptModule.class.getName(), this);
149150
final ScriptContext scriptContext = engine.getContext();
150151
if (output != null) scriptContext.setWriter(output);
151152
final PrintWriter errorPrinter;
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.script;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.io.IOException;
39+
import java.io.Reader;
40+
import java.io.StringReader;
41+
import java.util.Arrays;
42+
import java.util.HashMap;
43+
import java.util.List;
44+
import java.util.Map;
45+
46+
import javax.script.Bindings;
47+
import javax.script.ScriptEngine;
48+
import javax.script.ScriptException;
49+
50+
import org.junit.Test;
51+
import org.scijava.Context;
52+
import org.scijava.plugin.Plugin;
53+
54+
/**
55+
* Basic tests for the {@link ScriptService}.
56+
*
57+
* @author Johannes Schindelin
58+
*/
59+
public class ScriptEngineTest {
60+
61+
@Test
62+
public void testRot13() throws Exception {
63+
final Context context = new Context(ScriptService.class);
64+
final ScriptService scriptService = context.getService(ScriptService.class);
65+
final ScriptLanguage hello = scriptService.getLanguageByName("Hello");
66+
assertNotNull(hello);
67+
final ScriptLanguage rot13 = scriptService.getLanguageByName("Rot13");
68+
assertEquals(hello, rot13);
69+
assertEquals("Svool", rot13.getScriptEngine().eval("Hello"));
70+
}
71+
72+
@Test
73+
public void testScriptModuleValue() throws Exception {
74+
final Context context = new Context(ScriptService.class);
75+
final ScriptService scriptService = context.getService(ScriptService.class);
76+
final ScriptModule module =
77+
scriptService.run("test.rot13", ScriptModule.class.getName(), false,
78+
(Map<String, Object>) null).get();
79+
final ScriptModule scriptModule = Rot13Engine.latestModule;
80+
assertEquals(module, scriptModule);
81+
assertTrue(scriptModule instanceof ScriptModule);
82+
final ScriptInfo info = ((ScriptModule) scriptModule).getInfo();
83+
assertEquals(context, info.context());
84+
}
85+
86+
@Plugin(type = ScriptLanguage.class)
87+
public static class Rot13 extends AbstractScriptLanguage {
88+
89+
@Override
90+
public ScriptEngine getScriptEngine() {
91+
return new Rot13Engine();
92+
}
93+
94+
@Override
95+
public List<String> getNames() {
96+
return Arrays.asList("Hello", "World", "Rot13");
97+
}
98+
99+
@Override
100+
public List<String> getExtensions() {
101+
return Arrays.asList("rot13");
102+
}
103+
}
104+
105+
private static class Rot13Engine extends AbstractScriptEngine {
106+
107+
{
108+
engineScopeBindings = new Rot13Bindings();
109+
}
110+
111+
private static ScriptModule latestModule;
112+
113+
@Override
114+
public Object eval(String script) throws ScriptException {
115+
return eval(new StringReader(script));
116+
}
117+
118+
@Override
119+
public Object eval(Reader reader) throws ScriptException {
120+
latestModule = (ScriptModule) get(ScriptModule.class.getName());
121+
final StringBuilder builder = new StringBuilder();
122+
try {
123+
for (;;) {
124+
int c = reader.read();
125+
if (c < 0) {
126+
break;
127+
}
128+
if (c >= 'A' && c <= 'Z') {
129+
c = 'Z' - c + 'A';
130+
} else if (c >= 'a' && c <= 'z') {
131+
c = 'z' - c + 'a';
132+
}
133+
builder.append((char) c);
134+
}
135+
} catch (final IOException e) {
136+
throw new ScriptException(e);
137+
}
138+
return builder.toString();
139+
}
140+
}
141+
142+
private static class Rot13Bindings extends HashMap<String, Object> implements Bindings { }
143+
}

0 commit comments

Comments
 (0)