KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > JEditHistoryModelSaver


1 /*
2  * JEditHistoryModelSaver.java - Handles services.xml files in plugins
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2006 Matthieu Casanova
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22 package org.gjt.sp.jedit.gui;
23
24 import org.gjt.sp.util.Log;
25 import org.gjt.sp.util.IOUtilities;
26 import org.gjt.sp.jedit.MiscUtilities;
27 import org.gjt.sp.jedit.jEdit;
28
29 import java.io.*;
30 import java.util.*;
31
32 /**
33  * @author Matthieu Casanova
34  * @version $Id: FoldHandler.java 5568 2006-07-10 20:52:23Z kpouer $
35  */

36 public class JEditHistoryModelSaver implements HistoryModelSaver
37 {
38     //{{{ load() method
39
public Map<String JavaDoc, HistoryModel> load(Map<String JavaDoc, HistoryModel> models)
40     {
41         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
42         if(settingsDirectory == null)
43             return models;
44
45         history = new File(MiscUtilities.constructPath(
46             settingsDirectory,"history"));
47         if(!history.exists())
48             return models;
49
50         historyModTime = history.lastModified();
51
52         Log.log(Log.MESSAGE,HistoryModel.class,"Loading history");
53
54         if(models == null)
55             models = Collections.synchronizedMap(new HashMap<String JavaDoc, HistoryModel>());
56
57         BufferedReader in = null;
58
59         try
60         {
61             in = new BufferedReader(new FileReader(history));
62
63             HistoryModel currentModel = null;
64             String JavaDoc line;
65
66             while((line = in.readLine()) != null)
67             {
68                 if(line.length() > 0 && line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']')
69                 {
70                     if(currentModel != null)
71                     {
72                         models.put(currentModel.getName(),
73                             currentModel);
74                     }
75
76                     String JavaDoc modelName = MiscUtilities
77                         .escapesToChars(line.substring(
78                         1,line.length() - 1));
79                     currentModel = new HistoryModel(
80                         modelName);
81                 }
82                 else if(currentModel == null)
83                 {
84                     throw new IOException("History data starts"
85                         + " before model name");
86                 }
87                 else
88                 {
89                     currentModel.addElement(MiscUtilities
90                         .escapesToChars(line));
91                 }
92             }
93
94             if(currentModel != null)
95             {
96                 models.put(currentModel.getName(),currentModel);
97             }
98         }
99         catch(FileNotFoundException fnf)
100         {
101             //Log.log(Log.DEBUG,HistoryModel.class,fnf);
102
}
103         catch(IOException io)
104         {
105             Log.log(Log.ERROR,HistoryModel.class,io);
106         }
107         finally
108         {
109             IOUtilities.closeQuietly(in);
110         }
111         return models;
112     } //}}}
113

114     //{{{ save() method
115
public boolean save(Map<String JavaDoc, HistoryModel> models)
116     {
117         Log.log(Log.MESSAGE,HistoryModel.class,"Saving history");
118         File file1 = new File(MiscUtilities.constructPath(
119             jEdit.getSettingsDirectory(), "#history#save#"));
120         File file2 = new File(MiscUtilities.constructPath(
121             jEdit.getSettingsDirectory(), "history"));
122         if(file2.exists() && file2.lastModified() != historyModTime)
123         {
124             Log.log(Log.WARNING,HistoryModel.class,file2
125                 + " changed on disk; will not save history");
126             return false;
127         }
128
129         jEdit.backupSettingsFile(file2);
130
131         String JavaDoc lineSep = System.getProperty("line.separator");
132
133         BufferedWriter out = null;
134
135         try
136         {
137             out = new BufferedWriter(new FileWriter(file1));
138
139             if(models != null)
140             {
141                 Collection<HistoryModel> values = models.values();
142                 for (HistoryModel model : values)
143                 {
144                     if(model.getSize() == 0)
145                         continue;
146
147                     out.write('[');
148                     out.write(MiscUtilities.charsToEscapes(
149                         model.getName(),TO_ESCAPE));
150                     out.write(']');
151                     out.write(lineSep);
152
153                     for(int i = 0; i < model.getSize(); i++)
154                     {
155                         out.write(MiscUtilities.charsToEscapes(
156                             model.getItem(i),
157                             TO_ESCAPE));
158                         out.write(lineSep);
159                     }
160                 }
161             }
162
163             out.close();
164
165             /* to avoid data loss, only do this if the above
166              * completed successfully */

167             file2.delete();
168             file1.renameTo(file2);
169         }
170         catch(IOException io)
171         {
172             Log.log(Log.ERROR,HistoryModel.class,io);
173         }
174         finally
175         {
176             IOUtilities.closeQuietly(out);
177         }
178
179         historyModTime = file2.lastModified();
180         return true;
181     } //}}}
182

183     //{{{ Private members
184
private static final String JavaDoc TO_ESCAPE = "\r\n\t\\\"'[]";
185     private static File history;
186     private static long historyModTime;
187     //}}}
188

189 }
190
Popular Tags