1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.log4j.chainsaw;
18
19 import com.thoughtworks.xstream.XStream;
20 import com.thoughtworks.xstream.io.xml.DomDriver;
21 import org.apache.log4j.chainsaw.prefs.LoadSettingsEvent;
22 import org.apache.log4j.chainsaw.prefs.SaveSettingsEvent;
23 import org.apache.log4j.chainsaw.prefs.SettingsListener;
24 import org.apache.log4j.chainsaw.prefs.SettingsManager;
25
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.FileWriter;
29
30 /**
31 * Helper class that helps delegate the work of loading and saving the values
32 * of the ApplicationPreferenceModel, allowing that class to remain a simple
33 * bean.
34 * <p>
35 * The Model passed to this class' constructor is the instance of the ApplicationPreference
36 * that will be saved, and will have properties modified by loading from the
37 * 'chainsaw.settings.xml' file in the .chainsaw directory of the user's home directory.
38 *
39 * @author psmith
40 */
41 public class ApplicationPreferenceModelSaver implements SettingsListener {
42
43 private final ApplicationPreferenceModel model;
44
45 /**
46 * @param model
47 */
48 public ApplicationPreferenceModelSaver(final ApplicationPreferenceModel model) {
49 this.model = model;
50 }
51
52 public void loadSettings(LoadSettingsEvent event) {
53 XStream stream = new XStream(new DomDriver());
54 File file = getApplicationPreferenceXMLFile(SettingsManager.getInstance().getSettingsDirectory());
55 try {
56 if (file.exists()) {
57 FileReader reader = new FileReader(file);
58 ApplicationPreferenceModel loadedModel = (ApplicationPreferenceModel) stream
59 .fromXML(reader);
60 model.apply(loadedModel);
61 reader.close();
62 }
63 } catch (Exception e) {
64 // TODO exception handling
65 e.printStackTrace();
66 //unable to process - delete file
67 file.delete();
68 }
69 }
70
71 public void saveSettings(SaveSettingsEvent event) {
72 XStream stream = new XStream(new DomDriver());
73 try {
74 File file = getApplicationPreferenceXMLFile(event.getSettingsLocation());
75 FileWriter writer = new FileWriter(file);
76 stream.toXML(model, writer);
77 writer.close();
78 } catch (Exception e) {
79 // TODO exception handling
80 e.printStackTrace();
81 }
82
83 }
84
85 private File getApplicationPreferenceXMLFile(File settingsLocation) {
86 return new File(settingsLocation, "chainsaw.settings.xml");
87 }
88
89 }