1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.BufferedWriter;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.FileReader;
|
---|
9 | import java.io.FileWriter;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.util.HashMap;
|
---|
12 |
|
---|
13 | import org.expeditee.agents.ExistingFramesetException;
|
---|
14 | import org.expeditee.io.Conversion;
|
---|
15 | import org.expeditee.io.ExpReader;
|
---|
16 | import org.expeditee.io.ExpWriter;
|
---|
17 | import org.expeditee.io.FrameReader;
|
---|
18 | import org.expeditee.io.FrameWriter;
|
---|
19 | import org.expeditee.io.KMSReader;
|
---|
20 | import org.expeditee.io.KMSWriter;
|
---|
21 | import org.expeditee.io.Logger;
|
---|
22 | import org.expeditee.items.Item;
|
---|
23 | import org.expeditee.items.ItemUtils;
|
---|
24 | import org.expeditee.items.Text;
|
---|
25 | import org.expeditee.stats.SessionStats;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This class provides static methods for all saving and loading of Frames
|
---|
29 | * to\from disk. This class also handles any caching of previously loaded
|
---|
30 | * Frames.
|
---|
31 | *
|
---|
32 | * @author jdm18
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class FrameIO {
|
---|
36 |
|
---|
37 | private static final char FRAME_NAME_LAST_CHAR = 'A';
|
---|
38 |
|
---|
39 | public static void changeParentFolder(String newFolder) {
|
---|
40 | PARENT_FOLDER = newFolder;
|
---|
41 | FRAME_PATH = PARENT_FOLDER + "framesets" + File.separator;
|
---|
42 | TRASH_PATH = PARENT_FOLDER + "trash" + File.separator;
|
---|
43 | IMAGES_PATH = PARENT_FOLDER + "images" + File.separator;
|
---|
44 | HELP_PATH = PARENT_FOLDER + "help" + File.separator;
|
---|
45 | PROFILE_PATH = PARENT_FOLDER + "profiles" + File.separator;
|
---|
46 | EXPORTS_DIR = PARENT_FOLDER + "exports" + File.separator;
|
---|
47 | STATISTICS_DIR = PARENT_FOLDER + "statistics" + File.separator;
|
---|
48 | LOGS_DIR = PARENT_FOLDER + "logs" + File.separator;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * The default location for storing the framesets. Each frameset has its own
|
---|
53 | * subdirectory in this directory.
|
---|
54 | */
|
---|
55 |
|
---|
56 | public static String TRASH_PATH;
|
---|
57 |
|
---|
58 | public static String PARENT_FOLDER;
|
---|
59 |
|
---|
60 | public static String FRAME_PATH;
|
---|
61 |
|
---|
62 | public static String IMAGES_PATH;
|
---|
63 |
|
---|
64 | public static String HELP_PATH;
|
---|
65 |
|
---|
66 | public static String PROFILE_PATH;
|
---|
67 |
|
---|
68 | public static String EXPORTS_DIR;
|
---|
69 |
|
---|
70 | public static String STATISTICS_DIR;
|
---|
71 |
|
---|
72 | public static String LOGS_DIR;
|
---|
73 |
|
---|
74 | private static final String INF_FILENAME = "frame.inf";
|
---|
75 |
|
---|
76 | public static final String ILLEGAL_CHARS = ";:\\/?";
|
---|
77 |
|
---|
78 | public static final int MAX_NAME_LENGTH = 64;
|
---|
79 |
|
---|
80 | public static final int MAX_CACHE = 2000;
|
---|
81 |
|
---|
82 | private static HashMap<String, Frame> _Cache = new HashMap<String, Frame>();
|
---|
83 |
|
---|
84 | // private static HashMap<String, String> _FramesetNameCache = new
|
---|
85 | // HashMap<String, String>();
|
---|
86 |
|
---|
87 | private static boolean ENABLE_CACHE = true;
|
---|
88 |
|
---|
89 | private static boolean _UseCache = true;
|
---|
90 |
|
---|
91 | private static boolean _SuspendedCache = false;
|
---|
92 |
|
---|
93 | // All methods are static, this should not be instantiated
|
---|
94 | private FrameIO() {
|
---|
95 | }
|
---|
96 |
|
---|
97 | public static boolean isCacheOn() {
|
---|
98 | return _UseCache && ENABLE_CACHE;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public static void Precache(String framename) {
|
---|
102 | // if the cache is turned off, do nothing
|
---|
103 | if (!isCacheOn())
|
---|
104 | return;
|
---|
105 |
|
---|
106 | // if the frame is already in the cache, do nothing
|
---|
107 | if (_Cache.containsKey(framename.toLowerCase()))
|
---|
108 | return;
|
---|
109 |
|
---|
110 | // otherwise, load the frame and put it in the cache
|
---|
111 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Precaching " + framename + ".");
|
---|
112 |
|
---|
113 | // do not display errors encountered to the user
|
---|
114 | // (they will be shown at load time)
|
---|
115 | FrameGraphics.SupressErrors(true);
|
---|
116 | // loading automatically caches the frame is caching is turned on
|
---|
117 | LoadUnknownPath(framename);
|
---|
118 | FrameGraphics.SupressErrors(false);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Checks if a string is a representation of a positive integer.
|
---|
123 | *
|
---|
124 | * @param s
|
---|
125 | * @return true if s is a positive integer
|
---|
126 | */
|
---|
127 | public static boolean isPositiveInteger(String s) {
|
---|
128 | if (s == null || s.length() == 0)
|
---|
129 | return false;
|
---|
130 |
|
---|
131 | for (int i = 0; i < s.length(); i++) {
|
---|
132 | if (!Character.isDigit(s.charAt(i)))
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public static Frame LoadFrame(String frameName) {
|
---|
139 | // If the frame name is a positive integer add the frameset name
|
---|
140 | if (isPositiveInteger(frameName)) {
|
---|
141 | assert (false);
|
---|
142 | frameName = DisplayIO.getCurrentFrame().getFramesetName()
|
---|
143 | + frameName;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // first try reading from cache
|
---|
147 | if (isCacheOn() && _Cache.containsKey(frameName.toLowerCase())) {
|
---|
148 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
|
---|
149 | + " from cache.");
|
---|
150 | return _Cache.get(frameName.toLowerCase());
|
---|
151 | }
|
---|
152 |
|
---|
153 | Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
|
---|
154 | + " from disk.");
|
---|
155 | return LoadUnknownPath(frameName);
|
---|
156 | }
|
---|
157 |
|
---|
158 | private static Frame LoadUnknownPath(String framename) {
|
---|
159 | Frame loaded = null;
|
---|
160 |
|
---|
161 | for (String path : UserSettings.FrameDirs) {
|
---|
162 | loaded = LoadFrame(path, framename);
|
---|
163 | if (loaded != null) {
|
---|
164 | FrameUtils.Parse(loaded, true);
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | return loaded;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Gets a list of all the framesets available to the user
|
---|
174 | *
|
---|
175 | * @return a string containing a list of all the available framesets on
|
---|
176 | * separate lines
|
---|
177 | */
|
---|
178 | public static String getFramesetList() {
|
---|
179 | StringBuffer list = new StringBuffer();
|
---|
180 |
|
---|
181 | for (String path : UserSettings.FrameDirs) {
|
---|
182 | for (File f : (new File(path)).listFiles()) {
|
---|
183 | if (f.isDirectory()) {
|
---|
184 | list.append(f.getName()).append('\n');
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | // remove the final new line char
|
---|
189 | list.deleteCharAt(list.length() - 1);
|
---|
190 | return list.toString();
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Gets the full path and file name of the frame.
|
---|
195 | *
|
---|
196 | * @param path-
|
---|
197 | * the directory in which to look for the frameset containing the
|
---|
198 | * frame.
|
---|
199 | * @param frameName-
|
---|
200 | * the name of the frame for which the path is being requested.
|
---|
201 | * @return null if the frame can not be located.
|
---|
202 | */
|
---|
203 | public static String getFrameFullPathName(String path, String frameName) {
|
---|
204 | String source = path + Conversion.getFrameset(frameName)
|
---|
205 | + File.separator;
|
---|
206 |
|
---|
207 | File tester = new File(source);
|
---|
208 | if (!tester.exists())
|
---|
209 | return null;
|
---|
210 |
|
---|
211 | // check for the new file name format
|
---|
212 | String fullPath = source + Conversion.getFrameNumber(frameName)
|
---|
213 | + ExpReader.EXTENTION;
|
---|
214 | tester = new File(fullPath);
|
---|
215 |
|
---|
216 | if (tester.exists())
|
---|
217 | return fullPath;
|
---|
218 |
|
---|
219 | // check for oldfile name format
|
---|
220 | fullPath = source + Conversion.getFrameset(frameName) + "."
|
---|
221 | + Conversion.getFrameNumber(frameName);
|
---|
222 | tester = new File(fullPath);
|
---|
223 |
|
---|
224 | if (tester.exists())
|
---|
225 | return fullPath;
|
---|
226 |
|
---|
227 | return null;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public static boolean DoesFrameExist(String frameName) {
|
---|
231 | for (String path : UserSettings.FrameDirs) {
|
---|
232 | if (getFrameFullPathName(path, frameName) != null)
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 |
|
---|
238 | private static Frame LoadFrame(String path, String frameName) {
|
---|
239 | String fullPath = getFrameFullPathName(path, frameName);
|
---|
240 | if (fullPath == null)
|
---|
241 | return null;
|
---|
242 |
|
---|
243 | try {
|
---|
244 | FrameReader reader;
|
---|
245 |
|
---|
246 | if (fullPath.endsWith(ExpReader.EXTENTION)) {
|
---|
247 | reader = new ExpReader(frameName);
|
---|
248 | } else {
|
---|
249 | reader = new KMSReader();
|
---|
250 | }
|
---|
251 | Frame frame = reader.readFrame(fullPath);
|
---|
252 |
|
---|
253 | if (frame == null) {
|
---|
254 | FrameGraphics.ErrorMessage("Error: " + frameName
|
---|
255 | + " could not be successfully loaded.");
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | frame.path = path;
|
---|
260 |
|
---|
261 | // do not put 0 frames or virtual frames into the cache
|
---|
262 | if (_Cache.size() > MAX_CACHE)
|
---|
263 | _Cache.clear();
|
---|
264 |
|
---|
265 | if (frame.getFrameNumber() > 0 && isCacheOn())
|
---|
266 | _Cache.put(frameName.toLowerCase(), frame);
|
---|
267 |
|
---|
268 | return frame;
|
---|
269 | } catch (IOException ioe) {
|
---|
270 | ioe.printStackTrace();
|
---|
271 | Logger.Log(ioe);
|
---|
272 | } catch (Exception e) {
|
---|
273 | e.printStackTrace();
|
---|
274 | Logger.Log(e);
|
---|
275 | FrameGraphics.ErrorMessage("Error: " + frameName
|
---|
276 | + " could not be successfully loaded.");
|
---|
277 | }
|
---|
278 |
|
---|
279 | return null;
|
---|
280 | }
|
---|
281 |
|
---|
282 | public static void Reload() {
|
---|
283 | // disable cache
|
---|
284 | boolean cache = _UseCache;
|
---|
285 |
|
---|
286 | _UseCache = false;
|
---|
287 | Frame fresh = FrameIO.LoadFrame(DisplayIO.getCurrentFrame()
|
---|
288 | .getFrameName());
|
---|
289 | _UseCache = cache;
|
---|
290 | if (_Cache.containsKey(fresh.getFrameName().toLowerCase()))
|
---|
291 | _Cache.put(fresh.getFrameName().toLowerCase(), fresh);
|
---|
292 | DisplayIO.setCurrentFrame(fresh);
|
---|
293 | }
|
---|
294 |
|
---|
295 | public static Frame LoadPrevious(Frame current) {
|
---|
296 | checkTDFC(current);
|
---|
297 |
|
---|
298 | // the current name and number
|
---|
299 | String name = current.getFramesetName();
|
---|
300 | int num = current.getFrameNumber() - 1;
|
---|
301 |
|
---|
302 | // loop until a frame that exists is found
|
---|
303 | for (; num >= 0; num--) {
|
---|
304 | Frame f = LoadFrame(name + num);
|
---|
305 | if (f != null)
|
---|
306 | return f;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // if we did not find another Frame then this one must be the last one
|
---|
310 | // in the frameset
|
---|
311 | FrameGraphics.DisplayMessage("This is the first frame in the frameset");
|
---|
312 | return null;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Returns the next Frame in the current Frameset (The Frame with the next
|
---|
317 | * highest Frame number) If the current Frame is the last one in the
|
---|
318 | * Frameset, or an error occurs then null is returned.
|
---|
319 | *
|
---|
320 | * @return The Frame after this one in the current frameset, or null
|
---|
321 | */
|
---|
322 | public static Frame LoadNext(Frame current) {
|
---|
323 | checkTDFC(current);
|
---|
324 |
|
---|
325 | // the current name and number
|
---|
326 | int num = current.getFrameNumber() + 1;
|
---|
327 | int max = num + 1;
|
---|
328 | String name = current.getFramesetName();
|
---|
329 |
|
---|
330 | // read the maximum from the INF file
|
---|
331 | try {
|
---|
332 | max = ReadINF(current.path, current.getFramesetName());
|
---|
333 | } catch (IOException ioe) {
|
---|
334 | FrameGraphics.ErrorMessage("Error loading INF file for frameset '"
|
---|
335 | + name + "'");
|
---|
336 | return null;
|
---|
337 | }
|
---|
338 |
|
---|
339 | // loop until a frame that exists is found
|
---|
340 | for (; num <= max; num++) {
|
---|
341 | Frame f = LoadFrame(name + num);
|
---|
342 | if (f != null)
|
---|
343 | return f;
|
---|
344 | }
|
---|
345 |
|
---|
346 | // if we did not find another Frame then this one must be the last one
|
---|
347 | // in the frameset
|
---|
348 | FrameGraphics.DisplayMessage("This is the last frame in the frameset");
|
---|
349 | return null;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * This method checks if the current frame has just been created with TDFC.
|
---|
354 | * If it has the frame is saved regardless of whether it has been edited or
|
---|
355 | * not and the TDFC item property is cleared. This is to ensure that the
|
---|
356 | * link is saved on the parent frame.
|
---|
357 | *
|
---|
358 | * @param current
|
---|
359 | */
|
---|
360 | private static void checkTDFC(Frame current) {
|
---|
361 | if (FrameMouseActions.getTdfcItem() != null) {
|
---|
362 | FrameMouseActions.setTdfcItem(null);
|
---|
363 | current.change();
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | public static Frame LoadLast(String framesetName, String path) {
|
---|
368 | // read the maximum from the INF file
|
---|
369 | int max;
|
---|
370 | try {
|
---|
371 | max = ReadINF(path, framesetName);
|
---|
372 | } catch (IOException ioe) {
|
---|
373 | FrameGraphics.ErrorMessage("Error loading INF file for frameset '"
|
---|
374 | + framesetName + "'");
|
---|
375 | return null;
|
---|
376 | }
|
---|
377 |
|
---|
378 | // loop backwards until a frame that exists is found
|
---|
379 | for (int num = max; num > 0; num--) {
|
---|
380 | Frame f = LoadFrame(framesetName + num);
|
---|
381 | if (f != null)
|
---|
382 | return f;
|
---|
383 | }
|
---|
384 |
|
---|
385 | // if we did not find another Frame then this one must be the last one
|
---|
386 | // in the frameset
|
---|
387 | FrameGraphics.DisplayMessage("This is the last frame in the frameset");
|
---|
388 | return null;
|
---|
389 | }
|
---|
390 |
|
---|
391 | public static Frame LoadZero(String framesetName, String path) {
|
---|
392 | return LoadFrame(framesetName + 0);
|
---|
393 | }
|
---|
394 |
|
---|
395 | public static Frame LoadZero() {
|
---|
396 | Frame current = DisplayIO.getCurrentFrame();
|
---|
397 | return LoadZero(current.getFramesetName(), current.path);
|
---|
398 | }
|
---|
399 |
|
---|
400 | public static Frame LoadLast() {
|
---|
401 | Frame current = DisplayIO.getCurrentFrame();
|
---|
402 | return LoadLast(current.getFramesetName(), current.path);
|
---|
403 | }
|
---|
404 |
|
---|
405 | public static Frame LoadNext() {
|
---|
406 | return LoadNext(DisplayIO.getCurrentFrame());
|
---|
407 | }
|
---|
408 |
|
---|
409 | public static Frame LoadPrevious() {
|
---|
410 | return LoadPrevious(DisplayIO.getCurrentFrame());
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Deletes the given Frame on disk and removes the cached Frame if there is
|
---|
415 | * one. Also adds the deleted frame into the deletedFrames frameset.
|
---|
416 | *
|
---|
417 | * @param toDelete
|
---|
418 | * The Frame to be deleted
|
---|
419 | * @return The result of File.delete()
|
---|
420 | */
|
---|
421 | public static boolean DeleteFrame(Frame toDelete) throws IOException {
|
---|
422 | if (toDelete == null)
|
---|
423 | return false;
|
---|
424 |
|
---|
425 | SaveFrame(toDelete);
|
---|
426 |
|
---|
427 | // get the last used frame in the destination frameset
|
---|
428 | final String DELETED_FRAMES = "DeletedFrames";
|
---|
429 | int lastNumber = FrameIO.getLastNumber(DELETED_FRAMES);
|
---|
430 | String framePath;
|
---|
431 | try {
|
---|
432 | // create the new frameset
|
---|
433 | Frame one = FrameIO.CreateFrameset(DELETED_FRAMES, toDelete.path);
|
---|
434 | framePath = one.path;
|
---|
435 | lastNumber = 0;
|
---|
436 | } catch (Exception e) {
|
---|
437 | Frame zero = FrameIO.LoadFrame(DELETED_FRAMES + "0");
|
---|
438 | framePath = zero.path;
|
---|
439 | }
|
---|
440 |
|
---|
441 | // get the fill path to determine which file version it is
|
---|
442 | String source = getFrameFullPathName(toDelete.path, toDelete
|
---|
443 | .getFrameName());
|
---|
444 |
|
---|
445 | String oldFrameName = toDelete.getFrameName().toLowerCase();
|
---|
446 | // Now save the frame in the new location
|
---|
447 | toDelete.setFrameset(DELETED_FRAMES);
|
---|
448 | toDelete.setFrameNumber(lastNumber + 1);
|
---|
449 | toDelete.path = framePath;
|
---|
450 | FrameIO.SaveFrame(toDelete);
|
---|
451 |
|
---|
452 | if (_Cache.containsKey(oldFrameName))
|
---|
453 | _Cache.remove(oldFrameName);
|
---|
454 |
|
---|
455 | File del = new File(source);
|
---|
456 |
|
---|
457 | java.io.FileInputStream ff = new java.io.FileInputStream(del);
|
---|
458 | ff.close();
|
---|
459 |
|
---|
460 | return del.delete();
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Creates a new Frame in the given frameset and assigns it the given Title,
|
---|
465 | * which can be null. The newly created Frame is a copy of the frameset's .0
|
---|
466 | * file with the number updated based on the last recorded Frame name in the
|
---|
467 | * frameset's INF file.
|
---|
468 | *
|
---|
469 | * @param frameset
|
---|
470 | * The frameset to create the new Frame in
|
---|
471 | * @param frameTitle
|
---|
472 | * The title to assign to the newly created Frame (can be NULL).
|
---|
473 | * @return The newly created Frame.
|
---|
474 | */
|
---|
475 | public static Frame CreateFrame(String frameset, String frameTitle,
|
---|
476 | String templateFrame) {
|
---|
477 |
|
---|
478 | int next = -1;
|
---|
479 |
|
---|
480 | // disable caching of 0 frames
|
---|
481 | SuspendCache();
|
---|
482 |
|
---|
483 | Frame template = null;
|
---|
484 | if (templateFrame == null)
|
---|
485 | // load in frame.0
|
---|
486 | template = LoadFrame(frameset + "0");
|
---|
487 | else
|
---|
488 | template = LoadFrame(templateFrame);
|
---|
489 |
|
---|
490 | ResumeCache();
|
---|
491 |
|
---|
492 | // read the next number from the INF file
|
---|
493 | try {
|
---|
494 | next = ReadINF(template.path, frameset);
|
---|
495 | } catch (IOException ioe) {
|
---|
496 | ioe.printStackTrace();
|
---|
497 | Logger.Log(ioe);
|
---|
498 | return null;
|
---|
499 | }
|
---|
500 |
|
---|
501 | // set the number and title of the new frame
|
---|
502 | template.setFrameNumber(++next);
|
---|
503 | template.setTitle(frameTitle);
|
---|
504 |
|
---|
505 | Logger.Log(Logger.SYSTEM, Logger.TDFC, "Creating new frame: "
|
---|
506 | + template.getFrameName() + " from TDFC");
|
---|
507 |
|
---|
508 | // update INF file
|
---|
509 | try {
|
---|
510 | WriteINF(template.path, frameset, frameset + next);
|
---|
511 | } catch (IOException ioe) {
|
---|
512 | ioe.printStackTrace();
|
---|
513 | Logger.Log(ioe);
|
---|
514 | }
|
---|
515 |
|
---|
516 | template.setOwner(UserSettings.Username);
|
---|
517 | template.resetDateCreated();
|
---|
518 | for (Item i : template.getItems()) {
|
---|
519 | if (ItemUtils.isTag(i, ItemUtils.TAG_PARENT))
|
---|
520 | i.setLink(null);
|
---|
521 | }
|
---|
522 |
|
---|
523 | return template;
|
---|
524 | }
|
---|
525 |
|
---|
526 | public static void DisableCache() {
|
---|
527 | _UseCache = false;
|
---|
528 | }
|
---|
529 |
|
---|
530 | public static void EnableCache() {
|
---|
531 | _UseCache = true;
|
---|
532 | }
|
---|
533 |
|
---|
534 | public static void SuspendCache() {
|
---|
535 | if (_UseCache) {
|
---|
536 | DisableCache();
|
---|
537 | _SuspendedCache = true;
|
---|
538 | } else {
|
---|
539 | _SuspendedCache = false;
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 | public static void ResumeCache() {
|
---|
544 | if (_SuspendedCache) {
|
---|
545 | EnableCache();
|
---|
546 | _SuspendedCache = false;
|
---|
547 | }
|
---|
548 | }
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Creates a new frameset using the given name. This includes creating a new
|
---|
552 | * subdirectory in the <code>FRAME_PATH</code> directory, Copying over the
|
---|
553 | * default.0 frame from the default frameset, copying the .0 Frame to make a
|
---|
554 | * .1 Frame, and creating the frameset's INF file.
|
---|
555 | *
|
---|
556 | * @param frameset
|
---|
557 | * The name of the Frameset to create
|
---|
558 | * @return The first Frame of the new Frameset (Frame.1)
|
---|
559 | */
|
---|
560 | public static Frame CreateFrameset(String frameset, String path)
|
---|
561 | throws Exception {
|
---|
562 | return CreateFrameset(frameset, path, false);
|
---|
563 | }
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Tests if the given String is a 'proper' framename, that is, the String
|
---|
567 | * must begin with a character, end with a number with 0 or more letters and
|
---|
568 | * numbers in between.
|
---|
569 | *
|
---|
570 | * @param frameName
|
---|
571 | * The String to test for validity as a frame name
|
---|
572 | * @return True if the given framename is proper, false otherwise.
|
---|
573 | */
|
---|
574 | public static boolean isValidFrameName(String frameName) {
|
---|
575 |
|
---|
576 | if (frameName == null || frameName.length() < 2)
|
---|
577 | return false;
|
---|
578 |
|
---|
579 | int lastCharIndex = frameName.length() - 1;
|
---|
580 | // String must begin with a letter and end with a digit
|
---|
581 | if (!Character.isLetter(frameName.charAt(0))
|
---|
582 | || !Character.isDigit(frameName.charAt(lastCharIndex)))
|
---|
583 | return false;
|
---|
584 |
|
---|
585 | // All the characters between first and last must be letters
|
---|
586 | // or digits
|
---|
587 | for (int i = 1; i < lastCharIndex; i++) {
|
---|
588 | if (!Character.isLetterOrDigit(frameName.charAt(i)))
|
---|
589 | return false;
|
---|
590 | }
|
---|
591 | return true;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Saves the given Frame to disk in the corresponding frameset directory.
|
---|
596 | * This is the same as calling SaveFrame(toSave, true)
|
---|
597 | *
|
---|
598 | * @param toSave
|
---|
599 | * The Frame to save to disk in KMS format.
|
---|
600 | */
|
---|
601 | public static String SaveFrame(Frame toSave) {
|
---|
602 | return SaveFrame(toSave, true);
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Saves the given Frame to disk in the corresponding frameset directory, if
|
---|
607 | * inc is true then the saved frames counter is incremented, otherwise it is
|
---|
608 | * untouched.
|
---|
609 | *
|
---|
610 | * @param toSave
|
---|
611 | * The Frame to save to disk in KMS format.
|
---|
612 | * @param inc
|
---|
613 | * True if the saved frames counter should be incremented, false
|
---|
614 | * otherwise.
|
---|
615 | */
|
---|
616 | public static String SaveFrame(Frame toSave, boolean inc) {
|
---|
617 |
|
---|
618 | if (toSave == null)
|
---|
619 | return "";
|
---|
620 |
|
---|
621 | if (!toSave.hasChanged() || toSave.isSaved())
|
---|
622 | return "";
|
---|
623 |
|
---|
624 | // Get the full path only to determine which format to use for saving
|
---|
625 | // the frame
|
---|
626 | // At this stage use Exp format for saving Exp frames only.
|
---|
627 | // Later this will be changed so that KMS frames will be updated to the
|
---|
628 | // Exp format.
|
---|
629 | String fullPath = getFrameFullPathName(toSave.path, toSave
|
---|
630 | .getFrameName());
|
---|
631 |
|
---|
632 | FrameWriter writer = null;
|
---|
633 | try {
|
---|
634 | // if its a new frame or an existing Exp frame...
|
---|
635 | if (fullPath == null || fullPath.endsWith(ExpReader.EXTENTION)) {
|
---|
636 | writer = new ExpWriter();
|
---|
637 | } else {
|
---|
638 | writer = new KMSWriter();
|
---|
639 | }
|
---|
640 |
|
---|
641 | if (toSave.getFrameNumber() > 0
|
---|
642 | && ItemUtils.ContainsTag(toSave.getItems(),
|
---|
643 | ItemUtils.TAG_BACKUP)) {
|
---|
644 | SuspendCache();
|
---|
645 | Frame original = LoadFrame(toSave.getFrameName());
|
---|
646 | if (original == null)
|
---|
647 | original = toSave;
|
---|
648 | int orignum = original.getFrameNumber();
|
---|
649 | int nextnum = ReadINF(toSave.path, toSave.getFramesetName()) + 1;
|
---|
650 |
|
---|
651 | original.setFrameNumber(-nextnum);
|
---|
652 | SaveFrame(original, inc);
|
---|
653 |
|
---|
654 | Item i = ItemUtils.FindTag(toSave.getItems(),
|
---|
655 | ItemUtils.TAG_BACKUP);
|
---|
656 | i.setLink(original.getFrameName());
|
---|
657 | toSave.setFrameNumber(orignum);
|
---|
658 | ResumeCache();
|
---|
659 | } else if (toSave.getFrameNumber() < 0) {
|
---|
660 | toSave.setFrameNumber(-toSave.getFrameNumber());
|
---|
661 | }
|
---|
662 |
|
---|
663 | toSave.setLastModifyDate(Logger.EasyDateFormat("ddMMMyyyy:HHmm"));
|
---|
664 | toSave.setLastModifyUser(UserSettings.Username);
|
---|
665 | writer.writeFrame(toSave);
|
---|
666 | toSave.setSaved();
|
---|
667 | if (inc)
|
---|
668 | SessionStats.SavedFrame(toSave.getFrameName());
|
---|
669 |
|
---|
670 | // avoid out-of-sync frames (when in TwinFrames mode)
|
---|
671 | if (_Cache.containsKey(toSave.getFrameName().toLowerCase()))
|
---|
672 | _Cache.put(toSave.getFrameName().toLowerCase(), toSave);
|
---|
673 |
|
---|
674 | Logger.Log(Logger.SYSTEM, Logger.SAVE, "Saving "
|
---|
675 | + toSave.getFrameName() + " to disk.");
|
---|
676 |
|
---|
677 | // check that the INF file is not out of date
|
---|
678 | int last = ReadINF(toSave.path, toSave.getFramesetName());
|
---|
679 | if (last <= toSave.getFrameNumber())
|
---|
680 | WriteINF(toSave.path, toSave.getFramesetName(), toSave
|
---|
681 | .getFrameName());
|
---|
682 |
|
---|
683 | // check if this was the profile frame (and thus needs
|
---|
684 | // re-parsing)
|
---|
685 | if (toSave.getFramesetName().toLowerCase().equals(
|
---|
686 | UserSettings.Username.toLowerCase())) {
|
---|
687 | FrameUtils.ParseProfile(FrameIO.LoadFrame(toSave
|
---|
688 | .getFramesetName()
|
---|
689 | + "1"));
|
---|
690 | }
|
---|
691 | } catch (IOException ioe) {
|
---|
692 | // ioe.printStackTrace();
|
---|
693 | Logger.Log(ioe);
|
---|
694 | return null;
|
---|
695 | }
|
---|
696 |
|
---|
697 | return writer.getFileContents();
|
---|
698 | }
|
---|
699 |
|
---|
700 | public static Frame LoadProfile(String userName) {
|
---|
701 | return LoadFrame(userName + "1");
|
---|
702 | }
|
---|
703 |
|
---|
704 | public static Frame CreateNewProfile(String username) throws Exception {
|
---|
705 | Frame profile = CreateFrameset(username, PROFILE_PATH, true);
|
---|
706 | FrameUtils.CreateDefaultProfile(profile);
|
---|
707 | return profile;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Reads the INF file that corresponds to the given Frame name
|
---|
712 | *
|
---|
713 | * @param framename
|
---|
714 | * The Frame to lookup the INF file for
|
---|
715 | * @throws IOException
|
---|
716 | * Any exceptions encountered by the BufferedReader used to read
|
---|
717 | * the INF.
|
---|
718 | */
|
---|
719 | private static int ReadINF(String path, String frameset) throws IOException {
|
---|
720 | assert (!frameset.endsWith("."));
|
---|
721 |
|
---|
722 | // read INF
|
---|
723 | BufferedReader reader;
|
---|
724 | try {
|
---|
725 | reader = new BufferedReader(new FileReader(path
|
---|
726 | + frameset.toLowerCase() + File.separator + INF_FILENAME));
|
---|
727 | } catch (Exception e) {
|
---|
728 | reader = new BufferedReader(new FileReader(path
|
---|
729 | + frameset.toLowerCase() + File.separator
|
---|
730 | + frameset.toLowerCase() + ".inf"));
|
---|
731 | }
|
---|
732 | String inf = reader.readLine();
|
---|
733 | reader.close();
|
---|
734 |
|
---|
735 | return Conversion.getFrameNumber(inf);
|
---|
736 | }
|
---|
737 |
|
---|
738 | /**
|
---|
739 | * Writes the given String out to the INF file corresponding to the current
|
---|
740 | * frameset.
|
---|
741 | *
|
---|
742 | * @param toWrite
|
---|
743 | * The String to write to the file.
|
---|
744 | * @throws IOException
|
---|
745 | * Any exception encountered by the BufferedWriter.
|
---|
746 | */
|
---|
747 | private static void WriteINF(String path, String frameset, String frameName)
|
---|
748 | throws IOException {
|
---|
749 | assert (!frameset.endsWith("."));
|
---|
750 |
|
---|
751 | path += frameset.toLowerCase() + File.separator + INF_FILENAME;
|
---|
752 |
|
---|
753 | BufferedWriter writer = new BufferedWriter(new FileWriter(path));
|
---|
754 | writer.write(frameName);
|
---|
755 | writer.close();
|
---|
756 | }
|
---|
757 |
|
---|
758 | public static boolean FrameIsCached(String name) {
|
---|
759 | return _Cache.containsKey(name);
|
---|
760 | }
|
---|
761 |
|
---|
762 | public static String ConvertToValidFramesetName(String toValidate) {
|
---|
763 | assert (toValidate != null && toValidate.length() > 0);
|
---|
764 |
|
---|
765 | StringBuffer result = new StringBuffer();
|
---|
766 |
|
---|
767 | if (Character.isDigit(toValidate.charAt(0))) {
|
---|
768 | result.append(FRAME_NAME_LAST_CHAR);
|
---|
769 | }
|
---|
770 |
|
---|
771 | boolean capital = false;
|
---|
772 | for (int i = 0; i < toValidate.length()
|
---|
773 | && result.length() < MAX_NAME_LENGTH; i++) {
|
---|
774 | char cur = toValidate.charAt(i);
|
---|
775 |
|
---|
776 | // capitalize all characters after spaces
|
---|
777 | if (Character.isLetterOrDigit(cur)) {
|
---|
778 | if (capital) {
|
---|
779 | capital = false;
|
---|
780 | result.append(Character.toUpperCase(cur));
|
---|
781 | } else
|
---|
782 | result.append(cur);
|
---|
783 | } else {
|
---|
784 | capital = true;
|
---|
785 | }
|
---|
786 | }
|
---|
787 | assert (result.length() > 0);
|
---|
788 | int lastCharIndex = result.length() - 1;
|
---|
789 | if (!Character.isLetter(result.charAt(lastCharIndex))) {
|
---|
790 | if (lastCharIndex == MAX_NAME_LENGTH - 1)
|
---|
791 | result.setCharAt(lastCharIndex, FRAME_NAME_LAST_CHAR);
|
---|
792 | else
|
---|
793 | result.append(FRAME_NAME_LAST_CHAR);
|
---|
794 | }
|
---|
795 |
|
---|
796 | assert (IsValidFramesetName(result.toString()));
|
---|
797 | return result.toString();
|
---|
798 | }
|
---|
799 |
|
---|
800 | public static Frame CreateNewFrame(Item linker) {
|
---|
801 | String title = null;
|
---|
802 | if (linker instanceof Text)
|
---|
803 | title = ((Text) linker).getFirstLine();
|
---|
804 |
|
---|
805 | String frameset = DisplayIO.getCurrentFrame().getFramesetName();
|
---|
806 |
|
---|
807 | String templateLink = linker.getLinkTemplate();
|
---|
808 |
|
---|
809 | Frame newFrame = FrameIO.CreateFrame(frameset, title, templateLink);
|
---|
810 |
|
---|
811 | // do auto shrinking of the title
|
---|
812 | Text titleItem = newFrame.getTitle();
|
---|
813 |
|
---|
814 | // BROOK: This had recursion!! Changed to avoid...
|
---|
815 | if ((titleItem.getX() + 1) < newFrame.getFrameNameItem().getX()) {
|
---|
816 | while (titleItem.getBoundsWidth() + titleItem.getX() > newFrame
|
---|
817 | .getFrameNameItem().getX()) {
|
---|
818 | titleItem.setSize(titleItem.getSize() - 1);
|
---|
819 | }
|
---|
820 | } else {
|
---|
821 | System.out.println("Bad title x position: " + titleItem.getX());
|
---|
822 | }
|
---|
823 |
|
---|
824 | return newFrame;
|
---|
825 | }
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * Creates a new Frameset on disk, including a .0, .1, and .inf files. The
|
---|
829 | * Default.0 frame is copied to make the initial .0 and .1 Frames
|
---|
830 | *
|
---|
831 | * @param name
|
---|
832 | * The Frameset name to use
|
---|
833 | * @return The name of the first Frame in the newly created Frameset (the .1
|
---|
834 | * frame)
|
---|
835 | */
|
---|
836 | public static Frame CreateNewFrameset(String name) throws Exception {
|
---|
837 | String path = DisplayIO.getCurrentFrame().path;
|
---|
838 |
|
---|
839 | // if current frameset is profile directory change it to framesets
|
---|
840 | if (path.equals(FrameIO.PROFILE_PATH)) {
|
---|
841 | path = FrameIO.FRAME_PATH;
|
---|
842 | }
|
---|
843 |
|
---|
844 | return FrameIO.CreateFrameset(name, path);
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | *
|
---|
849 | * @param frameset
|
---|
850 | * @return
|
---|
851 | */
|
---|
852 | public static int getLastNumber(String frameset) { // Rob thinks it might
|
---|
853 | // have been
|
---|
854 | // GetHighestNumExFrame
|
---|
855 | // TODO minimise the number of frames being read in!!
|
---|
856 | int num = -1;
|
---|
857 |
|
---|
858 | Frame zero = LoadFrame(frameset + "0");
|
---|
859 |
|
---|
860 | // the frameset does not exist (or has no 0 frame)
|
---|
861 | if (zero == null)
|
---|
862 | return -1;
|
---|
863 |
|
---|
864 | try {
|
---|
865 | num = ReadINF(zero.path, frameset);
|
---|
866 | } catch (IOException e) {
|
---|
867 | // TODO Auto-generated catch block
|
---|
868 | // e.printStackTrace();
|
---|
869 | }
|
---|
870 |
|
---|
871 | /*
|
---|
872 | * Michael doesnt think the code below is really needed... it will just
|
---|
873 | * slow things down when we are reading frames over a network***** for (;
|
---|
874 | * num >= 0; num--) { System.out.println("This code is loading frames to
|
---|
875 | * find the highest existing frame..."); if (LoadFrame(frameset + num) !=
|
---|
876 | * null) break; }
|
---|
877 | */
|
---|
878 |
|
---|
879 | return num;
|
---|
880 | }
|
---|
881 |
|
---|
882 | public static Boolean DoesFramesetExist(String framesetName) {
|
---|
883 | String zeroFrame = framesetName + '0';
|
---|
884 | return FrameIO.DoesFrameExist(zeroFrame);
|
---|
885 | }
|
---|
886 |
|
---|
887 | public static Frame CreateFrameset(String frameset, String path,
|
---|
888 | boolean recreate) throws Exception {
|
---|
889 | String conversion = frameset + " --> ";
|
---|
890 |
|
---|
891 | if (!IsValidFramesetName(frameset)) {
|
---|
892 | throw new Exception("Invalid frameset name");
|
---|
893 | }
|
---|
894 |
|
---|
895 | if (!recreate && FrameIO.DoesFramesetExist(frameset)) {
|
---|
896 | throw new ExistingFramesetException(frameset);
|
---|
897 | }
|
---|
898 |
|
---|
899 | conversion += frameset;
|
---|
900 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Frameset Name: "
|
---|
901 | + conversion);
|
---|
902 | conversion = frameset;
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * TODO: Update this to exclude any\all invalid filename characters
|
---|
906 | */
|
---|
907 | // ignore annotation character
|
---|
908 | if (frameset.startsWith("@"))
|
---|
909 | frameset = frameset.substring(1);
|
---|
910 |
|
---|
911 | conversion += " --> " + frameset;
|
---|
912 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Name: " + conversion);
|
---|
913 |
|
---|
914 | // create the new Frameset directory
|
---|
915 | File dir = new File(path + frameset.toLowerCase() + File.separator);
|
---|
916 |
|
---|
917 | dir.mkdirs();
|
---|
918 |
|
---|
919 | // create the new INF file
|
---|
920 | try {
|
---|
921 | WriteINF(path, frameset, frameset + '1');
|
---|
922 | } catch (IOException ioe) {
|
---|
923 | ioe.printStackTrace();
|
---|
924 | Logger.Log(ioe);
|
---|
925 | }
|
---|
926 |
|
---|
927 | SuspendCache();
|
---|
928 | // copy the default .0 and .1 files
|
---|
929 | Frame base;
|
---|
930 | try {
|
---|
931 | base = LoadFrame(UserSettings.DefaultFrame);
|
---|
932 | } catch (Exception e) {
|
---|
933 | base = new Frame();
|
---|
934 | }
|
---|
935 | ResumeCache();
|
---|
936 |
|
---|
937 | base.resetDateCreated();
|
---|
938 | base.setFrameset(frameset);
|
---|
939 | base.setFrameNumber(0);
|
---|
940 | base.setTitle(base.getFramesetName() + "0");
|
---|
941 | base.path = path;
|
---|
942 | base.change();
|
---|
943 | SaveFrame(base, false);
|
---|
944 |
|
---|
945 | base.resetDateCreated();
|
---|
946 | base.setFrameNumber(1);
|
---|
947 | base.setTitle(frameset);
|
---|
948 | base.change();
|
---|
949 | SaveFrame(base, true);
|
---|
950 |
|
---|
951 | Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Created new frameset: "
|
---|
952 | + frameset);
|
---|
953 |
|
---|
954 | return base;
|
---|
955 | }
|
---|
956 |
|
---|
957 | /**
|
---|
958 | * Tests if a frameset name is valid. That is it must begin and end with a
|
---|
959 | * letter and contain only letters and digits in between.
|
---|
960 | *
|
---|
961 | * @param frameset
|
---|
962 | * the name to be tested
|
---|
963 | * @return true if the frameset name is valid
|
---|
964 | */
|
---|
965 | public static boolean IsValidFramesetName(String frameset) {
|
---|
966 | if (frameset == null) {
|
---|
967 | return false;
|
---|
968 | }
|
---|
969 |
|
---|
970 | int nameLength = frameset.length();
|
---|
971 | if (frameset.length() <= 0 || nameLength > MAX_NAME_LENGTH) {
|
---|
972 | return false;
|
---|
973 | }
|
---|
974 |
|
---|
975 | int lastCharIndex = nameLength - 1;
|
---|
976 |
|
---|
977 | if (!Character.isLetter(frameset.charAt(0))
|
---|
978 | || !Character.isLetter(frameset.charAt(lastCharIndex)))
|
---|
979 | return false;
|
---|
980 |
|
---|
981 | for (int i = 1; i < lastCharIndex; i++) {
|
---|
982 | if (!Character.isLetterOrDigit(frameset.charAt(i))) {
|
---|
983 | return false;
|
---|
984 | }
|
---|
985 | }
|
---|
986 | return true;
|
---|
987 | }
|
---|
988 |
|
---|
989 | public static boolean DeleteFrameset(String framesetName) {
|
---|
990 | if (!FrameIO.DoesFramesetExist(framesetName))
|
---|
991 | return false;
|
---|
992 | // Search all the available directories for the directory
|
---|
993 | for (String path : UserSettings.FrameDirs) {
|
---|
994 | String source = path + framesetName.toLowerCase() + File.separator;
|
---|
995 | File framesetDirectory = new File(source);
|
---|
996 | // Once we have found the directory move it to the trash
|
---|
997 | if (framesetDirectory.exists()) {
|
---|
998 | String destPath = FrameIO.TRASH_PATH
|
---|
999 | + framesetName.toLowerCase();
|
---|
1000 | int copyNumber = 1;
|
---|
1001 | File dest = new File(destPath + File.separator);
|
---|
1002 | //Create the trash folder if it doesnt already exist
|
---|
1003 | if (!dest.getParentFile().exists())
|
---|
1004 | dest.mkdirs();
|
---|
1005 | // If a frameset with the same name is already in the trash add
|
---|
1006 | // a number to the end
|
---|
1007 | while (dest.exists()) {
|
---|
1008 | dest = new File(destPath + ++copyNumber + File.separator );
|
---|
1009 | }
|
---|
1010 | return framesetDirectory.renameTo(dest);
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 | return false;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | public static boolean CopyFrameset(String framesetToCopy,
|
---|
1017 | String copiedFrameset) throws Exception {
|
---|
1018 | if (!FrameIO.DoesFramesetExist(framesetToCopy))
|
---|
1019 | return false;
|
---|
1020 | if (FrameIO.DoesFramesetExist(copiedFrameset))
|
---|
1021 | return false;
|
---|
1022 | // search through all the directories to find the frameset we are
|
---|
1023 | // copying
|
---|
1024 | for (String path : UserSettings.FrameDirs) {
|
---|
1025 | String source = path + framesetToCopy.toLowerCase()
|
---|
1026 | + File.separator;
|
---|
1027 | File framesetDirectory = new File(source);
|
---|
1028 | if (framesetDirectory.exists()) {
|
---|
1029 | // copy the frameset
|
---|
1030 | File copyFramesetDirectory = new File(path
|
---|
1031 | + copiedFrameset.toLowerCase() + File.separator);
|
---|
1032 | if (!copyFramesetDirectory.mkdirs())
|
---|
1033 | return false;
|
---|
1034 | // copy each of the frames
|
---|
1035 | for (File f : framesetDirectory.listFiles()) {
|
---|
1036 | String copyPath = copyFramesetDirectory.getAbsolutePath()
|
---|
1037 | + File.separator + f.getName();
|
---|
1038 | FrameIO.copyFile(f.getAbsolutePath(), copyPath);
|
---|
1039 | }
|
---|
1040 | return true;
|
---|
1041 | }
|
---|
1042 | }
|
---|
1043 | return false;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | public static void copyFile(String existingFile, String newFileName)
|
---|
1047 | throws Exception {
|
---|
1048 | FileInputStream is = new FileInputStream(existingFile);
|
---|
1049 | FileOutputStream os = new FileOutputStream(newFileName, false);
|
---|
1050 | int data;
|
---|
1051 | while ((data = is.read()) != -1) {
|
---|
1052 | os.write(data);
|
---|
1053 | }
|
---|
1054 | os.flush();
|
---|
1055 | os.close();
|
---|
1056 | is.close();
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * Saves a frame regardless of whether or not the frame is marked as having
|
---|
1061 | * been changed.
|
---|
1062 | *
|
---|
1063 | * @param frame
|
---|
1064 | * the frame to save
|
---|
1065 | * @return the contents of the frame or null if it could not be saved
|
---|
1066 | */
|
---|
1067 | public static String ForceSaveFrame(Frame frame) {
|
---|
1068 | frame.change();
|
---|
1069 | return SaveFrame(frame, false);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | } |
---|