/jEdit/branches/jedit43_nostrings/org/gjt/sp/jedit/gui/JEditHistoryModelSaver.java

# · Java · 189 lines · 131 code · 26 blank · 32 comment · 27 complexity · 450307ecc7fce4ef532646d5ee59fa35 MD5 · raw file

  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.jedit.MiscUtilities;
  26. import org.gjt.sp.jedit.jEdit;
  27. import java.io.*;
  28. import java.util.*;
  29. /**
  30. * @author Matthieu Casanova
  31. * @version $Id: FoldHandler.java 5568 2006-07-10 20:52:23Z kpouer $
  32. */
  33. public class JEditHistoryModelSaver implements HistoryModelSaver
  34. {
  35. //{{{ load() method
  36. public Map<String, HistoryModel> load(Map<String, HistoryModel> models)
  37. {
  38. String settingsDirectory = jEdit.getSettingsDirectory();
  39. if(settingsDirectory == null)
  40. return models;
  41. history = new File(MiscUtilities.constructPath(
  42. settingsDirectory,"history"));
  43. if(!history.exists())
  44. return models;
  45. historyModTime = history.lastModified();
  46. Log.log(Log.MESSAGE,HistoryModel.class,"Loading history");
  47. if(models == null)
  48. models = Collections.synchronizedMap(new HashMap<String, HistoryModel>());
  49. BufferedReader in = null;
  50. try
  51. {
  52. in = new BufferedReader(new FileReader(history));
  53. HistoryModel currentModel = null;
  54. String line;
  55. while((line = in.readLine()) != null)
  56. {
  57. if(line.length() > 0 && line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']')
  58. {
  59. if(currentModel != null)
  60. {
  61. models.put(currentModel.getName(),
  62. currentModel);
  63. }
  64. String modelName = MiscUtilities
  65. .escapesToChars(line.substring(
  66. 1,line.length() - 1));
  67. currentModel = new HistoryModel(
  68. modelName);
  69. }
  70. else if(currentModel == null)
  71. {
  72. throw new IOException("History data starts"
  73. + " before model name");
  74. }
  75. else
  76. {
  77. currentModel.addElement(MiscUtilities
  78. .escapesToChars(line));
  79. }
  80. }
  81. if(currentModel != null)
  82. {
  83. models.put(currentModel.getName(),currentModel);
  84. }
  85. }
  86. catch(FileNotFoundException fnf)
  87. {
  88. //Log.log(Log.DEBUG,HistoryModel.class,fnf);
  89. }
  90. catch(IOException io)
  91. {
  92. Log.log(Log.ERROR,HistoryModel.class,io);
  93. }
  94. finally
  95. {
  96. IOUtilities.closeQuietly(in);
  97. }
  98. return models;
  99. } //}}}
  100. //{{{ save() method
  101. public boolean save(Map<String, HistoryModel> models)
  102. {
  103. Log.log(Log.MESSAGE,HistoryModel.class,"Saving history");
  104. File file1 = new File(MiscUtilities.constructPath(
  105. jEdit.getSettingsDirectory(), "#history#save#"));
  106. File file2 = new File(MiscUtilities.constructPath(
  107. jEdit.getSettingsDirectory(), "history"));
  108. if(file2.exists() && file2.lastModified() != historyModTime)
  109. {
  110. Log.log(Log.WARNING,HistoryModel.class,file2
  111. + " changed on disk; will not save history");
  112. return false;
  113. }
  114. jEdit.backupSettingsFile(file2);
  115. String lineSep = System.getProperty("line.separator");
  116. BufferedWriter out = null;
  117. try
  118. {
  119. out = new BufferedWriter(new FileWriter(file1));
  120. if(models != null)
  121. {
  122. Collection<HistoryModel> values = models.values();
  123. for (HistoryModel model : values)
  124. {
  125. if(model.getSize() == 0)
  126. continue;
  127. out.write('[');
  128. out.write(MiscUtilities.charsToEscapes(
  129. model.getName(),TO_ESCAPE));
  130. out.write(']');
  131. out.write(lineSep);
  132. for(int i = 0; i < model.getSize(); i++)
  133. {
  134. out.write(MiscUtilities.charsToEscapes(
  135. model.getItem(i),
  136. TO_ESCAPE));
  137. out.write(lineSep);
  138. }
  139. }
  140. }
  141. out.close();
  142. /* to avoid data loss, only do this if the above
  143. * completed successfully */
  144. file2.delete();
  145. file1.renameTo(file2);
  146. }
  147. catch(IOException io)
  148. {
  149. Log.log(Log.ERROR,HistoryModel.class,io);
  150. }
  151. finally
  152. {
  153. IOUtilities.closeQuietly(out);
  154. }
  155. historyModTime = file2.lastModified();
  156. return true;
  157. } //}}}
  158. //{{{ Private members
  159. private static final String TO_ESCAPE = "\r\n\t\\\"'[]";
  160. private static File history;
  161. private static long historyModTime;
  162. //}}}
  163. }