1*31e76637SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*31e76637SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*31e76637SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*31e76637SAndrew Rist * distributed with this work for additional information 6*31e76637SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*31e76637SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*31e76637SAndrew Rist * "License"); you may not use this file except in compliance 9*31e76637SAndrew Rist * with the License. You may obtain a copy of the License at 10*31e76637SAndrew Rist * 11*31e76637SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*31e76637SAndrew Rist * 13*31e76637SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*31e76637SAndrew Rist * software distributed under the License is distributed on an 15*31e76637SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*31e76637SAndrew Rist * KIND, either express or implied. See the License for the 17*31e76637SAndrew Rist * specific language governing permissions and limitations 18*31e76637SAndrew Rist * under the License. 19*31e76637SAndrew Rist * 20*31e76637SAndrew Rist *************************************************************/ 21*31e76637SAndrew Rist 22*31e76637SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import java.io.File; 25cdf0e10cSrcweir import java.io.RandomAccessFile; 26cdf0e10cSrcweir import java.util.ArrayList; 27cdf0e10cSrcweir import java.util.Enumeration; 28cdf0e10cSrcweir 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** 31cdf0e10cSrcweir * Simple implementation of a inifile manager 32cdf0e10cSrcweir */ 33cdf0e10cSrcweir class GlobalLogWriter 34cdf0e10cSrcweir { println(String _s)35cdf0e10cSrcweir public static void println(String _s) 36cdf0e10cSrcweir { 37cdf0e10cSrcweir System.out.println(_s); 38cdf0e10cSrcweir } 39cdf0e10cSrcweir } 40cdf0e10cSrcweir 41cdf0e10cSrcweir /** 42cdf0e10cSrcweir Helper class to give a simple API to read/write windows like ini files 43cdf0e10cSrcweir */ 44cdf0e10cSrcweir 45cdf0e10cSrcweir /* public */ // is only need, if we need this class outside package convwatch 46cdf0e10cSrcweir public class IniFile implements Enumeration 47cdf0e10cSrcweir { 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** 50cdf0e10cSrcweir * internal representation of the ini file content. 51cdf0e10cSrcweir * Problem, if ini file changed why other write something difference, we don't realise this. 52cdf0e10cSrcweir */ 53cdf0e10cSrcweir private String m_sFilename; 54cdf0e10cSrcweir // private File m_aFile; 55cdf0e10cSrcweir private ArrayList<String> m_aList; 56cdf0e10cSrcweir boolean m_bListContainUnsavedChanges = false; 57cdf0e10cSrcweir private int m_aEnumerationPos = 0; 58cdf0e10cSrcweir 59cdf0e10cSrcweir /** 60cdf0e10cSrcweir open a ini file by it's name 61cdf0e10cSrcweir @param _sFilename string a filename, if the file doesn't exist, a new empty ini file will create. 62cdf0e10cSrcweir write back to disk only if there are really changes. 63cdf0e10cSrcweir */ IniFile(String _sFilename)64cdf0e10cSrcweir public IniFile(String _sFilename) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir m_sFilename = _sFilename; 67cdf0e10cSrcweir // m_aFile = new File(_sFilename); 68cdf0e10cSrcweir m_aList = loadLines(); 69cdf0e10cSrcweir m_aEnumerationPos = findNextSection(0); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir /** 73cdf0e10cSrcweir open a ini file by it's name 74cdf0e10cSrcweir @param _aFile a java.io.File object, if the file doesn't exist, a new empty ini file will create. 75cdf0e10cSrcweir write back to disk only if there are really changes. 76cdf0e10cSrcweir */ IniFile(File _aFile)77cdf0e10cSrcweir public IniFile(File _aFile) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir m_sFilename = _aFile.getAbsolutePath(); 80cdf0e10cSrcweir m_aList = loadLines(); 81cdf0e10cSrcweir m_aEnumerationPos = findNextSection(0); 82cdf0e10cSrcweir } 83cdf0e10cSrcweir insertFirstComment(String[] _aList)84cdf0e10cSrcweir public void insertFirstComment(String[] _aList) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir if (m_aList.size() == 0) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir // can only insert if there is nothing else already in the ini file 89cdf0e10cSrcweir for (int i = 0; i < _aList.length; i++) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir m_aList.add(_aList[i]); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94cdf0e10cSrcweir } 95cdf0e10cSrcweir loadLines()96cdf0e10cSrcweir private ArrayList<String> loadLines() 97cdf0e10cSrcweir { 98cdf0e10cSrcweir ArrayList<String> aLines = new ArrayList<String>(); 99cdf0e10cSrcweir File aFile = new File(m_sFilename); 100cdf0e10cSrcweir if (!aFile.exists()) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir // GlobalLogWriter.println("couldn't find file '" + m_sFilename + "', will be created."); 103cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, ""); 104cdf0e10cSrcweir // m_bListContainUnsavedChanges = false; 105cdf0e10cSrcweir return aLines; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir RandomAccessFile aReader = null; 108cdf0e10cSrcweir // BufferedReader aReader; 109cdf0e10cSrcweir try 110cdf0e10cSrcweir { 111cdf0e10cSrcweir aReader = new RandomAccessFile(aFile, "r"); 112cdf0e10cSrcweir String aLine = ""; 113cdf0e10cSrcweir while (aLine != null) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir aLine = aReader.readLine(); 116cdf0e10cSrcweir if (aLine != null && aLine.length() > 0) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir aLines.add(aLine); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir } 121cdf0e10cSrcweir } 122cdf0e10cSrcweir catch (java.io.FileNotFoundException fne) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir GlobalLogWriter.println("couldn't open file " + m_sFilename); 125cdf0e10cSrcweir GlobalLogWriter.println("Message: " + fne.getMessage()); 126cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, ""); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir catch (java.io.IOException ie) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir GlobalLogWriter.println("Exception occurs while reading from file " + m_sFilename); 131cdf0e10cSrcweir GlobalLogWriter.println("Message: " + ie.getMessage()); 132cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage()); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir try 135cdf0e10cSrcweir { 136cdf0e10cSrcweir aReader.close(); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir catch (java.io.IOException ie) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir GlobalLogWriter.println("Couldn't close file " + m_sFilename); 141cdf0e10cSrcweir GlobalLogWriter.println("Message: " + ie.getMessage()); 142cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage()); 143cdf0e10cSrcweir } 144cdf0e10cSrcweir return aLines; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir /** 148cdf0e10cSrcweir * @return true, if the ini file contain some readable data 149cdf0e10cSrcweir */ is()150cdf0e10cSrcweir public boolean is() 151cdf0e10cSrcweir { 152cdf0e10cSrcweir return m_aList.size() > 1 ? true : false; 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir /** 156cdf0e10cSrcweir * Check if a given Section and Key exists in the ini file 157cdf0e10cSrcweir * @param _sSectionName 158cdf0e10cSrcweir * @param _sKey 159cdf0e10cSrcweir * @return true if the given Section, Key exists, now you can get the value 160cdf0e10cSrcweir */ hasValue(String _sSectionName, String _sKey)161cdf0e10cSrcweir public boolean hasValue(String _sSectionName, String _sKey) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir int n = findKey(_sSectionName, _sKey); 164cdf0e10cSrcweir if (n > 0) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir return true; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir return false; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir // ----------------------------------------------------------------------------- 171cdf0e10cSrcweir isRemark(String _sLine)172cdf0e10cSrcweir private boolean isRemark(String _sLine) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir if (((_sLine.length() < 2)) || 175cdf0e10cSrcweir (_sLine.startsWith("#")) || 176cdf0e10cSrcweir (_sLine.startsWith(";"))) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir return true; 179cdf0e10cSrcweir } 180cdf0e10cSrcweir return false; 181cdf0e10cSrcweir } 182cdf0e10cSrcweir getItem(int i)183cdf0e10cSrcweir private String getItem(int i) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir return m_aList.get(i); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir buildSectionName(String _sSectionName)188cdf0e10cSrcweir private String buildSectionName(String _sSectionName) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir String sFindSection = "[" + _sSectionName + "]"; 191cdf0e10cSrcweir return sFindSection; 192cdf0e10cSrcweir } 193cdf0e10cSrcweir sectionToString(String _sSectionName)194cdf0e10cSrcweir private String sectionToString(String _sSectionName) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir String sKeyName = _sSectionName; 197cdf0e10cSrcweir if (sKeyName.startsWith("[") && 198cdf0e10cSrcweir sKeyName.endsWith("]")) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir sKeyName = sKeyName.substring(1, sKeyName.length() - 1); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir return sKeyName; 203cdf0e10cSrcweir } 204cdf0e10cSrcweir toLowerIfNeed(String _sName)205cdf0e10cSrcweir private String toLowerIfNeed(String _sName) 206cdf0e10cSrcweir { 207cdf0e10cSrcweir return _sName.toLowerCase(); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir 210cdf0e10cSrcweir // return the number where this section starts findSection(String _sSection)211cdf0e10cSrcweir private int findSection(String _sSection) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir String sFindSection = toLowerIfNeed(buildSectionName(_sSection)); 214cdf0e10cSrcweir // ----------- find _sSection --------------- 215cdf0e10cSrcweir int i; 216cdf0e10cSrcweir for (i = 0; i < m_aList.size(); i++) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir String sLine = toLowerIfNeed(getItem(i).trim()); 219cdf0e10cSrcweir if (isRemark(sLine)) 220cdf0e10cSrcweir { 221cdf0e10cSrcweir continue; 222cdf0e10cSrcweir } 223cdf0e10cSrcweir if (sFindSection.equals("[]")) 224cdf0e10cSrcweir { 225cdf0e10cSrcweir // special case, empty Section. 226cdf0e10cSrcweir return i - 1; 227cdf0e10cSrcweir } 228cdf0e10cSrcweir if (sLine.startsWith(sFindSection)) 229cdf0e10cSrcweir { 230cdf0e10cSrcweir return i; 231cdf0e10cSrcweir } 232cdf0e10cSrcweir } 233cdf0e10cSrcweir return -1; 234cdf0e10cSrcweir } 235cdf0e10cSrcweir 236cdf0e10cSrcweir /** 237cdf0e10cSrcweir * Checks if a given section exists in the ini file 238cdf0e10cSrcweir * @param _sSection 239cdf0e10cSrcweir * @return true if the given _sSection was found 240cdf0e10cSrcweir */ hasSection(String _sSection)241cdf0e10cSrcweir public boolean hasSection(String _sSection) 242cdf0e10cSrcweir { 243cdf0e10cSrcweir int i = findSection(_sSection); 244cdf0e10cSrcweir if (i == -1) 245cdf0e10cSrcweir { 246cdf0e10cSrcweir return false; 247cdf0e10cSrcweir } 248cdf0e10cSrcweir return true; 249cdf0e10cSrcweir } 250cdf0e10cSrcweir 251cdf0e10cSrcweir // return the line number, where the key is found. findKey(String _sSection, String _sKey)252cdf0e10cSrcweir private int findKey(String _sSection, String _sKey) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir int i = findSection(_sSection); 255cdf0e10cSrcweir if (i == -1) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir // Section not found, therefore the value can't exist 258cdf0e10cSrcweir return -1; 259cdf0e10cSrcweir } 260cdf0e10cSrcweir return findKeyFromKnownSection(i, _sKey); 261cdf0e10cSrcweir } 262cdf0e10cSrcweir 263cdf0e10cSrcweir // i must be the index in the list, where the well known section starts findKeyFromKnownSection(int _nSectionIndex, String _sKey)264cdf0e10cSrcweir private int findKeyFromKnownSection(int _nSectionIndex, String _sKey) 265cdf0e10cSrcweir { 266cdf0e10cSrcweir _sKey = toLowerIfNeed(_sKey); 267cdf0e10cSrcweir for (int j = _nSectionIndex + 1; j < m_aList.size(); j++) 268cdf0e10cSrcweir { 269cdf0e10cSrcweir String sLine = getItem(j).trim(); 270cdf0e10cSrcweir 271cdf0e10cSrcweir if (isRemark(sLine)) 272cdf0e10cSrcweir { 273cdf0e10cSrcweir continue; 274cdf0e10cSrcweir } 275cdf0e10cSrcweir if (sLine.startsWith("[") /* && sLine.endsWith("]") */) 276cdf0e10cSrcweir { 277cdf0e10cSrcweir // TODO: due to the fact we would like to insert an empty line before new sections 278cdf0e10cSrcweir // TODO: we should check if we are in an empty line and if, go back one line. 279cdf0e10cSrcweir 280cdf0e10cSrcweir // found end. 281cdf0e10cSrcweir break; 282cdf0e10cSrcweir } 283cdf0e10cSrcweir 284cdf0e10cSrcweir int nEqual = sLine.indexOf("="); 285cdf0e10cSrcweir if (nEqual >= 0) 286cdf0e10cSrcweir { 287cdf0e10cSrcweir String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim()); 288cdf0e10cSrcweir if (sKey.equals(_sKey)) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir return j; 291cdf0e10cSrcweir } 292cdf0e10cSrcweir } 293cdf0e10cSrcweir } 294cdf0e10cSrcweir return -1; 295cdf0e10cSrcweir } 296cdf0e10cSrcweir 297cdf0e10cSrcweir // i must be the index in the list, where the well known section starts findLastKnownKeyIndex(int _nSectionIndex, String _sKey)298cdf0e10cSrcweir private int findLastKnownKeyIndex(int _nSectionIndex, String _sKey) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir _sKey = toLowerIfNeed(_sKey); 301cdf0e10cSrcweir int i = _nSectionIndex + 1; 302cdf0e10cSrcweir for (int j = i; j < m_aList.size(); j++) 303cdf0e10cSrcweir { 304cdf0e10cSrcweir String sLine = getItem(j).trim(); 305cdf0e10cSrcweir 306cdf0e10cSrcweir if (isRemark(sLine)) 307cdf0e10cSrcweir { 308cdf0e10cSrcweir continue; 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir if (sLine.startsWith("[") /* && sLine.endsWith("]") */) 312cdf0e10cSrcweir { 313cdf0e10cSrcweir // found end. 314cdf0e10cSrcweir return j; 315cdf0e10cSrcweir } 316cdf0e10cSrcweir 317cdf0e10cSrcweir int nEqual = sLine.indexOf("="); 318cdf0e10cSrcweir if (nEqual >= 0) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim()); 321cdf0e10cSrcweir if (sKey.equals(_sKey)) 322cdf0e10cSrcweir { 323cdf0e10cSrcweir return j; 324cdf0e10cSrcweir } 325cdf0e10cSrcweir } 326cdf0e10cSrcweir } 327cdf0e10cSrcweir return i; 328cdf0e10cSrcweir } 329cdf0e10cSrcweir getValue(int _nIndex)330cdf0e10cSrcweir private String getValue(int _nIndex) 331cdf0e10cSrcweir { 332cdf0e10cSrcweir String sLine = getItem(_nIndex).trim(); 333cdf0e10cSrcweir if (isRemark(sLine)) 334cdf0e10cSrcweir { 335cdf0e10cSrcweir return ""; 336cdf0e10cSrcweir } 337cdf0e10cSrcweir int nEqual = sLine.indexOf("="); 338cdf0e10cSrcweir if (nEqual >= 0) 339cdf0e10cSrcweir { 340cdf0e10cSrcweir String sKey = sLine.substring(0, nEqual).trim(); 341cdf0e10cSrcweir String sValue = sLine.substring(nEqual + 1).trim(); 342cdf0e10cSrcweir return sValue; 343cdf0e10cSrcweir } 344cdf0e10cSrcweir return ""; 345cdf0e10cSrcweir } 346cdf0e10cSrcweir 347cdf0e10cSrcweir /** 348cdf0e10cSrcweir @param _sSection string 349cdf0e10cSrcweir @param _sKey string 350cdf0e10cSrcweir @return the value found in the inifile which is given by the section and key parameter 351cdf0e10cSrcweir */ 352cdf0e10cSrcweir // private int m_nCurrentPosition; 353cdf0e10cSrcweir // private String m_sOldKey; getValue(String _sSection, String _sKey)354cdf0e10cSrcweir public String getValue(String _sSection, String _sKey) 355cdf0e10cSrcweir { 356cdf0e10cSrcweir String sValue = ""; 357cdf0e10cSrcweir int m_nCurrentPosition = findKey(_sSection, _sKey); 358cdf0e10cSrcweir if (m_nCurrentPosition == -1) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir // Section not found, therefore the value can't exist 361cdf0e10cSrcweir return ""; 362cdf0e10cSrcweir } 363cdf0e10cSrcweir 364cdf0e10cSrcweir // m_sOldKey = _sKey; 365cdf0e10cSrcweir sValue = getValue(m_nCurrentPosition); 366cdf0e10cSrcweir 367cdf0e10cSrcweir return sValue; 368cdf0e10cSrcweir } 369cdf0e10cSrcweir 370cdf0e10cSrcweir // private String getNextValue() 371cdf0e10cSrcweir // { 372cdf0e10cSrcweir // if (m_nCurrentPosition >= 0) 373cdf0e10cSrcweir // { 374cdf0e10cSrcweir // ++m_nCurrentPosition; 375cdf0e10cSrcweir // String sValue = getValue(m_nCurrentPosition); 376cdf0e10cSrcweir // return sValue; 377cdf0e10cSrcweir // } 378cdf0e10cSrcweir // return ""; 379cdf0e10cSrcweir // } 380cdf0e10cSrcweir /** 381cdf0e10cSrcweir * Returns the value at Section, Key converted to an integer 382cdf0e10cSrcweir * Check with hasValue(Section, Key) to check before you get into trouble. 383cdf0e10cSrcweir * @param _sSection 384cdf0e10cSrcweir * @param _sKey 385cdf0e10cSrcweir * @param _nDefault if there is a problem, key not found... this value will return 386cdf0e10cSrcweir * @return the value as integer if possible to convert, if not return default value. 387cdf0e10cSrcweir */ getIntValue(String _sSection, String _sKey, int _nDefault)388cdf0e10cSrcweir public int getIntValue(String _sSection, String _sKey, int _nDefault) 389cdf0e10cSrcweir { 390cdf0e10cSrcweir String sValue = getValue(_sSection, _sKey); 391cdf0e10cSrcweir int nValue = _nDefault; 392cdf0e10cSrcweir if (sValue.length() > 0) 393cdf0e10cSrcweir { 394cdf0e10cSrcweir try 395cdf0e10cSrcweir { 396cdf0e10cSrcweir nValue = Integer.valueOf(sValue).intValue(); 397cdf0e10cSrcweir } 398cdf0e10cSrcweir catch (java.lang.NumberFormatException e) 399cdf0e10cSrcweir { 400cdf0e10cSrcweir GlobalLogWriter.println("IniFile.getIntValue(): Caught a number format exception, return the default value."); 401cdf0e10cSrcweir } 402cdf0e10cSrcweir } 403cdf0e10cSrcweir return nValue; 404cdf0e10cSrcweir } 405cdf0e10cSrcweir 406cdf0e10cSrcweir /** 407cdf0e10cSrcweir * close a open inifile. 408cdf0e10cSrcweir * If there are changes, all changes will store back to disk. 409cdf0e10cSrcweir */ close()410cdf0e10cSrcweir public void close() 411cdf0e10cSrcweir { 412cdf0e10cSrcweir store(); 413cdf0e10cSrcweir } 414cdf0e10cSrcweir 415cdf0e10cSrcweir /** 416cdf0e10cSrcweir write back the ini file to the disk, only if there exist changes 417cdf0e10cSrcweir * @deprecated use close() instead! 418cdf0e10cSrcweir */ 419cdf0e10cSrcweir 420cdf0e10cSrcweir // TODO: make private store()421cdf0e10cSrcweir private void store() 422cdf0e10cSrcweir { 423cdf0e10cSrcweir if (m_bListContainUnsavedChanges == false) 424cdf0e10cSrcweir { 425cdf0e10cSrcweir // nothing has changed, so no need to store 426cdf0e10cSrcweir return; 427cdf0e10cSrcweir } 428cdf0e10cSrcweir 429cdf0e10cSrcweir File aFile = new File(m_sFilename); 430cdf0e10cSrcweir if (aFile.exists()) 431cdf0e10cSrcweir { 432cdf0e10cSrcweir // System.out.println("couldn't find file " + m_sFilename); 433cdf0e10cSrcweir // TODO: little bit unsafe here, first rename, after write is complete, delete the old. 434cdf0e10cSrcweir aFile.delete(); 435cdf0e10cSrcweir if (aFile.exists()) 436cdf0e10cSrcweir { 437cdf0e10cSrcweir GlobalLogWriter.println("Couldn't delete the file " + m_sFilename); 438cdf0e10cSrcweir return; 439cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "Couldn't delete the file " + m_sFilename); 440cdf0e10cSrcweir } 441cdf0e10cSrcweir } 442cdf0e10cSrcweir // if (! aFile.canWrite()) 443cdf0e10cSrcweir // { 444cdf0e10cSrcweir // System.out.println("Couldn't write to file " + m_sFilename); 445cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ""); 446cdf0e10cSrcweir // } 447cdf0e10cSrcweir try 448cdf0e10cSrcweir { 449cdf0e10cSrcweir RandomAccessFile aWriter = new RandomAccessFile(aFile, "rw"); 450cdf0e10cSrcweir for (int i = 0; i < m_aList.size(); i++) 451cdf0e10cSrcweir { 452cdf0e10cSrcweir String sLine = getItem(i); 453cdf0e10cSrcweir if (sLine.startsWith("[")) 454cdf0e10cSrcweir { 455cdf0e10cSrcweir // write an extra empty line before next section. 456cdf0e10cSrcweir aWriter.writeByte((int) '\n'); 457cdf0e10cSrcweir } 458cdf0e10cSrcweir aWriter.writeBytes(sLine); 459cdf0e10cSrcweir aWriter.writeByte((int) '\n'); 460cdf0e10cSrcweir } 461cdf0e10cSrcweir aWriter.close(); 462cdf0e10cSrcweir } 463cdf0e10cSrcweir catch (java.io.FileNotFoundException fne) 464cdf0e10cSrcweir { 465cdf0e10cSrcweir GlobalLogWriter.println("couldn't open file for writing " + m_sFilename); 466cdf0e10cSrcweir GlobalLogWriter.println("Message: " + fne.getMessage()); 467cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, ""); 468cdf0e10cSrcweir } 469cdf0e10cSrcweir catch (java.io.IOException ie) 470cdf0e10cSrcweir { 471cdf0e10cSrcweir GlobalLogWriter.println("Exception occurs while writing to file " + m_sFilename); 472cdf0e10cSrcweir GlobalLogWriter.println("Message: " + ie.getMessage()); 473cdf0e10cSrcweir // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage()); 474cdf0e10cSrcweir } 475cdf0e10cSrcweir } 476cdf0e10cSrcweir insertValue(String _sSection, String _sKey, int _nValue)477cdf0e10cSrcweir public void insertValue(String _sSection, String _sKey, int _nValue) 478cdf0e10cSrcweir { 479cdf0e10cSrcweir insertValue(_sSection, _sKey, String.valueOf(_nValue)); 480cdf0e10cSrcweir } 481cdf0e10cSrcweir insertValue(String _sSection, String _sKey, long _nValue)482cdf0e10cSrcweir public void insertValue(String _sSection, String _sKey, long _nValue) 483cdf0e10cSrcweir { 484cdf0e10cSrcweir insertValue(_sSection, _sKey, String.valueOf(_nValue)); 485cdf0e10cSrcweir } 486cdf0e10cSrcweir 487cdf0e10cSrcweir /** 488cdf0e10cSrcweir insert a value 489cdf0e10cSrcweir there are 3 cases 490cdf0e10cSrcweir 1. section doesn't exist, goto end and insert a new section, insert a new key value pair 491cdf0e10cSrcweir 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 492cdf0e10cSrcweir 3. section exist and key exist, remove the old key and insert the key value pair at the same position 493cdf0e10cSrcweir * @param _sSection 494cdf0e10cSrcweir * @param _sKey 495cdf0e10cSrcweir * @param _sValue 496cdf0e10cSrcweir */ insertValue(String _sSection, String _sKey, String _sValue)497cdf0e10cSrcweir public void insertValue(String _sSection, String _sKey, String _sValue) 498cdf0e10cSrcweir { 499cdf0e10cSrcweir int i = findSection(_sSection); 500cdf0e10cSrcweir if (i == -1) 501cdf0e10cSrcweir { 502cdf0e10cSrcweir // case 1: section doesn't exist 503cdf0e10cSrcweir String sFindSection = buildSectionName(_sSection); 504cdf0e10cSrcweir 505cdf0e10cSrcweir // TODO: before create a new Section, insert a empty line 506cdf0e10cSrcweir m_aList.add(sFindSection); 507cdf0e10cSrcweir if (_sKey.length() > 0) 508cdf0e10cSrcweir { 509cdf0e10cSrcweir String sKeyValuePair = _sKey + "=" + _sValue; 510cdf0e10cSrcweir m_aList.add(sKeyValuePair); 511cdf0e10cSrcweir } 512cdf0e10cSrcweir m_bListContainUnsavedChanges = true; 513cdf0e10cSrcweir return; 514cdf0e10cSrcweir } 515cdf0e10cSrcweir int j = findKeyFromKnownSection(i, _sKey); 516cdf0e10cSrcweir if (j == -1) 517cdf0e10cSrcweir { 518cdf0e10cSrcweir // case 2: section exist, but not the key 519cdf0e10cSrcweir j = findLastKnownKeyIndex(i, _sKey); 520cdf0e10cSrcweir if (_sKey.length() > 0) 521cdf0e10cSrcweir { 522cdf0e10cSrcweir String sKeyValuePair = _sKey + "=" + _sValue; 523cdf0e10cSrcweir m_aList.add(j, sKeyValuePair); 524cdf0e10cSrcweir m_bListContainUnsavedChanges = true; 525cdf0e10cSrcweir } 526cdf0e10cSrcweir return; 527cdf0e10cSrcweir } 528cdf0e10cSrcweir else 529cdf0e10cSrcweir { 530cdf0e10cSrcweir // case 3: section exist, and also the key 531cdf0e10cSrcweir String sKeyValuePair = _sKey + "=" + _sValue; 532cdf0e10cSrcweir m_aList.set(j, sKeyValuePair); 533cdf0e10cSrcweir m_bListContainUnsavedChanges = true; 534cdf0e10cSrcweir } 535cdf0e10cSrcweir } 536cdf0e10cSrcweir // ----------------------------------------------------------------------------- 537cdf0e10cSrcweir // String replaceEvaluatedValue(String _sSection, String _sValue) 538cdf0e10cSrcweir // { 539cdf0e10cSrcweir // String sValue = _sValue; 540cdf0e10cSrcweir // int nIndex = 0; 541cdf0e10cSrcweir // while (( nIndex = sValue.indexOf("$(", nIndex)) >= 0) 542cdf0e10cSrcweir // { 543cdf0e10cSrcweir // int nNextIndex = sValue.indexOf(")", nIndex); 544cdf0e10cSrcweir // if (nNextIndex >= 0) 545cdf0e10cSrcweir // { 546cdf0e10cSrcweir // String sKey = sValue.substring(nIndex + 2, nNextIndex); 547cdf0e10cSrcweir // String sNewValue = getValue(_sSection, sKey); 548cdf0e10cSrcweir // if (sNewValue != null && sNewValue.length() > 0) 549cdf0e10cSrcweir // { 550cdf0e10cSrcweir // String sRegexpKey = "\\$\\(" + sKey + "\\)"; 551cdf0e10cSrcweir // sValue = sValue.replaceAll(sRegexpKey, sNewValue); 552cdf0e10cSrcweir // } 553cdf0e10cSrcweir // nIndex = nNextIndex; 554cdf0e10cSrcweir // } 555cdf0e10cSrcweir // else 556cdf0e10cSrcweir // { 557cdf0e10cSrcweir // nIndex += 2; 558cdf0e10cSrcweir // } 559cdf0e10cSrcweir // } 560cdf0e10cSrcweir // return sValue; 561cdf0e10cSrcweir // } 562cdf0e10cSrcweir // ----------------------------------------------------------------------------- 563cdf0e10cSrcweir 564cdf0e10cSrcweir // public String getLocalEvaluatedValue(String _sSection, String _sKey) 565cdf0e10cSrcweir // { 566cdf0e10cSrcweir // String sValue = getValue(_sSection, _sKey); 567cdf0e10cSrcweir // sValue = replaceEvaluatedValue(_sSection, sValue); 568cdf0e10cSrcweir // return sValue; 569cdf0e10cSrcweir // } 570cdf0e10cSrcweir 571cdf0e10cSrcweir // ----------------------------------------------------------------------------- 572cdf0e10cSrcweir 573cdf0e10cSrcweir // this is a special behaviour. 574cdf0e10cSrcweir // public String getGlobalLocalEvaluatedValue(String _sSection, String _sKey) 575cdf0e10cSrcweir // { 576cdf0e10cSrcweir // String sGlobalValue = getKey("global", _sKey); 577cdf0e10cSrcweir // String sLocalValue = getKey(_sSection, _sKey); 578cdf0e10cSrcweir // if (sLocalValue.length() == 0) 579cdf0e10cSrcweir // { 580cdf0e10cSrcweir // sGlobalValue = replaceEvaluatedKey(_sSection, sGlobalValue); 581cdf0e10cSrcweir // sGlobalValue = replaceEvaluatedKey("global", sGlobalValue); 582cdf0e10cSrcweir // return sGlobalValue; 583cdf0e10cSrcweir // } 584cdf0e10cSrcweir // sLocalValue = replaceEvaluatedKey(_sSection, sLocalValue); 585cdf0e10cSrcweir // sLocalValue = replaceEvaluatedKey("global", sLocalValue); 586cdf0e10cSrcweir // 587cdf0e10cSrcweir // return sLocalValue; 588cdf0e10cSrcweir // } removeSection(String _sSectionToRemove)589cdf0e10cSrcweir public void removeSection(String _sSectionToRemove) 590cdf0e10cSrcweir { 591cdf0e10cSrcweir // first, search for the name 592cdf0e10cSrcweir int i = findSection(_sSectionToRemove); 593cdf0e10cSrcweir if (i == -1) 594cdf0e10cSrcweir { 595cdf0e10cSrcweir // Section to remove not found, do nothing. 596cdf0e10cSrcweir return; 597cdf0e10cSrcweir } 598cdf0e10cSrcweir // second, find the next section 599cdf0e10cSrcweir int j = findNextSection(i + 1); 600cdf0e10cSrcweir if (j == -1) 601cdf0e10cSrcweir { 602cdf0e10cSrcweir // if we are at the end, use size() as second section 603cdf0e10cSrcweir j = m_aList.size(); 604cdf0e10cSrcweir } 605cdf0e10cSrcweir // remove all between first and second section 606cdf0e10cSrcweir for (int k = i; k < j; k++) 607cdf0e10cSrcweir { 608cdf0e10cSrcweir m_aList.remove(i); 609cdf0e10cSrcweir } 610cdf0e10cSrcweir // mark the list as changed 611cdf0e10cSrcweir m_bListContainUnsavedChanges = true; 612cdf0e10cSrcweir } 613cdf0e10cSrcweir 614cdf0e10cSrcweir /** 615cdf0e10cSrcweir * some tests for this class 616cdf0e10cSrcweir */ 617cdf0e10cSrcweir // public static void main(String[] args) 618cdf0e10cSrcweir // { 619cdf0e10cSrcweir // String sTempFile = System.getProperty("java.io.tmpdir"); 620cdf0e10cSrcweir // sTempFile += "inifile"; 621cdf0e10cSrcweir // 622cdf0e10cSrcweir // 623cdf0e10cSrcweir // IniFile aIniFile = new IniFile(sTempFile); 624cdf0e10cSrcweir // String sValue = aIniFile.getValue("Section", "Key"); 625cdf0e10cSrcweir // // insert a new value to a already exist section 626cdf0e10cSrcweir // aIniFile.insertValue("Section", "Key2", "a new value in a existing section"); 627cdf0e10cSrcweir // // replace a value 628cdf0e10cSrcweir // aIniFile.insertValue("Section", "Key", "replaced value"); 629cdf0e10cSrcweir // // create a new value 630cdf0e10cSrcweir // aIniFile.insertValue("New Section", "Key", "a new key value pair"); 631cdf0e10cSrcweir // aIniFile.insertValue("New Section", "Key2", "a new second key value pair"); 632cdf0e10cSrcweir // 633cdf0e10cSrcweir // String sValue2 = aIniFile.getValue("Section2", "Key"); 634cdf0e10cSrcweir // 635cdf0e10cSrcweir // aIniFile.removeSection("Section"); 636cdf0e10cSrcweir // aIniFile.removeSection("New Section"); 637cdf0e10cSrcweir // 638cdf0e10cSrcweir // aIniFile.close(); 639cdf0e10cSrcweir // } 640cdf0e10cSrcweir 641cdf0e10cSrcweir /** 642cdf0e10cSrcweir * Enumeration Interface 643cdf0e10cSrcweir * @return true, if there are more Key values 644cdf0e10cSrcweir */ hasMoreElements()645cdf0e10cSrcweir public boolean hasMoreElements() 646cdf0e10cSrcweir { 647cdf0e10cSrcweir if (m_aEnumerationPos >= 0 && 648cdf0e10cSrcweir m_aEnumerationPos < m_aList.size()) 649cdf0e10cSrcweir { 650cdf0e10cSrcweir return true; 651cdf0e10cSrcweir } 652cdf0e10cSrcweir return false; 653cdf0e10cSrcweir } 654cdf0e10cSrcweir 655cdf0e10cSrcweir /** 656cdf0e10cSrcweir * Find the next line, which starts with '[' 657cdf0e10cSrcweir * @param i start position 658cdf0e10cSrcweir * @return the line where '[' found or -1 659cdf0e10cSrcweir */ findNextSection(int i)660cdf0e10cSrcweir private int findNextSection(int i) 661cdf0e10cSrcweir { 662cdf0e10cSrcweir if (i >= 0) 663cdf0e10cSrcweir { 664cdf0e10cSrcweir while (i < m_aList.size()) 665cdf0e10cSrcweir { 666cdf0e10cSrcweir String sLine = m_aList.get(i); 667cdf0e10cSrcweir if (sLine.startsWith("[")) 668cdf0e10cSrcweir { 669cdf0e10cSrcweir return i; 670cdf0e10cSrcweir } 671cdf0e10cSrcweir i++; 672cdf0e10cSrcweir } 673cdf0e10cSrcweir } 674cdf0e10cSrcweir return -1; 675cdf0e10cSrcweir } 676cdf0e10cSrcweir 677cdf0e10cSrcweir /** 678cdf0e10cSrcweir * Enumeration Interface 679cdf0e10cSrcweir * @return a key without the enveloped '[' ']' 680cdf0e10cSrcweir */ nextElement()681cdf0e10cSrcweir public Object nextElement() 682cdf0e10cSrcweir { 683cdf0e10cSrcweir int nLineWithSection = findNextSection(m_aEnumerationPos); 684cdf0e10cSrcweir if (nLineWithSection != -1) 685cdf0e10cSrcweir { 686cdf0e10cSrcweir String sSection = m_aList.get(nLineWithSection); 687cdf0e10cSrcweir m_aEnumerationPos = findNextSection(nLineWithSection + 1); 688cdf0e10cSrcweir sSection = sectionToString(sSection); 689cdf0e10cSrcweir return sSection; 690cdf0e10cSrcweir } 691cdf0e10cSrcweir else 692cdf0e10cSrcweir { 693cdf0e10cSrcweir m_aEnumerationPos = m_aList.size(); 694cdf0e10cSrcweir } 695cdf0e10cSrcweir return null; 696cdf0e10cSrcweir } 697cdf0e10cSrcweir 698cdf0e10cSrcweir /** 699cdf0e10cSrcweir * Helper to count the occurence of Sections 700cdf0e10cSrcweir * @return returns the count of '^['.*']$' Elements 701cdf0e10cSrcweir */ getElementCount()702cdf0e10cSrcweir public int getElementCount() 703cdf0e10cSrcweir { 704cdf0e10cSrcweir int nCount = 0; 705cdf0e10cSrcweir int nPosition = 0; 706cdf0e10cSrcweir while ((nPosition = findNextSection(nPosition)) != -1) 707cdf0e10cSrcweir { 708cdf0e10cSrcweir nCount++; 709cdf0e10cSrcweir nPosition++; 710cdf0e10cSrcweir } 711cdf0e10cSrcweir return nCount; 712cdf0e10cSrcweir } 713cdf0e10cSrcweir } 714cdf0e10cSrcweir 715