PageRenderTime 91ms CodeModel.GetById 81ms app.highlight 8ms RepoModel.GetById 1ms 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
  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
 22package org.gjt.sp.jedit;
 23
 24//{{{ Imports
 25import java.io.File;
 26import java.io.IOException;
 27import java.io.FileOutputStream;
 28import java.io.FileInputStream;
 29import java.io.BufferedWriter;
 30import java.io.OutputStreamWriter;
 31
 32import org.xml.sax.helpers.DefaultHandler;
 33
 34import org.gjt.sp.util.XMLUtilities;
 35//}}}
 36
 37/**
 38 * A XML file in the settings directory.
 39 * This class provides some common operations to load/save settings
 40 * from/into a XML file.
 41 *   - Proper encoding and XML declaration.
 42 *   - Two stage save.
 43 *   - Making backup on each save.
 44 *   - Detection of change on disk.
 45 */
 46public class SettingsXML
 47{
 48	//{{{ Saver class
 49	/**
 50	 * A Writer to write XML for saving.
 51	 * The real settings file is not changed until the finish()
 52	 * method succeeds, in which case the previous settings file is
 53	 * backuped.
 54	 */
 55	public class Saver extends BufferedWriter
 56	{
 57		//{{{ writeXMLDeclaration() method
 58		/**
 59		 * Write the XML 1.0 declaration.
 60		 * This should be the first output.
 61		 */
 62		public void writeXMLDeclaration() throws IOException
 63		{
 64			writeXMLDeclaration("1.0");
 65		} //}}}
 66
 67		//{{{ writeXMLDeclaration() method
 68		/**
 69		 * Write the XML declaration of a specific version.
 70		 * This should be the first output.
 71		 */
 72		public void writeXMLDeclaration(String version)
 73			throws IOException
 74		{
 75			write("<?xml"
 76				+ " version=\"" + version + "\""
 77				+ " encoding=\"" + encoding + "\""
 78				+ " ?>");
 79			newLine();
 80		} //}}}
 81
 82		//{{{ finish() method
 83		/**
 84		 * Perform the final step of saving.
 85		 */
 86		public void finish() throws IOException
 87		{
 88			close();
 89			jEdit.backupSettingsFile(file);
 90			file.delete();
 91			twoStageSaveFile.renameTo(file);
 92			knownLastModified = file.lastModified();
 93		} //}}}
 94
 95		//{{{ Private members
 96		private File twoStageSaveFile;
 97		private static final String encoding = "UTF-8";
 98
 99		// Only used by SettingsXML#opneSaver().
100		Saver() throws IOException
101		{
102			this(new File(file.getParentFile(),
103				"#" + file.getName() + "#save#"));
104		}
105
106		// A workaround for a restriction of super().
107		private Saver(File twoStageSaveFile) throws IOException
108		{
109			super(new OutputStreamWriter(
110				new FileOutputStream(twoStageSaveFile)
111				, encoding));
112			this.twoStageSaveFile = twoStageSaveFile;
113		}
114
115		//}}}
116	} //}}}
117
118	//{{{ Constructor
119	/**
120	 * Construct a SettingsXML with specific location and name.
121	 * @param settingsDirectory
122	 * 	The settings directory of jedit
123	 * @param name
124	 * 	The file name will be (name + ".xml")
125	 */
126	public SettingsXML(String settingsDirectory, String name)
127	{
128		String filename = name + ".xml";
129		file = new File(MiscUtilities.constructPath(
130			settingsDirectory, filename));
131	} //}}}
132	
133	public SettingsXML(File f)
134	{
135		file = f;
136	}
137
138	//{{{ fileExits() method
139	/**
140	 * Returns true if the file exists.
141	 */
142	public boolean fileExists()
143	{
144		return file.exists();
145	} //}}}
146
147	//{{{ load() method
148	/**
149	 * Parse the XML file to load.
150	 * @param handler
151	 * 	The handler to receive SAX notifications.
152	 */
153	public void load(DefaultHandler handler) throws IOException
154	{
155		XMLUtilities.parseXML(new FileInputStream(file), handler);
156		knownLastModified = file.lastModified();
157	} //}}}
158
159	//{{{ openSaver() method
160	/**
161	 * Open the file to save in XML.
162	 */
163	public Saver openSaver() throws IOException
164	{
165		return new Saver();
166	} //}}}
167
168	//{{{ hasChangedOnDisk() method
169	/**
170	 * Returns true if the file has been changed on disk.
171	 * This is based on the last modified time at the last saving
172	 * or loading.
173	 */
174	public boolean hasChangedOnDisk()
175	{
176		return file.exists()
177			&& (file.lastModified() != knownLastModified);
178	} //}}}
179
180	//{{{ toString() method
181	/**
182	 * Returns the file's path.
183	 */
184	public String toString()
185	{
186		return file.toString();
187	} //}}}
188
189	//{{{ Private members
190	private File file;
191	private long knownLastModified;
192	//}}}
193}