01: package org.dbbrowser.ui.helper;
02:
03: import infrastructure.propertymanager.PropertyManager;
04: import java.io.BufferedReader;
05: import java.io.BufferedWriter;
06: import java.io.FileNotFoundException;
07: import java.io.FileReader;
08: import java.io.FileWriter;
09: import java.io.IOException;
10: import java.util.List;
11: import org.dbbrowser.ui.KeyBinding;
12: import com.thoughtworks.xstream.XStream;
13:
14: /**
15: * Serializes the key bindings using XStream. serializes the list of key bindings as XML
16: * @author amangat
17: *
18: */
19: public class KeyBindingsSerializer {
20: private static final String KEY_BINDING_FILE_NAME = PropertyManager
21: .getInstance().getProperty(
22: "dbbrowser-ui-key-bindings-filename");
23:
24: /**
25: * Converts XML to a list of KeyBindings
26: * @return - java.util.List - a list of KeyBinding classes
27: * @throws FileNotFoundException
28: * @throws IOException
29: */
30: public static List deserializeKeyBindings()
31: throws FileNotFoundException, IOException {
32: //Load the key bindings
33: StringBuffer xml = new StringBuffer();
34: FileReader fr = new FileReader(KEY_BINDING_FILE_NAME);
35: BufferedReader reader = new BufferedReader(fr);
36:
37: String line = reader.readLine();
38:
39: while (line != null) {
40: xml.append(line);
41: line = reader.readLine();
42: }
43:
44: XStream xstream = new XStream();
45: xstream.alias("keyBinding", KeyBinding.class);
46: return (List) xstream.fromXML(xml.toString());
47: }
48:
49: /**
50: * Stores the list of KeyBindings as XML
51: * @param keyBindings
52: * @throws IOException
53: */
54: public static void serializeKeyBindings(List keyBindings)
55: throws IOException {
56: //Save the changes to XML file
57: XStream xstream = new XStream();
58: xstream.alias("keyBinding", KeyBinding.class);
59: String xml = xstream.toXML(keyBindings);
60:
61: //Write key bindings to file
62: FileWriter fw = new FileWriter(KEY_BINDING_FILE_NAME);
63: BufferedWriter writer = new BufferedWriter(fw);
64:
65: writer.write(xml);
66: writer.flush();
67: writer.close();
68: }
69: }
|