source: trunk/src/org/expeditee/actions/Python.java@ 1080

Last change on this file since 1080 was 1080, checked in by davidb, 7 years ago

Updates to cope with changes in Java ScriptEngine

File size: 4.5 KB
Line 
1/**
2 * Python.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 * @author davidb
50 */
51public class Python extends ScriptBase {
52
53 protected void init() {
54 ERROR_FRAMESET = "PythonErrors";
55 scriptEngine = scriptEngineManager.getEngineByMimeType("application/python");
56 scriptEngine.put("invocable", (Invocable) scriptEngine);
57 }
58
59 public static void printPythonFrame(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("printPythonFrame")) {
66 isThis = true;
67 break;
68 }
69 }
70 if(isThis) {
71 System.out.println(new Python(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 Python(item.getChild(), true));
78 }
79 }
80
81 public static void runPythonFrame(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("runPythonFrame")) {
88 isThis = true;
89 break;
90 }
91 }
92 if(isThis) {
93 Python.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 Python.runFrameFollow(item.getChild());
100 }
101 }
102
103
104 private static synchronized void runFrameFollow(Frame frame) throws Exception {
105 Python python = new Python(frame, true);
106 python.runFrame(frame,true);
107 }
108
109 private Python(Frame frame, boolean followLinks) {
110 super(frame,followLinks);
111 }
112
113
114 protected void processItems(List<Item> overlapping_y_ordered_items, boolean followLinks, int depth) {
115
116 // Loop through the items looking for code and links to new frames
117 for(Item i : overlapping_y_ordered_items) {
118 if(followLinks && i.hasLink()) {
119 Frame child = i.getChild();
120 if(child != null && !seen.contains(child)) {
121 this.parseFrame(child, true, depth);
122 }
123 }
124 if(i instanceof Text && !i.isAnnotation()) {
125 String text = ((Text)i).getText();
126 if (i == org.expeditee.io.flowlayout.XGroupItem.GROUPSEP_START) {
127 text = null;
128 depth++;
129 }
130 else if (i == org.expeditee.io.flowlayout.XGroupItem.GROUPSEP_END) {
131 text = null;
132 depth--;
133 }
134
135 if (text != null) {
136 int lineNumber = 0;
137 for(String line : text.trim().split("[\\n\\r]+")) {
138 if (depth>0) {
139 sb.append(String.format("%1$" + (4*depth) + "s", "")); // indent with 'depth' x 4 spaces
140 }
141 sb.append(line).append("\n");
142 lines.add(new CodeLine((Text)i, lineNumber++, line));
143 }
144 }
145 }
146 }
147
148
149 }
150
151}
Note: See TracBrowser for help on using the repository browser.