*** empty log message ***
[thera-pi.git] / BMIRechner / src / Tools / INIFile.java
blob6f6b3a060cf0bcccd72e9553692c097721e17bb5
1 package Tools;
3 /*------------------------------------------------------------------------------
4 * PACKAGE: com.freeware.inifiles
5 * FILE : iniFile.java
6 * CREATED: Jun 30, 2004
7 * AUTHOR : Prasad P. Khandekar
8 *------------------------------------------------------------------------------
9 * Change Log:
10 * 05/07/2004 - Added support for date time formats.
11 * Added support for environment variables.
12 * 07/07/2004 - Added support for data type specific getters and setters.
13 * Updated main method to reflect above changes.
14 * 26/08/2004 - Added support for section level and property level comments.
15 * Introduction of seperate class for property values.
16 * Added addSection method.
17 * Sections and properties now retail their order (LinkedHashMap)
18 * Method implementation changes.
19 *-----------------------------------------------------------------------------*/
20 //package com.freeware.inifiles;
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.io.Writer;
31 import java.sql.Timestamp;
32 import java.text.DateFormat;
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.Collections;
36 import java.util.Date;
37 import java.util.Iterator;
38 import java.util.LinkedHashMap;
39 import java.util.Map;
40 import java.util.NoSuchElementException;
41 import java.util.Properties;
42 import java.util.Set;
46 /**
47 * INIFile class provides methods for manipulating (Read/Write) windows ini files.
49 * @author Prasad P. Khandekar
50 * @version 1.0
51 * @since 1.0
53 public final class INIFile
55 /** Variable to represent the date format */
56 private String mstrDateFmt = "dd.mm.yyyy";
58 /** Variable to represent the timestamp format */
59 private String mstrTimeStampFmt = "dd.mm.yyyy hh:mm:ss";
61 /** Variable to denote the successfull load operation. */
62 private boolean mblnLoaded = false;
64 /** Variable to hold the ini file name and full path */
65 private String mstrFile;
67 /** Variable to hold the sections in an ini file. */
68 private LinkedHashMap mhmapSections;
70 /** Variable to hold environment variables **/
71 private Properties mpropEnv;
73 /**
74 * Create a iniFile object from the file named in the parameter.
75 * @param pstrPathAndName The full path and name of the ini file to be used.
77 public INIFile(String pstrPathAndName)
79 this.mpropEnv = getEnvVars();
80 this.mhmapSections = new LinkedHashMap();
81 this.mstrFile = pstrPathAndName;
82 // Load the specified INI file.
83 if (checkFile(pstrPathAndName)) loadFile();
86 /*------------------------------------------------------------------------------
87 * Getters
88 ------------------------------------------------------------------------------*/
89 /**
90 * Returns the ini file name being used.
91 * @return the INI file name.
93 public String getFileName()
95 return this.mstrFile;
98 /**
99 * Returns the specified string property from the specified section.
100 * @param pstrSection the INI section name.
101 * @param pstrProp the property to be retrieved.
102 * @return the string property value.
104 public String getStringProperty(String pstrSection, String pstrProp)
106 String strRet = null;
107 INIProperty objProp = null;
108 INISection objSec = null;
110 objSec = (INISection) this.mhmapSections.get(pstrSection);
111 if (objSec != null)
113 objProp = objSec.getProperty(pstrProp);
114 if (objProp != null)
116 strRet = objProp.getPropValue();
117 objProp = null;
119 objSec = null;
121 return strRet;
125 * Returns the specified boolean property from the specified section.
126 * This method considers the following values as boolean values.
127 * <ol>
128 * <li>YES/yes/Yes - boolean true</li>
129 * <li>NO/no/No - boolean false</li>
130 * <li>1 - boolean true</li>
131 * <li>0 - boolean false</li>
132 * <li>TRUE/True/true - boolean true</li>
133 * <li>FALSE/False/false - boolean false</li>
134 * </ol>
135 * @param pstrSection the INI section name.
136 * @param pstrProp the property to be retrieved.
137 * @return the boolean value
139 public Boolean getBooleanProperty(String pstrSection, String pstrProp)
141 boolean blnRet = false;
142 String strVal = null;
143 INIProperty objProp = null;
144 INISection objSec = null;
146 objSec = (INISection) this.mhmapSections.get(pstrSection);
147 if (objSec != null)
149 objProp = objSec.getProperty(pstrProp);
150 if (objProp != null)
152 strVal = objProp.getPropValue().toUpperCase();
153 if (strVal.equals("YES") || strVal.equals("TRUE") ||
154 strVal.equals("1"))
156 blnRet = true;
158 objProp = null;
160 objSec = null;
162 return new Boolean(blnRet);
166 * Returns the specified integer property from the specified section.
167 * @param pstrSection the INI section name.
168 * @param pstrProp the property to be retrieved.
169 * @return the integer property value.
171 public Integer getIntegerProperty(String pstrSection, String pstrProp)
173 Integer intRet = null;
174 String strVal = null;
175 INIProperty objProp = null;
176 INISection objSec = null;
178 objSec = (INISection) this.mhmapSections.get(pstrSection);
179 if (objSec != null)
181 objProp = objSec.getProperty(pstrProp);
184 if (objProp != null)
186 strVal = objProp.getPropValue();
187 if (strVal != null) intRet = new Integer(strVal);
190 catch (NumberFormatException NFExIgnore)
193 finally
195 if (objProp != null) objProp = null;
197 objSec = null;
199 return intRet;
203 * Returns the specified long property from the specified section.
204 * @param pstrSection the INI section name.
205 * @param pstrProp the property to be retrieved.
206 * @return the long property value.
208 public Long getLongProperty(String pstrSection, String pstrProp)
210 Long lngRet = null;
211 String strVal = null;
212 INIProperty objProp = null;
213 INISection objSec = null;
215 objSec = (INISection) this.mhmapSections.get(pstrSection);
216 if (objSec != null)
218 objProp = objSec.getProperty(pstrProp);
221 if (objProp != null)
223 strVal = objProp.getPropValue();
224 if (strVal != null) lngRet = new Long(strVal);
227 catch (NumberFormatException NFExIgnore)
230 finally
232 if (objProp != null) objProp = null;
234 objSec = null;
236 return lngRet;
240 * Returns the specified double property from the specified section.
241 * @param pstrSection the INI section name.
242 * @param pstrProp the property to be retrieved.
243 * @return the double property value.
245 public Double getDoubleProperty(String pstrSection, String pstrProp)
247 Double dblRet = null;
248 String strVal = null;
249 INIProperty objProp = null;
250 INISection objSec = null;
252 objSec = (INISection) this.mhmapSections.get(pstrSection);
253 if (objSec != null)
255 objProp = objSec.getProperty(pstrProp);
258 if (objProp != null)
260 strVal = objProp.getPropValue();
261 if (strVal != null) dblRet = new Double(strVal);
264 catch (NumberFormatException NFExIgnore)
267 finally
269 if (objProp != null) objProp = null;
271 objSec = null;
273 return dblRet;
277 * Returns the specified date property from the specified section.
278 * @param pstrSection the INI section name.
279 * @param pstrProp the property to be retrieved.
280 * @return the date property value.
282 public Date getDateProperty(String pstrSection, String pstrProp)
284 Date dtRet = null;
285 String strVal = null;
286 DateFormat dtFmt = null;
287 INIProperty objProp = null;
288 INISection objSec = null;
290 objSec = (INISection) this.mhmapSections.get(pstrSection);
291 if (objSec != null)
293 objProp = objSec.getProperty(pstrProp);
296 if (objProp != null) strVal = objProp.getPropValue();
297 if (strVal != null)
299 dtFmt = new SimpleDateFormat(this.mstrDateFmt);
300 dtRet = dtFmt.parse(strVal);
303 catch (ParseException PExIgnore)
306 catch (IllegalArgumentException IAEx)
309 finally
311 if (objProp != null) objProp = null;
313 objSec = null;
315 return dtRet;
319 * Returns the specified date property from the specified section.
320 * @param pstrSection the INI section name.
321 * @param pstrProp the property to be retrieved.
322 * @return the date property value.
324 public Date getTimestampProperty(String pstrSection, String pstrProp)
326 Timestamp tsRet = null;
327 Date dtTmp = null;
328 String strVal = null;
329 DateFormat dtFmt = null;
330 INIProperty objProp = null;
331 INISection objSec = null;
333 objSec = (INISection) this.mhmapSections.get(pstrSection);
334 if (objSec != null)
336 objProp = objSec.getProperty(pstrProp);
339 if (objProp != null) strVal = objProp.getPropValue();
340 if (strVal != null)
342 dtFmt = new SimpleDateFormat(this.mstrDateFmt);
343 dtTmp = dtFmt.parse(strVal);
344 tsRet = new Timestamp(dtTmp.getTime());
347 catch (ParseException PExIgnore)
350 catch (IllegalArgumentException IAEx)
353 finally
355 if (objProp != null) objProp = null;
357 objSec = null;
359 return tsRet;
362 /*------------------------------------------------------------------------------
363 * Setters
364 ------------------------------------------------------------------------------*/
366 * Sets the comments associated with a section.
367 * @param pstrSection the section name
368 * @param pstrComments the comments.
370 public void addSection(String pstrSection, String pstrComments)
372 INISection objSec = null;
374 objSec = (INISection) this.mhmapSections.get(pstrSection);
375 if (objSec == null)
377 objSec = new INISection(pstrSection);
378 this.mhmapSections.put(pstrSection, objSec);
380 objSec.setSecComments(delRemChars(pstrComments));
381 objSec = null;
385 * Sets the specified string property.
386 * @param pstrSection the INI section name.
387 * @param pstrProp the property to be set.
388 * @pstrVal the string value to be persisted
390 public void setStringProperty(String pstrSection, String pstrProp,
391 String pstrVal, String pstrComments)
393 INISection objSec = null;
395 objSec = (INISection) this.mhmapSections.get(pstrSection);
396 if (objSec == null)
398 objSec = new INISection(pstrSection);
399 this.mhmapSections.put(pstrSection, objSec);
401 objSec.setProperty(pstrProp, pstrVal, pstrComments);
405 * Sets the specified boolean property.
406 * @param pstrSection the INI section name.
407 * @param pstrProp the property to be set.
408 * @param pblnVal the boolean value to be persisted
410 public void setBooleanProperty(String pstrSection, String pstrProp,
411 boolean pblnVal, String pstrComments)
413 INISection objSec = null;
415 objSec = (INISection) this.mhmapSections.get(pstrSection);
416 if (objSec == null)
418 objSec = new INISection(pstrSection);
419 this.mhmapSections.put(pstrSection, objSec);
421 if (pblnVal)
422 objSec.setProperty(pstrProp, "TRUE", pstrComments);
423 else
424 objSec.setProperty(pstrProp, "FALSE", pstrComments);
428 * Sets the specified integer property.
429 * @param pstrSection the INI section name.
430 * @param pstrProp the property to be set.
431 * @param pintVal the int property to be persisted.
433 public void setIntegerProperty(String pstrSection, String pstrProp,
434 int pintVal, String pstrComments)
436 INISection objSec = null;
438 objSec = (INISection) this.mhmapSections.get(pstrSection);
439 if (objSec == null)
441 objSec = new INISection(pstrSection);
442 this.mhmapSections.put(pstrSection, objSec);
444 objSec.setProperty(pstrProp, Integer.toString(pintVal), pstrComments);
448 * Sets the specified long property.
449 * @param pstrSection the INI section name.
450 * @param pstrProp the property to be set.
451 * @param plngVal the long value to be persisted.
453 public void setLongProperty(String pstrSection, String pstrProp,
454 long plngVal, String pstrComments)
456 INISection objSec = null;
458 objSec = (INISection) this.mhmapSections.get(pstrSection);
459 if (objSec == null)
461 objSec = new INISection(pstrSection);
462 this.mhmapSections.put(pstrSection, objSec);
464 objSec.setProperty(pstrProp, Long.toString(plngVal), pstrComments);
468 * Sets the specified double property.
469 * @param pstrSection the INI section name.
470 * @param pstrProp the property to be set.
471 * @param pdblVal the double value to be persisted.
473 public void setDoubleProperty(String pstrSection, String pstrProp,
474 double pdblVal, String pstrComments)
476 INISection objSec = null;
478 objSec = (INISection) this.mhmapSections.get(pstrSection);
479 if (objSec == null)
481 objSec = new INISection(pstrSection);
482 this.mhmapSections.put(pstrSection, objSec);
484 objSec.setProperty(pstrProp, Double.toString(pdblVal), pstrComments);
488 * Sets the specified java.util.Date property.
489 * @param pstrSection the INI section name.
490 * @param pstrProp the property to be set.
491 * @param pdtVal the date value to be persisted.
493 public void setDateProperty(String pstrSection, String pstrProp,
494 Date pdtVal, String pstrComments)
496 INISection objSec = null;
498 objSec = (INISection) this.mhmapSections.get(pstrSection);
499 if (objSec == null)
501 objSec = new INISection(pstrSection);
502 this.mhmapSections.put(pstrSection, objSec);
504 objSec.setProperty(pstrProp, utilDateToStr(pdtVal, this.mstrDateFmt),
505 pstrComments);
509 * Sets the specified java.sql.Timestamp property.
510 * @param pstrSection the INI section name.
511 * @param pstrProp the property to be set.
512 * @param ptsVal the timestamp value to be persisted.
514 public void setTimestampProperty(String pstrSection, String pstrProp,
515 Timestamp ptsVal, String pstrComments)
517 INISection objSec = null;
519 objSec = (INISection) this.mhmapSections.get(pstrSection);
520 if (objSec == null)
522 objSec = new INISection(pstrSection);
523 this.mhmapSections.put(pstrSection, objSec);
525 objSec.setProperty(pstrProp, timeToStr(ptsVal, this.mstrTimeStampFmt),
526 pstrComments);
530 * Sets the format to be used to interpreat date values.
531 * @param pstrDtFmt the format string
532 * @throws IllegalArgumentException if the if the given pattern is invalid
534 public void setDateFormat(String pstrDtFmt) throws IllegalArgumentException
536 if (!checkDateTimeFormat(pstrDtFmt))
537 throw new IllegalArgumentException("The specified date pattern is invalid!");
538 this.mstrDateFmt = pstrDtFmt;
542 * Sets the format to be used to interpreat timestamp values.
543 * @param pstrTSFmt the format string
544 * @throws IllegalArgumentException if the if the given pattern is invalid
546 public void setTimeStampFormat(String pstrTSFmt)
548 if (!checkDateTimeFormat(pstrTSFmt))
549 throw new IllegalArgumentException("The specified timestamp pattern is invalid!");
550 this.mstrTimeStampFmt = pstrTSFmt;
553 /*------------------------------------------------------------------------------
554 * Public methods
555 ------------------------------------------------------------------------------*/
556 public int getTotalSections()
558 return this.mhmapSections.size();
562 * Returns a string array containing names of all sections in INI file.
563 * @return the string array of section names
565 public String[] getAllSectionNames()
567 int iCntr = 0;
568 Iterator iter = null;
569 String[] arrRet = null;
573 if (this.mhmapSections.size() > 0)
575 arrRet = new String[this.mhmapSections.size()];
576 for (iter = this.mhmapSections.keySet().iterator();;iter.hasNext())
578 arrRet[iCntr] = (String) iter.next();
579 iCntr++;
583 catch (NoSuchElementException NSEExIgnore)
586 finally
588 if (iter != null) iter = null;
590 return arrRet;
594 * Returns a string array containing names of all the properties under specified section.
595 * @param pstrSection the name of the section for which names of properties is to be retrieved.
596 * @return the string array of property names.
598 public String[] getPropertyNames(String pstrSection)
600 String[] arrRet = null;
601 INISection objSec = null;
603 objSec = (INISection) this.mhmapSections.get(pstrSection);
604 if (objSec != null)
606 arrRet = objSec.getPropNames();
607 objSec = null;
609 return arrRet;
613 * Returns a map containing all the properties under specified section.
614 * @param pstrSection the name of the section for which properties are to be retrieved.
615 * @return the map of properties.
617 public Map getProperties(String pstrSection)
619 Map hmRet = null;
620 INISection objSec = null;
622 objSec = (INISection) this.mhmapSections.get(pstrSection);
623 if (objSec != null)
625 hmRet = objSec.getProperties();
626 objSec = null;
628 return hmRet;
632 * Removed specified property from the specified section. If the specified
633 * section or the property does not exist, does nothing.
634 * @param pstrSection the section name.
635 * @param pstrProp the name of the property to be removed.
637 public void removeProperty(String pstrSection, String pstrProp)
639 INISection objSec = null;
641 objSec = (INISection) this.mhmapSections.get(pstrSection);
642 if (objSec != null)
644 objSec.removeProperty(pstrProp);
645 objSec = null;
650 * Removes the specified section if one exists, otherwise does nothing.
651 * @param pstrSection the name of the section to be removed.
653 public void removeSection(String pstrSection)
655 if (this.mhmapSections.containsKey(pstrSection))
656 this.mhmapSections.remove(pstrSection);
660 * Flush changes back to the disk file. If the disk file does not exists then
661 * creates the new one.
663 public boolean save()
665 boolean blnRet = false;
666 File objFile = null;
667 String strName = null;
668 String strTemp = null;
669 Iterator itrSec = null;
670 INISection objSec = null;
671 FileWriter objWriter = null;
675 if (this.mhmapSections.size() == 0) return false;
676 objFile = new File(this.mstrFile);
677 if (objFile.exists()) objFile.delete();
678 objWriter = new FileWriter(objFile);
679 itrSec = this.mhmapSections.keySet().iterator();
680 while (itrSec.hasNext())
682 strName = (String) itrSec.next();
683 objSec = (INISection) this.mhmapSections.get(strName);
684 strTemp = objSec.toString();
685 objWriter.write(strTemp);
686 objWriter.write("\r\n");
687 objSec = null;
689 blnRet = true;
691 catch (IOException IOExIgnore)
694 finally
696 if (objWriter != null)
698 closeWriter(objWriter);
699 objWriter = null;
701 if (objFile != null) objFile = null;
702 if (itrSec != null) itrSec = null;
704 return blnRet;
707 /*------------------------------------------------------------------------------
708 * Helper functions
709 *----------------------------------------------------------------------------*/
711 * Procedure to read environment variables.
712 * Thanx to http://www.rgagnon.com/howto.html for this implementation.
714 private Properties getEnvVars()
716 Process p = null;
717 Properties envVars = new Properties();
721 Runtime r = Runtime.getRuntime();
722 String OS = System.getProperty("os.name").toLowerCase();
724 if (OS.indexOf("windows 9") > -1)
726 p = r.exec("command.com /c set");
728 else if ((OS.indexOf("nt") > -1) ||
729 (OS.indexOf("windows 2000") > -1) ||
730 (OS.indexOf("windows xp") > -1))
732 p = r.exec("cmd.exe /c set");
734 else
736 // our last hope, we assume Unix (thanks to H. Ware for the fix)
737 p = r.exec("env");
739 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
740 String line;
741 while((line = br.readLine()) != null)
743 int idx = line.indexOf('=');
744 String key = line.substring(0, idx);
745 String value = line.substring(idx + 1);
746 envVars.setProperty(key, value);
749 catch (Exception ExIgnore)
752 return envVars;
756 * Helper function to check the date time formats.
757 * @param pstrDtFmt the date time format string to be checked.
758 * @return true for valid date/time format, false otherwise.
760 private boolean checkDateTimeFormat(String pstrDtFmt)
762 boolean blnRet = false;
763 DateFormat objFmt = null;
767 objFmt = new SimpleDateFormat(pstrDtFmt);
768 blnRet = true;
770 catch (NullPointerException NPExIgnore)
773 catch (IllegalArgumentException IAExIgnore)
776 finally
778 if (objFmt != null) objFmt = null;
780 return blnRet;
784 * Reads the INI file and load its contentens into a section collection after
785 * parsing the file line by line.
787 private void loadFile()
789 int iPos = -1;
790 String strLine = null;
791 String strSection = null;
792 String strRemarks = null;
793 BufferedReader objBRdr = null;
794 FileReader objFRdr = null;
795 INISection objSec = null;
799 objFRdr = new FileReader(this.mstrFile);
800 if (objFRdr != null)
802 objBRdr = new BufferedReader(objFRdr);
803 if (objBRdr != null)
805 while (objBRdr.ready())
807 iPos = -1;
808 strLine = null;
809 strLine = objBRdr.readLine().trim();
810 if (strLine == null)
813 else if (strLine.length() == 0)
816 else if (strLine.substring(0, 1).equals(";"))
818 if (strRemarks == null)
819 strRemarks = strLine.substring(1);
820 else if (strRemarks.length() == 0)
821 strRemarks = strLine.substring(1);
822 else
823 strRemarks = strRemarks + "\r\n" + strLine.substring(1);
825 else if (strLine.startsWith("[") && strLine.endsWith("]"))
827 // Section start reached create new section
828 if (objSec != null)
829 this.mhmapSections.put(strSection.trim(), objSec);
830 objSec = null;
831 strSection = strLine.substring(1, strLine.length() - 1);
832 objSec = new INISection(strSection.trim(), strRemarks);
833 strRemarks = null;
835 else if ((iPos = strLine.indexOf("=")) > 0 && objSec != null)
837 // read the key value pair 012345=789
838 objSec.setProperty(strLine.substring(0, iPos).trim(),
839 strLine.substring(iPos + 1).trim(),
840 strRemarks);
841 strRemarks = null;
844 if (objSec != null)
845 this.mhmapSections.put(strSection.trim(), objSec);
846 this.mblnLoaded = true;
850 catch (FileNotFoundException FNFExIgnore)
852 this.mhmapSections.clear();
854 catch (IOException IOExIgnore)
856 this.mhmapSections.clear();
858 catch (NullPointerException NPExIgnore)
860 this.mhmapSections.clear();
862 finally
864 if (objBRdr != null)
866 closeReader(objBRdr);
867 objBRdr = null;
869 if (objFRdr != null)
871 closeReader(objFRdr);
872 objFRdr = null;
874 if (objSec != null) objSec = null;
879 * Helper function to close a reader object.
880 * @param pobjRdr the reader to be closed.
882 private void closeReader(Reader pobjRdr)
884 if (pobjRdr == null) return;
887 pobjRdr.close();
889 catch (IOException IOExIgnore)
895 * Helper function to close a writer object.
896 * @param pobjWriter the writer to be closed.
898 private void closeWriter(Writer pobjWriter)
900 if (pobjWriter == null) return;
904 pobjWriter.close();
906 catch (IOException IOExIgnore)
912 * Helper method to check the existance of a file.
913 * @param the full path and name of the file to be checked.
914 * @return true if file exists, false otherwise.
916 private boolean checkFile(String pstrFile)
918 boolean blnRet = false;
919 File objFile = null;
923 objFile = new File(pstrFile);
924 blnRet = (objFile.exists() && objFile.isFile());
926 catch (Exception e)
928 blnRet = false;
930 finally
932 if (objFile != null) objFile = null;
934 return blnRet;
938 * Converts a java.util.date into String
939 * @param pd Date that need to be converted to String
940 * @param pstrFmt The date format pattern.
941 * @return String
943 private String utilDateToStr(Date pdt, String pstrFmt)
945 String strRet = null;
946 SimpleDateFormat dtFmt = null;
950 dtFmt = new SimpleDateFormat(pstrFmt);
951 strRet = dtFmt.format(pdt);
953 catch (Exception e)
955 strRet = null;
957 finally
959 if (dtFmt != null) dtFmt = null;
961 return strRet;
965 * Converts the given sql timestamp object to a string representation. The format
966 * to be used is to be obtained from the configuration file.
968 * @param pobjTS the sql timestamp object to be converted.
969 * @param pblnGMT If true formats the string using GMT timezone
970 * otherwise using local timezone.
971 * @return the formatted string representation of the timestamp.
973 private String timeToStr(Timestamp pobjTS, String pstrFmt)
975 String strRet = null;
976 SimpleDateFormat dtFmt = null;
980 dtFmt = new SimpleDateFormat(pstrFmt);
981 strRet = dtFmt.format(pobjTS);
983 catch (IllegalArgumentException iae)
985 strRet = "";
987 catch (NullPointerException npe)
989 strRet = "";
991 finally
993 if (dtFmt != null) dtFmt = null;
995 return strRet;
999 * This function deletes the remark characters ';' from source string
1000 * @param pstrSrc the source string
1001 * @return the converted string
1003 private String delRemChars(String pstrSrc)
1005 int intPos = 0;
1007 if (pstrSrc == null) return null;
1008 while ((intPos = pstrSrc.indexOf(";")) >= 0)
1010 if (intPos == 0)
1011 pstrSrc = pstrSrc.substring(intPos + 1);
1012 else if (intPos > 0)
1013 pstrSrc = pstrSrc.substring(0, intPos) + pstrSrc.substring(intPos + 1);
1015 return pstrSrc;
1019 * This function adds a remark character ';' in source string.
1020 * @param pstrSrc source string
1021 * @return converted string.
1023 private String addRemChars(String pstrSrc)
1025 int intLen = 2;
1026 int intPos = 0;
1027 int intPrev = 0;
1029 String strLeft = null;
1030 String strRight = null;
1032 if (pstrSrc == null) return null;
1033 while (intPos >= 0)
1035 intLen = 2;
1036 intPos = pstrSrc.indexOf("\r\n", intPrev);
1037 if (intPos < 0)
1039 intLen = 1;
1040 intPos = pstrSrc.indexOf("\n", intPrev);
1041 if (intPos < 0) intPos = pstrSrc.indexOf("\r", intPrev);
1043 if (intPos == 0)
1045 pstrSrc = ";\r\n" + pstrSrc.substring(intPos + intLen);
1046 intPrev = intPos + intLen + 1;
1048 else if (intPos > 0)
1050 strLeft = pstrSrc.substring(0, intPos);
1051 strRight = pstrSrc.substring(intPos + intLen);
1052 if (strRight == null)
1053 pstrSrc = strLeft;
1054 else if (strRight.length() == 0)
1055 pstrSrc = strLeft;
1056 else
1057 pstrSrc = strLeft + "\r\n;" + strRight;
1058 intPrev = intPos + intLen + 1;
1061 if (!pstrSrc.substring(0, 1).equals(";"))
1062 pstrSrc = ";" + pstrSrc;
1063 pstrSrc = pstrSrc + "\r\n";
1064 return pstrSrc;
1066 /*------------------------------------------------------------------------------
1067 * Main entry point to test the functionality.
1068 *----------------------------------------------------------------------------*/
1070 * The main entry point for testing.
1071 * @param pstrArgs the command line arguments array if any.
1073 public static void main(String[] pstrArgs)
1075 INIFile objINI = null;
1076 String strFile = null;
1078 if (pstrArgs.length == 0) return;
1080 strFile = pstrArgs[0];
1081 // Following call will load the strFile if one exists.
1082 objINI = new INIFile(strFile);
1084 // objINI.addSection("QADatabase", "QA database connection details\nUsed for QA Testing");
1085 // objINI.setStringProperty("QADatabase", "SID", "ORCL", null);
1086 // objINI.setStringProperty("QADatabase", "UserId", "System", null);
1087 // objINI.setStringProperty("QADatabase", "Password", "Manager", null);
1088 // objINI.setStringProperty("QADatabase", "HostName", "DBServer", null);
1089 // objINI.setIntegerProperty("QADatabase", "Port", 1521, null);
1090 // objINI.setStringProperty("QADatabase", "OracleHome", "%ORACLE_HOME%", null);
1092 //objINI.setSectionComments("Folders", "Directories where generated files are stored");
1093 objINI.setStringProperty("Folders", "folder1", "G:\\Temp", null);
1094 objINI.setStringProperty("Folders", "folder2", "G:\\Temp\\Backup", null);
1096 // Save changes back to strFile.
1097 objINI.save();
1098 objINI = null;
1101 /*------------------------------------------------------------------------------
1102 * Private class representing the INI Section.
1103 *----------------------------------------------------------------------------*/
1105 * Class to represent the individual ini file section.
1106 * @author Prasad P. Khandekar
1107 * @version 1.0
1108 * @since 1.0
1110 private class INISection
1112 /** Variable to hold any comments associated with this section */
1113 private String mstrComment;
1115 /** Variable to hold the section name. */
1116 private String mstrName;
1118 /** Variable to hold the properties falling under this section. */
1119 private LinkedHashMap mhmapProps;
1122 * Construct a new section object identified by the name specified in
1123 * parameter.
1124 * @param pstrSection The new sections name.
1126 public INISection(String pstrSection)
1128 this.mstrName = pstrSection;
1129 this.mhmapProps = new LinkedHashMap();
1133 * Construct a new section object identified by the name specified in
1134 * parameter and associated comments.
1135 * @param pstrSection The new sections name.
1136 * @param pstrComments the comments associated with this section.
1138 public INISection(String pstrSection, String pstrComments)
1140 this.mstrName = pstrSection;
1141 this.mstrComment = delRemChars(pstrComments);
1142 this.mhmapProps = new LinkedHashMap();
1146 * Returns any comments associated with this section
1147 * @return the comments
1149 public String getSecComments()
1151 return this.mstrComment;
1155 * Returns name of the section.
1156 * @return Name of the section.
1158 public String getSecName()
1160 return this.mstrName;
1164 * Sets the comments associated with this section.
1165 * @param pstrComments the comments
1167 public void setSecComments(String pstrComments)
1169 this.mstrComment = delRemChars(pstrComments);
1173 * Sets the section name.
1174 * @param pstrName the section name.
1176 public void setSecName(String pstrName)
1178 this.mstrName = pstrName;
1182 * Removes specified property value from this section.
1183 * @param pstrProp The name of the property to be removed.
1185 public void removeProperty(String pstrProp)
1187 if (this.mhmapProps.containsKey(pstrProp))
1188 this.mhmapProps.remove(pstrProp);
1192 * Creates or modifies the specified property value.
1193 * @param pstrProp The name of the property to be created or modified.
1194 * @param pstrValue The new value for the property.
1195 * @param pstrComments the associated comments
1197 public void setProperty(String pstrProp, String pstrValue, String pstrComments)
1199 this.mhmapProps.put(pstrProp, new INIProperty(pstrProp, pstrValue, pstrComments));
1203 * Returns a map of all properties.
1204 * @return a map of all properties
1206 public Map getProperties()
1208 return Collections.unmodifiableMap(this.mhmapProps);
1212 * Returns a string array containing names of all the properties under
1213 * this section.
1214 * @return the string array of property names.
1216 public String[] getPropNames()
1218 int iCntr = 0;
1219 String[] arrRet = null;
1220 Iterator iter = null;
1224 if (this.mhmapProps.size() > 0)
1226 arrRet = new String[this.mhmapProps.size()];
1227 for (iter = this.mhmapProps.keySet().iterator();iter.hasNext();)
1229 arrRet[iCntr] = (String) iter.next();
1230 iCntr++;
1234 catch (NoSuchElementException NSEExIgnore)
1236 arrRet = null;
1238 return arrRet;
1242 * Returns underlying value of the specified property.
1243 * @param pstrProp the property whose underlying value is to be etrieved.
1244 * @return the property value.
1246 public INIProperty getProperty(String pstrProp)
1248 INIProperty objRet = null;
1250 if (this.mhmapProps.containsKey(pstrProp))
1251 objRet = (INIProperty) this.mhmapProps.get(pstrProp);
1252 return objRet;
1255 /* (non-Javadoc)
1256 * @see java.lang.Object#toString()
1258 public String toString()
1260 Set colKeys = null;
1261 String strRet = "";
1262 Iterator iter = null;
1263 INIProperty objProp = null;
1264 StringBuffer objBuf = new StringBuffer();
1266 if (this.mstrComment != null)
1267 objBuf.append(addRemChars(this.mstrComment));
1268 objBuf.append("[" + this.mstrName + "]\r\n");
1269 colKeys = this.mhmapProps.keySet();
1270 if (colKeys != null)
1272 iter = colKeys.iterator();
1273 if (iter != null)
1275 while (iter.hasNext())
1277 objProp = (INIProperty) this.mhmapProps.get(iter.next());
1278 objBuf.append(objProp.toString());
1279 objBuf.append("\r\n");
1280 objProp = null;
1284 strRet = objBuf.toString();
1286 objBuf = null;
1287 iter = null;
1288 colKeys = null;
1289 return strRet;
1293 /*------------------------------------------------------------------------------
1294 * Private class representing the INI Property.
1295 *----------------------------------------------------------------------------*/
1297 * This class represents a key value pair called property in an INI file.
1298 * @author Prasad P. Khandekar
1299 * @version 1.0
1300 * @since 1.0
1302 private class INIProperty
1304 /** Variable to hold name of this property */
1305 private String mstrName;
1306 /** Variable to hold value of this property */
1307 private String mstrValue;
1308 /** Variable to hold comments associated with this property */
1309 private String mstrComments;
1312 * Constructor
1313 * @param pstrName the name of this property.
1314 * @param pstrValue the value of this property.
1316 public INIProperty(String pstrName, String pstrValue)
1318 this.mstrName = pstrName;
1319 this.mstrValue = pstrValue;
1323 * Constructor
1324 * @param pstrName the name of this property.
1325 * @param pstrValue the value of this property.
1326 * @param pstrComments the comments associated with this property.
1328 public INIProperty(String pstrName, String pstrValue, String pstrComments)
1330 this.mstrName = pstrName;
1331 this.mstrValue = pstrValue;
1332 this.mstrComments = delRemChars(pstrComments);
1336 * Returns the string identifier (key part) of this property.
1337 * @return the string identifier of this property.
1339 public String getPropName()
1341 return this.mstrName;
1345 * Returns value of this property. If value contains a reference to
1346 * environment avriable then this reference is replaced by actual value
1347 * before the value is returned.
1348 * @return the value of this property.
1350 public String getPropValue()
1352 int intStart = 0;
1353 int intEnd = 0;
1354 String strVal = null;
1355 String strVar = null;
1356 String strRet = null;
1358 strRet = this.mstrValue;
1359 intStart = strRet.indexOf("%");
1360 if (intStart >= 0)
1362 intEnd = strRet.indexOf("%", intStart + 1);
1363 strVar = strRet.substring(intStart + 1, intEnd);
1364 strVal = mpropEnv.getProperty(strVar);
1365 if (strVal != null)
1367 strRet = strRet.substring(0, intStart) + strVal +
1368 strRet.substring(intEnd + 1);
1371 return strRet;
1375 * Returns comments associated with this property.
1376 * @return the associated comments if any.
1378 public String getPropComments()
1380 return this.mstrComments;
1384 * Sets the string identifier (key part) of a property
1385 * @param pstrName the string identifier of a property
1387 public void setPropName(String pstrName)
1389 this.mstrName = pstrName;
1393 * Sets the property value
1394 * @param pstrValue the value for the property
1396 public void setPropValue(String pstrValue)
1398 this.mstrValue = pstrValue;
1402 * Sets the comments for a property
1403 * @param pstrComments the comments
1405 public void setPropComments(String pstrComments)
1407 this.mstrComments = delRemChars(pstrComments);
1410 /* (non-Javadoc)
1411 * @see java.lang.Object#toString()
1413 public String toString()
1415 String strRet = "";
1417 if (this.mstrComments != null)
1418 strRet = addRemChars(mstrComments);
1419 strRet = strRet + this.mstrName + " = " + this.mstrValue;
1420 return strRet;
1424 public void renameSection(String pstrSection,String newpstrSection, String pstrComments)
1426 INISection objSec = null;
1428 objSec = (INISection) this.mhmapSections.get(pstrSection);
1429 if (objSec != null)
1431 objSec.mstrName = new String(newpstrSection);
1433 objSec.setSecComments(delRemChars(pstrComments));
1434 objSec = null;