1 package com.explosion.utilities.preferences.impl.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Color;
24 import java.awt.Font;
25 import java.io.File;
26
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31
32 import com.explosion.utilities.GeneralConstants;
33 import com.explosion.utilities.preferences.Preference;
34 import com.explosion.utilities.preferences.groups.PreferenceGroup;
35 import com.explosion.utilities.preferences.groups.PreferencePersistenceException;
36 import com.explosion.utilities.preferences.persist.PreferencePersister;
37
38 /***
39 * @author: Stephen Cowx
40 * Date: Nov 7, 2002
41 * Time: 11:13:26 PM
42 *
43 * Assists with saving Preferences as XML
44 *
45 */
46
47 public class XMLPreferencePersister implements PreferencePersister
48 {
49
50 private Node node;
51
52 private Document document = null;
53
54 /***
55 * Hidden constructor, want to use the factory method
56 */
57 private XMLPreferencePersister() {
58 }
59
60 /***
61 * Constructs an instance of a SunPreferencePersistor
62 * @param referenceToStore
63 * @return
64 */
65 public static XMLPreferencePersister createPreferencePersistor(Object referenceToStore)
66 {
67 XMLPreferencePersister persister = new XMLPreferencePersister();
68 persister.node = (Node) referenceToStore;
69 return persister;
70 }
71
72 /***
73 * Returns a reference to the backing store as understood by the PreferencePersister
74 * @return
75 */
76 public Object getReferenceToStore()
77 {
78 return node;
79 }
80
81 /***
82 * Sets the reference to the backing store as understood by the PreferencePersister
83 * Effectively moving it from one store to another or moving it from one location in the same store to another
84 * @return
85 */
86 public void setReferenceToStore(Object referenceToStore)
87 {
88 this.node = (Node) referenceToStore;
89 }
90
91 /***
92 * This method saves the property to persistent storage, if the value is
93 * null, it should delete the entire key/value pair persisted if it exists
94 * or not save it if it does.
95 *
96 * @throws Exception
97 */
98 public void save(Preference preference) throws PreferencePersistenceException
99 {
100 try
101 {
102 if (preference.isDefaulted())
103 {
104
105 if (getPreferenceValue(node, preference.getUniqueIdentifier(), null) != null) node.getParentNode().removeChild(node);
106
107 return;
108 }
109
110 switch (preference.getType())
111 {
112 case (Preference.PROPERTY_TYPE_TEXT):
113 setPreferenceValue(node, preference.getUniqueIdentifier(), (String) preference.getValue());
114 break;
115 case (Preference.PROPERTY_TYPE_FILE):
116 setPreferenceValue(node, preference.getUniqueIdentifier(), ((File) preference.getValue()).getAbsolutePath());
117 break;
118 case (Preference.PROPERTY_TYPE_DIRECTORY):
119 setPreferenceValue(node, preference.getUniqueIdentifier(), ((File) preference.getValue()).getAbsolutePath());
120 break;
121 case (Preference.PROPERTY_TYPE_COLOR):
122 setPreferenceValue(node, preference.getUniqueIdentifier(), Integer.toString(((Color) preference.getValue()).getRGB()));
123 break;
124 case (Preference.PROPERTY_TYPE_INT):
125 setPreferenceValue(node, preference.getUniqueIdentifier(), ((Integer) preference.getValue()).toString());
126 break;
127 case (Preference.PROPERTY_TYPE_FLOAT):
128 setPreferenceValue(node, preference.getUniqueIdentifier(), ((Float) preference.getValue()).toString());
129 break;
130 case (Preference.PROPERTY_TYPE_STRING_CHOICE):
131 setPreferenceValue(node, preference.getUniqueIdentifier(), ((String) preference.getValue()));
132 break;
133 case (Preference.PROPERTY_TYPE_INT_CHOICE):
134 setPreferenceValue(node, preference.getUniqueIdentifier(), ((Integer) preference.getValue()).toString());
135 break;
136 case (Preference.PROPERTY_TYPE_BOOLEAN):
137 setPreferenceValue(node, preference.getUniqueIdentifier(), ((Boolean) preference.getValue()).toString());
138 break;
139 case (Preference.PROPERTY_TYPE_FONT):
140 String fontString = ((Font) preference.getValue()).getStyle() + "," + ((Font) preference.getValue()).getSize() + "," + ((Font) preference.getValue()).getName();
141 setPreferenceValue(node, preference.getUniqueIdentifier(), fontString);
142 break;
143 case (Preference.PROPERTY_TYPE_ENCRYPTED):
144
145 setPreferenceValue(node, preference.getUniqueIdentifier(), Preference.encrypt((String) preference.getValue()));
146 break;
147 }
148 }
149 catch (Exception e)
150 {
151 throw new PreferencePersistenceException("Cannot save value",e);
152 }
153
154 }
155
156 private void setPreferenceValue(Node node, String uniqueName, String newValue)
157 {
158 NodeList list = node.getChildNodes();
159 for (int i = 0; i < list.getLength(); i++)
160 {
161 Node child = list.item(i);
162 if (child.getNodeName().equalsIgnoreCase(uniqueName))
163 {
164 ((Element) child).setAttribute("value", newValue.trim());
165 return;
166 }
167 }
168
169
170 Node newNode = document.createElement(uniqueName);
171 newNode.setNodeValue(newValue.trim());
172 node.appendChild(newNode);
173 }
174
175 private String getPreferenceValue(Node node, String uniqueName, String defaultValue)
176 {
177 NodeList list = node.getChildNodes();
178 for (int i = 0; i < list.getLength(); i++)
179 {
180 Node child = list.item(i);
181 if (child.getNodeName().equalsIgnoreCase(uniqueName))
182 {
183 String nodeData = ((Element) child).getAttribute("value");
184 if (nodeData != null && nodeData.trim().length() > 0) { return nodeData.trim(); }
185 }
186 }
187 return defaultValue;
188 }
189
190 /***
191 * This method loads the property from persistent storage if it is there, if
192 * it is not it should not populate the value.
193 *
194 * @throws Exception
195 */
196 public void load(Preference preference) throws PreferencePersistenceException
197 {
198 try
199 {
200
201 if (getPreferenceValue(node, preference.getUniqueIdentifier(), null) == null) { return; }
202
203
204 switch (preference.getType())
205 {
206 case (Preference.PROPERTY_TYPE_TEXT):
207 preference.setValue(getPreferenceValue(node, preference.getUniqueIdentifier(), (String) preference.getDefaultValue()));
208 break;
209 case (Preference.PROPERTY_TYPE_FILE):
210 preference.setValue(new File(getPreferenceValue(node, preference.getUniqueIdentifier(), ((File) preference.getDefaultValue()).toString())));
211 if (((File) preference.getValue()).getName().indexOf(GeneralConstants.DEFAULT_FILE) >= 0) preference.setValue(preference.createDefaultFile());
212 break;
213 case (Preference.PROPERTY_TYPE_DIRECTORY):
214 preference.setValue(new File(getPreferenceValue(node, preference.getUniqueIdentifier(), ((File) preference.getDefaultValue()).toString())));
215 if (((File) preference.getValue()).getName().equals(GeneralConstants.DEFAULT_FILE)) preference.setValue(preference.createDefaultFile());
216 break;
217 case (Preference.PROPERTY_TYPE_COLOR):
218 preference.setValue(new Color(Integer.parseInt(getPreferenceValue(node, preference.getUniqueIdentifier(), Integer.toString(((Color) preference.getDefaultValue()).getRGB())))));
219 break;
220 case (Preference.PROPERTY_TYPE_INT):
221 preference.setValue(new Integer(Integer.parseInt(getPreferenceValue(node, preference.getUniqueIdentifier(), ((Integer) preference.getDefaultValue()).toString()))));
222 break;
223 case (Preference.PROPERTY_TYPE_FLOAT):
224 preference.setValue(new Float(Float.parseFloat(getPreferenceValue(node, preference.getUniqueIdentifier(), ((Float) preference.getDefaultValue()).toString()))));
225 break;
226 case (Preference.PROPERTY_TYPE_STRING_CHOICE):
227 preference.setValue(getPreferenceValue(node, preference.getUniqueIdentifier(), (String) preference.getDefaultValue()));
228 break;
229 case (Preference.PROPERTY_TYPE_INT_CHOICE):
230 preference.setValue(new Integer(Integer.parseInt(getPreferenceValue(node, preference.getUniqueIdentifier(), ((Integer) preference.getDefaultValue()).toString()))));
231 break;
232 case (Preference.PROPERTY_TYPE_BOOLEAN):
233 preference.setValue(new Boolean(getPreferenceValue(node, preference.getUniqueIdentifier(), ((Boolean) preference.getDefaultValue()).toString())));
234 break;
235 case (Preference.PROPERTY_TYPE_FONT):
236 String defaultFontString = ((Font) preference.getDefaultValue()).getStyle() + "," + ((Font) preference.getDefaultValue()).getSize() + "," + ((Font) preference.getDefaultValue()).getName();
237 preference.setValue(Preference.deriveFont(getPreferenceValue(node, preference.getUniqueIdentifier(), defaultFontString)));
238 break;
239 case (Preference.PROPERTY_TYPE_ENCRYPTED):
240
241 preference.setValue(Preference.decrypt(getPreferenceValue(node, preference.getUniqueIdentifier(), (String) preference.getDefaultValue())));
242 }
243 }
244 catch (Exception e)
245 {
246 throw new PreferencePersistenceException("Cannot load value",e);
247 }
248 }
249
250
251 /***
252 * This method deletes the preferenceGroup from the backing store
253 * @param group
254 * @throws PreferencePersistenceException
255 */
256 public void deleteGroup(PreferenceGroup group) throws PreferencePersistenceException
257 {
258
259
260
261 }
262
263 /***
264 * This method saves a preference group in the store
265 * @param group
266 * @throws PreferencePersistenceException
267 */
268 public void saveGroup(PreferenceGroup group) throws PreferencePersistenceException
269 {
270
271 }
272
273 public void relocateGroup(PreferenceGroup group, Object referenceToNewStore) throws PreferencePersistenceException
274 {
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 }
300 }