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

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

#
Java | 193 lines | 84 code | 19 blank | 90 comment | 2 complexity | 8d7ab35dfe798493ff3a75b1b00b5f3d 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. * :tabSize=8:indentSize=8:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2007 Kazutoshi Satoda
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package org.gjt.sp.jedit;
  22. //{{{ Imports
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.FileOutputStream;
  26. import java.io.FileInputStream;
  27. import java.io.BufferedWriter;
  28. import java.io.OutputStreamWriter;
  29. import org.xml.sax.helpers.DefaultHandler;
  30. import org.gjt.sp.util.XMLUtilities;
  31. //}}}
  32. /**
  33. * A XML file in the settings directory.
  34. * This class provides some common operations to load/save settings
  35. * from/into a XML file.
  36. * - Proper encoding and XML declaration.
  37. * - Two stage save.
  38. * - Making backup on each save.
  39. * - Detection of change on disk.
  40. */
  41. public class SettingsXML
  42. {
  43. //{{{ Saver class
  44. /**
  45. * A Writer to write XML for saving.
  46. * The real settings file is not changed until the finish()
  47. * method succeeds, in which case the previous settings file is
  48. * backuped.
  49. */
  50. public class Saver extends BufferedWriter
  51. {
  52. //{{{ writeXMLDeclaration() method
  53. /**
  54. * Write the XML 1.0 declaration.
  55. * This should be the first output.
  56. */
  57. public void writeXMLDeclaration() throws IOException
  58. {
  59. writeXMLDeclaration("1.0");
  60. } //}}}
  61. //{{{ writeXMLDeclaration() method
  62. /**
  63. * Write the XML declaration of a specific version.
  64. * This should be the first output.
  65. */
  66. public void writeXMLDeclaration(String version)
  67. throws IOException
  68. {
  69. write("<?xml"
  70. + " version=\"" + version + "\""
  71. + " encoding=\"" + encoding + "\""
  72. + " ?>");
  73. newLine();
  74. } //}}}
  75. //{{{ finish() method
  76. /**
  77. * Perform the final step of saving.
  78. */
  79. public void finish() throws IOException
  80. {
  81. close();
  82. jEdit.backupSettingsFile(file);
  83. file.delete();
  84. twoStageSaveFile.renameTo(file);
  85. knownLastModified = file.lastModified();
  86. } //}}}
  87. //{{{ Private members
  88. private File twoStageSaveFile;
  89. private static final String encoding = "UTF-8";
  90. // Only used by SettingsXML#opneSaver().
  91. Saver() throws IOException
  92. {
  93. this(new File(file.getParentFile(),
  94. "#" + file.getName() + "#save#"));
  95. }
  96. // A workaround for a restriction of super().
  97. private Saver(File twoStageSaveFile) throws IOException
  98. {
  99. super(new OutputStreamWriter(
  100. new FileOutputStream(twoStageSaveFile)
  101. , encoding));
  102. this.twoStageSaveFile = twoStageSaveFile;
  103. }
  104. //}}}
  105. } //}}}
  106. //{{{ Constructor
  107. /**
  108. * Construct a SettingsXML with specific location and name.
  109. * @param settingsDirectory
  110. * The settings directory of jedit
  111. * @param name
  112. * The file name will be (name + ".xml")
  113. */
  114. public SettingsXML(String settingsDirectory, String name)
  115. {
  116. String filename = name + ".xml";
  117. file = new File(MiscUtilities.constructPath(
  118. settingsDirectory, filename));
  119. } //}}}
  120. public SettingsXML(File f)
  121. {
  122. file = f;
  123. }
  124. //{{{ fileExits() method
  125. /**
  126. * Returns true if the file exists.
  127. */
  128. public boolean fileExists()
  129. {
  130. return file.exists();
  131. } //}}}
  132. //{{{ load() method
  133. /**
  134. * Parse the XML file to load.
  135. * @param handler
  136. * The handler to receive SAX notifications.
  137. */
  138. public void load(DefaultHandler handler) throws IOException
  139. {
  140. XMLUtilities.parseXML(new FileInputStream(file), handler);
  141. knownLastModified = file.lastModified();
  142. } //}}}
  143. //{{{ openSaver() method
  144. /**
  145. * Open the file to save in XML.
  146. */
  147. public Saver openSaver() throws IOException
  148. {
  149. return new Saver();
  150. } //}}}
  151. //{{{ hasChangedOnDisk() method
  152. /**
  153. * Returns true if the file has been changed on disk.
  154. * This is based on the last modified time at the last saving
  155. * or loading.
  156. */
  157. public boolean hasChangedOnDisk()
  158. {
  159. return file.exists()
  160. && (file.lastModified() != knownLastModified);
  161. } //}}}
  162. //{{{ toString() method
  163. /**
  164. * Returns the file's path.
  165. */
  166. public String toString()
  167. {
  168. return file.toString();
  169. } //}}}
  170. //{{{ Private members
  171. private File file;
  172. private long knownLastModified;
  173. //}}}
  174. }