merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / convwatch / IniFile.java
blobfbcaaea0b33b7c0b6531c9cbd96d7354fcec2d97
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 ************************************************************************/
28 package convwatch;
30 import java.io.File;
31 import java.io.RandomAccessFile;
32 import java.util.ArrayList;
34 /**
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
38 class IniFile
40 /**
41 * internal representation of the ini file content.
42 * Problem, if ini file changed why other write something difference, we don't realise this.
44 String m_sFilename;
45 ArrayList m_aList;
46 boolean m_bListContainUnsavedChanges = false;
48 /**
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();
59 ArrayList loadLines()
61 File aFile = new File(m_sFilename);
62 ArrayList aLines = new ArrayList();
63 if (! aFile.exists())
65 GlobalLogWriter.get().println("couldn't find file " + m_sFilename);
66 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
67 // m_bListContainUnsavedChanges = false;
68 return aLines;
70 RandomAccessFile aReader = null;
71 try
73 aReader = new RandomAccessFile(aFile,"r");
74 String aLine = "";
75 while (aLine != null)
77 aLine = aReader.readLine();
78 if (aLine != null)
80 aLines.add(aLine);
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());
96 try
98 aReader.close();
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());
106 return aLines;
110 * @return true, if the ini file contain some readable data
112 public boolean is()
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(";")) )
125 return true;
127 return false;
130 String getItem(int i)
132 return (String)m_aList.get(i);
135 String buildSectionName(String _sSectionName)
137 String sFindSection = "[" + _sSectionName + "]";
138 return sFindSection;
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 ---------------
150 int i;
151 for (i=0; i<m_aList.size();i++)
153 String sLine = toLowerIfNeed(getItem(i).trim());
154 if (isRemark(sLine))
156 continue;
158 if (sFindSection.equals("[]"))
160 // special case, empty Section.
161 return i - 1;
163 if (sLine.startsWith(sFindSection))
165 return i;
168 return -1;
171 // return the line number, where the key is found.
172 int findKey(String _sSection, String _sKey)
174 int i = findSection(_sSection);
175 if (i == -1)
177 // Section not found, therefore the value can't exist
178 return -1;
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();
191 if (isRemark(sLine))
193 continue;
196 if (sLine.startsWith("[") /* && sLine.endsWith("]") */)
198 // found end.
199 break;
202 int nEqual = sLine.indexOf("=");
203 if (nEqual >= 0)
205 String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim());
206 if (sKey.equals(_sKey))
208 return j;
212 return -1;
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();
224 if (isRemark(sLine))
226 continue;
229 if (sLine.startsWith("[") /* && sLine.endsWith("]") */)
231 // found end.
232 return j;
235 int nEqual = sLine.indexOf("=");
236 if (nEqual >= 0)
238 String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim());
239 if (sKey.equals(_sKey))
241 return j;
245 return i;
248 String getValue(int _nIndex)
250 String sLine = getItem(_nIndex).trim();
251 if (isRemark(sLine))
253 return "";
255 int nEqual = sLine.indexOf("=");
256 if (nEqual >= 0)
258 String sKey = sLine.substring(0, nEqual).trim();
259 String sValue = sLine.substring(nEqual + 1).trim();
260 return sValue;
262 return "";
266 @param _sSection string
267 @param _sKey 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)
272 String sValue = "";
273 int i = findKey(_sSection, _sKey);
274 if (i == -1)
276 // Section not found, therefore the value can't exist
277 return "";
280 sValue = getValue(i);
282 return sValue;
286 write back the ini file to the disk, only if there exist changes
288 public void store()
290 if (m_bListContainUnsavedChanges == false)
292 // nothing has changed, so no need to store
293 return;
296 File aFile = new File(m_sFilename);
297 if (aFile.exists())
299 // System.out.println("couldn't find file " + m_sFilename);
300 aFile.delete();
301 if (aFile.exists())
303 GlobalLogWriter.get().println("Couldn't delete the file " + m_sFilename);
304 return;
305 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "Couldn't delete the file " + m_sFilename);
308 // if (! aFile.canWrite())
309 // {
310 // System.out.println("Couldn't write to file " + m_sFilename);
311 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "");
312 // }
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');
322 aWriter.close();
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());
342 insert a value
343 there are 3 cases
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);
351 if (i == -1)
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;
360 return;
362 int j = findKeyFromKnownSection(i, _sKey);
363 if (j == -1)
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;
370 return;
372 else
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)
382 // {
383 // String sValue = _sValue;
384 // int nIndex = 0;
385 // while (( nIndex = sValue.indexOf("$(", nIndex)) >= 0)
386 // {
387 // int nNextIndex = sValue.indexOf(")", nIndex);
388 // if (nNextIndex >= 0)
389 // {
390 // String sKey = sValue.substring(nIndex + 2, nNextIndex);
391 // String sNewValue = getValue(_sSection, sKey);
392 // if (sNewValue != null && sNewValue.length() > 0)
393 // {
394 // String sRegexpKey = "\\$\\(" + sKey + "\\)";
395 // sValue = sValue.replaceAll(sRegexpKey, sNewValue);
396 // }
397 // nIndex = nNextIndex;
398 // }
399 // else
400 // {
401 // nIndex += 2;
402 // }
403 // }
404 // return sValue;
405 // }
406 // -----------------------------------------------------------------------------
408 // public String getLocalEvaluatedValue(String _sSection, String _sKey)
409 // {
410 // String sValue = getValue(_sSection, _sKey);
411 // sValue = replaceEvaluatedValue(_sSection, sValue);
412 // return sValue;
413 // }
415 // -----------------------------------------------------------------------------
417 // this is a special behaviour.
418 // public String getGlobalLocalEvaluatedValue(String _sSection, String _sKey)
419 // {
420 // String sGlobalValue = getKey("global", _sKey);
421 // String sLocalValue = getKey(_sSection, _sKey);
422 // if (sLocalValue.length() == 0)
423 // {
424 // sGlobalValue = replaceEvaluatedKey(_sSection, sGlobalValue);
425 // sGlobalValue = replaceEvaluatedKey("global", sGlobalValue);
426 // return sGlobalValue;
427 // }
428 // sLocalValue = replaceEvaluatedKey(_sSection, sLocalValue);
429 // sLocalValue = replaceEvaluatedKey("global", sLocalValue);
431 // return sLocalValue;
432 // }
436 * some tests for this class
438 // public static void main(String[] args)
439 // {
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");
450 // aIniFile.store();
451 // }