source: trunk/src/org/expeditee/actions/Javascript2.java@ 1078

Last change on this file since 1078 was 1078, checked in by davidb, 8 years ago

Refactoring to allow support for additional scriptable languages. In this case, Python being added in through Jythohn Java library

File size: 3.5 KB
Line 
1/**
2 * Javascript2.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.actions;
20
21import java.awt.Color;
22import java.util.Collection;
23import java.util.LinkedList;
24import java.util.List;
25
26import javax.script.Bindings;
27import javax.script.Invocable;
28import javax.script.ScriptContext;
29import javax.script.ScriptEngine;
30import javax.script.ScriptEngineManager;
31import javax.script.ScriptException;
32import javax.script.SimpleScriptContext;
33
34import org.expeditee.gui.Frame;
35import org.expeditee.gui.FrameIO;
36import org.expeditee.gui.FrameUtils;
37import org.expeditee.gui.MessageBay;
38import org.expeditee.io.flowlayout.XGroupItem;
39import org.expeditee.items.Item;
40import org.expeditee.items.ItemUtils;
41import org.expeditee.items.Text;
42
43/**
44 * Javascript parser.
45 * Works differently to the other Javascript class in that it
46 * parses a frame as a whole rather than parsing individual text items as separate statements
47 *
48 * @author jts21
49 */
50public class Javascript2 extends ScriptBase {
51
52 protected void init() {
53 ERROR_FRAMESET = "JavascriptErrors";
54 scriptEngineManager = new ScriptEngineManager();
55 scriptEngine = scriptEngineManager.getEngineByMimeType("application/javascript");
56 scriptEngine.put("invocable", (Invocable) scriptEngine);
57 }
58
59 public static void printJSFrame(Item item) {
60 if(item.getChild() == null) {
61 // if the user clicked on the action without an item on their cursor
62 if(item.hasAction()) {
63 boolean isThis = false;
64 for(String s : item.getAction()) {
65 if(s.equalsIgnoreCase("printJSFrame")) {
66 isThis = true;
67 break;
68 }
69 }
70 if(isThis) {
71 System.out.println(new Javascript2(item.getParentOrCurrentFrame(), true));
72 return;
73 }
74 }
75 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
76 } else {
77 System.out.println(new Javascript2(item.getChild(), true));
78 }
79 }
80
81 public static void runJSFrame(Item item) throws Exception {
82 if(item.getChild() == null) {
83 // if the user clicked on the action without an item on their cursor
84 if(item.hasAction()) {
85 boolean isThis = false;
86 for(String s : item.getAction()) {
87 if(s.equalsIgnoreCase("runJSFrame")) {
88 isThis = true;
89 break;
90 }
91 }
92 if(isThis) {
93 Javascript2.runFrameFollow(item.getParentOrCurrentFrame());
94 return;
95 }
96 }
97 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
98 } else {
99 Javascript2.runFrameFollow(item.getChild());
100 }
101 }
102
103
104 private static synchronized void runFrameFollow(Frame frame) throws Exception {
105 Javascript2 js = new Javascript2(frame, true);
106 js.runFrame(frame,true);
107 }
108
109
110 private Javascript2(Frame frame, boolean followLinks) {
111 super(frame,followLinks);
112 }
113
114}
Note: See TracBrowser for help on using the repository browser.