741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
791 |
23 Oct 06 |
olle |
55 |
package se.lu.thep.coreftpd.common; |
741 |
10 Oct 06 |
olle |
56 |
|
741 |
10 Oct 06 |
olle |
57 |
import java.io.BufferedReader; |
741 |
10 Oct 06 |
olle |
58 |
import java.io.BufferedWriter; |
741 |
10 Oct 06 |
olle |
59 |
import java.io.File; |
741 |
10 Oct 06 |
olle |
60 |
import java.io.FileReader; |
741 |
10 Oct 06 |
olle |
61 |
import java.io.FileWriter; |
741 |
10 Oct 06 |
olle |
62 |
import java.io.PrintWriter; |
741 |
10 Oct 06 |
olle |
63 |
|
741 |
10 Oct 06 |
olle |
64 |
/** |
741 |
10 Oct 06 |
olle |
* <B>About this class:</B> <BR> |
741 |
10 Oct 06 |
olle |
* A very useful class if you want an application with a settings file which can |
741 |
10 Oct 06 |
olle |
* easily be edited, both by a human and by this class. <BR> |
741 |
10 Oct 06 |
olle |
* This class can both write to and read from the database (the file). <BR> |
741 |
10 Oct 06 |
olle |
* This class can also make basic file operations such as deleting, copying and |
741 |
10 Oct 06 |
olle |
* renameing the file. |
741 |
10 Oct 06 |
olle |
71 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
74 |
*/ |
1652 |
22 May 07 |
gregory |
75 |
final public class DatabaseFile { |
741 |
10 Oct 06 |
olle |
76 |
private File f_file; |
1652 |
22 May 07 |
gregory |
77 |
|
741 |
10 Oct 06 |
olle |
78 |
private String s_filePath; |
1652 |
22 May 07 |
gregory |
79 |
|
741 |
10 Oct 06 |
olle |
80 |
/** |
741 |
10 Oct 06 |
olle |
* Lines starting with this character are comments. |
741 |
10 Oct 06 |
olle |
82 |
*/ |
741 |
10 Oct 06 |
olle |
83 |
private char commentChar = ';'; |
1652 |
22 May 07 |
gregory |
84 |
|
741 |
10 Oct 06 |
olle |
85 |
/** |
741 |
10 Oct 06 |
olle |
* This is the separator between a name and its value. <BR> |
741 |
10 Oct 06 |
olle |
* Example: Name=John Smith |
741 |
10 Oct 06 |
olle |
88 |
*/ |
741 |
10 Oct 06 |
olle |
89 |
private char assignChar = '='; |
741 |
10 Oct 06 |
olle |
90 |
|
1652 |
22 May 07 |
gregory |
91 |
public DatabaseFile(String argFile) throws Exception { |
741 |
10 Oct 06 |
olle |
92 |
this(new File(argFile)); |
741 |
10 Oct 06 |
olle |
93 |
} |
741 |
10 Oct 06 |
olle |
94 |
|
1652 |
22 May 07 |
gregory |
95 |
public DatabaseFile(File argFile) throws Exception { |
741 |
10 Oct 06 |
olle |
// if (argFile.exists()) |
741 |
10 Oct 06 |
olle |
97 |
// { |
741 |
10 Oct 06 |
olle |
98 |
f_file = argFile; |
741 |
10 Oct 06 |
olle |
99 |
s_filePath = f_file.getPath(); |
741 |
10 Oct 06 |
olle |
100 |
// } |
741 |
10 Oct 06 |
olle |
// else |
741 |
10 Oct 06 |
olle |
102 |
// { |
741 |
10 Oct 06 |
olle |
// new Exception("File does not exists when creating a new |
741 |
10 Oct 06 |
olle |
// DatabaseFile!"); |
741 |
10 Oct 06 |
olle |
105 |
// } |
741 |
10 Oct 06 |
olle |
106 |
} |
741 |
10 Oct 06 |
olle |
107 |
|
741 |
10 Oct 06 |
olle |
108 |
/** |
741 |
10 Oct 06 |
olle |
* Sets <CODE>assignChar</CODE> (see <CODE>assignChar</CODE> |
741 |
10 Oct 06 |
olle |
* documentation). |
741 |
10 Oct 06 |
olle |
111 |
*/ |
1652 |
22 May 07 |
gregory |
112 |
synchronized public void setAssignChar(char argAssign) { |
741 |
10 Oct 06 |
olle |
113 |
assignChar = argAssign; |
741 |
10 Oct 06 |
olle |
114 |
} |
741 |
10 Oct 06 |
olle |
115 |
|
741 |
10 Oct 06 |
olle |
116 |
/** |
741 |
10 Oct 06 |
olle |
* Sets <CODE>commentChar</CODE> (see <CODE>commentChar</CODE> |
741 |
10 Oct 06 |
olle |
* documentation). |
741 |
10 Oct 06 |
olle |
119 |
*/ |
1652 |
22 May 07 |
gregory |
120 |
synchronized public void setCommentChar(char argComment) { |
741 |
10 Oct 06 |
olle |
121 |
commentChar = argComment; |
741 |
10 Oct 06 |
olle |
122 |
} |
741 |
10 Oct 06 |
olle |
123 |
|
741 |
10 Oct 06 |
olle |
124 |
/** |
741 |
10 Oct 06 |
olle |
* Removes a value from the database. |
741 |
10 Oct 06 |
olle |
126 |
*/ |
1652 |
22 May 07 |
gregory |
127 |
synchronized public boolean removeValue(String name) throws Exception { |
741 |
10 Oct 06 |
olle |
128 |
return updateValue(name, null); |
741 |
10 Oct 06 |
olle |
129 |
} |
741 |
10 Oct 06 |
olle |
130 |
|
741 |
10 Oct 06 |
olle |
131 |
/** |
741 |
10 Oct 06 |
olle |
* Updates the value of this variable. <BR> |
741 |
10 Oct 06 |
olle |
* IMPORTANT: If "<CODE>value</CODE>" is <CODE>null</CODE>, then it |
741 |
10 Oct 06 |
olle |
* means that we shall REMOVE "<CODE>name</CODE>" from the file! |
741 |
10 Oct 06 |
olle |
135 |
* |
741 |
10 Oct 06 |
olle |
* @return Returns true iff "name" exists in file and the value has been |
741 |
10 Oct 06 |
olle |
* replaced with "value". Returns false iff "name" does not exists |
741 |
10 Oct 06 |
olle |
* in file. |
741 |
10 Oct 06 |
olle |
139 |
*/ |
741 |
10 Oct 06 |
olle |
140 |
synchronized public boolean updateValue(String name, String value) |
1652 |
22 May 07 |
gregory |
141 |
throws Exception { |
741 |
10 Oct 06 |
olle |
142 |
boolean retValue = false; |
741 |
10 Oct 06 |
olle |
143 |
File f_tmpFile = File.createTempFile("Xerver", ".tmp"); |
741 |
10 Oct 06 |
olle |
144 |
BufferedReader br_userFile = new BufferedReader(new FileReader(f_file)); |
741 |
10 Oct 06 |
olle |
145 |
PrintWriter bw_newFile = new PrintWriter(new BufferedWriter( |
1652 |
22 May 07 |
gregory |
146 |
new FileWriter(f_tmpFile))); |
741 |
10 Oct 06 |
olle |
147 |
String lineNotModified, tmpLine; |
1652 |
22 May 07 |
gregory |
148 |
while ((lineNotModified = tmpLine = br_userFile.readLine()) != null) { |
741 |
10 Oct 06 |
olle |
149 |
tmpLine.trim(); |
1652 |
22 May 07 |
gregory |
150 |
if (tmpLine.length() > 0) { |
1652 |
22 May 07 |
gregory |
151 |
if (tmpLine.charAt(0) != commentChar) { |
741 |
10 Oct 06 |
olle |
152 |
String varName, varValue; |
741 |
10 Oct 06 |
olle |
153 |
int indexOfValue = tmpLine.indexOf(assignChar); |
741 |
10 Oct 06 |
olle |
154 |
varName = tmpLine.substring(0, indexOfValue); |
741 |
10 Oct 06 |
olle |
155 |
varValue = tmpLine.substring(indexOfValue + 1); |
1652 |
22 May 07 |
gregory |
156 |
if (name.equalsIgnoreCase(varName)) { |
741 |
10 Oct 06 |
olle |
157 |
retValue = true; |
741 |
10 Oct 06 |
olle |
158 |
if (value != null) // If we have a value for "name", we |
1652 |
22 May 07 |
gregory |
// shall UPDATE name (otherwise |
1652 |
22 May 07 |
gregory |
// remove name) |
741 |
10 Oct 06 |
olle |
161 |
bw_newFile.println(varName + assignChar + value); |
741 |
10 Oct 06 |
olle |
// else //Otherwise we don't do anything here, resulting |
741 |
10 Oct 06 |
olle |
// that we REMOIVE "name" from the file! |
741 |
10 Oct 06 |
olle |
164 |
continue; // Don't write "lineNotModified" to file |
741 |
10 Oct 06 |
olle |
165 |
} |
741 |
10 Oct 06 |
olle |
166 |
} |
741 |
10 Oct 06 |
olle |
167 |
} |
741 |
10 Oct 06 |
olle |
168 |
bw_newFile.println(lineNotModified); |
741 |
10 Oct 06 |
olle |
169 |
} |
741 |
10 Oct 06 |
olle |
// Close everything so file won't be locked by JVM to long |
741 |
10 Oct 06 |
olle |
171 |
br_userFile.close(); |
741 |
10 Oct 06 |
olle |
172 |
bw_newFile.flush(); |
741 |
10 Oct 06 |
olle |
173 |
bw_newFile.close(); |
741 |
10 Oct 06 |
olle |
174 |
bw_newFile = null; |
741 |
10 Oct 06 |
olle |
175 |
br_userFile = null; |
1652 |
22 May 07 |
gregory |
176 |
if (f_file.delete()) { |
741 |
10 Oct 06 |
olle |
177 |
if (!f_tmpFile.renameTo(f_file)) // On Solaris File.renameTo() |
1652 |
22 May 07 |
gregory |
// cannot move a file from one |
1652 |
22 May 07 |
gregory |
// file system to another... |
741 |
10 Oct 06 |
olle |
180 |
{ |
741 |
10 Oct 06 |
olle |
181 |
FileWithCopy.copyFile(f_tmpFile, f_file); |
741 |
10 Oct 06 |
olle |
182 |
} |
741 |
10 Oct 06 |
olle |
183 |
} |
741 |
10 Oct 06 |
olle |
184 |
f_file = new File(s_filePath); // Renew "f_file" for next run... |
741 |
10 Oct 06 |
olle |
185 |
return retValue; |
741 |
10 Oct 06 |
olle |
186 |
} |
741 |
10 Oct 06 |
olle |
187 |
|
741 |
10 Oct 06 |
olle |
188 |
/** |
741 |
10 Oct 06 |
olle |
* Add a new empty line to the file. |
741 |
10 Oct 06 |
olle |
190 |
*/ |
1652 |
22 May 07 |
gregory |
191 |
synchronized public void addNewLine() throws Exception { |
741 |
10 Oct 06 |
olle |
192 |
PrintWriter bw_newFile = new PrintWriter(new BufferedWriter( |
1652 |
22 May 07 |
gregory |
193 |
new FileWriter(f_file.getAbsolutePath(), true))); // Append |
1652 |
22 May 07 |
gregory |
// data... Note: |
1652 |
22 May 07 |
gregory |
// we need |
1652 |
22 May 07 |
gregory |
// ".getAbsolutePath()") |
1652 |
22 May 07 |
gregory |
// becuase |
1652 |
22 May 07 |
gregory |
// "FileWriter(File,boolean)" |
1652 |
22 May 07 |
gregory |
// only exists |
1652 |
22 May 07 |
gregory |
// on Java 1.4 |
1652 |
22 May 07 |
gregory |
// and above. |
1652 |
22 May 07 |
gregory |
// (Without |
1652 |
22 May 07 |
gregory |
// this, users |
1652 |
22 May 07 |
gregory |
// with older |
1652 |
22 May 07 |
gregory |
// Java versions |
1652 |
22 May 07 |
gregory |
// would get an |
1652 |
22 May 07 |
gregory |
// error during |
1652 |
22 May 07 |
gregory |
// runtime). |
741 |
10 Oct 06 |
olle |
209 |
bw_newFile.println(); |
741 |
10 Oct 06 |
olle |
210 |
bw_newFile.close(); |
741 |
10 Oct 06 |
olle |
211 |
bw_newFile = null; |
741 |
10 Oct 06 |
olle |
212 |
} |
741 |
10 Oct 06 |
olle |
213 |
|
741 |
10 Oct 06 |
olle |
214 |
/** |
741 |
10 Oct 06 |
olle |
* This adds a new <CODE>name</CODE>/<CODE>value</CODE> couple at the |
741 |
10 Oct 06 |
olle |
* end of the file. <B>IMPORTANT:</B> If "name" already exists, a new name |
741 |
10 Oct 06 |
olle |
* will be added anyway, resulting in having two (or more) "variables" with |
741 |
10 Oct 06 |
olle |
* possibly different values!!! <BR> |
741 |
10 Oct 06 |
olle |
* This member shall be used with CARE!! <BR> |
741 |
10 Oct 06 |
olle |
* Also, if you use this member, make sure the file's last line ends with a |
741 |
10 Oct 06 |
olle |
* newline-character!! <BR> |
741 |
10 Oct 06 |
olle |
* Use setValue or updateValue instead to avoid these problems. (or just |
741 |
10 Oct 06 |
olle |
* call addNewLine() once before you call this method the very first time |
741 |
10 Oct 06 |
olle |
* and you will be safe). |
741 |
10 Oct 06 |
olle |
225 |
*/ |
741 |
10 Oct 06 |
olle |
226 |
synchronized public void addNewValue(String name, String value) |
1652 |
22 May 07 |
gregory |
227 |
throws Exception { |
741 |
10 Oct 06 |
olle |
228 |
PrintWriter bw_newFile = new PrintWriter(new BufferedWriter( |
1652 |
22 May 07 |
gregory |
229 |
new FileWriter(f_file.getAbsolutePath(), true))); // Append |
1652 |
22 May 07 |
gregory |
// data... Note: |
1652 |
22 May 07 |
gregory |
// we need |
1652 |
22 May 07 |
gregory |
// ".getAbsolutePath()") |
1652 |
22 May 07 |
gregory |
// becuase |
1652 |
22 May 07 |
gregory |
// "FileWriter(File,boolean)" |
1652 |
22 May 07 |
gregory |
// only exists |
1652 |
22 May 07 |
gregory |
// on Java 1.4 |
1652 |
22 May 07 |
gregory |
// and above. |
1652 |
22 May 07 |
gregory |
// (Without |
1652 |
22 May 07 |
gregory |
// this, users |
1652 |
22 May 07 |
gregory |
// with older |
1652 |
22 May 07 |
gregory |
// Java versions |
1652 |
22 May 07 |
gregory |
// would get an |
1652 |
22 May 07 |
gregory |
// error during |
1652 |
22 May 07 |
gregory |
// runtime). |
741 |
10 Oct 06 |
olle |
245 |
bw_newFile.println(name + assignChar + value); |
741 |
10 Oct 06 |
olle |
246 |
bw_newFile.close(); |
741 |
10 Oct 06 |
olle |
247 |
bw_newFile = null; |
741 |
10 Oct 06 |
olle |
248 |
} |
741 |
10 Oct 06 |
olle |
249 |
|
741 |
10 Oct 06 |
olle |
250 |
/** |
741 |
10 Oct 06 |
olle |
* Add a comment to the end of this file. <BR> |
741 |
10 Oct 06 |
olle |
* <B>IMPORTANT:</B> If you use this member, make sure the file's last line |
741 |
10 Oct 06 |
olle |
* ends with a newline-character. (or just call addNewLine() once before you |
741 |
10 Oct 06 |
olle |
* call this method the very first time and you will be safe). |
741 |
10 Oct 06 |
olle |
255 |
*/ |
1652 |
22 May 07 |
gregory |
256 |
synchronized public void addNewComment(String comment) throws Exception { |
741 |
10 Oct 06 |
olle |
257 |
PrintWriter bw_newFile = new PrintWriter(new BufferedWriter( |
1652 |
22 May 07 |
gregory |
258 |
new FileWriter(f_file.getAbsolutePath(), true))); // Append |
1652 |
22 May 07 |
gregory |
// data... Note: |
1652 |
22 May 07 |
gregory |
// we need |
1652 |
22 May 07 |
gregory |
// ".getAbsolutePath()") |
1652 |
22 May 07 |
gregory |
// becuase |
1652 |
22 May 07 |
gregory |
// "FileWriter(File,boolean)" |
1652 |
22 May 07 |
gregory |
// only exists |
1652 |
22 May 07 |
gregory |
// on Java 1.4 |
1652 |
22 May 07 |
gregory |
// and above. |
1652 |
22 May 07 |
gregory |
// (Without |
1652 |
22 May 07 |
gregory |
// this, users |
1652 |
22 May 07 |
gregory |
// with older |
1652 |
22 May 07 |
gregory |
// Java versions |
1652 |
22 May 07 |
gregory |
// would get an |
1652 |
22 May 07 |
gregory |
// error during |
1652 |
22 May 07 |
gregory |
// runtime). |
741 |
10 Oct 06 |
olle |
274 |
bw_newFile.println(commentChar + comment); |
741 |
10 Oct 06 |
olle |
275 |
bw_newFile.close(); |
741 |
10 Oct 06 |
olle |
276 |
bw_newFile = null; |
741 |
10 Oct 06 |
olle |
277 |
} |
741 |
10 Oct 06 |
olle |
278 |
|
741 |
10 Oct 06 |
olle |
279 |
/** |
741 |
10 Oct 06 |
olle |
* If <CODE>name</CODE> already exists, update its value- <BR> |
741 |
10 Oct 06 |
olle |
* If <CODE>name</CODE> doesn't exists, creates a new "variable" with this |
741 |
10 Oct 06 |
olle |
* name/value. |
741 |
10 Oct 06 |
olle |
283 |
*/ |
741 |
10 Oct 06 |
olle |
284 |
synchronized public void setValue(String name, String value) |
1652 |
22 May 07 |
gregory |
285 |
throws Exception { |
741 |
10 Oct 06 |
olle |
// First we try to update the value, if not possible, we add the new |
741 |
10 Oct 06 |
olle |
// value |
1652 |
22 May 07 |
gregory |
288 |
if (!updateValue(name, value)) { |
741 |
10 Oct 06 |
olle |
289 |
addNewLine();// Add a new line to avoid problems if file does not |
1652 |
22 May 07 |
gregory |
// end with a newline-character in the end of the |
1652 |
22 May 07 |
gregory |
// last line in the file. |
741 |
10 Oct 06 |
olle |
292 |
addNewValue(name, value); |
741 |
10 Oct 06 |
olle |
293 |
} |
741 |
10 Oct 06 |
olle |
294 |
} |
741 |
10 Oct 06 |
olle |
295 |
|
741 |
10 Oct 06 |
olle |
296 |
/** |
741 |
10 Oct 06 |
olle |
* Returns the value from the variable "name". <BR> |
741 |
10 Oct 06 |
olle |
* This means searching the file for a line containing "name=value" and |
741 |
10 Oct 06 |
olle |
* returning "value". |
741 |
10 Oct 06 |
olle |
300 |
*/ |
1652 |
22 May 07 |
gregory |
301 |
synchronized public String getValue(String name) throws Exception { |
741 |
10 Oct 06 |
olle |
302 |
BufferedReader br_userFile = new BufferedReader(new FileReader(f_file)); |
741 |
10 Oct 06 |
olle |
303 |
String retValue = null; |
741 |
10 Oct 06 |
olle |
304 |
String tmpLine; |
1652 |
22 May 07 |
gregory |
305 |
while ((tmpLine = br_userFile.readLine()) != null) { |
741 |
10 Oct 06 |
olle |
306 |
tmpLine.trim(); |
1652 |
22 May 07 |
gregory |
307 |
if (tmpLine.length() > 0) { |
1652 |
22 May 07 |
gregory |
308 |
if (tmpLine.charAt(0) != commentChar) { |
741 |
10 Oct 06 |
olle |
309 |
String varName, varValue; |
741 |
10 Oct 06 |
olle |
310 |
int indexOfValue = tmpLine.indexOf(assignChar); |
741 |
10 Oct 06 |
olle |
311 |
varName = tmpLine.substring(0, indexOfValue); |
741 |
10 Oct 06 |
olle |
312 |
varValue = tmpLine.substring(indexOfValue + 1); |
1652 |
22 May 07 |
gregory |
313 |
if (name.equalsIgnoreCase(varName)) { |
741 |
10 Oct 06 |
olle |
314 |
retValue = varValue; |
741 |
10 Oct 06 |
olle |
315 |
break; |
741 |
10 Oct 06 |
olle |
316 |
} |
741 |
10 Oct 06 |
olle |
317 |
} |
741 |
10 Oct 06 |
olle |
318 |
} |
741 |
10 Oct 06 |
olle |
319 |
} |
741 |
10 Oct 06 |
olle |
320 |
br_userFile.close(); |
741 |
10 Oct 06 |
olle |
321 |
br_userFile = null; |
741 |
10 Oct 06 |
olle |
322 |
return retValue; // Value not found |
741 |
10 Oct 06 |
olle |
323 |
} |
741 |
10 Oct 06 |
olle |
324 |
|
741 |
10 Oct 06 |
olle |
325 |
/** |
741 |
10 Oct 06 |
olle |
* Renames the database file. <BR> |
741 |
10 Oct 06 |
olle |
327 |
* |
741 |
10 Oct 06 |
olle |
* @return Returns true iff renaming file was successful. |
741 |
10 Oct 06 |
olle |
329 |
*/ |
1652 |
22 May 07 |
gregory |
330 |
synchronized public boolean renameFile(String newPath) { |
741 |
10 Oct 06 |
olle |
331 |
boolean retValue = f_file.renameTo(new File(newPath)); |
1652 |
22 May 07 |
gregory |
332 |
if (retValue) { |
741 |
10 Oct 06 |
olle |
333 |
s_filePath = f_file.getPath(); |
741 |
10 Oct 06 |
olle |
334 |
} |
741 |
10 Oct 06 |
olle |
335 |
return retValue; |
741 |
10 Oct 06 |
olle |
336 |
} |
741 |
10 Oct 06 |
olle |
337 |
|
741 |
10 Oct 06 |
olle |
338 |
/** |
741 |
10 Oct 06 |
olle |
* Creates a copy of this database file. <BR> |
741 |
10 Oct 06 |
olle |
340 |
* |
741 |
10 Oct 06 |
olle |
* @return Returns true iff copying file was successful. |
741 |
10 Oct 06 |
olle |
342 |
*/ |
1652 |
22 May 07 |
gregory |
343 |
synchronized public boolean copyFile(String newPath) { |
741 |
10 Oct 06 |
olle |
344 |
File newFile = new File(newPath); |
741 |
10 Oct 06 |
olle |
345 |
if (newFile.exists()) |
741 |
10 Oct 06 |
olle |
346 |
return false; // We choose that this method shall not overwrite |
1652 |
22 May 07 |
gregory |
// the destination file in case it already exists |
741 |
10 Oct 06 |
olle |
348 |
return FileWithCopy.copyFile(f_file, newFile); |
741 |
10 Oct 06 |
olle |
349 |
} |
741 |
10 Oct 06 |
olle |
350 |
|
741 |
10 Oct 06 |
olle |
351 |
/** |
741 |
10 Oct 06 |
olle |
* Deletes the database file. <BR> |
741 |
10 Oct 06 |
olle |
353 |
* |
741 |
10 Oct 06 |
olle |
* @return Returns true iff deleting file was successful. |
741 |
10 Oct 06 |
olle |
355 |
*/ |
1652 |
22 May 07 |
gregory |
356 |
synchronized public boolean deleteFile() { |
741 |
10 Oct 06 |
olle |
357 |
return f_file.delete(); |
741 |
10 Oct 06 |
olle |
358 |
} |
741 |
10 Oct 06 |
olle |
359 |
|
741 |
10 Oct 06 |
olle |
360 |
/** |
741 |
10 Oct 06 |
olle |
* @return Returns true iff file exists. |
741 |
10 Oct 06 |
olle |
362 |
*/ |
1652 |
22 May 07 |
gregory |
363 |
synchronized public boolean fileExists() { |
741 |
10 Oct 06 |
olle |
364 |
return f_file.exists() && !f_file.isDirectory(); |
741 |
10 Oct 06 |
olle |
365 |
} |
741 |
10 Oct 06 |
olle |
366 |
|
741 |
10 Oct 06 |
olle |
367 |
/** |
741 |
10 Oct 06 |
olle |
* Creates this database file. <BR> |
741 |
10 Oct 06 |
olle |
369 |
* |
741 |
10 Oct 06 |
olle |
* @return Returns true iff creating file was successful. |
741 |
10 Oct 06 |
olle |
371 |
*/ |
1652 |
22 May 07 |
gregory |
372 |
synchronized public boolean createNewFile() { |
1652 |
22 May 07 |
gregory |
373 |
try { |
741 |
10 Oct 06 |
olle |
374 |
return f_file.createNewFile(); |
1652 |
22 May 07 |
gregory |
375 |
} catch (Exception e) { |
741 |
10 Oct 06 |
olle |
376 |
return false; // Don't throw back exception, just return false... |
1652 |
22 May 07 |
gregory |
// (For example when file already exists) |
741 |
10 Oct 06 |
olle |
378 |
} |
741 |
10 Oct 06 |
olle |
379 |
} |
741 |
10 Oct 06 |
olle |
380 |
|
741 |
10 Oct 06 |
olle |
381 |
/** |
741 |
10 Oct 06 |
olle |
* Run this when you are finished with reading/writing to the database. In |
741 |
10 Oct 06 |
olle |
* case the JVM has locked the database file, it will be released after |
741 |
10 Oct 06 |
olle |
* this. |
741 |
10 Oct 06 |
olle |
385 |
*/ |
1652 |
22 May 07 |
gregory |
386 |
public void destroy() { |
741 |
10 Oct 06 |
olle |
387 |
f_file = null; |
741 |
10 Oct 06 |
olle |
388 |
s_filePath = null; |
741 |
10 Oct 06 |
olle |
389 |
System.gc(); // If this is not runned the file might be locked by JVM |
1652 |
22 May 07 |
gregory |
// until the Garbage Collector (gc) is run. To make sure |
1652 |
22 May 07 |
gregory |
// the file is released we must force JVM to run gc. |
1652 |
22 May 07 |
gregory |
// However, it might take time to run garbage collector, |
1652 |
22 May 07 |
gregory |
// so this is a priority issue. |
741 |
10 Oct 06 |
olle |
394 |
} |
741 |
10 Oct 06 |
olle |
395 |
} |