xref: /AOO41X/main/qadevOOo/runner/convwatch/IniFile.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package convwatch;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.File;
31*cdf0e10cSrcweir import java.io.RandomAccessFile;
32*cdf0e10cSrcweir import java.util.ArrayList;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /**
35*cdf0e10cSrcweir    Helper class to give a simple API to read/write windows like ini files
36*cdf0e10cSrcweir  */
37*cdf0e10cSrcweir /* public */ // is only need, if we need this class outside package convwatch
38*cdf0e10cSrcweir class IniFile
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir     /**
41*cdf0e10cSrcweir      * internal representation of the ini file content.
42*cdf0e10cSrcweir      * Problem, if ini file changed why other write something difference, we don't realise this.
43*cdf0e10cSrcweir      */
44*cdf0e10cSrcweir     String m_sFilename;
45*cdf0e10cSrcweir     ArrayList m_aList;
46*cdf0e10cSrcweir     boolean m_bListContainUnsavedChanges = false;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir     /**
49*cdf0e10cSrcweir        open a ini file by it's name
50*cdf0e10cSrcweir        @param _sFilename string a filename, if the file doesn't exist, a new empty ini file will create.
51*cdf0e10cSrcweir        write back to disk only if there are really changes.
52*cdf0e10cSrcweir      */
53*cdf0e10cSrcweir     public IniFile(String _sFilename)
54*cdf0e10cSrcweir         {
55*cdf0e10cSrcweir             m_sFilename = _sFilename;
56*cdf0e10cSrcweir             m_aList = loadLines();
57*cdf0e10cSrcweir         }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     ArrayList loadLines()
60*cdf0e10cSrcweir         {
61*cdf0e10cSrcweir             File aFile = new File(m_sFilename);
62*cdf0e10cSrcweir             ArrayList aLines = new ArrayList();
63*cdf0e10cSrcweir             if (! aFile.exists())
64*cdf0e10cSrcweir             {
65*cdf0e10cSrcweir                 GlobalLogWriter.get().println("couldn't find file " + m_sFilename);
66*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
67*cdf0e10cSrcweir                 // m_bListContainUnsavedChanges = false;
68*cdf0e10cSrcweir                 return aLines;
69*cdf0e10cSrcweir             }
70*cdf0e10cSrcweir             RandomAccessFile aReader = null;
71*cdf0e10cSrcweir             try
72*cdf0e10cSrcweir             {
73*cdf0e10cSrcweir                 aReader = new RandomAccessFile(aFile,"r");
74*cdf0e10cSrcweir                 String aLine = "";
75*cdf0e10cSrcweir                 while (aLine != null)
76*cdf0e10cSrcweir                 {
77*cdf0e10cSrcweir                     aLine = aReader.readLine();
78*cdf0e10cSrcweir                     if (aLine != null)
79*cdf0e10cSrcweir                     {
80*cdf0e10cSrcweir                         aLines.add(aLine);
81*cdf0e10cSrcweir                         }
82*cdf0e10cSrcweir                     }
83*cdf0e10cSrcweir                 }
84*cdf0e10cSrcweir             catch (java.io.FileNotFoundException fne)
85*cdf0e10cSrcweir             {
86*cdf0e10cSrcweir                 GlobalLogWriter.get().println("couldn't open file " + m_sFilename);
87*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Message: " + fne.getMessage());
88*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
89*cdf0e10cSrcweir             }
90*cdf0e10cSrcweir             catch (java.io.IOException ie)
91*cdf0e10cSrcweir             {
92*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Exception occurs while reading from file " + m_sFilename);
93*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
94*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
95*cdf0e10cSrcweir             }
96*cdf0e10cSrcweir             try
97*cdf0e10cSrcweir             {
98*cdf0e10cSrcweir                 aReader.close();
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir             catch (java.io.IOException ie)
101*cdf0e10cSrcweir             {
102*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Couldn't close file " + m_sFilename);
103*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
104*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
105*cdf0e10cSrcweir             }
106*cdf0e10cSrcweir             return aLines;
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     /**
110*cdf0e10cSrcweir      * @return true, if the ini file contain some readable data
111*cdf0e10cSrcweir      */
112*cdf0e10cSrcweir     public boolean is()
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             return m_aList.size() > 1 ? true : false;
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     boolean isRemark(String _sLine)
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             if ( ((_sLine.length() < 2) ) ||
122*cdf0e10cSrcweir                  ( _sLine.startsWith("#")) ||
123*cdf0e10cSrcweir                  ( _sLine.startsWith(";")) )
124*cdf0e10cSrcweir             {
125*cdf0e10cSrcweir                 return true;
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir             return false;
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     String getItem(int i)
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             return (String)m_aList.get(i);
133*cdf0e10cSrcweir         }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     String buildSectionName(String _sSectionName)
136*cdf0e10cSrcweir         {
137*cdf0e10cSrcweir             String sFindSection = "[" + _sSectionName + "]";
138*cdf0e10cSrcweir             return sFindSection;
139*cdf0e10cSrcweir         }
140*cdf0e10cSrcweir     String toLowerIfNeed(String _sName)
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir             return _sName.toLowerCase();
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     // return the number where this section starts
146*cdf0e10cSrcweir     int findSection(String _sSection)
147*cdf0e10cSrcweir         {
148*cdf0e10cSrcweir             String sFindSection = toLowerIfNeed(buildSectionName(_sSection));
149*cdf0e10cSrcweir             // ----------- find _sSection ---------------
150*cdf0e10cSrcweir             int i;
151*cdf0e10cSrcweir             for (i=0; i<m_aList.size();i++)
152*cdf0e10cSrcweir             {
153*cdf0e10cSrcweir                 String sLine = toLowerIfNeed(getItem(i).trim());
154*cdf0e10cSrcweir                 if (isRemark(sLine))
155*cdf0e10cSrcweir             {
156*cdf0e10cSrcweir                     continue;
157*cdf0e10cSrcweir                 }
158*cdf0e10cSrcweir                 if (sFindSection.equals("[]"))
159*cdf0e10cSrcweir                 {
160*cdf0e10cSrcweir                     // special case, empty Section.
161*cdf0e10cSrcweir                     return i - 1;
162*cdf0e10cSrcweir                 }
163*cdf0e10cSrcweir                 if (sLine.startsWith(sFindSection))
164*cdf0e10cSrcweir                 {
165*cdf0e10cSrcweir                     return i;
166*cdf0e10cSrcweir                 }
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir             return -1;
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     // return the line number, where the key is found.
172*cdf0e10cSrcweir     int findKey(String _sSection, String _sKey)
173*cdf0e10cSrcweir         {
174*cdf0e10cSrcweir             int i = findSection(_sSection);
175*cdf0e10cSrcweir             if (i == -1)
176*cdf0e10cSrcweir             {
177*cdf0e10cSrcweir                 // Section not found, therefore the value can't exist
178*cdf0e10cSrcweir                 return -1;
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir             return findKeyFromKnownSection(i, _sKey);
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     // i must be the index in the list, where the well known section starts
184*cdf0e10cSrcweir     int findKeyFromKnownSection(int _nSectionIndex, String _sKey)
185*cdf0e10cSrcweir         {
186*cdf0e10cSrcweir             _sKey = toLowerIfNeed(_sKey);
187*cdf0e10cSrcweir             for (int j=_nSectionIndex + 1; j<m_aList.size();j++)
188*cdf0e10cSrcweir             {
189*cdf0e10cSrcweir                 String sLine = getItem(j).trim();
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir                 if (isRemark(sLine))
192*cdf0e10cSrcweir                 {
193*cdf0e10cSrcweir                     continue;
194*cdf0e10cSrcweir                 }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir                 if (sLine.startsWith("[") /* && sLine.endsWith("]") */)
197*cdf0e10cSrcweir                 {
198*cdf0e10cSrcweir                     // found end.
199*cdf0e10cSrcweir                     break;
200*cdf0e10cSrcweir                 }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir                 int nEqual = sLine.indexOf("=");
203*cdf0e10cSrcweir                 if (nEqual >= 0)
204*cdf0e10cSrcweir                 {
205*cdf0e10cSrcweir                     String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim());
206*cdf0e10cSrcweir                     if (sKey.equals(_sKey))
207*cdf0e10cSrcweir                     {
208*cdf0e10cSrcweir                         return j;
209*cdf0e10cSrcweir                     }
210*cdf0e10cSrcweir             }
211*cdf0e10cSrcweir             }
212*cdf0e10cSrcweir             return -1;
213*cdf0e10cSrcweir         }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     // i must be the index in the list, where the well known section starts
216*cdf0e10cSrcweir     int findLastKnownKeyIndex(int _nSectionIndex, String _sKey)
217*cdf0e10cSrcweir         {
218*cdf0e10cSrcweir             _sKey = toLowerIfNeed(_sKey);
219*cdf0e10cSrcweir             int i = _nSectionIndex + 1;
220*cdf0e10cSrcweir             for (int j=i; j<m_aList.size();j++)
221*cdf0e10cSrcweir             {
222*cdf0e10cSrcweir                 String sLine = getItem(j).trim();
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir                 if (isRemark(sLine))
225*cdf0e10cSrcweir                 {
226*cdf0e10cSrcweir                     continue;
227*cdf0e10cSrcweir                 }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir                 if (sLine.startsWith("[") /* && sLine.endsWith("]") */)
230*cdf0e10cSrcweir                 {
231*cdf0e10cSrcweir                     // found end.
232*cdf0e10cSrcweir                     return j;
233*cdf0e10cSrcweir                 }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir                 int nEqual = sLine.indexOf("=");
236*cdf0e10cSrcweir                 if (nEqual >= 0)
237*cdf0e10cSrcweir                 {
238*cdf0e10cSrcweir                     String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim());
239*cdf0e10cSrcweir                     if (sKey.equals(_sKey))
240*cdf0e10cSrcweir                     {
241*cdf0e10cSrcweir                         return j;
242*cdf0e10cSrcweir                     }
243*cdf0e10cSrcweir                 }
244*cdf0e10cSrcweir             }
245*cdf0e10cSrcweir             return i;
246*cdf0e10cSrcweir         }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir     String getValue(int _nIndex)
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             String sLine = getItem(_nIndex).trim();
251*cdf0e10cSrcweir             if (isRemark(sLine))
252*cdf0e10cSrcweir             {
253*cdf0e10cSrcweir                 return "";
254*cdf0e10cSrcweir             }
255*cdf0e10cSrcweir             int nEqual = sLine.indexOf("=");
256*cdf0e10cSrcweir             if (nEqual >= 0)
257*cdf0e10cSrcweir             {
258*cdf0e10cSrcweir                 String sKey =   sLine.substring(0, nEqual).trim();
259*cdf0e10cSrcweir                 String sValue = sLine.substring(nEqual + 1).trim();
260*cdf0e10cSrcweir                 return sValue;
261*cdf0e10cSrcweir                     }
262*cdf0e10cSrcweir             return "";
263*cdf0e10cSrcweir                 }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     /**
266*cdf0e10cSrcweir        @param _sSection string
267*cdf0e10cSrcweir        @param _sKey string
268*cdf0e10cSrcweir        @return the value found in the inifile which is given by the section and key parameter
269*cdf0e10cSrcweir     */
270*cdf0e10cSrcweir     public String getValue(String _sSection, String _sKey)
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             String sValue = "";
273*cdf0e10cSrcweir             int i = findKey(_sSection, _sKey);
274*cdf0e10cSrcweir             if (i == -1)
275*cdf0e10cSrcweir             {
276*cdf0e10cSrcweir                 // Section not found, therefore the value can't exist
277*cdf0e10cSrcweir                 return "";
278*cdf0e10cSrcweir             }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir             sValue = getValue(i);
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir             return sValue;
283*cdf0e10cSrcweir         }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     /**
286*cdf0e10cSrcweir        write back the ini file to the disk, only if there exist changes
287*cdf0e10cSrcweir     */
288*cdf0e10cSrcweir     public void store()
289*cdf0e10cSrcweir         {
290*cdf0e10cSrcweir             if (m_bListContainUnsavedChanges == false)
291*cdf0e10cSrcweir             {
292*cdf0e10cSrcweir                 // nothing has changed, so no need to store
293*cdf0e10cSrcweir                 return;
294*cdf0e10cSrcweir             }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir             File aFile = new File(m_sFilename);
297*cdf0e10cSrcweir             if (aFile.exists())
298*cdf0e10cSrcweir             {
299*cdf0e10cSrcweir                 // System.out.println("couldn't find file " + m_sFilename);
300*cdf0e10cSrcweir                 aFile.delete();
301*cdf0e10cSrcweir                 if (aFile.exists())
302*cdf0e10cSrcweir                 {
303*cdf0e10cSrcweir                     GlobalLogWriter.get().println("Couldn't delete the file " + m_sFilename);
304*cdf0e10cSrcweir                     return;
305*cdf0e10cSrcweir                     // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "Couldn't delete the file " + m_sFilename);
306*cdf0e10cSrcweir                 }
307*cdf0e10cSrcweir             }
308*cdf0e10cSrcweir             // if (! aFile.canWrite())
309*cdf0e10cSrcweir             // {
310*cdf0e10cSrcweir             //    System.out.println("Couldn't write to file " + m_sFilename);
311*cdf0e10cSrcweir             //    DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "");
312*cdf0e10cSrcweir             // }
313*cdf0e10cSrcweir             try
314*cdf0e10cSrcweir             {
315*cdf0e10cSrcweir                 RandomAccessFile aWriter = new RandomAccessFile(aFile, "rw");
316*cdf0e10cSrcweir                 for (int i=0; i<m_aList.size();i++)
317*cdf0e10cSrcweir                 {
318*cdf0e10cSrcweir                     String sLine = getItem(i);
319*cdf0e10cSrcweir                     aWriter.writeBytes(sLine);
320*cdf0e10cSrcweir                     aWriter.writeByte((int)'\n');
321*cdf0e10cSrcweir                 }
322*cdf0e10cSrcweir                 aWriter.close();
323*cdf0e10cSrcweir             }
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir             catch (java.io.FileNotFoundException fne)
326*cdf0e10cSrcweir             {
327*cdf0e10cSrcweir                 GlobalLogWriter.get().println("couldn't open file for writing " + m_sFilename);
328*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Message: " + fne.getMessage());
329*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
330*cdf0e10cSrcweir             }
331*cdf0e10cSrcweir             catch(java.io.IOException ie)
332*cdf0e10cSrcweir             {
333*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Exception occurs while writing to file " + m_sFilename);
334*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Message: " + ie.getMessage());
335*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
336*cdf0e10cSrcweir             }
337*cdf0e10cSrcweir         }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir     /**
342*cdf0e10cSrcweir        insert a value
343*cdf0e10cSrcweir        there are 3 cases
344*cdf0e10cSrcweir        1. section doesn't exist, goto end and insert a new section, insert a new key value pair
345*cdf0e10cSrcweir        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*cdf0e10cSrcweir        3. section exist and key exist, remove the old key and insert the key value pair at the same position
347*cdf0e10cSrcweir     */
348*cdf0e10cSrcweir     public void insertValue(String _sSection, String _sKey, String _sValue)
349*cdf0e10cSrcweir         {
350*cdf0e10cSrcweir             int i = findSection(_sSection);
351*cdf0e10cSrcweir             if (i == -1)
352*cdf0e10cSrcweir             {
353*cdf0e10cSrcweir                 // case 1: section doesn't exist
354*cdf0e10cSrcweir                 String sFindSection = buildSectionName(_sSection);
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir                 m_aList.add(sFindSection);
357*cdf0e10cSrcweir                 String sKeyValuePair = _sKey + "=" + _sValue;
358*cdf0e10cSrcweir                 m_aList.add(sKeyValuePair);
359*cdf0e10cSrcweir                 m_bListContainUnsavedChanges = true;
360*cdf0e10cSrcweir                 return;
361*cdf0e10cSrcweir             }
362*cdf0e10cSrcweir             int j = findKeyFromKnownSection(i, _sKey);
363*cdf0e10cSrcweir             if (j == -1)
364*cdf0e10cSrcweir             {
365*cdf0e10cSrcweir                 // case 2: section exist, but not the key
366*cdf0e10cSrcweir                 j = findLastKnownKeyIndex(i, _sKey);
367*cdf0e10cSrcweir                 String sKeyValuePair = _sKey + "=" + _sValue;
368*cdf0e10cSrcweir                 m_aList.add(j, sKeyValuePair);
369*cdf0e10cSrcweir                 m_bListContainUnsavedChanges = true;
370*cdf0e10cSrcweir                 return;
371*cdf0e10cSrcweir             }
372*cdf0e10cSrcweir             else
373*cdf0e10cSrcweir             {
374*cdf0e10cSrcweir                 // case 3: section exist, and also the key
375*cdf0e10cSrcweir                 String sKeyValuePair = _sKey + "=" + _sValue;
376*cdf0e10cSrcweir                 m_aList.set(j, sKeyValuePair);
377*cdf0e10cSrcweir                 m_bListContainUnsavedChanges = true;
378*cdf0e10cSrcweir             }
379*cdf0e10cSrcweir         }
380*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
381*cdf0e10cSrcweir     // String replaceEvaluatedValue(String _sSection, String _sValue)
382*cdf0e10cSrcweir     //     {
383*cdf0e10cSrcweir     //         String sValue = _sValue;
384*cdf0e10cSrcweir     //         int nIndex = 0;
385*cdf0e10cSrcweir     //         while (( nIndex = sValue.indexOf("$(", nIndex)) >= 0)
386*cdf0e10cSrcweir     //         {
387*cdf0e10cSrcweir     //             int nNextIndex = sValue.indexOf(")", nIndex);
388*cdf0e10cSrcweir     //             if (nNextIndex >= 0)
389*cdf0e10cSrcweir     //             {
390*cdf0e10cSrcweir     //                 String sKey = sValue.substring(nIndex + 2, nNextIndex);
391*cdf0e10cSrcweir     //                 String sNewValue = getValue(_sSection, sKey);
392*cdf0e10cSrcweir     //                 if (sNewValue != null && sNewValue.length() > 0)
393*cdf0e10cSrcweir     //                 {
394*cdf0e10cSrcweir     //                     String sRegexpKey = "\\$\\(" + sKey + "\\)";
395*cdf0e10cSrcweir     //                     sValue = sValue.replaceAll(sRegexpKey, sNewValue);
396*cdf0e10cSrcweir     //                 }
397*cdf0e10cSrcweir     //                 nIndex = nNextIndex;
398*cdf0e10cSrcweir     //             }
399*cdf0e10cSrcweir     //             else
400*cdf0e10cSrcweir     //             {
401*cdf0e10cSrcweir     //                 nIndex += 2;
402*cdf0e10cSrcweir     //             }
403*cdf0e10cSrcweir     //         }
404*cdf0e10cSrcweir     //         return sValue;
405*cdf0e10cSrcweir     //     }
406*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     // public String getLocalEvaluatedValue(String _sSection, String _sKey)
409*cdf0e10cSrcweir     //     {
410*cdf0e10cSrcweir     //         String sValue = getValue(_sSection, _sKey);
411*cdf0e10cSrcweir     //         sValue = replaceEvaluatedValue(_sSection, sValue);
412*cdf0e10cSrcweir     //         return sValue;
413*cdf0e10cSrcweir     //     }
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir     // this is a special behaviour.
418*cdf0e10cSrcweir     // public String getGlobalLocalEvaluatedValue(String _sSection, String _sKey)
419*cdf0e10cSrcweir     //     {
420*cdf0e10cSrcweir     //         String sGlobalValue = getKey("global", _sKey);
421*cdf0e10cSrcweir     //         String sLocalValue = getKey(_sSection, _sKey);
422*cdf0e10cSrcweir     //         if (sLocalValue.length() == 0)
423*cdf0e10cSrcweir     //         {
424*cdf0e10cSrcweir     //             sGlobalValue = replaceEvaluatedKey(_sSection, sGlobalValue);
425*cdf0e10cSrcweir     //             sGlobalValue = replaceEvaluatedKey("global", sGlobalValue);
426*cdf0e10cSrcweir     //             return sGlobalValue;
427*cdf0e10cSrcweir     //         }
428*cdf0e10cSrcweir     //         sLocalValue = replaceEvaluatedKey(_sSection, sLocalValue);
429*cdf0e10cSrcweir     //         sLocalValue = replaceEvaluatedKey("global", sLocalValue);
430*cdf0e10cSrcweir     //
431*cdf0e10cSrcweir     //         return sLocalValue;
432*cdf0e10cSrcweir     //     }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir     /**
436*cdf0e10cSrcweir      * some tests for this class
437*cdf0e10cSrcweir      */
438*cdf0e10cSrcweir //    public static void main(String[] args)
439*cdf0e10cSrcweir //        {
440*cdf0e10cSrcweir //            IniFile aIniFile = new IniFile("/tmp/inifile");
441*cdf0e10cSrcweir //            String sValue = aIniFile.getValue("Section","Key");
442*cdf0e10cSrcweir //            // insert a new value to a already exist section
443*cdf0e10cSrcweir //            aIniFile.insertValue("Section","Key2","a new value in a existing section");
444*cdf0e10cSrcweir //            // replace a value
445*cdf0e10cSrcweir //            aIniFile.insertValue("Section","Key","replaced value");
446*cdf0e10cSrcweir //            // create a new value
447*cdf0e10cSrcweir //            aIniFile.insertValue("New Section", "Key", "a new key value pair");
448*cdf0e10cSrcweir //
449*cdf0e10cSrcweir //            String sValue2 = aIniFile.getValue("Section2","Key");
450*cdf0e10cSrcweir //            aIniFile.store();
451*cdf0e10cSrcweir //        }
452*cdf0e10cSrcweir }
453