source: trunk/src/org/expeditee/simple/Context.java@ 362

Last change on this file since 362 was 362, checked in by ra33, 16 years ago

Added Spell Checker
Added word count stats
Fixed some mail stuff

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