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