1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
31 import java
.io
.RandomAccessFile
;
32 import java
.util
.ArrayList
;
35 Helper class to give a simple API to read/write windows like ini files
37 /* public */ // is only need, if we need this class outside package convwatch
41 * internal representation of the ini file content.
42 * Problem, if ini file changed why other write something difference, we don't realise this.
46 boolean m_bListContainUnsavedChanges
= false;
49 open a ini file by it's name
50 @param _sFilename string a filename, if the file doesn't exist, a new empty ini file will create.
51 write back to disk only if there are really changes.
53 public IniFile(String _sFilename
)
55 m_sFilename
= _sFilename
;
56 m_aList
= loadLines();
61 File aFile
= new File(m_sFilename
);
62 ArrayList aLines
= new ArrayList();
65 GlobalLogWriter
.get().println("couldn't find file " + m_sFilename
);
66 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
67 // m_bListContainUnsavedChanges = false;
70 RandomAccessFile aReader
= null;
73 aReader
= new RandomAccessFile(aFile
,"r");
77 aLine
= aReader
.readLine();
84 catch (java
.io
.FileNotFoundException fne
)
86 GlobalLogWriter
.get().println("couldn't open file " + m_sFilename
);
87 GlobalLogWriter
.get().println("Message: " + fne
.getMessage());
88 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
90 catch (java
.io
.IOException ie
)
92 GlobalLogWriter
.get().println("Exception occurs while reading from file " + m_sFilename
);
93 GlobalLogWriter
.get().println("Message: " + ie
.getMessage());
94 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
100 catch (java
.io
.IOException ie
)
102 GlobalLogWriter
.get().println("Couldn't close file " + m_sFilename
);
103 GlobalLogWriter
.get().println("Message: " + ie
.getMessage());
104 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
110 * @return true, if the ini file contain some readable data
114 return m_aList
.size() > 1 ?
true : false;
117 // -----------------------------------------------------------------------------
119 boolean isRemark(String _sLine
)
121 if ( ((_sLine
.length() < 2) ) ||
122 ( _sLine
.startsWith("#")) ||
123 ( _sLine
.startsWith(";")) )
130 String
getItem(int i
)
132 return (String
)m_aList
.get(i
);
135 String
buildSectionName(String _sSectionName
)
137 String sFindSection
= "[" + _sSectionName
+ "]";
140 String
toLowerIfNeed(String _sName
)
142 return _sName
.toLowerCase();
145 // return the number where this section starts
146 int findSection(String _sSection
)
148 String sFindSection
= toLowerIfNeed(buildSectionName(_sSection
));
149 // ----------- find _sSection ---------------
151 for (i
=0; i
<m_aList
.size();i
++)
153 String sLine
= toLowerIfNeed(getItem(i
).trim());
158 if (sFindSection
.equals("[]"))
160 // special case, empty Section.
163 if (sLine
.startsWith(sFindSection
))
171 // return the line number, where the key is found.
172 int findKey(String _sSection
, String _sKey
)
174 int i
= findSection(_sSection
);
177 // Section not found, therefore the value can't exist
180 return findKeyFromKnownSection(i
, _sKey
);
183 // i must be the index in the list, where the well known section starts
184 int findKeyFromKnownSection(int _nSectionIndex
, String _sKey
)
186 _sKey
= toLowerIfNeed(_sKey
);
187 for (int j
=_nSectionIndex
+ 1; j
<m_aList
.size();j
++)
189 String sLine
= getItem(j
).trim();
196 if (sLine
.startsWith("[") /* && sLine.endsWith("]") */)
202 int nEqual
= sLine
.indexOf("=");
205 String sKey
= toLowerIfNeed(sLine
.substring(0, nEqual
).trim());
206 if (sKey
.equals(_sKey
))
215 // i must be the index in the list, where the well known section starts
216 int findLastKnownKeyIndex(int _nSectionIndex
, String _sKey
)
218 _sKey
= toLowerIfNeed(_sKey
);
219 int i
= _nSectionIndex
+ 1;
220 for (int j
=i
; j
<m_aList
.size();j
++)
222 String sLine
= getItem(j
).trim();
229 if (sLine
.startsWith("[") /* && sLine.endsWith("]") */)
235 int nEqual
= sLine
.indexOf("=");
238 String sKey
= toLowerIfNeed(sLine
.substring(0, nEqual
).trim());
239 if (sKey
.equals(_sKey
))
248 String
getValue(int _nIndex
)
250 String sLine
= getItem(_nIndex
).trim();
255 int nEqual
= sLine
.indexOf("=");
258 String sKey
= sLine
.substring(0, nEqual
).trim();
259 String sValue
= sLine
.substring(nEqual
+ 1).trim();
266 @param _sSection string
268 @return the value found in the inifile which is given by the section and key parameter
270 public String
getValue(String _sSection
, String _sKey
)
273 int i
= findKey(_sSection
, _sKey
);
276 // Section not found, therefore the value can't exist
280 sValue
= getValue(i
);
286 write back the ini file to the disk, only if there exist changes
290 if (m_bListContainUnsavedChanges
== false)
292 // nothing has changed, so no need to store
296 File aFile
= new File(m_sFilename
);
299 // System.out.println("couldn't find file " + m_sFilename);
303 GlobalLogWriter
.get().println("Couldn't delete the file " + m_sFilename
);
305 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "Couldn't delete the file " + m_sFilename);
308 // if (! aFile.canWrite())
310 // System.out.println("Couldn't write to file " + m_sFilename);
311 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "");
315 RandomAccessFile aWriter
= new RandomAccessFile(aFile
, "rw");
316 for (int i
=0; i
<m_aList
.size();i
++)
318 String sLine
= getItem(i
);
319 aWriter
.writeBytes(sLine
);
320 aWriter
.writeByte((int)'\n');
325 catch (java
.io
.FileNotFoundException fne
)
327 GlobalLogWriter
.get().println("couldn't open file for writing " + m_sFilename
);
328 GlobalLogWriter
.get().println("Message: " + fne
.getMessage());
329 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
331 catch(java
.io
.IOException ie
)
333 GlobalLogWriter
.get().println("Exception occurs while writing to file " + m_sFilename
);
334 GlobalLogWriter
.get().println("Message: " + ie
.getMessage());
335 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
344 1. section doesn't exist, goto end and insert a new section, insert a new key value pair
345 2. section exist but key not, search section, search key, if key is -1 get last known key position and insert new key value pair there
346 3. section exist and key exist, remove the old key and insert the key value pair at the same position
348 public void insertValue(String _sSection
, String _sKey
, String _sValue
)
350 int i
= findSection(_sSection
);
353 // case 1: section doesn't exist
354 String sFindSection
= buildSectionName(_sSection
);
356 m_aList
.add(sFindSection
);
357 String sKeyValuePair
= _sKey
+ "=" + _sValue
;
358 m_aList
.add(sKeyValuePair
);
359 m_bListContainUnsavedChanges
= true;
362 int j
= findKeyFromKnownSection(i
, _sKey
);
365 // case 2: section exist, but not the key
366 j
= findLastKnownKeyIndex(i
, _sKey
);
367 String sKeyValuePair
= _sKey
+ "=" + _sValue
;
368 m_aList
.add(j
, sKeyValuePair
);
369 m_bListContainUnsavedChanges
= true;
374 // case 3: section exist, and also the key
375 String sKeyValuePair
= _sKey
+ "=" + _sValue
;
376 m_aList
.set(j
, sKeyValuePair
);
377 m_bListContainUnsavedChanges
= true;
380 // -----------------------------------------------------------------------------
381 // String replaceEvaluatedValue(String _sSection, String _sValue)
383 // String sValue = _sValue;
385 // while (( nIndex = sValue.indexOf("$(", nIndex)) >= 0)
387 // int nNextIndex = sValue.indexOf(")", nIndex);
388 // if (nNextIndex >= 0)
390 // String sKey = sValue.substring(nIndex + 2, nNextIndex);
391 // String sNewValue = getValue(_sSection, sKey);
392 // if (sNewValue != null && sNewValue.length() > 0)
394 // String sRegexpKey = "\\$\\(" + sKey + "\\)";
395 // sValue = sValue.replaceAll(sRegexpKey, sNewValue);
397 // nIndex = nNextIndex;
406 // -----------------------------------------------------------------------------
408 // public String getLocalEvaluatedValue(String _sSection, String _sKey)
410 // String sValue = getValue(_sSection, _sKey);
411 // sValue = replaceEvaluatedValue(_sSection, sValue);
415 // -----------------------------------------------------------------------------
417 // this is a special behaviour.
418 // public String getGlobalLocalEvaluatedValue(String _sSection, String _sKey)
420 // String sGlobalValue = getKey("global", _sKey);
421 // String sLocalValue = getKey(_sSection, _sKey);
422 // if (sLocalValue.length() == 0)
424 // sGlobalValue = replaceEvaluatedKey(_sSection, sGlobalValue);
425 // sGlobalValue = replaceEvaluatedKey("global", sGlobalValue);
426 // return sGlobalValue;
428 // sLocalValue = replaceEvaluatedKey(_sSection, sLocalValue);
429 // sLocalValue = replaceEvaluatedKey("global", sLocalValue);
431 // return sLocalValue;
436 * some tests for this class
438 // public static void main(String[] args)
440 // IniFile aIniFile = new IniFile("/tmp/inifile");
441 // String sValue = aIniFile.getValue("Section","Key");
442 // // insert a new value to a already exist section
443 // aIniFile.insertValue("Section","Key2","a new value in a existing section");
444 // // replace a value
445 // aIniFile.insertValue("Section","Key","replaced value");
446 // // create a new value
447 // aIniFile.insertValue("New Section", "Key", "a new key value pair");
449 // String sValue2 = aIniFile.getValue("Section2","Key");