1 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 36 public class JEditHistoryModelSaver implements HistoryModelSaver 37 { 38 public Map<String , HistoryModel> load(Map<String , HistoryModel> models) 40 { 41 String 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 , HistoryModel>()); 56 57 BufferedReader in = null; 58 59 try 60 { 61 in = new BufferedReader(new FileReader(history)); 62 63 HistoryModel currentModel = null; 64 String 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 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 } 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 } 114 public boolean save(Map<String , 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 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 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 } 183 private static final String TO_ESCAPE = "\r\n\t\\\"'[]"; 185 private static File history; 186 private static long historyModTime; 187 189 } 190 | Popular Tags |