001package org.hl7.fhir.utilities; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.BufferedReader; 035import java.io.File; 036import java.io.FileNotFoundException; 037import java.io.FileReader; 038import java.io.FileWriter; 039import java.io.IOException; 040import java.io.InputStream; 041import java.io.InputStreamReader; 042import java.io.OutputStream; 043import java.io.OutputStreamWriter; 044import java.io.Reader; 045import java.io.Writer; 046 047import java.text.DateFormat; 048import java.text.ParseException; 049import java.text.SimpleDateFormat; 050import java.time.ZonedDateTime; 051import java.time.ZoneId; 052import java.time.format.DateTimeFormatter; 053import java.util.*; 054 055import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 056 057 058/** 059 * IniFile class provides methods for manipulating (Read/Write) windows ini files. 060 * 061 * @author Prasad P. Khandekar 062 * @version 1.0 063 * @since 1.0 064 */ 065public final class IniFile 066{ 067 /** Variable to represent the date format */ 068 private String mstrDateFmt = "yyyy-MM-dd"; 069 070 /** Variable to represent the timestamp format */ 071 private String mstrTimeStampFmt = "yyyy-MM-dd HH:mm:ss"; 072 073 /** Variable to denote the successful load operation. */ 074 @SuppressWarnings("unused") 075 private boolean fileSuccessfullyLoaded = false; 076 077 /** Variable to hold the ini file name and full path */ 078 private String filepath; 079 080 /** Variable to hold the sections in an ini file. */ 081 private LinkedHashMap<String, INISection> iniSections; 082 083 /** Variable to hold environment variables **/ 084 private Properties mpropEnv; 085 086 /** 087 * Create a IniFile object from the file named in the parameter. 088 * @param pathname The full path and name of the ini file to be used. 089 */ 090 public IniFile(String pathname) 091 { 092 this.mpropEnv = getEnvVars(); 093 this.iniSections = new LinkedHashMap<String, INISection>(); 094 this.filepath = pathname; 095 // Load the specified INI file. 096 if (checkFile(pathname)) loadFile(); 097 } 098 099 public IniFile(InputStream stream) { 100 this.mpropEnv = getEnvVars(); 101 this.iniSections = new LinkedHashMap<String, INISection>(); 102 this.filepath = null; 103 // Load the specified INI file. 104 loadStream(stream); 105 } 106 107 /*------------------------------------------------------------------------------ 108 * Getters 109------------------------------------------------------------------------------*/ 110 /** 111 * Returns the ini file name being used. 112 * @return the INI file name. 113 */ 114 public String getFileName() 115 { 116 return this.filepath; 117 } 118 119 /** 120 * Returns the specified string property from the specified section. 121 * @param pstrSection the INI section name. 122 * @param pstrProp the property to be retrieved. 123 * @return the string property value. 124 */ 125 public String getStringProperty(String pstrSection, String pstrProp) 126 { 127 String strRet = null; 128 INIProperty objProp = null; 129 INISection objSec = null; 130 131 objSec = (INISection) this.iniSections.get(pstrSection); 132 if (objSec != null) 133 { 134 objProp = objSec.getProperty(pstrProp); 135 if (objProp != null) 136 { 137 strRet = objProp.getPropValue(); 138 objProp = null; 139 } 140 objSec = null; 141 } 142 return strRet; 143 } 144 145 /** 146 * Returns the specified boolean property from the specified section. 147 * This method considers the following values as boolean values. 148 * <ol> 149 * <li>YES/yes/Yes - boolean true</li> 150 * <li>NO/no/No - boolean false</li> 151 * <li>1 - boolean true</li> 152 * <li>0 - boolean false</li> 153 * <li>TRUE/True/true - boolean true</li> 154 * <li>FALSE/False/false - boolean false</li> 155 * </ol> 156 * @param pstrSection the INI section name. 157 * @param pstrProp the property to be retrieved. 158 * @return the boolean value 159 */ 160 public Boolean getBooleanProperty(String pstrSection, String pstrProp) 161 { 162 boolean blnRet = false; 163 String strVal = null; 164 INIProperty objProp = null; 165 INISection objSec = null; 166 167 objSec = (INISection) this.iniSections.get(pstrSection); 168 if (objSec != null) 169 { 170 objProp = objSec.getProperty(pstrProp); 171 if (objProp != null) 172 { 173 strVal = objProp.getPropValue().toUpperCase(); 174 if (strVal.equals("YES") || strVal.equals("TRUE") || 175 strVal.equals("1")) 176 { 177 blnRet = true; 178 } 179 objProp = null; 180 } 181 objSec = null; 182 } 183 return blnRet; 184 } 185 186 /** 187 * Returns the specified integer property from the specified section. 188 * @param pstrSection the INI section name. 189 * @param pstrProp the property to be retrieved. 190 * @return the integer property value. 191 */ 192 public Integer getIntegerProperty(String pstrSection, String pstrProp) 193 { 194 Integer intRet = null; 195 String strVal = null; 196 INIProperty objProp = null; 197 INISection objSec = null; 198 199 objSec = (INISection) this.iniSections.get(pstrSection); 200 if (objSec != null) 201 { 202 objProp = objSec.getProperty(pstrProp); 203 try 204 { 205 if (objProp != null) 206 { 207 strVal = objProp.getPropValue(); 208 if (strVal != null) intRet = Integer.valueOf(strVal); 209 } 210 } 211 catch (NumberFormatException NFExIgnore) 212 { 213 } 214 finally 215 { 216 if (objProp != null) objProp = null; 217 } 218 objSec = null; 219 } 220 return intRet; 221 } 222 223 /** 224 * Returns the specified long property from the specified section. 225 * @param pstrSection the INI section name. 226 * @param pstrProp the property to be retrieved. 227 * @return the long property value. 228 */ 229 public Long getLongProperty(String pstrSection, String pstrProp) 230 { 231 Long lngRet = null; 232 String strVal = null; 233 INIProperty objProp = null; 234 INISection objSec = null; 235 236 objSec = (INISection) this.iniSections.get(pstrSection); 237 if (objSec != null) 238 { 239 objProp = objSec.getProperty(pstrProp); 240 try 241 { 242 if (objProp != null) 243 { 244 strVal = objProp.getPropValue(); 245 if (strVal != null) lngRet = Long.valueOf(strVal); 246 } 247 } 248 catch (NumberFormatException NFExIgnore) 249 { 250 } 251 finally 252 { 253 if (objProp != null) objProp = null; 254 } 255 objSec = null; 256 } 257 return lngRet; 258 } 259 260 /** 261 * Returns the specified double property from the specified section. 262 * @param pstrSection the INI section name. 263 * @param pstrProp the property to be retrieved. 264 * @return the double property value. 265 */ 266 public Double getDoubleProperty(String pstrSection, String pstrProp) 267 { 268 Double dblRet = null; 269 String strVal = null; 270 INIProperty objProp = null; 271 INISection objSec = null; 272 273 objSec = (INISection) this.iniSections.get(pstrSection); 274 if (objSec != null) 275 { 276 objProp = objSec.getProperty(pstrProp); 277 try 278 { 279 if (objProp != null) 280 { 281 strVal = objProp.getPropValue(); 282 if (strVal != null) dblRet = Double.valueOf(strVal); 283 } 284 } 285 catch (NumberFormatException NFExIgnore) 286 { 287 } 288 finally 289 { 290 if (objProp != null) objProp = null; 291 } 292 objSec = null; 293 } 294 return dblRet; 295 } 296 297 /** 298 * Returns the specified date property from the specified section. 299 * @param pstrSection the INI section name. 300 * @param pstrProp the property to be retrieved. 301 * @return the date property value. 302 */ 303 public Date getDateProperty(String pstrSection, String pstrProp) 304 { 305 Date dtRet = null; 306 String strVal = null; 307 DateFormat dtFmt = null; 308 INIProperty objProp = null; 309 INISection objSec = null; 310 311 objSec = (INISection) this.iniSections.get(pstrSection); 312 if (objSec != null) 313 { 314 objProp = objSec.getProperty(pstrProp); 315 try 316 { 317 if (objProp != null) strVal = objProp.getPropValue(); 318 if (strVal != null) 319 { 320 dtFmt = new SimpleDateFormat(this.mstrDateFmt); 321 dtRet = dtFmt.parse(strVal); 322 } 323 } 324 catch (ParseException PExIgnore) 325 { 326 } 327 catch (IllegalArgumentException IAEx) 328 { 329 } 330 finally 331 { 332 if (objProp != null) objProp = null; 333 } 334 objSec = null; 335 } 336 return dtRet; 337 } 338 339 /** 340 * Returns the specified date property from the specified section. 341 * @param iniSectionName the INI section name. 342 * @param propertyKey the property to be retrieved. 343 * @return the date property value. 344 */ 345 public Date getTimestampProperty(String iniSectionName, String propertyKey) { 346 347 Date dateToReturn = null; 348 349 INISection iniSection = this.iniSections.get(iniSectionName); 350 if (iniSection != null) { 351 INIProperty objProp = iniSection.getProperty(propertyKey); 352 try { 353 String propertyStringValue = null; 354 if (objProp != null) propertyStringValue = objProp.getPropValue(); 355 if (propertyStringValue != null) { 356 DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern(this.mstrTimeStampFmt).withZone(ZoneId.systemDefault()); 357 358 ZonedDateTime zonedDateTime = ZonedDateTime.parse(propertyStringValue, dtFmt); 359 dateToReturn = Date.from(zonedDateTime.toInstant()); 360 } 361 } catch (IllegalArgumentException IAEx) { 362 } 363 } 364 return dateToReturn; 365 } 366 367/*------------------------------------------------------------------------------ 368 * Setters 369------------------------------------------------------------------------------*/ 370 /** 371 * Sets the comments associated with a section. 372 * @param pstrSection the section name 373 * @param pstrComments the comments. 374 */ 375 public void addSection(String pstrSection, String pstrComments) 376 { 377 INISection objSec = null; 378 379 objSec = (INISection) this.iniSections.get(pstrSection); 380 if (objSec == null) 381 { 382 objSec = new INISection(pstrSection); 383 this.iniSections.put(pstrSection, objSec); 384 } 385 objSec.setSecComments(delRemChars(pstrComments)); 386 objSec = null; 387 } 388 389 /** 390 * Sets the specified string property. 391 * @param pstrSection the INI section name. 392 * @param pstrProp the property to be set. 393 * @pstrVal the string value to be persisted 394 */ 395 public void setStringProperty(String pstrSection, String pstrProp, 396 String pstrVal, String pstrComments) 397 { 398 INISection objSec = null; 399 400 objSec = (INISection) this.iniSections.get(pstrSection); 401 if (objSec == null) 402 { 403 objSec = new INISection(pstrSection); 404 this.iniSections.put(pstrSection, objSec); 405 } 406 objSec.setProperty(pstrProp, pstrVal, pstrComments); 407 } 408 409 /** 410 * Sets the specified boolean property. 411 * @param pstrSection the INI section name. 412 * @param pstrProp the property to be set. 413 * @param pblnVal the boolean value to be persisted 414 */ 415 public void setBooleanProperty(String pstrSection, String pstrProp, 416 boolean pblnVal, String pstrComments) 417 { 418 INISection objSec = null; 419 420 objSec = (INISection) this.iniSections.get(pstrSection); 421 if (objSec == null) 422 { 423 objSec = new INISection(pstrSection); 424 this.iniSections.put(pstrSection, objSec); 425 } 426 if (pblnVal) 427 objSec.setProperty(pstrProp, "TRUE", pstrComments); 428 else 429 objSec.setProperty(pstrProp, "FALSE", pstrComments); 430 } 431 432 /** 433 * Sets the specified integer property. 434 * @param pstrSection the INI section name. 435 * @param pstrProp the property to be set. 436 * @param pintVal the int property to be persisted. 437 */ 438 public void setIntegerProperty(String pstrSection, String pstrProp, 439 int pintVal, String pstrComments) 440 { 441 INISection objSec = null; 442 443 objSec = (INISection) this.iniSections.get(pstrSection); 444 if (objSec == null) 445 { 446 objSec = new INISection(pstrSection); 447 this.iniSections.put(pstrSection, objSec); 448 } 449 objSec.setProperty(pstrProp, Integer.toString(pintVal), pstrComments); 450 } 451 452 /** 453 * Sets the specified long property. 454 * @param pstrSection the INI section name. 455 * @param pstrProp the property to be set. 456 * @param plngVal the long value to be persisted. 457 */ 458 public void setLongProperty(String pstrSection, String pstrProp, 459 long plngVal, String pstrComments) 460 { 461 INISection objSec = null; 462 463 objSec = this.iniSections.get(pstrSection); 464 if (objSec == null) 465 { 466 objSec = new INISection(pstrSection); 467 this.iniSections.put(pstrSection, objSec); 468 } 469 objSec.setProperty(pstrProp, Long.toString(plngVal), pstrComments); 470 } 471 472 /** 473 * Sets the specified double property. 474 * @param pstrSection the INI section name. 475 * @param pstrProp the property to be set. 476 * @param pdblVal the double value to be persisted. 477 */ 478 public void setDoubleProperty(String pstrSection, String pstrProp, 479 double pdblVal, String pstrComments) 480 { 481 INISection objSec = null; 482 483 objSec = (INISection) this.iniSections.get(pstrSection); 484 if (objSec == null) 485 { 486 objSec = new INISection(pstrSection); 487 this.iniSections.put(pstrSection, objSec); 488 } 489 objSec.setProperty(pstrProp, Double.toString(pdblVal), pstrComments); 490 } 491 492 /** 493 * Sets the specified java.util.Date property. 494 * @param pstrSection the INI section name. 495 * @param pstrProp the property to be set. 496 * @param pdtVal the date value to be persisted. 497 */ 498 public void setDateProperty(String pstrSection, String pstrProp, 499 Date pdtVal, String pstrComments) 500 { 501 INISection objSec = null; 502 503 objSec = (INISection) this.iniSections.get(pstrSection); 504 if (objSec == null) 505 { 506 objSec = new INISection(pstrSection); 507 this.iniSections.put(pstrSection, objSec); 508 } 509 objSec.setProperty(pstrProp, utilDateToStr(pdtVal, this.mstrDateFmt), 510 pstrComments); 511 } 512 513 /** 514 * Sets the specified java.sql.Timestamp property. 515 * @param iniSection the INI section name. 516 * @param propertyKey the property to be set. 517 * @param propertyValue the timestamp value to be persisted. 518 */ 519 public void setTimestampProperty(String iniSection, String propertyKey, 520 ZonedDateTime propertyValue, String propertyComments) 521 { 522 INISection objSec = null; 523 524 objSec = (INISection) this.iniSections.get(iniSection); 525 if (objSec == null) 526 { 527 objSec = new INISection(iniSection); 528 this.iniSections.put(iniSection, objSec); 529 } 530 objSec.setProperty(propertyKey, timeToString(propertyValue, this.mstrTimeStampFmt), 531 propertyComments); 532 } 533 534 /** 535 * Sets the format to be used to interpret date values. 536 * @param pstrDtFmt the format string 537 * @throws IllegalArgumentException if the if the given pattern is invalid 538 */ 539 public void setDateFormat(String pstrDtFmt) throws IllegalArgumentException 540 { 541 if (!checkDateTimeFormat(pstrDtFmt)) 542 throw new IllegalArgumentException("The specified date pattern is invalid!"); 543 this.mstrDateFmt = pstrDtFmt; 544 } 545 546 /** 547 * Sets the format to be used to interpreat timestamp values. 548 * @param pstrTSFmt the format string 549 * @throws IllegalArgumentException if the if the given pattern is invalid 550 */ 551 public void setTimeStampFormat(String pstrTSFmt) 552 { 553 if (!checkDateTimeFormat(pstrTSFmt)) 554 throw new IllegalArgumentException("The specified timestamp pattern is invalid!"); 555 this.mstrTimeStampFmt = pstrTSFmt; 556 } 557 558/*------------------------------------------------------------------------------ 559 * Public methods 560------------------------------------------------------------------------------*/ 561 public int getTotalSections() 562 { 563 return this.iniSections.size(); 564 } 565 566 /** 567 * Returns a string array containing names of all sections in INI file. 568 * @return the string array of section names 569 */ 570 public String[] getAllSectionNames() 571 { 572 int iCntr = 0; 573 Iterator<String> iter = null; 574 String[] arrRet = null; 575 576 try 577 { 578 if (this.iniSections.size() > 0) 579 { 580 arrRet = new String[this.iniSections.size()]; 581 for (iter = this.iniSections.keySet().iterator();; iter.hasNext()) 582 { 583 arrRet[iCntr] = (String) iter.next(); 584 iCntr++; 585 } 586 } 587 } 588 catch (NoSuchElementException NSEExIgnore) 589 { 590 } 591 finally 592 { 593 if (iter != null) iter = null; 594 } 595 return arrRet; 596 } 597 598 /** 599 * Returns a string array containing names of all the properties under specified section. 600 * @param pstrSection the name of the section for which names of properties is to be retrieved. 601 * @return the string array of property names. 602 */ 603 public String[] getPropertyNames(String pstrSection) 604 { 605 String[] arrRet = null; 606 INISection objSec = null; 607 608 objSec = (INISection) this.iniSections.get(pstrSection); 609 if (objSec != null) 610 { 611 arrRet = objSec.getPropNames(); 612 objSec = null; 613 } 614 return arrRet; 615 } 616 617 /** 618 * Returns a map containing all the properties under specified section. 619 * @param pstrSection the name of the section for which properties are to be retrieved. 620 * @return the map of properties. 621 */ 622 public Map<String, INIProperty> getProperties(String pstrSection) 623 { 624 Map<String, INIProperty> hmRet = null; 625 INISection objSec = null; 626 627 objSec = (INISection) this.iniSections.get(pstrSection); 628 if (objSec != null) 629 { 630 hmRet = objSec.getProperties(); 631 objSec = null; 632 } 633 return hmRet; 634 } 635 636 /** 637 * Removed specified property from the specified section. If the specified 638 * section or the property does not exist, does nothing. 639 * @param pstrSection the section name. 640 * @param pstrProp the name of the property to be removed. 641 */ 642 public void removeProperty(String pstrSection, String pstrProp) 643 { 644 INISection objSec = null; 645 646 objSec = (INISection) this.iniSections.get(pstrSection); 647 if (objSec != null) 648 { 649 objSec.removeProperty(pstrProp); 650 objSec = null; 651 } 652 } 653 654 /** 655 * Removes the specified section if one exists, otherwise does nothing. 656 * @param pstrSection the name of the section to be removed. 657 */ 658 public void removeSection(String pstrSection) 659 { 660 if (this.iniSections.containsKey(pstrSection)) 661 this.iniSections.remove(pstrSection); 662 } 663 664 /** 665 * Flush changes back to the disk file. If the disk file does not exists then 666 * creates the new one. 667 * @ 668 */ 669 public boolean save() 670 { 671 boolean blnRet = false; 672 File objFile = null; 673 String strName = null; 674 String strTemp = null; 675 Iterator<String> itrSec = null; 676 INISection objSec = null; 677 FileWriter objWriter = null; 678 679 try 680 { 681 if (this.iniSections.size() == 0) return false; 682 objFile = ManagedFileAccess.csfile(this.filepath); 683 if (objFile.exists()) objFile.delete(); 684 objWriter = new FileWriter(objFile); 685 itrSec = this.iniSections.keySet().iterator(); 686 while (itrSec.hasNext()) 687 { 688 strName = (String) itrSec.next(); 689 objSec = (INISection) this.iniSections.get(strName); 690 strTemp = objSec.toString(); 691 objWriter.write(strTemp); 692 objWriter.write("\r\n"); 693 objSec = null; 694 } 695 blnRet = true; 696 } 697 catch (IOException IOExIgnore) 698 { 699 } 700 finally 701 { 702 if (objWriter != null) 703 { 704 closeWriter(objWriter); 705 objWriter = null; 706 } 707 if (objFile != null) objFile = null; 708 if (itrSec != null) itrSec = null; 709 } 710 return blnRet; 711 } 712 713 public boolean save(OutputStream stream) 714 { 715 boolean blnRet = false; 716 String strName = null; 717 String strTemp = null; 718 Iterator<String> itrSec = null; 719 INISection objSec = null; 720 OutputStreamWriter objWriter = null; 721 722 try 723 { 724 if (this.iniSections.size() == 0) return false; 725 objWriter = new OutputStreamWriter(stream, "UTF-8"); 726 itrSec = this.iniSections.keySet().iterator(); 727 while (itrSec.hasNext()) 728 { 729 strName = (String) itrSec.next(); 730 objSec = (INISection) this.iniSections.get(strName); 731 strTemp = objSec.toString(); 732 objWriter.write(strTemp); 733 objWriter.write("\r\n"); 734 objSec = null; 735 } 736 blnRet = true; 737 } 738 catch (IOException IOExIgnore) 739 { 740 } 741 finally 742 { 743 if (objWriter != null) 744 { 745 closeWriter(objWriter); 746 objWriter = null; 747 } 748 if (itrSec != null) itrSec = null; 749 } 750 return blnRet; 751 } 752 753 754/*------------------------------------------------------------------------------ 755 * Helper functions 756 *----------------------------------------------------------------------------*/ 757 /** 758 * Procedure to read environment variables. 759 * Thanx to http://www.rgagnon.com/howto.html for this implementation. 760 */ 761 private Properties getEnvVars() 762 { 763 Process p = null; 764 Properties envVars = new Properties(); 765 766 try 767 { 768 Runtime r = Runtime.getRuntime(); 769 String OS = System.getProperty("os.name").toLowerCase(); 770 771 if (OS.indexOf("windows 9") > -1) 772 { 773 p = r.exec("command.com /c set"); 774 } 775 else if ((OS.indexOf("nt") > -1) || 776 (OS.indexOf("windows 2000") > -1) || 777 (OS.indexOf("windows xp") > -1)) 778 { 779 p = r.exec("cmd.exe /c set"); 780 } 781 else 782 { 783 // our last hope, we assume Unix (thanks to H. Ware for the fix) 784 p = r.exec("env"); 785 } 786 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 787 String line; 788 while((line = br.readLine()) != null) 789 { 790 int idx = line.indexOf('='); 791 String key = line.substring(0, idx); 792 String value = line.substring(idx + 1); 793 envVars.setProperty(key, value); 794 } 795 } 796 catch (Exception ExIgnore) 797 { 798 } 799 return envVars; 800 } 801 802 /** 803 * Helper function to check the date time formats. 804 * @param pstrDtFmt the date time format string to be checked. 805 * @return true for valid date/time format, false otherwise. 806 */ 807 private boolean checkDateTimeFormat(String pstrDtFmt) 808 { 809 boolean blnRet = false; 810 DateFormat objFmt = null; 811 812 try 813 { 814 objFmt = new SimpleDateFormat(pstrDtFmt); 815 blnRet = true; 816 } 817 catch (NullPointerException NPExIgnore) 818 { 819 } 820 catch (IllegalArgumentException IAExIgnore) 821 { 822 } 823 finally 824 { 825 if (objFmt != null) objFmt = null; 826 } 827 return blnRet; 828 } 829 830 /** 831 * Reads the INI file and load its contentens into a section collection after 832 * parsing the file line by line. 833 */ 834 private void loadStream(InputStream stream) 835 { 836 int iPos = -1; 837 String strLine = null; 838 String strSection = null; 839 String strRemarks = null; 840 BufferedReader objBRdr = null; 841 InputStreamReader objFRdr = null; 842 INISection objSec = null; 843 844 try 845 { 846 objFRdr = new InputStreamReader(stream); 847 if (objFRdr != null) 848 { 849 objBRdr = new BufferedReader(objFRdr); 850 if (objBRdr != null) 851 { 852 while (objBRdr.ready()) 853 { 854 iPos = -1; 855 strLine = null; 856 strLine = objBRdr.readLine().trim(); 857 if (strLine == null) 858 { 859 } 860 else if (strLine.length() == 0) 861 { 862 } 863 else if (strLine.substring(0, 1).equals(";")) 864 { 865 if (strRemarks == null) 866 strRemarks = strLine.substring(1); 867 else if (strRemarks.length() == 0) 868 strRemarks = strLine.substring(1); 869 else 870 strRemarks = strRemarks + "\r\n" + strLine.substring(1); 871 } 872 else if (strLine.startsWith("[") && strLine.endsWith("]")) 873 { 874 // Section start reached create new section 875 if (objSec != null) 876 this.iniSections.put(strSection.trim(), objSec); 877 objSec = null; 878 strSection = strLine.substring(1, strLine.length() - 1); 879 objSec = new INISection(strSection.trim(), strRemarks); 880 strRemarks = null; 881 } 882 else if ((iPos = strLine.indexOf("=")) > 0 && objSec != null) 883 { 884 // read the key value pair 012345=789 885 objSec.setProperty(strLine.substring(0, iPos).trim(), 886 strLine.substring(iPos + 1).trim(), 887 strRemarks); 888 strRemarks = null; 889 } 890 else 891 { 892 objSec.setProperty(strLine, "", strRemarks); 893 894 } 895 } 896 if (objSec != null) 897 this.iniSections.put(strSection.trim(), objSec); 898 this.fileSuccessfullyLoaded = true; 899 } 900 } 901 } 902 catch (FileNotFoundException FNFExIgnore) 903 { 904 this.iniSections.clear(); 905 } 906 catch (IOException IOExIgnore) 907 { 908 this.iniSections.clear(); 909 } 910 catch (NullPointerException NPExIgnore) 911 { 912 this.iniSections.clear(); 913 } 914 finally 915 { 916 if (objBRdr != null) 917 { 918 closeReader(objBRdr); 919 objBRdr = null; 920 } 921 if (objFRdr != null) 922 { 923 closeReader(objFRdr); 924 objFRdr = null; 925 } 926 if (objSec != null) objSec = null; 927 } 928 } 929 930 /** 931 * Reads the INI file and load its contentens into a section collection after 932 * parsing the file line by line. 933 */ 934 private void loadFile() 935 { 936 int iPos = -1; 937 String strLine = null; 938 String strSection = null; 939 String strRemarks = null; 940 BufferedReader objBRdr = null; 941 FileReader objFRdr = null; 942 INISection objSec = null; 943 944 try 945 { 946 objFRdr = new FileReader(this.filepath); 947 if (objFRdr != null) 948 { 949 objBRdr = new BufferedReader(objFRdr); 950 if (objBRdr != null) 951 { 952 while (objBRdr.ready()) 953 { 954 iPos = -1; 955 strLine = null; 956 strLine = objBRdr.readLine().trim(); 957 if (strLine == null) 958 { 959 } 960 else if (strLine.length() == 0) 961 { 962 } 963 else if (strLine.substring(0, 1).equals(";")) 964 { 965 if (strRemarks == null) 966 strRemarks = strLine.substring(1); 967 else if (strRemarks.length() == 0) 968 strRemarks = strLine.substring(1); 969 else 970 strRemarks = strRemarks + "\r\n" + strLine.substring(1); 971 } 972 else if (strLine.startsWith("[") && strLine.endsWith("]")) 973 { 974 // Section start reached create new section 975 if (objSec != null) 976 this.iniSections.put(strSection.trim(), objSec); 977 objSec = null; 978 strSection = strLine.substring(1, strLine.length() - 1); 979 objSec = new INISection(strSection.trim(), strRemarks); 980 strRemarks = null; 981 } 982 else if ((iPos = strLine.indexOf("=")) > 0 && objSec != null) 983 { 984 // read the key value pair 012345=789 985 objSec.setProperty(strLine.substring(0, iPos).trim(), 986 strLine.substring(iPos + 1).trim(), 987 strRemarks); 988 strRemarks = null; 989 } 990 else 991 { 992 objSec.setProperty(strLine, "", strRemarks); 993 994 } 995 } 996 if (objSec != null) 997 this.iniSections.put(strSection.trim(), objSec); 998 this.fileSuccessfullyLoaded = true; 999 } 1000 } 1001 } 1002 catch (FileNotFoundException FNFExIgnore) 1003 { 1004 this.iniSections.clear(); 1005 } 1006 catch (IOException IOExIgnore) 1007 { 1008 this.iniSections.clear(); 1009 } 1010 catch (NullPointerException NPExIgnore) 1011 { 1012 this.iniSections.clear(); 1013 } 1014 finally 1015 { 1016 if (objBRdr != null) 1017 { 1018 closeReader(objBRdr); 1019 objBRdr = null; 1020 } 1021 if (objFRdr != null) 1022 { 1023 closeReader(objFRdr); 1024 objFRdr = null; 1025 } 1026 if (objSec != null) objSec = null; 1027 } 1028 } 1029 1030 /** 1031 * Helper function to close a reader object. 1032 * @param pobjRdr the reader to be closed. 1033 */ 1034 private void closeReader(Reader pobjRdr) 1035 { 1036 if (pobjRdr == null) return; 1037 try 1038 { 1039 pobjRdr.close(); 1040 } 1041 catch (IOException IOExIgnore) 1042 { 1043 } 1044 } 1045 1046 /** 1047 * Helper function to close a writer object. 1048 * @param pobjWriter the writer to be closed. 1049 */ 1050 private void closeWriter(Writer pobjWriter) 1051 { 1052 if (pobjWriter == null) return; 1053 1054 try 1055 { 1056 pobjWriter.close(); 1057 } 1058 catch (IOException IOExIgnore) 1059 { 1060 } 1061 } 1062 1063 /** 1064 * Helper method to check the existance of a file. 1065 * @param pathName the full path and name of the file to be checked. 1066 * @return true if file exists, false otherwise. 1067 */ 1068 private boolean checkFile(String pathName) 1069 { 1070 boolean blnRet = false; 1071 File objFile = null; 1072 1073 try 1074 { 1075 objFile = ManagedFileAccess.csfile(pathName); 1076 blnRet = (objFile.exists() && objFile.isFile()); 1077 } 1078 catch (Exception e) 1079 { 1080 blnRet = false; 1081 } 1082 finally 1083 { 1084 if (objFile != null) objFile = null; 1085 } 1086 return blnRet; 1087 } 1088 1089 /** 1090 * Converts a java.util.date into String 1091 * @param date Date to be converted to String 1092 * @param dateFormat The date format pattern. 1093 * @return String 1094 */ 1095 private String utilDateToStr(Date date, String dateFormat) 1096 { 1097 String strRet = null; 1098 SimpleDateFormat dtFmt = null; 1099 1100 try 1101 { 1102 dtFmt = new SimpleDateFormat(dateFormat); 1103 strRet = dtFmt.format(date); 1104 } 1105 catch (Exception e) 1106 { 1107 strRet = null; 1108 } 1109 finally 1110 { 1111 if (dtFmt != null) dtFmt = null; 1112 } 1113 return strRet; 1114 } 1115 1116 /** 1117 * Converts the given sql timestamp object to a string representation. The format 1118 * to be used is to be obtained from the configuration file. 1119 * 1120 * @param zonedDateTime the sql timestamp object to be converted. 1121 * @param timeFormat the time format used to store the zonedDateTime value. 1122 * @return the formatted string representation of the timestamp. 1123 */ 1124 private String timeToString(ZonedDateTime zonedDateTime, String timeFormat) 1125 { 1126 String stringValue = null; 1127 DateTimeFormatter dateTimeFormatter = null; 1128 1129 try 1130 { 1131 dateTimeFormatter = DateTimeFormatter.ofPattern(timeFormat); 1132 stringValue = dateTimeFormatter.format(zonedDateTime); 1133 } 1134 catch (IllegalArgumentException iae) 1135 { 1136 stringValue = ""; 1137 } 1138 catch (NullPointerException npe) 1139 { 1140 stringValue = ""; 1141 } 1142 finally 1143 { 1144 if (dateTimeFormatter != null) dateTimeFormatter = null; 1145 } 1146 return stringValue; 1147 } 1148 1149 /** 1150 * This function deletes the remark characters ';' from source string 1151 * @param pstrSrc the source string 1152 * @return the converted string 1153 */ 1154 private String delRemChars(String pstrSrc) 1155 { 1156 int intPos = 0; 1157 1158 if (pstrSrc == null) return null; 1159 while ((intPos = pstrSrc.indexOf(";")) >= 0) 1160 { 1161 if (intPos == 0) 1162 pstrSrc = pstrSrc.substring(intPos + 1); 1163 else if (intPos > 0) 1164 pstrSrc = pstrSrc.substring(0, intPos) + pstrSrc.substring(intPos + 1); 1165 } 1166 return pstrSrc; 1167 } 1168 1169 /** 1170 * This function adds a remark character ';' in source string. 1171 * @param pstrSrc source string 1172 * @return converted string. 1173 */ 1174 private String addRemChars(String pstrSrc) 1175 { 1176 int intLen = 2; 1177 int intPos = 0; 1178 int intPrev = 0; 1179 1180 String strLeft = null; 1181 String strRight = null; 1182 1183 if (pstrSrc == null) return null; 1184 while (intPos >= 0) 1185 { 1186 intLen = 2; 1187 intPos = pstrSrc.indexOf("\r\n", intPrev); 1188 if (intPos < 0) 1189 { 1190 intLen = 1; 1191 intPos = pstrSrc.indexOf("\n", intPrev); 1192 if (intPos < 0) intPos = pstrSrc.indexOf("\r", intPrev); 1193 } 1194 if (intPos == 0) 1195 { 1196 pstrSrc = ";\r\n" + pstrSrc.substring(intPos + intLen); 1197 intPrev = intPos + intLen + 1; 1198 } 1199 else if (intPos > 0) 1200 { 1201 strLeft = pstrSrc.substring(0, intPos); 1202 strRight = pstrSrc.substring(intPos + intLen); 1203 if (strRight == null) 1204 pstrSrc = strLeft; 1205 else if (strRight.length() == 0) 1206 pstrSrc = strLeft; 1207 else 1208 pstrSrc = strLeft + "\r\n;" + strRight; 1209 intPrev = intPos + intLen + 1; 1210 } 1211 } 1212 if (!pstrSrc.substring(0, 1).equals(";")) 1213 pstrSrc = ";" + pstrSrc; 1214 pstrSrc = pstrSrc + "\r\n"; 1215 return pstrSrc; 1216 } 1217/*------------------------------------------------------------------------------ 1218 * Main entry point to test the functionality. 1219 *----------------------------------------------------------------------------*/ 1220 /** 1221 * The main entry point for testing. 1222 * @param pstrArgs the command line arguments array if any. 1223 * @ 1224 */ 1225 public static void main(String[] pstrArgs) 1226 { 1227 IniFile objINI = null; 1228 String strFile = null; 1229 1230 if (pstrArgs.length == 0) return; 1231 1232 strFile = pstrArgs[0]; 1233 // Following call will load the strFile if one exists. 1234 objINI = new IniFile(strFile); 1235 1236// objINI.addSection("QADatabase", "QA database connection details\nUsed for QA Testing"); 1237// objINI.setStringProperty("QADatabase", "SID", "ORCL", null); 1238// objINI.setStringProperty("QADatabase", "UserId", "System", null); 1239// objINI.setStringProperty("QADatabase", "Password", "Manager", null); 1240// objINI.setStringProperty("QADatabase", "HostName", "DBServer", null); 1241// objINI.setIntegerProperty("QADatabase", "Port", 1521, null); 1242// objINI.setStringProperty("QADatabase", "OracleHome", "%ORACLE_HOME%", null); 1243// 1244 // objINI.setSectionComments("Folders", "Directories where generated files are stored"); 1245 objINI.setStringProperty("Folders", "folder1", "G:\\Temp", null); 1246 objINI.setStringProperty("Folders", "folder2", "G:\\Temp\\Backup", null); 1247 1248 // Save changes back to strFile. 1249 objINI.save(); 1250 objINI = null; 1251 } 1252 1253/*------------------------------------------------------------------------------ 1254 * Private class representing the INI Section. 1255 *----------------------------------------------------------------------------*/ 1256 /** 1257 * Class to represent the individual ini file section. 1258 * @author Prasad P. Khandekar 1259 * @version 1.0 1260 * @since 1.0 1261 */ 1262 private class INISection 1263 { 1264 /** Variable to hold any comments associated with this section */ 1265 private String mstrComment; 1266 1267 /** Variable to hold the section name. */ 1268 private String mstrName; 1269 1270 /** Variable to hold the properties falling under this section. */ 1271 private LinkedHashMap<String, INIProperty> mhmapProps; 1272 1273 /** 1274 * Construct a new section object identified by the name specified in 1275 * parameter. 1276 * @param pstrSection The new sections name. 1277 */ 1278 public INISection(String pstrSection) 1279 { 1280 this.mstrName = pstrSection; 1281 this.mhmapProps = new LinkedHashMap<String, INIProperty>(); 1282 } 1283 1284 /** 1285 * Construct a new section object identified by the name specified in 1286 * parameter and associated comments. 1287 * @param pstrSection The new sections name. 1288 * @param pstrComments the comments associated with this section. 1289 */ 1290 public INISection(String pstrSection, String pstrComments) 1291 { 1292 this.mstrName = pstrSection; 1293 this.mstrComment = delRemChars(pstrComments); 1294 this.mhmapProps = new LinkedHashMap<String, INIProperty>(); 1295 } 1296 1297 /** 1298 * Sets the comments associated with this section. 1299 * @param pstrComments the comments 1300 */ 1301 public void setSecComments(String pstrComments) 1302 { 1303 this.mstrComment = delRemChars(pstrComments); 1304 } 1305 1306 /** 1307 * Removes specified property value from this section. 1308 * @param pstrProp The name of the property to be removed. 1309 */ 1310 public void removeProperty(String pstrProp) 1311 { 1312 if (this.mhmapProps.containsKey(pstrProp)) 1313 this.mhmapProps.remove(pstrProp); 1314 } 1315 1316 /** 1317 * Creates or modifies the specified property value. 1318 * @param pstrProp The name of the property to be created or modified. 1319 * @param pstrValue The new value for the property. 1320 * @param pstrComments the associated comments 1321 */ 1322 public void setProperty(String pstrProp, String pstrValue, String pstrComments) 1323 { 1324 this.mhmapProps.put(pstrProp, new INIProperty(pstrProp, pstrValue, pstrComments)); 1325 } 1326 1327 /** 1328 * Returns a map of all properties. 1329 * @return a map of all properties 1330 */ 1331 public Map<String, INIProperty> getProperties() 1332 { 1333 return Collections.unmodifiableMap(this.mhmapProps); 1334 } 1335 1336 /** 1337 * Returns a string array containing names of all the properties under 1338 * this section. 1339 * @return the string array of property names. 1340 */ 1341 public String[] getPropNames() 1342 { 1343 int iCntr = 0; 1344 String[] arrRet = null; 1345 Iterator<String> iter = null; 1346 1347 try 1348 { 1349 if (this.mhmapProps.size() > 0) 1350 { 1351 arrRet = new String[this.mhmapProps.size()]; 1352 for (iter = this.mhmapProps.keySet().iterator();iter.hasNext();) 1353 { 1354 arrRet[iCntr] = (String) iter.next(); 1355 iCntr++; 1356 } 1357 } 1358 } 1359 catch (NoSuchElementException NSEExIgnore) 1360 { 1361 arrRet = null; 1362 } 1363 return arrRet; 1364 } 1365 1366 /** 1367 * Returns underlying value of the specified property. 1368 * @param pstrProp the property whose underlying value is to be etrieved. 1369 * @return the property value. 1370 */ 1371 public INIProperty getProperty(String pstrProp) 1372 { 1373 INIProperty objRet = null; 1374 1375 if (this.mhmapProps.containsKey(pstrProp)) 1376 objRet = (INIProperty) this.mhmapProps.get(pstrProp); 1377 return objRet; 1378 } 1379 1380 /* (non-Javadoc) 1381 * @see java.lang.Object#toString() 1382 */ 1383 @Override 1384 public String toString() 1385 { 1386 Set<String> colKeys = null; 1387 String strRet = ""; 1388 Iterator<String> iter = null; 1389 INIProperty objProp = null; 1390 StringBuffer objBuf = new StringBuffer(); 1391 1392 if (this.mstrComment != null) 1393 objBuf.append(addRemChars(this.mstrComment)); 1394 objBuf.append("[" + this.mstrName + "]\r\n"); 1395 colKeys = this.mhmapProps.keySet(); 1396 if (colKeys != null) 1397 { 1398 iter = colKeys.iterator(); 1399 if (iter != null) 1400 { 1401 while (iter.hasNext()) 1402 { 1403 objProp = (INIProperty) this.mhmapProps.get(iter.next()); 1404 objBuf.append(objProp.toString()); 1405 objBuf.append("\r\n"); 1406 objProp = null; 1407 } 1408 } 1409 } 1410 strRet = objBuf.toString(); 1411 1412 objBuf = null; 1413 iter = null; 1414 colKeys = null; 1415 return strRet; 1416 } 1417 } 1418 1419/*------------------------------------------------------------------------------ 1420 * Private class representing the INI Property. 1421 *----------------------------------------------------------------------------*/ 1422 /** 1423 * This class represents a key value pair called property in an INI file. 1424 * @author Prasad P. Khandekar 1425 * @version 1.0 1426 * @since 1.0 1427 */ 1428 private class INIProperty 1429 { 1430 /** Variable to hold name of this property */ 1431 private String mstrName; 1432 /** Variable to hold value of this property */ 1433 private String mstrValue; 1434 /** Variable to hold comments associated with this property */ 1435 private String mstrComments; 1436 1437 /** 1438 * Constructor 1439 * @param pstrName the name of this property. 1440 * @param pstrValue the value of this property. 1441 * @param pstrComments the comments associated with this property. 1442 */ 1443 public INIProperty(String pstrName, String pstrValue, String pstrComments) 1444 { 1445 this.mstrName = pstrName; 1446 this.mstrValue = pstrValue; 1447 this.mstrComments = delRemChars(pstrComments); 1448 } 1449 1450 /** 1451 * Returns value of this property. If value contains a reference to 1452 * environment avriable then this reference is replaced by actual value 1453 * before the value is returned. 1454 * @return the value of this property. 1455 */ 1456 public String getPropValue() 1457 { 1458 int intStart = 0; 1459 int intEnd = 0; 1460 String strVal = null; 1461 String strVar = null; 1462 String strRet = null; 1463 1464 strRet = this.mstrValue; 1465 intStart = strRet.indexOf("%"); 1466 if (intStart >= 0) 1467 { 1468 intEnd = strRet.indexOf("%", intStart + 1); 1469 strVar = strRet.substring(intStart + 1, intEnd); 1470 strVal = mpropEnv.getProperty(strVar); 1471 if (strVal != null) 1472 { 1473 strRet = strRet.substring(0, intStart) + strVal + 1474 strRet.substring(intEnd + 1); 1475 } 1476 } 1477 return strRet; 1478 } 1479 1480 /* (non-Javadoc) 1481 * @see java.lang.Object#toString() 1482 */ 1483 @Override 1484 public String toString() 1485 { 1486 String strRet = ""; 1487 1488 if (this.mstrComments != null) 1489 strRet = addRemChars(mstrComments); 1490 strRet = strRet + this.mstrName + " = " + this.mstrValue; 1491 return strRet; 1492 } 1493 } 1494 1495 public boolean hasSection(String sectionName) { 1496 if (getAllSectionNames() != null) { 1497 for (String s : getAllSectionNames()) { 1498 if (s.equalsIgnoreCase(sectionName)) { 1499 return true; 1500 } 1501 } 1502 } 1503 return false; 1504 } 1505 1506 public boolean hasProperty(String section, String name) { 1507 return getStringProperty(section, name) != null; 1508 } 1509}