PageRenderTime 71ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/JEditHistoryModelSaver.java

#
Java | 224 lines | 156 code | 29 blank | 39 comment | 27 complexity | 13e30251960a8f3eb6e21a399de9c72b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. import org.gjt.sp.util.Log;
  24. import org.gjt.sp.util.IOUtilities;
  25. import org.gjt.sp.util.StandardUtilities;
  26. import org.gjt.sp.jedit.MiscUtilities;
  27. import org.gjt.sp.jedit.jEdit;
  28. import java.io.*;
  29. import java.nio.charset.Charset;
  30. import java.nio.charset.CharacterCodingException;
  31. import java.util.*;
  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, HistoryModel> load(Map<String, HistoryModel> models)
  40. {
  41. String settingsDirectory = jEdit.getSettingsDirectory();
  42. if(settingsDirectory == null)
  43. return models;
  44. history = new File(MiscUtilities.constructPath(
  45. settingsDirectory,"history"));
  46. if(!history.exists())
  47. return models;
  48. historyModTime = history.lastModified();
  49. Log.log(Log.MESSAGE,HistoryModel.class,"Loading history");
  50. if(models == null)
  51. models = Collections.synchronizedMap(new HashMap<String, HistoryModel>());
  52. BufferedReader in = null;
  53. try
  54. {
  55. // Try loading with UTF-8 and fallback to the system
  56. // default encoding to load a history which was made by
  57. // an old version as well.
  58. try
  59. {
  60. // Pass the decoder explicitly to report a decode error
  61. // as an exception instead of replacing with \xFFFD.
  62. in = new BufferedReader(new InputStreamReader(
  63. new FileInputStream(history),
  64. Charset.forName("UTF-8").newDecoder()));
  65. models.putAll(loadFromReader(in));
  66. }
  67. catch(CharacterCodingException e)
  68. {
  69. // It seems to be made by an old version of jEdit.
  70. in.close();
  71. Log.log(Log.MESSAGE,HistoryModel.class,
  72. "Failed to load history with UTF-8." +
  73. " Fallbacking to the system default encoding.");
  74. in = new BufferedReader(new FileReader(history));
  75. models.putAll(loadFromReader(in));
  76. }
  77. }
  78. catch(FileNotFoundException fnf)
  79. {
  80. //Log.log(Log.DEBUG,HistoryModel.class,fnf);
  81. }
  82. catch(IOException io)
  83. {
  84. Log.log(Log.ERROR,HistoryModel.class,io);
  85. }
  86. finally
  87. {
  88. IOUtilities.closeQuietly(in);
  89. }
  90. return models;
  91. } //}}}
  92. //{{{ save() method
  93. public boolean save(Map<String, HistoryModel> models)
  94. {
  95. Log.log(Log.MESSAGE,HistoryModel.class,"Saving history");
  96. File file1 = new File(MiscUtilities.constructPath(
  97. jEdit.getSettingsDirectory(), "#history#save#"));
  98. File file2 = new File(MiscUtilities.constructPath(
  99. jEdit.getSettingsDirectory(), "history"));
  100. if(file2.exists() && file2.lastModified() != historyModTime)
  101. {
  102. Log.log(Log.WARNING,HistoryModel.class,file2
  103. + " changed on disk; will not save history");
  104. return false;
  105. }
  106. jEdit.backupSettingsFile(file2);
  107. String lineSep = System.getProperty("line.separator");
  108. BufferedWriter out = null;
  109. try
  110. {
  111. out = new BufferedWriter(new OutputStreamWriter(
  112. new FileOutputStream(file1), "UTF-8"));
  113. if(models != null)
  114. {
  115. Collection<HistoryModel> values = models.values();
  116. for (HistoryModel model : values)
  117. {
  118. if(model.getSize() == 0)
  119. continue;
  120. out.write('[');
  121. out.write(StandardUtilities.charsToEscapes(
  122. model.getName(),TO_ESCAPE));
  123. out.write(']');
  124. out.write(lineSep);
  125. for(int i = 0; i < model.getSize(); i++)
  126. {
  127. out.write(StandardUtilities.charsToEscapes(
  128. model.getItem(i),
  129. TO_ESCAPE));
  130. out.write(lineSep);
  131. }
  132. }
  133. }
  134. out.close();
  135. /* to avoid data loss, only do this if the above
  136. * completed successfully */
  137. file2.delete();
  138. file1.renameTo(file2);
  139. }
  140. catch(IOException io)
  141. {
  142. Log.log(Log.ERROR,HistoryModel.class,io);
  143. }
  144. finally
  145. {
  146. IOUtilities.closeQuietly(out);
  147. }
  148. historyModTime = file2.lastModified();
  149. return true;
  150. } //}}}
  151. //{{{ Private members
  152. private static final String TO_ESCAPE = "\r\n\t\\\"'[]";
  153. private static File history;
  154. private static long historyModTime;
  155. //{{{ loadFromReader() method
  156. private static Map<String, HistoryModel> loadFromReader(BufferedReader in)
  157. throws IOException
  158. {
  159. Map<String, HistoryModel> result = new HashMap<String, HistoryModel>();
  160. HistoryModel currentModel = null;
  161. String line;
  162. while((line = in.readLine()) != null)
  163. {
  164. if(line.length() > 0 && line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']')
  165. {
  166. if(currentModel != null)
  167. {
  168. result.put(currentModel.getName(),
  169. currentModel);
  170. }
  171. String modelName = MiscUtilities
  172. .escapesToChars(line.substring(
  173. 1,line.length() - 1));
  174. currentModel = new HistoryModel(
  175. modelName);
  176. }
  177. else if(currentModel == null)
  178. {
  179. throw new IOException("History data starts"
  180. + " before model name");
  181. }
  182. else
  183. {
  184. currentModel.addElement(MiscUtilities
  185. .escapesToChars(line));
  186. }
  187. }
  188. if(currentModel != null)
  189. {
  190. result.put(currentModel.getName(),currentModel);
  191. }
  192. return result;
  193. } //}}}
  194. //}}}
  195. }