 |
|
 |
If the INIFile instance variable (or a static member) is not in the same function as the save() call then i get some exceptions in the save() function after using setters functions e.g. setStringProperty("Section","Key","Value","");
To resolve this i have changed code in the INIFile class toString() functions.
class INISection and similar in class INIProperty:if (this.mstrComment != null) objBuf.append(addRemChars(this.mstrComment));
changed toif (this.mstrComment != null) if (this.mstrComment.length()>0) objBuf.append(addRemChars(this.mstrComment));
have fun
cheers,
Frido
|
|
|
|
 |
|
 |
Hi.
It looks quite stupid to keep INI file case-sensitive.
Because 99% INI files are case-insensitive.
Would be nice if you can fix it, or at least implement an option.
|
|
|
|
 |
|
 |
I agree, is there someway to make it case-insensitive?
I guess it would require changes to the LinkedHashMap object though?
EDIT: hmm...if we replace the LinkedHashMap with LinkedCaseInsensitiveMap then all might be ok
cheers,
Paul
|
|
|
|
 |
|
 |
Hello Paul,
Yes. Just by using LinkedCaseInsensitiveMap(Spring Framework), it will work. Because it extends LinkedHashMap. Just replace all LinkedHashMap with LinkedCaseInsensitiveMap. Then you will able to read case-insensitive Properties from INI file.
Thanks
Charan Kolhe
|
|
|
|
 |
|
 |
duplicate the functionality of properties file.
can confuse beginner into using an unproven lib.
|
|
|
|
 |
|
 |
public boolean save(){
return this.save(this.mstrFile);
}
public boolean save(String strPath)
{....
.........
objFile = new File(strPath);
......
.......
Regards
Satish
|
|
|
|
 |
|
 |
Hi, Prasad:
Our old VB product uses INI file to store settings. Now we are working on a new Java product which need to read those settings. We sell both product. I wonder if I may use your INIFile for reading the settings. Thank you!
|
|
|
|
 |
|
 |
Hello,
You can use this class in your product. Just don't forget to keep the flowerboxes (comment block containing original author name and info) intact in the source.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
Go ahead man without adding the flowerbox, believe me he is not gonna sue you for that.
|
|
|
|
 |
|
 |
show some respect you fool
|
|
|
|
 |
|
 |
Hi
I'm a Java newbie
I added INIFile.java to my eclipse project
When i opened it, i got 2 errors
Line 18
The declared package does not match the expected package
Line 1088
The method setSectionComments(String, String) is undefined for the type INIFile
How can i fix these bugs ?
Thanks
|
|
|
|
 |
|
 |
Hello,
Assuming that you have folder named src as your source folder set in eclipse create a directory structure as shown below under src folder.
src
|__ com
|__ freeware
|__ inifiles
Now copy INIFile.java in inifiles folder.
Alternatively open INIFile.java in eclipse editor and let click on the marker (A bulb with x) shown on the left side margin in code editor near the line which reads as package com.freeware.inifiles. You will be presented with a dropdown with a option which reads as Move 'INIFile.java' to package 'com.freeware.inifiles' click this to move INIFile.java to appropriate package.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
-- modified at 8:32 Friday 13th January, 2006
|
|
|
|
 |
|
 |
Hi,
I cannot find the method setSectionComments in the file that was downloaded. Is it possible to add this method?
Thank you
|
|
|
|
 |
|
 |
Hello,
The method to set Section Comments is available on INISection object the signature is
public void setSecComments(String pstrComments)
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
The source still not updated, it loss setSectionComments method.
Best regards,
Vengoal
|
|
|
|
 |
|
|
 |
|
 |
Greetings. I'm trying to deal with an INI file that uses multiple entries with the same name to represent an array. For example:
[SomeIniArea]
Data1=SomeData
Array=Value1
Array=Value2
Array=Value3
Array=Value4
Data2=SomeOtherData
is there a way to retrieve this info with this ini reader?
XpawX
|
|
|
|
 |
|
 |
No, because it stores the properties in a java.util.LinkedHashMap (mhmapProps in INISection), which is a simple Map that allows no duplicate keys.
To solve this, you could replace this LinkedHashMap by a MultiMap found on the net (I think the java api lib doesn't have any multi map).
Alex
|
|
|
|
 |
|
 |
I found a bug in INIFile. If I use setStringProperty(some_section, some_property, some_value, ""), it throws up StringIndexOutOfBoundsException (the same problem is in the simmilar methods set...Property).
If pstrComments is null or some String longer then 0, there isn't any problem.
I thing, in the case "", there should by an empty comment or no comment in inifile.
Something like this:
private String delRemChars(String pstrSrc)
{
int intPos = 0;
if (pstrSrc == null) return null;
if (pstrSrc.length() == 0) return null;
while ((intPos = pstrSrc.indexOf(";")) >= 0)
{
if (intPos == 0)
pstrSrc = pstrSrc.substring(intPos + 1);
else if (intPos > 0)
pstrSrc = pstrSrc.substring(0, intPos) + pstrSrc.substring(intPos + 1);
}
return pstrSrc;
}
private String addRemChars(String pstrSrc)
{
int intLen = 2;
int intPos = 0;
int intPrev = 0;
String strLeft = null;
String strRight = null;
if (pstrSrc == null) return null;
if (pstrSrc.length() == 0) return null;
while (intPos >= 0)
.....
These changes remove raising of the exception in the set...Property, but I'm not sure, if it is 100%.
|
|
|
|
 |
|
|
 |
|
 |
Reading the previous thread, how do you handle line breaks, tabs and other "invisible" chars in your class?
See Run-Control Files from "Art of Unix Programming" for a discussion.
Best regards,
Jens
--
Jens Scheidtmann
|
|
|
|
 |
|
 |
Hello Jens,
Currently tab characters are retained as is. The file is read using readLine method to avoid any problems beacause of windows/unix file formats. However while writing to a file, CrLf sequence is always written at the end of each line.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
Yep, as mentioned the other thread, changing from "=" to " = " is not optimal.
A better modification could be to change the toString method in the INISection. However, an even better way would be to trim away spaces when reading so that "KEY = 12" and "KEY=12" would return the same property value for the key "KEY". That way we are less sensitive to errors in the INI file.
So, the final reading loop in loadFile() method would be:
while (objBRdr.ready())
{
iPos = -1;
strLine = null;
strLine = objBRdr.readLine().trim();
if (strLine.startsWith("[") && strLine.endsWith("]"))
{
strSection = strLine.substring(1, strLine.length() - 1);
objSec = new INISection(strSection);
this.mhmapSections.put(strSection, objSec);
}
else if ((iPos = strLine.indexOf("=")) > 0 && objSec != null
&& (!strLine.startsWith(";")))
{
if (objSec != null )
objSec.setProperty(trim(strLine.substring(0, iPos)),
trim(strLine.substring(iPos + 1)));
}
}
Two notes:
1. I use my own trim(...) method (available later on, but hold on!) since the trim() method in the String class will also trim away control characters.
2. I add a check for comments. Sorry if this puts a constraint on the key, which I find reasonable (i.e. key cannot start with a ';')
Now there is a problem: the reader uses trim() when reading the lines and we trim away spaces from the value, so how do we make sure that we can have string properties with spaces at the beginning and/or the end of the line? A quick answer is to optionally enclose string properties in braces or any other non-space/control character. Quotes would not be a good idea as they may be used frequently. This puts a constraint on the format however: string properties that include enclosing braces must be enclosed within another set of braces. Another constraint I also find reasonable, though. Thus, the line
KEY={{ your name }}
will map the string '{ your name }' to the key KEY, while
KEY={ your name }
will map the string ' your name '. I think this is a reasonable constraint. Now note that
KEY="Enter a value"
will map the string '"Enter a value"', including the enclosing quotes!
If we add the check for braces in the trim() method, we get a bonus: even keys may include initial and terminal spaces. A stupid bonus maybe, but maybe someone finds it useful? So, the trim() method becomes:
private String trim(String str)
{
Pattern pattern = Pattern.compile("(?s)( *)(.*)( *)");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
String strRet = matcher.group(2);
if (strRet.startsWith("{") && strRet.endsWith("}"))
return strRet.substring(1, strRet.length()-1);
else
return strRet;
} else {
return str;
}
}
Then we add an if clause to the while loop in the toString method, which checks for spaces or braces at the beginning or/and the end and in that case adds a set of enclosing braces:
while (iter.hasNext())
{
strProp = (String) iter.next();
strVal = (String) this.mhmapProps.get(strProp);
if ( strVal.startsWith(" ") | strVal.endsWith(" ") |
(strVal.startsWith("{") && strVal.endsWith("}")) )
strVal = "{" + strVal + "}";
objBuf.append(strProp + "=" + strVal + "\r\n");
strProp = null;
strVal = null;
}
I have a working source file with the following complete list of changes:
- Added support for comments
- Added support for initial/terminal spaces in keys & values
- Changed from HashMap to LinkedHashMap to preserve section and key order
- Added support for default values
- Removed main() method.
- Code readability: Changed name of checkFile to fileExists
- Fixed bugs: iter.hasNext() at the wrong place in for clause
- Fixed bug: negation in Constructor for checkFile()
It may be downloaded from the ReqSimile SourceForge project at
http://cvs.sourceforge.net/viewcvs.py/reqsimile/ReqSimile/source/reqsimile/
Revision 1.1 is the original. Revision 1.2 incorporates the above changes.
I hope I didn't miss anything or introduced additional bugs. It has past my non-thorough test. Please post any omissions or errors to this thread.
|
|
|
|
 |
|
 |
Hello Johan,
Thank's a lot for introducing such nice enhancements.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
Hi Prasad,
I'm happy to share! Thanks for the initial code that got me going!
Now, I quickly realized and must admit one annoyance. However, for me it currently has low priority, but will be mended later this year:
Comments are stripped away alright upon reading, but they are not restored when writing and are thus lost in the process!
I haven't figured out what I think is a good solution yet. Since I have added methods for returning default values for keys that are not in the INI file from start, the problem is actually a little intricate:
How should the positions of the comments be handled correctly? Relative to what? An example is required I guess.
Suppose we use an INI file with the following expected structure and "arbitrary" comments added by the user:
; Application preferences
[DB_A]
; Database connection
driverClassName=sun.jdbc.odbc.JdbcOdbcDriver
URL=jdbc:odbc:DB_example
; Table
table=MarketReqs
; Fields
idField=Key
summaryFields=[Id, Label]
detailFields=[Id, Label, Description]
calcFields=[Label, Description]
If the line in bold is not initially specified, it may end up before or after the comment upon writing, depending on the implementaton. I guess this tiny problem could be ignored all together. A compromise or a restriction is required anyhow.
Furthermore, I would like to allow comments to appear at the same line as a key.
Software development is amazing, it gets complex so quickly.
Regards,
Johan Natt och Dag
|
|
|
|
 |