1 | /**
|
---|
2 | * Context.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 |
|
---|
19 | package org.expeditee.simple;
|
---|
20 |
|
---|
21 | import java.io.BufferedReader;
|
---|
22 | import java.io.BufferedWriter;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.FileOutputStream;
|
---|
25 | import java.io.FileReader;
|
---|
26 | import java.io.OutputStream;
|
---|
27 | import java.io.OutputStreamWriter;
|
---|
28 | import java.io.Writer;
|
---|
29 |
|
---|
30 | import org.expeditee.actions.Simple;
|
---|
31 | import org.expeditee.gui.Frame;
|
---|
32 | import org.expeditee.gui.FrameIO;
|
---|
33 | import org.expeditee.gui.MessageBay;
|
---|
34 | import org.expeditee.items.Text;
|
---|
35 |
|
---|
36 | public class Context {
|
---|
37 | private Primitives primitives_ = new Primitives();
|
---|
38 |
|
---|
39 | private Pointers pointers_ = new Pointers();
|
---|
40 |
|
---|
41 | public Primitives getPrimitives() {
|
---|
42 | return primitives_;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Pointers getPointers() {
|
---|
46 | return pointers_;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public Context() {
|
---|
50 | }
|
---|
51 |
|
---|
52 | public int size() {
|
---|
53 | return primitives_.size() + pointers_.size();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void clear() {
|
---|
57 | primitives_.clear();
|
---|
58 | pointers_.clear();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void display() {
|
---|
62 | if (size() == 0) {
|
---|
63 | MessageBay.errorMessage("No variables exist!");
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | primitives_.display();
|
---|
68 | pointers_.display();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | *
|
---|
73 | * @param var1
|
---|
74 | * @param var2
|
---|
75 | * @return
|
---|
76 | * @throws Exception
|
---|
77 | * if the variables could not be converted to the same type.
|
---|
78 | */
|
---|
79 | public boolean equalValues(String var1, String var2) throws Exception {
|
---|
80 | if (Primitives.isPrimitive(var1) && Primitives.isPrimitive(var2)) {
|
---|
81 | return primitives_.equalValues(var1, var2);
|
---|
82 | } else if (Pointers.isPointer(var1) && Pointers.isPointer(var2)) {
|
---|
83 | throw new UnsupportedOperationException(
|
---|
84 | "Pointer comparison not yet implemented");
|
---|
85 | }
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void readFrame(String frameNameVar, String frameVar,
|
---|
90 | String successVar) throws Exception {
|
---|
91 | // Get the values to be set
|
---|
92 | String frameName = primitives_.getStringValue(frameNameVar);
|
---|
93 | Frame currentFrame = FrameIO.LoadFrame(frameName);
|
---|
94 |
|
---|
95 | pointers_.setObject(frameVar, currentFrame);
|
---|
96 |
|
---|
97 | if (successVar != null) {
|
---|
98 | Boolean success = currentFrame != null;
|
---|
99 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void createFrame(String framesetNameVar, String frameVar,
|
---|
104 | String successVar) throws Exception {
|
---|
105 | // Get the values to be set
|
---|
106 | String framesetName = primitives_.getStringValue(framesetNameVar);
|
---|
107 | Frame currentFrame = FrameIO.CreateFrame(framesetName, "", null);
|
---|
108 | pointers_.setObject(frameVar, currentFrame);
|
---|
109 |
|
---|
110 | if (successVar != null) {
|
---|
111 | Boolean success = currentFrame != null;
|
---|
112 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void closeFrame(String frameVar, String successVar) throws Exception {
|
---|
117 | // Get the values to be set
|
---|
118 | Frame frame = (Frame) pointers_.getVariable(frameVar).getValue();
|
---|
119 | frame.change();
|
---|
120 | String contents = FrameIO.SaveFrame(frame, false);
|
---|
121 |
|
---|
122 | if (successVar != null) {
|
---|
123 | Boolean success = contents != null;
|
---|
124 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public void closeWriteFile(String fileVar) throws Exception {
|
---|
129 | BufferedWriter bw = (BufferedWriter) pointers_.getVariable(fileVar)
|
---|
130 | .getValue();
|
---|
131 | bw.close();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void closeReadFile(String fileVar) throws Exception {
|
---|
135 | BufferedReader br = (BufferedReader) pointers_.getVariable(fileVar)
|
---|
136 | .getValue();
|
---|
137 | br.close();
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void writeFile(String fileVar, String text) throws Exception {
|
---|
141 | BufferedWriter bw = (BufferedWriter) pointers_.getVariable(fileVar)
|
---|
142 | .getValue();
|
---|
143 | bw.write(text);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void openWriteFile(String fileNameVar, String fileVar)
|
---|
147 | throws Exception {
|
---|
148 | openWriteFile(fileNameVar, fileVar, null);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void openWriteFile(String fileNameVar, String fileVar,
|
---|
152 | String successVar) throws Exception {
|
---|
153 | // Get the values to be set
|
---|
154 | File filePath = new File(primitives_.getStringValue(fileNameVar));
|
---|
155 |
|
---|
156 | // Open an Output Stream Writer to set encoding
|
---|
157 | OutputStream fout = new FileOutputStream(filePath);
|
---|
158 | Writer out = new OutputStreamWriter(fout, "UTF-8");
|
---|
159 | BufferedWriter currentFile = new BufferedWriter(
|
---|
160 | out);
|
---|
161 |
|
---|
162 | pointers_.setObject(fileVar, currentFile);
|
---|
163 |
|
---|
164 | if (successVar != null) {
|
---|
165 | Boolean success = currentFile != null;
|
---|
166 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void readLineFile(String fileVar, String textVar) throws Exception {
|
---|
171 | readLineFile(fileVar, fileVar, null);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public void readLineFile(String fileVar, String textVar, String successVar)
|
---|
175 | throws Exception {
|
---|
176 |
|
---|
177 | BufferedReader br = (BufferedReader) pointers_.getVariable(fileVar)
|
---|
178 | .getValue();
|
---|
179 |
|
---|
180 | String text = br.readLine();
|
---|
181 | boolean success = text != null;
|
---|
182 | primitives_.setValue(textVar, text);
|
---|
183 |
|
---|
184 | if (successVar != null) {
|
---|
185 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void readItemFile(String fileVar, String itemVar) throws Exception {
|
---|
190 | readItemFile(fileVar, fileVar, null);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void readItemFile(String fileVar, String itemVar, String successVar)
|
---|
194 | throws Exception {
|
---|
195 |
|
---|
196 | BufferedReader br = (BufferedReader) pointers_.getVariable(fileVar)
|
---|
197 | .getValue();
|
---|
198 | boolean success = true;
|
---|
199 | try {
|
---|
200 | Text item = (Text) pointers_.getVariable(itemVar).getValue();
|
---|
201 | String nextLine = br.readLine();
|
---|
202 | String text = nextLine;
|
---|
203 | success = (text != null);
|
---|
204 | while ((nextLine = br.readLine()) != null)
|
---|
205 | text += '\n' + nextLine;
|
---|
206 | item.setText(text);
|
---|
207 | } catch (Exception e) {
|
---|
208 | success = false;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (successVar != null) {
|
---|
212 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void openReadFile(String fileNameVar, String fileVar)
|
---|
217 | throws Exception {
|
---|
218 | openReadFile(fileNameVar, fileVar, null);
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void openReadFile(String fileNameVar, String fileVar,
|
---|
222 | String successVar) throws Exception {
|
---|
223 | // Get the values to be set
|
---|
224 | File filePath = new File(primitives_.getStringValue(fileNameVar));
|
---|
225 | Boolean success = true;
|
---|
226 | BufferedReader currentFile = null;
|
---|
227 | try {
|
---|
228 | currentFile = new BufferedReader(new FileReader(filePath));
|
---|
229 | } catch (Exception e) {
|
---|
230 | success = false;
|
---|
231 | }
|
---|
232 | pointers_.setObject(fileVar, currentFile);
|
---|
233 |
|
---|
234 | if (successVar != null) {
|
---|
235 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | public boolean isNull(String varName) {
|
---|
240 | try {
|
---|
241 | Object o = null;
|
---|
242 | if (Primitives.isPrimitive(varName)) {
|
---|
243 | SPrimitive sp = getPrimitives().getVariable(varName);
|
---|
244 | if(sp == null)
|
---|
245 | o = sp;
|
---|
246 | else
|
---|
247 | o = sp.getValue();
|
---|
248 | } else {
|
---|
249 | o = getPointers().getVariable(varName);
|
---|
250 | }
|
---|
251 | return o == null;
|
---|
252 | } catch (Exception e) {
|
---|
253 | return false;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | public boolean isDefined(String varName) {
|
---|
258 | try {
|
---|
259 | if (Primitives.isPrimitive(varName)) {
|
---|
260 | getPrimitives().getVariable(varName);
|
---|
261 | } else {
|
---|
262 | getPointers().getVariable(varName);
|
---|
263 | }
|
---|
264 | return true;
|
---|
265 | } catch (Exception e) {
|
---|
266 | return false;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | public void createFrameset(String framesetNameVar, String successVar)
|
---|
271 | throws IncorrectTypeException {
|
---|
272 | boolean success = true;
|
---|
273 | String framesetName = "frameset";
|
---|
274 | try {
|
---|
275 | // Get the values to be set
|
---|
276 | framesetName = primitives_.getStringValue(framesetNameVar);
|
---|
277 | Frame firstFrame = FrameIO.CreateNewFrameset(framesetName, null);
|
---|
278 | success = firstFrame != null;
|
---|
279 | } catch (Exception e) {
|
---|
280 | success = false;
|
---|
281 | }
|
---|
282 | if (!success && Simple.isVerbose())
|
---|
283 | MessageBay.warningMessage("Error creating " + framesetName);
|
---|
284 | if (successVar != null) {
|
---|
285 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
286 | }
|
---|
287 |
|
---|
288 | }
|
---|
289 |
|
---|
290 | public boolean searchItem(Text itemToSearch, String pattern,
|
---|
291 | String itemVar, String startVar, String endVar,
|
---|
292 | String replacementString) throws Exception {
|
---|
293 | String searchStr = itemToSearch.getText();
|
---|
294 | String[] result = searchStr.split(pattern, 2);
|
---|
295 | boolean bFound = result.length > 1;
|
---|
296 | if (bFound) {
|
---|
297 | if (itemVar != null)
|
---|
298 | getPointers().setObject(itemVar, itemToSearch);
|
---|
299 | if (startVar != null) {
|
---|
300 | getPrimitives().setValue(startVar,
|
---|
301 | new SInteger(result[0].length() + 1));
|
---|
302 | assert (endVar != null);
|
---|
303 | getPrimitives().setValue(endVar,
|
---|
304 | new SInteger(searchStr.length() - result[1].length()));
|
---|
305 | }
|
---|
306 | // If it is a find and replace... then replace with the replacement
|
---|
307 | // string
|
---|
308 | if (replacementString != null) {
|
---|
309 | itemToSearch.setText(searchStr.replaceAll(pattern,
|
---|
310 | replacementString));
|
---|
311 | }
|
---|
312 | }
|
---|
313 | return bFound;
|
---|
314 | }
|
---|
315 | } |
---|