Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading the configurtation file divided in the sections  RSS feed

 
berender mavi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,

It is my first post on this site.
i have a configuration file which is divided in the blocks like
MyServer=localhost
MyName=ussd
MyUser=root
MyPasswd=rnd
MyPortNum=3306
Req_thread=20
Sleep_thread=2000
url_timeout=1000
HIT_URL_IP=10.43.14.70
HIT_URL_port=80
[*515#]
URL_SUB=GET /subscription/subservice.aspx?Mobileno=?&Channel=USSD&planid=13404&clientid=511&requestType=1&SubscriptionMode=USSD&isbill=1&subid=1340&shortcodeid=49 HTTP/1.0\r\n\r\n
URL_UNSUB=
PRODUCT=
METHOD=
[END]

[*522#]
URL_SUB=
URL_UNSUB=
PRODUCT=
METHOD=
[END]

[*560#]
URL_SUB=
URL_UNSUB=
PRODUCT=
METHOD=
[END]
there are 3 different block like this.what i want to do is when i got a value from db for eg 575 then only default and block related to 575 is used not other..

Please help.it's critical
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

berender mavi wrote:Please help.it's critical

You should still EaseUp.

That file looks very much like a Windows INI file. That lead me to search for java ini files. I'm sure you can use some of those results.
 
berender mavi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for that

but i am using this config file in linux not in windows
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't matter. The format is what matters.

I did just notice the [END] blocks. These are not part of the INI format, so I don't know how libraries will respond to those. They may throw exceptions, or may include one empty END section.

If existing INI libraries do not work you need to write your own parsing class. That shouldn't be too hard though. In pseudo code:
Now this simple example does not handle multi-line properties, but if you need that you can take a look at how java.lang.Properties handles that. Its source code is available in the src.zip file in your JDK root folder.
 
berender mavi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you help me if it could be done better in the other way either using xml or other.

but what io want is if i get 515 from db then only 515's attributes should be used
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML would be easier to parse, just because there are number of Java APIs that you can use instead of "rolling your own" solution. Is that an option for you though? I mean, do you have control over the format of that configuration file, or is the format set in stone, and it's up to you to work with it?

Another design consideration is how many times you will need to get configuration information. If it's just once, then you would want to design your parser so it could scan through the file looking for the section you need, and then parse that. On the other hand, if your program needs to continuously read values from the database and look up configuration information, then you'd be better off parsing the whole file into Java objects, and afterward just work with the object tree. You also may need to consider that the config file might change while your program is running, and define what behavior you want if that happens.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!