Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
11 | - | 1 | /*------------------------------------------------------------------------------ |
2 | * PACKAGE: com.freeware.inifiles |
||
3 | * FILE : iniFile.java |
||
4 | * CREATED: Jun 30, 2004 |
||
5 | * AUTHOR : Prasad P. Khandekar |
||
6 | *------------------------------------------------------------------------------ |
||
7 | * Change Log: |
||
8 | * 05/07/2004 - Added support for date time formats. |
||
9 | * Added support for environment variables. |
||
10 | * 07/07/2004 - Added support for data type specific getters and setters. |
||
11 | * Updated main method to reflect above changes. |
||
12 | * 26/08/2004 - Added support for section level and property level comments. |
||
13 | * Introduction of seperate class for property values. |
||
14 | * Added addSection method. |
||
15 | * Sections and properties now retail their order (LinkedHashMap) |
||
16 | * Method implementation changes. |
||
17 | *-----------------------------------------------------------------------------*/ |
||
18 | //package com.freeware.inifiles; |
||
19 | |||
20 | package org.geoscope.JSpec; |
||
21 | import java.io.BufferedReader; |
||
22 | import java.io.File; |
||
23 | import java.io.FileNotFoundException; |
||
24 | import java.io.FileReader; |
||
25 | import java.io.FileWriter; |
||
26 | import java.io.IOException; |
||
27 | import java.io.InputStreamReader; |
||
28 | import java.io.Reader; |
||
29 | import java.io.Writer; |
||
30 | import java.sql.Timestamp; |
||
31 | import java.text.DateFormat; |
||
32 | import java.text.ParseException; |
||
33 | import java.text.SimpleDateFormat; |
||
34 | import java.util.Collections; |
||
35 | import java.util.Date; |
||
36 | import java.util.Iterator; |
||
37 | import java.util.LinkedHashMap; |
||
38 | import java.util.Map; |
||
39 | import java.util.NoSuchElementException; |
||
40 | import java.util.Properties; |
||
41 | import java.util.Set; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * INIFile class provides methods for manipulating (Read/Write) windows ini files. |
||
46 | * |
||
47 | * @author Prasad P. Khandekar |
||
48 | * @version 1.0 |
||
49 | * @since 1.0 |
||
50 | */ |
||
51 | public final class INIFile |
||
52 | { |
||
53 | /** Variable to represent the date format */ |
||
54 | private String mstrDateFmt = "yyyy-MM-dd"; |
||
55 | |||
56 | /** Variable to represent the timestamp format */ |
||
57 | private String mstrTimeStampFmt = "yyyy-MM-dd HH:mm:ss"; |
||
58 | |||
59 | /** Variable to denote the successfull load operation. */ |
||
60 | private boolean mblnLoaded = false; |
||
61 | |||
62 | /** Variable to hold the ini file name and full path */ |
||
63 | private String mstrFile; |
||
64 | |||
65 | /** Variable to hold the sections in an ini file. */ |
||
66 | private LinkedHashMap mhmapSections; |
||
67 | |||
68 | /** Variable to hold environment variables **/ |
||
69 | private Properties mpropEnv; |
||
70 | |||
71 | /** |
||
72 | * Create a iniFile object from the file named in the parameter. |
||
73 | * @param pstrPathAndName The full path and name of the ini file to be used. |
||
74 | */ |
||
75 | public INIFile(String pstrPathAndName) |
||
76 | { |
||
77 | this.mpropEnv = getEnvVars(); |
||
78 | this.mhmapSections = new LinkedHashMap(); |
||
79 | this.mstrFile = pstrPathAndName; |
||
80 | // Load the specified INI file. |
||
81 | if (checkFile(pstrPathAndName)) loadFile(); |
||
82 | } |
||
83 | |||
84 | /*------------------------------------------------------------------------------ |
||
85 | * Getters |
||
86 | ------------------------------------------------------------------------------*/ |
||
87 | /** |
||
88 | * Returns the ini file name being used. |
||
89 | * @return the INI file name. |
||
90 | */ |
||
91 | public String getFileName() |
||
92 | { |
||
93 | return this.mstrFile; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns the specified string property from the specified section. |
||
98 | * @param pstrSection the INI section name. |
||
99 | * @param pstrProp the property to be retrieved. |
||
100 | * @return the string property value. |
||
101 | */ |
||
102 | public String getStringProperty(String pstrSection, String pstrProp) |
||
103 | { |
||
104 | String strRet = null; |
||
105 | INIProperty objProp = null; |
||
106 | INISection objSec = null; |
||
107 | |||
108 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
109 | if (objSec != null) |
||
110 | { |
||
111 | objProp = objSec.getProperty(pstrProp); |
||
112 | if (objProp != null) |
||
113 | { |
||
114 | strRet = objProp.getPropValue(); |
||
115 | objProp = null; |
||
116 | } |
||
117 | objSec = null; |
||
118 | } |
||
119 | return strRet; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Returns the specified boolean property from the specified section. |
||
124 | * This method considers the following values as boolean values. |
||
125 | * <ol> |
||
126 | * <li>YES/yes/Yes - boolean true</li> |
||
127 | * <li>NO/no/No - boolean false</li> |
||
128 | * <li>1 - boolean true</li> |
||
129 | * <li>0 - boolean false</li> |
||
130 | * <li>TRUE/True/true - boolean true</li> |
||
131 | * <li>FALSE/False/false - boolean false</li> |
||
132 | * </ol> |
||
133 | * @param pstrSection the INI section name. |
||
134 | * @param pstrProp the property to be retrieved. |
||
135 | * @return the boolean value |
||
136 | */ |
||
137 | public Boolean getBooleanProperty(String pstrSection, String pstrProp) |
||
138 | { |
||
139 | boolean blnRet = false; |
||
140 | String strVal = null; |
||
141 | INIProperty objProp = null; |
||
142 | INISection objSec = null; |
||
143 | |||
144 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
145 | if (objSec != null) |
||
146 | { |
||
147 | objProp = objSec.getProperty(pstrProp); |
||
148 | if (objProp != null) |
||
149 | { |
||
150 | strVal = objProp.getPropValue().toUpperCase(); |
||
151 | if (strVal.equals("YES") || strVal.equals("TRUE") || |
||
152 | strVal.equals("1")) |
||
153 | { |
||
154 | blnRet = true; |
||
155 | } |
||
156 | objProp = null; |
||
157 | } |
||
158 | objSec = null; |
||
159 | } |
||
160 | return new Boolean(blnRet); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Returns the specified integer property from the specified section. |
||
165 | * @param pstrSection the INI section name. |
||
166 | * @param pstrProp the property to be retrieved. |
||
167 | * @return the integer property value. |
||
168 | */ |
||
169 | public Integer getIntegerProperty(String pstrSection, String pstrProp) |
||
170 | { |
||
171 | Integer intRet = null; |
||
172 | String strVal = null; |
||
173 | INIProperty objProp = null; |
||
174 | INISection objSec = null; |
||
175 | |||
176 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
177 | if (objSec != null) |
||
178 | { |
||
179 | objProp = objSec.getProperty(pstrProp); |
||
180 | try |
||
181 | { |
||
182 | if (objProp != null) |
||
183 | { |
||
184 | strVal = objProp.getPropValue(); |
||
185 | if (strVal != null) intRet = new Integer(strVal); |
||
186 | } |
||
187 | } |
||
188 | catch (NumberFormatException NFExIgnore) |
||
189 | { |
||
190 | } |
||
191 | finally |
||
192 | { |
||
193 | if (objProp != null) objProp = null; |
||
194 | } |
||
195 | objSec = null; |
||
196 | } |
||
197 | return intRet; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Returns the specified long property from the specified section. |
||
202 | * @param pstrSection the INI section name. |
||
203 | * @param pstrProp the property to be retrieved. |
||
204 | * @return the long property value. |
||
205 | */ |
||
206 | public Long getLongProperty(String pstrSection, String pstrProp) |
||
207 | { |
||
208 | Long lngRet = null; |
||
209 | String strVal = null; |
||
210 | INIProperty objProp = null; |
||
211 | INISection objSec = null; |
||
212 | |||
213 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
214 | if (objSec != null) |
||
215 | { |
||
216 | objProp = objSec.getProperty(pstrProp); |
||
217 | try |
||
218 | { |
||
219 | if (objProp != null) |
||
220 | { |
||
221 | strVal = objProp.getPropValue(); |
||
222 | if (strVal != null) lngRet = new Long(strVal); |
||
223 | } |
||
224 | } |
||
225 | catch (NumberFormatException NFExIgnore) |
||
226 | { |
||
227 | } |
||
228 | finally |
||
229 | { |
||
230 | if (objProp != null) objProp = null; |
||
231 | } |
||
232 | objSec = null; |
||
233 | } |
||
234 | return lngRet; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Returns the specified double property from the specified section. |
||
239 | * @param pstrSection the INI section name. |
||
240 | * @param pstrProp the property to be retrieved. |
||
241 | * @return the double property value. |
||
242 | */ |
||
243 | public Double getDoubleProperty(String pstrSection, String pstrProp) |
||
244 | { |
||
245 | Double dblRet = null; |
||
246 | String strVal = null; |
||
247 | INIProperty objProp = null; |
||
248 | INISection objSec = null; |
||
249 | |||
250 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
251 | if (objSec != null) |
||
252 | { |
||
253 | objProp = objSec.getProperty(pstrProp); |
||
254 | try |
||
255 | { |
||
256 | if (objProp != null) |
||
257 | { |
||
258 | strVal = objProp.getPropValue(); |
||
259 | if (strVal != null) dblRet = new Double(strVal); |
||
260 | } |
||
261 | } |
||
262 | catch (NumberFormatException NFExIgnore) |
||
263 | { |
||
264 | } |
||
265 | finally |
||
266 | { |
||
267 | if (objProp != null) objProp = null; |
||
268 | } |
||
269 | objSec = null; |
||
270 | } |
||
271 | return dblRet; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Returns the specified date property from the specified section. |
||
276 | * @param pstrSection the INI section name. |
||
277 | * @param pstrProp the property to be retrieved. |
||
278 | * @return the date property value. |
||
279 | */ |
||
280 | public Date getDateProperty(String pstrSection, String pstrProp) |
||
281 | { |
||
282 | Date dtRet = null; |
||
283 | String strVal = null; |
||
284 | DateFormat dtFmt = null; |
||
285 | INIProperty objProp = null; |
||
286 | INISection objSec = null; |
||
287 | |||
288 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
289 | if (objSec != null) |
||
290 | { |
||
291 | objProp = objSec.getProperty(pstrProp); |
||
292 | try |
||
293 | { |
||
294 | if (objProp != null) strVal = objProp.getPropValue(); |
||
295 | if (strVal != null) |
||
296 | { |
||
297 | dtFmt = new SimpleDateFormat(this.mstrDateFmt); |
||
298 | dtRet = dtFmt.parse(strVal); |
||
299 | } |
||
300 | } |
||
301 | catch (ParseException PExIgnore) |
||
302 | { |
||
303 | } |
||
304 | catch (IllegalArgumentException IAEx) |
||
305 | { |
||
306 | } |
||
307 | finally |
||
308 | { |
||
309 | if (objProp != null) objProp = null; |
||
310 | } |
||
311 | objSec = null; |
||
312 | } |
||
313 | return dtRet; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Returns the specified date property from the specified section. |
||
318 | * @param pstrSection the INI section name. |
||
319 | * @param pstrProp the property to be retrieved. |
||
320 | * @return the date property value. |
||
321 | */ |
||
322 | public Date getTimestampProperty(String pstrSection, String pstrProp) |
||
323 | { |
||
324 | Timestamp tsRet = null; |
||
325 | Date dtTmp = null; |
||
326 | String strVal = null; |
||
327 | DateFormat dtFmt = null; |
||
328 | INIProperty objProp = null; |
||
329 | INISection objSec = null; |
||
330 | |||
331 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
332 | if (objSec != null) |
||
333 | { |
||
334 | objProp = objSec.getProperty(pstrProp); |
||
335 | try |
||
336 | { |
||
337 | if (objProp != null) strVal = objProp.getPropValue(); |
||
338 | if (strVal != null) |
||
339 | { |
||
340 | dtFmt = new SimpleDateFormat(this.mstrDateFmt); |
||
341 | dtTmp = dtFmt.parse(strVal); |
||
342 | tsRet = new Timestamp(dtTmp.getTime()); |
||
343 | } |
||
344 | } |
||
345 | catch (ParseException PExIgnore) |
||
346 | { |
||
347 | } |
||
348 | catch (IllegalArgumentException IAEx) |
||
349 | { |
||
350 | } |
||
351 | finally |
||
352 | { |
||
353 | if (objProp != null) objProp = null; |
||
354 | } |
||
355 | objSec = null; |
||
356 | } |
||
357 | return tsRet; |
||
358 | } |
||
359 | |||
360 | /*------------------------------------------------------------------------------ |
||
361 | * Setters |
||
362 | ------------------------------------------------------------------------------*/ |
||
363 | /** |
||
364 | * Sets the comments associated with a section. |
||
365 | * @param pstrSection the section name |
||
366 | * @param pstrComments the comments. |
||
367 | */ |
||
368 | public void addSection(String pstrSection, String pstrComments) |
||
369 | { |
||
370 | INISection objSec = null; |
||
371 | |||
372 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
373 | if (objSec == null) |
||
374 | { |
||
375 | objSec = new INISection(pstrSection); |
||
376 | this.mhmapSections.put(pstrSection, objSec); |
||
377 | } |
||
378 | objSec.setSecComments(delRemChars(pstrComments)); |
||
379 | objSec = null; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Sets the specified string property. |
||
384 | * @param pstrSection the INI section name. |
||
385 | * @param pstrProp the property to be set. |
||
386 | * @pstrVal the string value to be persisted |
||
387 | */ |
||
388 | public void setStringProperty(String pstrSection, String pstrProp, |
||
389 | String pstrVal, String pstrComments) |
||
390 | { |
||
391 | INISection objSec = null; |
||
392 | |||
393 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
394 | if (objSec == null) |
||
395 | { |
||
396 | objSec = new INISection(pstrSection); |
||
397 | this.mhmapSections.put(pstrSection, objSec); |
||
398 | } |
||
399 | objSec.setProperty(pstrProp, pstrVal, pstrComments); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Sets the specified boolean property. |
||
404 | * @param pstrSection the INI section name. |
||
405 | * @param pstrProp the property to be set. |
||
406 | * @param pblnVal the boolean value to be persisted |
||
407 | */ |
||
408 | public void setBooleanProperty(String pstrSection, String pstrProp, |
||
409 | boolean pblnVal, String pstrComments) |
||
410 | { |
||
411 | INISection objSec = null; |
||
412 | |||
413 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
414 | if (objSec == null) |
||
415 | { |
||
416 | objSec = new INISection(pstrSection); |
||
417 | this.mhmapSections.put(pstrSection, objSec); |
||
418 | } |
||
419 | if (pblnVal) |
||
420 | objSec.setProperty(pstrProp, "TRUE", pstrComments); |
||
421 | else |
||
422 | objSec.setProperty(pstrProp, "FALSE", pstrComments); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Sets the specified integer property. |
||
427 | * @param pstrSection the INI section name. |
||
428 | * @param pstrProp the property to be set. |
||
429 | * @param pintVal the int property to be persisted. |
||
430 | */ |
||
431 | public void setIntegerProperty(String pstrSection, String pstrProp, |
||
432 | int pintVal, String pstrComments) |
||
433 | { |
||
434 | INISection objSec = null; |
||
435 | |||
436 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
437 | if (objSec == null) |
||
438 | { |
||
439 | objSec = new INISection(pstrSection); |
||
440 | this.mhmapSections.put(pstrSection, objSec); |
||
441 | } |
||
442 | objSec.setProperty(pstrProp, Integer.toString(pintVal), pstrComments); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Sets the specified long property. |
||
447 | * @param pstrSection the INI section name. |
||
448 | * @param pstrProp the property to be set. |
||
449 | * @param plngVal the long value to be persisted. |
||
450 | */ |
||
451 | public void setLongProperty(String pstrSection, String pstrProp, |
||
452 | long plngVal, String pstrComments) |
||
453 | { |
||
454 | INISection objSec = null; |
||
455 | |||
456 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
457 | if (objSec == null) |
||
458 | { |
||
459 | objSec = new INISection(pstrSection); |
||
460 | this.mhmapSections.put(pstrSection, objSec); |
||
461 | } |
||
462 | objSec.setProperty(pstrProp, Long.toString(plngVal), pstrComments); |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Sets the specified double property. |
||
467 | * @param pstrSection the INI section name. |
||
468 | * @param pstrProp the property to be set. |
||
469 | * @param pdblVal the double value to be persisted. |
||
470 | */ |
||
471 | public void setDoubleProperty(String pstrSection, String pstrProp, |
||
472 | double pdblVal, String pstrComments) |
||
473 | { |
||
474 | INISection objSec = null; |
||
475 | |||
476 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
477 | if (objSec == null) |
||
478 | { |
||
479 | objSec = new INISection(pstrSection); |
||
480 | this.mhmapSections.put(pstrSection, objSec); |
||
481 | } |
||
482 | objSec.setProperty(pstrProp, Double.toString(pdblVal), pstrComments); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Sets the specified java.util.Date property. |
||
487 | * @param pstrSection the INI section name. |
||
488 | * @param pstrProp the property to be set. |
||
489 | * @param pdtVal the date value to be persisted. |
||
490 | */ |
||
491 | public void setDateProperty(String pstrSection, String pstrProp, |
||
492 | Date pdtVal, String pstrComments) |
||
493 | { |
||
494 | INISection objSec = null; |
||
495 | |||
496 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
497 | if (objSec == null) |
||
498 | { |
||
499 | objSec = new INISection(pstrSection); |
||
500 | this.mhmapSections.put(pstrSection, objSec); |
||
501 | } |
||
502 | objSec.setProperty(pstrProp, utilDateToStr(pdtVal, this.mstrDateFmt), |
||
503 | pstrComments); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Sets the specified java.sql.Timestamp property. |
||
508 | * @param pstrSection the INI section name. |
||
509 | * @param pstrProp the property to be set. |
||
510 | * @param ptsVal the timestamp value to be persisted. |
||
511 | */ |
||
512 | public void setTimestampProperty(String pstrSection, String pstrProp, |
||
513 | Timestamp ptsVal, String pstrComments) |
||
514 | { |
||
515 | INISection objSec = null; |
||
516 | |||
517 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
518 | if (objSec == null) |
||
519 | { |
||
520 | objSec = new INISection(pstrSection); |
||
521 | this.mhmapSections.put(pstrSection, objSec); |
||
522 | } |
||
523 | objSec.setProperty(pstrProp, timeToStr(ptsVal, this.mstrTimeStampFmt), |
||
524 | pstrComments); |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Sets the format to be used to interpreat date values. |
||
529 | * @param pstrDtFmt the format string |
||
530 | * @throws IllegalArgumentException if the if the given pattern is invalid |
||
531 | */ |
||
532 | public void setDateFormat(String pstrDtFmt) throws IllegalArgumentException |
||
533 | { |
||
534 | if (!checkDateTimeFormat(pstrDtFmt)) |
||
535 | throw new IllegalArgumentException("The specified date pattern is invalid!"); |
||
536 | this.mstrDateFmt = pstrDtFmt; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Sets the format to be used to interpreat timestamp values. |
||
541 | * @param pstrTSFmt the format string |
||
542 | * @throws IllegalArgumentException if the if the given pattern is invalid |
||
543 | */ |
||
544 | public void setTimeStampFormat(String pstrTSFmt) |
||
545 | { |
||
546 | if (!checkDateTimeFormat(pstrTSFmt)) |
||
547 | throw new IllegalArgumentException("The specified timestamp pattern is invalid!"); |
||
548 | this.mstrTimeStampFmt = pstrTSFmt; |
||
549 | } |
||
550 | |||
551 | /*------------------------------------------------------------------------------ |
||
552 | * Public methods |
||
553 | ------------------------------------------------------------------------------*/ |
||
554 | public int getTotalSections() |
||
555 | { |
||
556 | return this.mhmapSections.size(); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Returns a string array containing names of all sections in INI file. |
||
561 | * @return the string array of section names |
||
562 | */ |
||
563 | public String[] getAllSectionNames() |
||
564 | { |
||
565 | int iCntr = 0; |
||
566 | Iterator iter = null; |
||
567 | String[] arrRet = null; |
||
568 | |||
569 | try |
||
570 | { |
||
571 | if (this.mhmapSections.size() > 0) |
||
572 | { |
||
573 | arrRet = new String[this.mhmapSections.size()]; |
||
574 | for (iter = this.mhmapSections.keySet().iterator();;iter.hasNext()) |
||
575 | { |
||
576 | arrRet[iCntr] = (String) iter.next(); |
||
577 | iCntr++; |
||
578 | } |
||
579 | } |
||
580 | } |
||
581 | catch (NoSuchElementException NSEExIgnore) |
||
582 | { |
||
583 | } |
||
584 | finally |
||
585 | { |
||
586 | if (iter != null) iter = null; |
||
587 | } |
||
588 | return arrRet; |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Returns a string array containing names of all the properties under specified section. |
||
593 | * @param pstrSection the name of the section for which names of properties is to be retrieved. |
||
594 | * @return the string array of property names. |
||
595 | */ |
||
596 | public String[] getPropertyNames(String pstrSection) |
||
597 | { |
||
598 | String[] arrRet = null; |
||
599 | INISection objSec = null; |
||
600 | |||
601 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
602 | if (objSec != null) |
||
603 | { |
||
604 | arrRet = objSec.getPropNames(); |
||
605 | objSec = null; |
||
606 | } |
||
607 | return arrRet; |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * Returns a map containing all the properties under specified section. |
||
612 | * @param pstrSection the name of the section for which properties are to be retrieved. |
||
613 | * @return the map of properties. |
||
614 | */ |
||
615 | public Map getProperties(String pstrSection) |
||
616 | { |
||
617 | Map hmRet = null; |
||
618 | INISection objSec = null; |
||
619 | |||
620 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
621 | if (objSec != null) |
||
622 | { |
||
623 | hmRet = objSec.getProperties(); |
||
624 | objSec = null; |
||
625 | } |
||
626 | return hmRet; |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * Removed specified property from the specified section. If the specified |
||
631 | * section or the property does not exist, does nothing. |
||
632 | * @param pstrSection the section name. |
||
633 | * @param pstrProp the name of the property to be removed. |
||
634 | */ |
||
635 | public void removeProperty(String pstrSection, String pstrProp) |
||
636 | { |
||
637 | INISection objSec = null; |
||
638 | |||
639 | objSec = (INISection) this.mhmapSections.get(pstrSection); |
||
640 | if (objSec != null) |
||
641 | { |
||
642 | objSec.removeProperty(pstrProp); |
||
643 | objSec = null; |
||
644 | } |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Removes the specified section if one exists, otherwise does nothing. |
||
649 | * @param pstrSection the name of the section to be removed. |
||
650 | */ |
||
651 | public void removeSection(String pstrSection) |
||
652 | { |
||
653 | if (this.mhmapSections.containsKey(pstrSection)) |
||
654 | this.mhmapSections.remove(pstrSection); |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * Flush changes back to the disk file. If the disk file does not exists then |
||
659 | * creates the new one. |
||
660 | */ |
||
661 | public boolean save() |
||
662 | { |
||
663 | boolean blnRet = false; |
||
664 | File objFile = null; |
||
665 | String strName = null; |
||
666 | String strTemp = null; |
||
667 | Iterator itrSec = null; |
||
668 | INISection objSec = null; |
||
669 | FileWriter objWriter = null; |
||
670 | |||
671 | try |
||
672 | { |
||
673 | if (this.mhmapSections.size() == 0) return false; |
||
674 | objFile = new File(this.mstrFile); |
||
675 | if (objFile.exists()) objFile.delete(); |
||
676 | objWriter = new FileWriter(objFile); |
||
677 | itrSec = this.mhmapSections.keySet().iterator(); |
||
678 | while (itrSec.hasNext()) |
||
679 | { |
||
680 | strName = (String) itrSec.next(); |
||
681 | objSec = (INISection) this.mhmapSections.get(strName); |
||
682 | strTemp = objSec.toString(); |
||
683 | objWriter.write(strTemp); |
||
684 | objWriter.write("\r\n"); |
||
685 | objSec = null; |
||
686 | } |
||
687 | blnRet = true; |
||
688 | } |
||
689 | catch (IOException IOExIgnore) |
||
690 | { |
||
691 | } |
||
692 | finally |
||
693 | { |
||
694 | if (objWriter != null) |
||
695 | { |
||
696 | closeWriter(objWriter); |
||
697 | objWriter = null; |
||
698 | } |
||
699 | if (objFile != null) objFile = null; |
||
700 | if (itrSec != null) itrSec = null; |
||
701 | } |
||
702 | return blnRet; |
||
703 | } |
||
704 | |||
705 | /*------------------------------------------------------------------------------ |
||
706 | * Helper functions |
||
707 | *----------------------------------------------------------------------------*/ |
||
708 | /** |
||
709 | * Procedure to read environment variables. |
||
710 | * Thanx to http://www.rgagnon.com/howto.html for this implementation. |
||
711 | */ |
||
712 | private Properties getEnvVars() |
||
713 | { |
||
714 | Process p = null; |
||
715 | Properties envVars = new Properties(); |
||
716 | |||
717 | try |
||
718 | { |
||
719 | Runtime r = Runtime.getRuntime(); |
||
720 | String OS = System.getProperty("os.name").toLowerCase(); |
||
721 | |||
722 | if (OS.indexOf("windows 9") > -1) |
||
723 | { |
||
724 | p = r.exec("command.com /c set"); |
||
725 | } |
||
726 | else if ((OS.indexOf("nt") > -1) || |
||
727 | (OS.indexOf("windows 2000") > -1) || |
||
728 | (OS.indexOf("windows xp") > -1)) |
||
729 | { |
||
730 | p = r.exec("cmd.exe /c set"); |
||
731 | } |
||
732 | else |
||
733 | { |
||
734 | // our last hope, we assume Unix (thanks to H. Ware for the fix) |
||
735 | p = r.exec("env"); |
||
736 | } |
||
737 | BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); |
||
738 | String line; |
||
739 | while((line = br.readLine()) != null) |
||
740 | { |
||
741 | int idx = line.indexOf('='); |
||
742 | String key = line.substring(0, idx); |
||
743 | String value = line.substring(idx + 1); |
||
744 | envVars.setProperty(key, value); |
||
745 | } |
||
746 | } |
||
747 | catch (Exception ExIgnore) |
||
748 | { |
||
749 | } |
||
750 | return envVars; |
||
751 | } |
||
752 | |||
753 | /** |
||
754 | * Helper function to check the date time formats. |
||
755 | * @param pstrDtFmt the date time format string to be checked. |
||
756 | * @return true for valid date/time format, false otherwise. |
||
757 | */ |
||
758 | private boolean checkDateTimeFormat(String pstrDtFmt) |
||
759 | { |
||
760 | boolean blnRet = false; |
||
761 | DateFormat objFmt = null; |
||
762 | |||
763 | try |
||
764 | { |
||
765 | objFmt = new SimpleDateFormat(pstrDtFmt); |
||
766 | blnRet = true; |
||
767 | } |
||
768 | catch (NullPointerException NPExIgnore) |
||
769 | { |
||
770 | } |
||
771 | catch (IllegalArgumentException IAExIgnore) |
||
772 | { |
||
773 | } |
||
774 | finally |
||
775 | { |
||
776 | if (objFmt != null) objFmt = null; |
||
777 | } |
||
778 | return blnRet; |
||
779 | } |
||
780 | |||
781 | /** |
||
782 | * Reads the INI file and load its contentens into a section collection after |
||
783 | * parsing the file line by line. |
||
784 | */ |
||
785 | private void loadFile() |
||
786 | { |
||
787 | int iPos = -1; |
||
788 | String strLine = null; |
||
789 | String strSection = null; |
||
790 | String strRemarks = null; |
||
791 | BufferedReader objBRdr = null; |
||
792 | FileReader objFRdr = null; |
||
793 | INISection objSec = null; |
||
794 | |||
795 | try |
||
796 | { |
||
797 | objFRdr = new FileReader(this.mstrFile); |
||
798 | if (objFRdr != null) |
||
799 | { |
||
800 | objBRdr = new BufferedReader(objFRdr); |
||
801 | if (objBRdr != null) |
||
802 | { |
||
803 | while (objBRdr.ready()) |
||
804 | { |
||
805 | iPos = -1; |
||
806 | strLine = null; |
||
807 | strLine = objBRdr.readLine().trim(); |
||
808 | if (strLine == null) |
||
809 | { |
||
810 | } |
||
811 | else if (strLine.length() == 0) |
||
812 | { |
||
813 | } |
||
814 | else if (strLine.substring(0, 1).equals(";")) |
||
815 | { |
||
816 | if (strRemarks == null) |
||
817 | strRemarks = strLine.substring(1); |
||
818 | else if (strRemarks.length() == 0) |
||
819 | strRemarks = strLine.substring(1); |
||
820 | else |
||
821 | strRemarks = strRemarks + "\r\n" + strLine.substring(1); |
||
822 | } |
||
823 | else if (strLine.startsWith("[") && strLine.endsWith("]")) |
||
824 | { |
||
825 | // Section start reached create new section |
||
826 | if (objSec != null) |
||
827 | this.mhmapSections.put(strSection.trim(), objSec); |
||
828 | objSec = null; |
||
829 | strSection = strLine.substring(1, strLine.length() - 1); |
||
830 | objSec = new INISection(strSection.trim(), strRemarks); |
||
831 | strRemarks = null; |
||
832 | } |
||
833 | else if ((iPos = strLine.indexOf("=")) > 0 && objSec != null) |
||
834 | { |
||
835 | // read the key value pair 012345=789 |
||
836 | objSec.setProperty(strLine.substring(0, iPos).trim(), |
||
837 | strLine.substring(iPos + 1).trim(), |
||
838 | strRemarks); |
||
839 | strRemarks = null; |
||
840 | } |
||
841 | } |
||
842 | if (objSec != null) |
||
843 | this.mhmapSections.put(strSection.trim(), objSec); |
||
844 | this.mblnLoaded = true; |
||
845 | } |
||
846 | } |
||
847 | } |
||
848 | catch (FileNotFoundException FNFExIgnore) |
||
849 | { |
||
850 | this.mhmapSections.clear(); |
||
851 | } |
||
852 | catch (IOException IOExIgnore) |
||
853 | { |
||
854 | this.mhmapSections.clear(); |
||
855 | } |
||
856 | catch (NullPointerException NPExIgnore) |
||
857 | { |
||
858 | this.mhmapSections.clear(); |
||
859 | } |
||
860 | finally |
||
861 | { |
||
862 | if (objBRdr != null) |
||
863 | { |
||
864 | closeReader(objBRdr); |
||
865 | objBRdr = null; |
||
866 | } |
||
867 | if (objFRdr != null) |
||
868 | { |
||
869 | closeReader(objFRdr); |
||
870 | objFRdr = null; |
||
871 | } |
||
872 | if (objSec != null) objSec = null; |
||
873 | } |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * Helper function to close a reader object. |
||
878 | * @param pobjRdr the reader to be closed. |
||
879 | */ |
||
880 | private void closeReader(Reader pobjRdr) |
||
881 | { |
||
882 | if (pobjRdr == null) return; |
||
883 | try |
||
884 | { |
||
885 | pobjRdr.close(); |
||
886 | } |
||
887 | catch (IOException IOExIgnore) |
||
888 | { |
||
889 | } |
||
890 | } |
||
891 | |||
892 | /** |
||
893 | * Helper function to close a writer object. |
||
894 | * @param pobjWriter the writer to be closed. |
||
895 | */ |
||
896 | private void closeWriter(Writer pobjWriter) |
||
897 | { |
||
898 | if (pobjWriter == null) return; |
||
899 | |||
900 | try |
||
901 | { |
||
902 | pobjWriter.close(); |
||
903 | } |
||
904 | catch (IOException IOExIgnore) |
||
905 | { |
||
906 | } |
||
907 | } |
||
908 | |||
909 | /** |
||
910 | * Helper method to check the existance of a file. |
||
911 | * @param the full path and name of the file to be checked. |
||
912 | * @return true if file exists, false otherwise. |
||
913 | */ |
||
914 | private boolean checkFile(String pstrFile) |
||
915 | { |
||
916 | boolean blnRet = false; |
||
917 | File objFile = null; |
||
918 | |||
919 | try |
||
920 | { |
||
921 | objFile = new File(pstrFile); |
||
922 | blnRet = (objFile.exists() && objFile.isFile()); |
||
923 | } |
||
924 | catch (Exception e) |
||
925 | { |
||
926 | blnRet = false; |
||
927 | } |
||
928 | finally |
||
929 | { |
||
930 | if (objFile != null) objFile = null; |
||
931 | } |
||
932 | return blnRet; |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * Converts a java.util.date into String |
||
937 | * @param pd Date that need to be converted to String |
||
938 | * @param pstrFmt The date format pattern. |
||
939 | * @return String |
||
940 | */ |
||
941 | private String utilDateToStr(Date pdt, String pstrFmt) |
||
942 | { |
||
943 | String strRet = null; |
||
944 | SimpleDateFormat dtFmt = null; |
||
945 | |||
946 | try |
||
947 | { |
||
948 | dtFmt = new SimpleDateFormat(pstrFmt); |
||
949 | strRet = dtFmt.format(pdt); |
||
950 | } |
||
951 | catch (Exception e) |
||
952 | { |
||
953 | strRet = null; |
||
954 | } |
||
955 | finally |
||
956 | { |
||
957 | if (dtFmt != null) dtFmt = null; |
||
958 | } |
||
959 | return strRet; |
||
960 | } |
||
961 | |||
962 | /** |
||
963 | * Converts the given sql timestamp object to a string representation. The format |
||
964 | * to be used is to be obtained from the configuration file. |
||
965 | * |
||
966 | * @param pobjTS the sql timestamp object to be converted. |
||
967 | * @param pblnGMT If true formats the string using GMT timezone |
||
968 | * otherwise using local timezone. |
||
969 | * @return the formatted string representation of the timestamp. |
||
970 | */ |
||
971 | private String timeToStr(Timestamp pobjTS, String pstrFmt) |
||
972 | { |
||
973 | String strRet = null; |
||
974 | SimpleDateFormat dtFmt = null; |
||
975 | |||
976 | try |
||
977 | { |
||
978 | dtFmt = new SimpleDateFormat(pstrFmt); |
||
979 | strRet = dtFmt.format(pobjTS); |
||
980 | } |
||
981 | catch (IllegalArgumentException iae) |
||
982 | { |
||
983 | strRet = ""; |
||
984 | } |
||
985 | catch (NullPointerException npe) |
||
986 | { |
||
987 | strRet = ""; |
||
988 | } |
||
989 | finally |
||
990 | { |
||
991 | if (dtFmt != null) dtFmt = null; |
||
992 | } |
||
993 | return strRet; |
||
994 | } |
||
995 | |||
996 | /** |
||
997 | * This function deletes the remark characters ';' from source string |
||
998 | * @param pstrSrc the source string |
||
999 | * @return the converted string |
||
1000 | */ |
||
1001 | private String delRemChars(String pstrSrc) |
||
1002 | { |
||
1003 | int intPos = 0; |
||
1004 | |||
1005 | if (pstrSrc == null) return null; |
||
1006 | while ((intPos = pstrSrc.indexOf(";")) >= 0) |
||
1007 | { |
||
1008 | if (intPos == 0) |
||
1009 | pstrSrc = pstrSrc.substring(intPos + 1); |
||
1010 | else if (intPos > 0) |
||
1011 | pstrSrc = pstrSrc.substring(0, intPos) + pstrSrc.substring(intPos + 1); |
||
1012 | } |
||
1013 | return pstrSrc; |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * This function adds a remark character ';' in source string. |
||
1018 | * @param pstrSrc source string |
||
1019 | * @return converted string. |
||
1020 | */ |
||
1021 | private String addRemChars(String pstrSrc) |
||
1022 | { |
||
1023 | int intLen = 2; |
||
1024 | int intPos = 0; |
||
1025 | int intPrev = 0; |
||
1026 | |||
1027 | String strLeft = null; |
||
1028 | String strRight = null; |
||
1029 | |||
1030 | if (pstrSrc == null) return null; |
||
1031 | while (intPos >= 0) |
||
1032 | { |
||
1033 | intLen = 2; |
||
1034 | intPos = pstrSrc.indexOf("\r\n", intPrev); |
||
1035 | if (intPos < 0) |
||
1036 | { |
||
1037 | intLen = 1; |
||
1038 | intPos = pstrSrc.indexOf("\n", intPrev); |
||
1039 | if (intPos < 0) intPos = pstrSrc.indexOf("\r", intPrev); |
||
1040 | } |
||
1041 | if (intPos == 0) |
||
1042 | { |
||
1043 | pstrSrc = ";\r\n" + pstrSrc.substring(intPos + intLen); |
||
1044 | intPrev = intPos + intLen + 1; |
||
1045 | } |
||
1046 | else if (intPos > 0) |
||
1047 | { |
||
1048 | strLeft = pstrSrc.substring(0, intPos); |
||
1049 | strRight = pstrSrc.substring(intPos + intLen); |
||
1050 | if (strRight == null) |
||
1051 | pstrSrc = strLeft; |
||
1052 | else if (strRight.length() == 0) |
||
1053 | pstrSrc = strLeft; |
||
1054 | else |
||
1055 | pstrSrc = strLeft + "\r\n;" + strRight; |
||
1056 | intPrev = intPos + intLen + 1; |
||
1057 | } |
||
1058 | } |
||
1059 | if (!pstrSrc.substring(0, 1).equals(";")) |
||
1060 | pstrSrc = ";" + pstrSrc; |
||
1061 | pstrSrc = pstrSrc + "\r\n"; |
||
1062 | return pstrSrc; |
||
1063 | } |
||
1064 | /*------------------------------------------------------------------------------ |
||
1065 | * Main entry point to test the functionality. |
||
1066 | *----------------------------------------------------------------------------*/ |
||
1067 | /** |
||
1068 | * The main entry point for testing. |
||
1069 | * @param pstrArgs the command line arguments array if any. |
||
1070 | */ |
||
1071 | public static void main(String[] pstrArgs) |
||
1072 | { |
||
1073 | INIFile objINI = null; |
||
1074 | String strFile = null; |
||
1075 | |||
1076 | if (pstrArgs.length == 0) return; |
||
1077 | |||
1078 | strFile = pstrArgs[0]; |
||
1079 | // Following call will load the strFile if one exists. |
||
1080 | objINI = new INIFile(strFile); |
||
1081 | |||
1082 | // objINI.addSection("QADatabase", "QA database connection details\nUsed for QA Testing"); |
||
1083 | // objINI.setStringProperty("QADatabase", "SID", "ORCL", null); |
||
1084 | // objINI.setStringProperty("QADatabase", "UserId", "System", null); |
||
1085 | // objINI.setStringProperty("QADatabase", "Password", "Manager", null); |
||
1086 | // objINI.setStringProperty("QADatabase", "HostName", "DBServer", null); |
||
1087 | // objINI.setIntegerProperty("QADatabase", "Port", 1521, null); |
||
1088 | // objINI.setStringProperty("QADatabase", "OracleHome", "%ORACLE_HOME%", null); |
||
1089 | // |
||
1090 | // objINI.setSectionComments("Folders", "Directories where generated files are stored"); |
||
1091 | objINI.setStringProperty("Folders", "folder1", "G:\\Temp", null); |
||
1092 | objINI.setStringProperty("Folders", "folder2", "G:\\Temp\\Backup", null); |
||
1093 | |||
1094 | // Save changes back to strFile. |
||
1095 | objINI.save(); |
||
1096 | objINI = null; |
||
1097 | } |
||
1098 | |||
1099 | /*------------------------------------------------------------------------------ |
||
1100 | * Private class representing the INI Section. |
||
1101 | *----------------------------------------------------------------------------*/ |
||
1102 | /** |
||
1103 | * Class to represent the individual ini file section. |
||
1104 | * @author Prasad P. Khandekar |
||
1105 | * @version 1.0 |
||
1106 | * @since 1.0 |
||
1107 | */ |
||
1108 | private class INISection |
||
1109 | { |
||
1110 | /** Variable to hold any comments associated with this section */ |
||
1111 | private String mstrComment; |
||
1112 | |||
1113 | /** Variable to hold the section name. */ |
||
1114 | private String mstrName; |
||
1115 | |||
1116 | /** Variable to hold the properties falling under this section. */ |
||
1117 | private LinkedHashMap mhmapProps; |
||
1118 | |||
1119 | /** |
||
1120 | * Construct a new section object identified by the name specified in |
||
1121 | * parameter. |
||
1122 | * @param pstrSection The new sections name. |
||
1123 | */ |
||
1124 | public INISection(String pstrSection) |
||
1125 | { |
||
1126 | this.mstrName = pstrSection; |
||
1127 | this.mhmapProps = new LinkedHashMap(); |
||
1128 | } |
||
1129 | |||
1130 | /** |
||
1131 | * Construct a new section object identified by the name specified in |
||
1132 | * parameter and associated comments. |
||
1133 | * @param pstrSection The new sections name. |
||
1134 | * @param pstrComments the comments associated with this section. |
||
1135 | */ |
||
1136 | public INISection(String pstrSection, String pstrComments) |
||
1137 | { |
||
1138 | this.mstrName = pstrSection; |
||
1139 | this.mstrComment = delRemChars(pstrComments); |
||
1140 | this.mhmapProps = new LinkedHashMap(); |
||
1141 | } |
||
1142 | |||
1143 | /** |
||
1144 | * Returns any comments associated with this section |
||
1145 | * @return the comments |
||
1146 | */ |
||
1147 | public String getSecComments() |
||
1148 | { |
||
1149 | return this.mstrComment; |
||
1150 | } |
||
1151 | |||
1152 | /** |
||
1153 | * Returns name of the section. |
||
1154 | * @return Name of the section. |
||
1155 | */ |
||
1156 | public String getSecName() |
||
1157 | { |
||
1158 | return this.mstrName; |
||
1159 | } |
||
1160 | |||
1161 | /** |
||
1162 | * Sets the comments associated with this section. |
||
1163 | * @param pstrComments the comments |
||
1164 | */ |
||
1165 | public void setSecComments(String pstrComments) |
||
1166 | { |
||
1167 | this.mstrComment = delRemChars(pstrComments); |
||
1168 | } |
||
1169 | |||
1170 | /** |
||
1171 | * Sets the section name. |
||
1172 | * @param pstrName the section name. |
||
1173 | */ |
||
1174 | public void setSecName(String pstrName) |
||
1175 | { |
||
1176 | this.mstrName = pstrName; |
||
1177 | } |
||
1178 | |||
1179 | /** |
||
1180 | * Removes specified property value from this section. |
||
1181 | * @param pstrProp The name of the property to be removed. |
||
1182 | */ |
||
1183 | public void removeProperty(String pstrProp) |
||
1184 | { |
||
1185 | if (this.mhmapProps.containsKey(pstrProp)) |
||
1186 | this.mhmapProps.remove(pstrProp); |
||
1187 | } |
||
1188 | |||
1189 | /** |
||
1190 | * Creates or modifies the specified property value. |
||
1191 | * @param pstrProp The name of the property to be created or modified. |
||
1192 | * @param pstrValue The new value for the property. |
||
1193 | * @param pstrComments the associated comments |
||
1194 | */ |
||
1195 | public void setProperty(String pstrProp, String pstrValue, String pstrComments) |
||
1196 | { |
||
1197 | this.mhmapProps.put(pstrProp, new INIProperty(pstrProp, pstrValue, pstrComments)); |
||
1198 | } |
||
1199 | |||
1200 | /** |
||
1201 | * Returns a map of all properties. |
||
1202 | * @return a map of all properties |
||
1203 | */ |
||
1204 | public Map getProperties() |
||
1205 | { |
||
1206 | return Collections.unmodifiableMap(this.mhmapProps); |
||
1207 | } |
||
1208 | |||
1209 | /** |
||
1210 | * Returns a string array containing names of all the properties under |
||
1211 | * this section. |
||
1212 | * @return the string array of property names. |
||
1213 | */ |
||
1214 | public String[] getPropNames() |
||
1215 | { |
||
1216 | int iCntr = 0; |
||
1217 | String[] arrRet = null; |
||
1218 | Iterator iter = null; |
||
1219 | |||
1220 | try |
||
1221 | { |
||
1222 | if (this.mhmapProps.size() > 0) |
||
1223 | { |
||
1224 | arrRet = new String[this.mhmapProps.size()]; |
||
1225 | for (iter = this.mhmapProps.keySet().iterator();iter.hasNext();) |
||
1226 | { |
||
1227 | arrRet[iCntr] = (String) iter.next(); |
||
1228 | iCntr++; |
||
1229 | } |
||
1230 | } |
||
1231 | } |
||
1232 | catch (NoSuchElementException NSEExIgnore) |
||
1233 | { |
||
1234 | arrRet = null; |
||
1235 | } |
||
1236 | return arrRet; |
||
1237 | } |
||
1238 | |||
1239 | /** |
||
1240 | * Returns underlying value of the specified property. |
||
1241 | * @param pstrProp the property whose underlying value is to be etrieved. |
||
1242 | * @return the property value. |
||
1243 | */ |
||
1244 | public INIProperty getProperty(String pstrProp) |
||
1245 | { |
||
1246 | INIProperty objRet = null; |
||
1247 | |||
1248 | if (this.mhmapProps.containsKey(pstrProp)) |
||
1249 | objRet = (INIProperty) this.mhmapProps.get(pstrProp); |
||
1250 | return objRet; |
||
1251 | } |
||
1252 | |||
1253 | /* (non-Javadoc) |
||
1254 | * @see java.lang.Object#toString() |
||
1255 | */ |
||
1256 | public String toString() |
||
1257 | { |
||
1258 | Set colKeys = null; |
||
1259 | String strRet = ""; |
||
1260 | Iterator iter = null; |
||
1261 | INIProperty objProp = null; |
||
1262 | StringBuffer objBuf = new StringBuffer(); |
||
1263 | |||
1264 | if (this.mstrComment != null) |
||
1265 | objBuf.append(addRemChars(this.mstrComment)); |
||
1266 | objBuf.append("[" + this.mstrName + "]\r\n"); |
||
1267 | colKeys = this.mhmapProps.keySet(); |
||
1268 | if (colKeys != null) |
||
1269 | { |
||
1270 | iter = colKeys.iterator(); |
||
1271 | if (iter != null) |
||
1272 | { |
||
1273 | while (iter.hasNext()) |
||
1274 | { |
||
1275 | objProp = (INIProperty) this.mhmapProps.get(iter.next()); |
||
1276 | objBuf.append(objProp.toString()); |
||
1277 | objBuf.append("\r\n"); |
||
1278 | objProp = null; |
||
1279 | } |
||
1280 | } |
||
1281 | } |
||
1282 | strRet = objBuf.toString(); |
||
1283 | |||
1284 | objBuf = null; |
||
1285 | iter = null; |
||
1286 | colKeys = null; |
||
1287 | return strRet; |
||
1288 | } |
||
1289 | } |
||
1290 | |||
1291 | /*------------------------------------------------------------------------------ |
||
1292 | * Private class representing the INI Property. |
||
1293 | *----------------------------------------------------------------------------*/ |
||
1294 | /** |
||
1295 | * This class represents a key value pair called property in an INI file. |
||
1296 | * @author Prasad P. Khandekar |
||
1297 | * @version 1.0 |
||
1298 | * @since 1.0 |
||
1299 | */ |
||
1300 | private class INIProperty |
||
1301 | { |
||
1302 | /** Variable to hold name of this property */ |
||
1303 | private String mstrName; |
||
1304 | /** Variable to hold value of this property */ |
||
1305 | private String mstrValue; |
||
1306 | /** Variable to hold comments associated with this property */ |
||
1307 | private String mstrComments; |
||
1308 | |||
1309 | /** |
||
1310 | * Constructor |
||
1311 | * @param pstrName the name of this property. |
||
1312 | * @param pstrValue the value of this property. |
||
1313 | */ |
||
1314 | public INIProperty(String pstrName, String pstrValue) |
||
1315 | { |
||
1316 | this.mstrName = pstrName; |
||
1317 | this.mstrValue = pstrValue; |
||
1318 | } |
||
1319 | |||
1320 | /** |
||
1321 | * Constructor |
||
1322 | * @param pstrName the name of this property. |
||
1323 | * @param pstrValue the value of this property. |
||
1324 | * @param pstrComments the comments associated with this property. |
||
1325 | */ |
||
1326 | public INIProperty(String pstrName, String pstrValue, String pstrComments) |
||
1327 | { |
||
1328 | this.mstrName = pstrName; |
||
1329 | this.mstrValue = pstrValue; |
||
1330 | this.mstrComments = delRemChars(pstrComments); |
||
1331 | } |
||
1332 | |||
1333 | /** |
||
1334 | * Returns the string identifier (key part) of this property. |
||
1335 | * @return the string identifier of this property. |
||
1336 | */ |
||
1337 | public String getPropName() |
||
1338 | { |
||
1339 | return this.mstrName; |
||
1340 | } |
||
1341 | |||
1342 | /** |
||
1343 | * Returns value of this property. If value contains a reference to |
||
1344 | * environment avriable then this reference is replaced by actual value |
||
1345 | * before the value is returned. |
||
1346 | * @return the value of this property. |
||
1347 | */ |
||
1348 | public String getPropValue() |
||
1349 | { |
||
1350 | int intStart = 0; |
||
1351 | int intEnd = 0; |
||
1352 | String strVal = null; |
||
1353 | String strVar = null; |
||
1354 | String strRet = null; |
||
1355 | |||
1356 | strRet = this.mstrValue; |
||
1357 | intStart = strRet.indexOf("%"); |
||
1358 | if (intStart >= 0) |
||
1359 | { |
||
1360 | intEnd = strRet.indexOf("%", intStart + 1); |
||
1361 | strVar = strRet.substring(intStart + 1, intEnd); |
||
1362 | strVal = mpropEnv.getProperty(strVar); |
||
1363 | if (strVal != null) |
||
1364 | { |
||
1365 | strRet = strRet.substring(0, intStart) + strVal + |
||
1366 | strRet.substring(intEnd + 1); |
||
1367 | } |
||
1368 | } |
||
1369 | return strRet; |
||
1370 | } |
||
1371 | |||
1372 | /** |
||
1373 | * Returns comments associated with this property. |
||
1374 | * @return the associated comments if any. |
||
1375 | */ |
||
1376 | public String getPropComments() |
||
1377 | { |
||
1378 | return this.mstrComments; |
||
1379 | } |
||
1380 | |||
1381 | /** |
||
1382 | * Sets the string identifier (key part) of a property |
||
1383 | * @param pstrName the string identifier of a property |
||
1384 | */ |
||
1385 | public void setPropName(String pstrName) |
||
1386 | { |
||
1387 | this.mstrName = pstrName; |
||
1388 | } |
||
1389 | |||
1390 | /** |
||
1391 | * Sets the property value |
||
1392 | * @param pstrValue the value for the property |
||
1393 | */ |
||
1394 | public void setPropValue(String pstrValue) |
||
1395 | { |
||
1396 | this.mstrValue = pstrValue; |
||
1397 | } |
||
1398 | |||
1399 | /** |
||
1400 | * Sets the comments for a property |
||
1401 | * @param pstrComments the comments |
||
1402 | */ |
||
1403 | public void setPropComments(String pstrComments) |
||
1404 | { |
||
1405 | this.mstrComments = delRemChars(pstrComments); |
||
1406 | } |
||
1407 | |||
1408 | /* (non-Javadoc) |
||
1409 | * @see java.lang.Object#toString() |
||
1410 | */ |
||
1411 | public String toString() |
||
1412 | { |
||
1413 | String strRet = ""; |
||
1414 | |||
1415 | if (this.mstrComments != null) |
||
1416 | strRet = addRemChars(mstrComments); |
||
1417 | strRet = strRet + this.mstrName + " = " + this.mstrValue; |
||
1418 | return strRet; |
||
1419 | } |
||
1420 | } |
||
1421 | } |