You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by vikas rao <vi...@gmail.com> on 2007/05/21 12:29:22 UTC

OT- need some help

 Hi,

Can someone point out a tutorial or some place where I can learn how to read
values from a .cfg file?
Say I have a Input.cfg file which goes like:
<com>
 <MyCompany>
   <Common
     inputDir="C:/vray/xml_files/"
      outputDir="C:/xm_output"
   />
</MyCompany>
</com>

Now, if I need the inputDir value inside my program, how do I do it?

Sorry that this is off topic from struts, but i cant seem to find some
sensible help.

Any help will be appreciated.
Thanks.
Vikas.

Re: OT- need some help

Posted by vikas rao <vi...@gmail.com>.
Hi martin,
Yes, Now that I look at Digester's faq, seems like this can be pretty
useful.Once the info in the files are available through java beans, i can
use them anywhere I want using the getter methods.
However, in my current scenario, looks like a simple key value pair, ie
having a .properties file would suit me better.
    public static ResourceBundle confBundle = ResourceBundle.getBundle("package
name");

    public static String getPropValue(String key){
        String propValue = null;
            propValue = confBundle.getString(key);
        return propValue;
    }
}
And i could call getPropValue to access the value stored in the .properties
file right?
However, tonight is going to be spent on learning digester:)

Thanks,
Vikas.


On 5/21/07, Martin Kindler <ki...@arcor.de> wrote:
>
> vikas rao schrieb:
> > No, Martin,  I wasnt looking at plain xml reading ,in that case i could
> > just
> > use a normal parser and get the values right?
> > Thanks,
> > Vikas.
> Yes, sure, you could just use an XML parser and do your own stuff. The
> problem with this approach would be that changing your XML structure
> would probably mean changing a lot of the processing.
>
> This type of processing is handled by Digester which was originally made
> to deal with Struts config files. It will (called at an appropriate time
> in your app's lifecycle) convert your configuration information into
> (one or several) Java beans which your application can access. Seems for
> me exactly the thing you are looking for.
>
> The other question is whether this machinery is worthwhile, if you are
> just having a few key-value pairs which probably would be easier be put
> into some plain text file (e. g a .properties file).
>
> To give you an example how I do configuration in my Struts (1.x) based
> apps:
> I introduce a plugin where I pass the path of the config-file as a
> parameter in struts-config.xml.
> In the init-method of the plugin I use digester to transfer the
> information from my config file into a configuration bean which is put
> into application scope. From there it can be accessed when needed.
>
> Martin
>
>
> > On 5/21/07, vikas rao <vi...@gmail.com> wrote:
> >>
> >>  Hi,
> >>
> >> Can someone point out a tutorial or some place where I can learn how to
> >> read values from a .cfg file?
> >> Say I have a Input.cfg file which goes like:
> >> <com>
> >>  <MyCompany>
> >>    <Common
> >>      inputDir="C:/vray/xml_files/"
> >>       outputDir="C:/xm_output"
> >>    />
> >> </MyCompany>
> >> </com>
> >>
> >> Now, if I need the inputDir value inside my program, how do I do it?
> >>
> >> Sorry that this is off topic from struts, but i cant seem to find some
> >> sensible help.
> >>
> >> Any help will be appreciated.
> >> Thanks.
> >> Vikas.
> >>
> >
>
>
> --
> ----------------------------------------------
> Martin Kindler
> Kaulbachstr. 20a
> D-12247 Berlin
> Deutschland
>
> E-Mail: kindlerm@acm.org
> Tel. +49 (030) 260 78 48 1
> Fax  +49 (030) 260 78 48 3
> GSM  +49 (0160) 977 636 14
> Skype martin.kindler
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: OT- need some help

Posted by Martin Kindler <ki...@arcor.de>.
vikas rao schrieb:
> No, Martin,  I wasnt looking at plain xml reading ,in that case i could 
> just
> use a normal parser and get the values right?
> Thanks,
> Vikas.
Yes, sure, you could just use an XML parser and do your own stuff. The 
problem with this approach would be that changing your XML structure 
would probably mean changing a lot of the processing.

This type of processing is handled by Digester which was originally made 
to deal with Struts config files. It will (called at an appropriate time 
in your app's lifecycle) convert your configuration information into 
(one or several) Java beans which your application can access. Seems for 
me exactly the thing you are looking for.

The other question is whether this machinery is worthwhile, if you are 
just having a few key-value pairs which probably would be easier be put 
into some plain text file (e. g a .properties file).

To give you an example how I do configuration in my Struts (1.x) based apps:
I introduce a plugin where I pass the path of the config-file as a 
parameter in struts-config.xml.
In the init-method of the plugin I use digester to transfer the 
information from my config file into a configuration bean which is put 
into application scope. From there it can be accessed when needed.

Martin


> On 5/21/07, vikas rao <vi...@gmail.com> wrote:
>>
>>  Hi,
>>
>> Can someone point out a tutorial or some place where I can learn how to
>> read values from a .cfg file?
>> Say I have a Input.cfg file which goes like:
>> <com>
>>  <MyCompany>
>>    <Common
>>      inputDir="C:/vray/xml_files/"
>>       outputDir="C:/xm_output"
>>    />
>> </MyCompany>
>> </com>
>>
>> Now, if I need the inputDir value inside my program, how do I do it?
>>
>> Sorry that this is off topic from struts, but i cant seem to find some
>> sensible help.
>>
>> Any help will be appreciated.
>> Thanks.
>> Vikas.
>>
> 


-- 
----------------------------------------------
Martin Kindler
Kaulbachstr. 20a
D-12247 Berlin
Deutschland

E-Mail: kindlerm@acm.org
Tel. +49 (030) 260 78 48 1
Fax  +49 (030) 260 78 48 3
GSM  +49 (0160) 977 636 14
Skype martin.kindler

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: OT- need some help

Posted by vikas rao <vi...@gmail.com>.
No, Martin,  I wasnt looking at plain xml reading ,in that case i could just
use a normal parser and get the values right?
Thanks,
Vikas.

On 5/21/07, vikas rao <vi...@gmail.com> wrote:
>
>  Hi,
>
> Can someone point out a tutorial or some place where I can learn how to
> read values from a .cfg file?
> Say I have a Input.cfg file which goes like:
> <com>
>  <MyCompany>
>    <Common
>      inputDir="C:/vray/xml_files/"
>       outputDir="C:/xm_output"
>    />
> </MyCompany>
> </com>
>
> Now, if I need the inputDir value inside my program, how do I do it?
>
> Sorry that this is off topic from struts, but i cant seem to find some
> sensible help.
>
> Any help will be appreciated.
> Thanks.
> Vikas.
>

Re: OT- need some help

Posted by Martin Kindler <ki...@arcor.de>.
Seems like you are looking for some type of XML reading.
I would propose to look at Digester (which stems from  the Struts 
project originally) (http://jakarta.apache.org/commons/digester/)
or Betwixt (also from Apache Jakarta Commons 
http://jakarta.apache.org/commons/betwixt/) which builds on Digester and 
as an add-on allows you to write Java beans to an XML file.

Both are fairly easy to use and if your application is Struts based you 
should already have Digester included.

Tutorials and docs can be found on the Jakarta Commons site.

Martin

vikas rao schrieb:
> Hi,
> 
> Can someone point out a tutorial or some place where I can learn how to 
> read
> values from a .cfg file?
> Say I have a Input.cfg file which goes like:
> <com>
> <MyCompany>
>   <Common
>     inputDir="C:/vray/xml_files/"
>      outputDir="C:/xm_output"
>   />
> </MyCompany>
> </com>
> 
> Now, if I need the inputDir value inside my program, how do I do it?
> 
> Sorry that this is off topic from struts, but i cant seem to find some
> sensible help.
> 
> Any help will be appreciated.
> Thanks.
> Vikas.
> 


-- 
----------------------------------------------
Martin Kindler
Kaulbachstr. 20a
D-12247 Berlin
Deutschland

E-Mail: kindlerm@acm.org
Tel. +49 (030) 260 78 48 1
Fax  +49 (030) 260 78 48 3
GSM  +49 (0160) 977 636 14
Skype martin.kindler

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: OT- need some help

Posted by MK Tan <mk...@gmail.com>.
On 5/21/07, vikas rao <vi...@gmail.com> wrote:
>
> Can you give me an example?




The reason I chose to do it this way is basically I can change the
> inputDirectory, outputdirectory and other values without making any
> changes
> in the code, hence using it anywhere becomes easy.
> In our company, we have a jar file which is included in the build path and
> then we access the config files like:
> Config configRoot = ConfigFactory.create(ConfigFactory.DEFAULT);
> and then using the Config object, go onto access the values in the .cfg
> files...but If i want to use this elsewhere, how would i do a similar
> thing?


As long as your jar file and the cfg file are under the same classpath,
it shouldn't be any problem to read the value.

That was my question.
>
> Thanks.
> Vikas.
>
> On 5/21/07, MK Tan <mk...@gmail.com> wrote:
> >
> > Hi,
> >
> > Just wonder why you need to do this in xml way?
> > Wasn't it much simpler if you do it in properties (key value) format?
> > Most of the time, I just found it people like to overuse xml :-p
> >
> > Best regards,
> > MK Tan
> >
> > On 5/21/07, vikas rao <vi...@gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > Can someone point out a tutorial or some place where I can learn how
> to
> > > read
> > > values from a .cfg file?
> > > Say I have a Input.cfg file which goes like:
> > > <com>
> > > <MyCompany>
> > >    <Common
> > >      inputDir="C:/vray/xml_files/"
> > >       outputDir="C:/xm_output"
> > >    />
> > > </MyCompany>
> > > </com>
> > >
> > > Now, if I need the inputDir value inside my program, how do I do it?
> > >
> > > Sorry that this is off topic from struts, but i cant seem to find some
> > > sensible help.
> > >
> > > Any help will be appreciated.
> > > Thanks.
> > > Vikas.
> > >
> >
>

Re: OT- need some help

Posted by vikas rao <vi...@gmail.com>.
Can you give me an example?
The reason I chose to do it this way is basically I can change the
inputDirectory, outputdirectory and other values without making any changes
in the code, hence using it anywhere becomes easy.
In our company, we have a jar file which is included in the build path and
then we access the config files like:
Config configRoot = ConfigFactory.create(ConfigFactory.DEFAULT);
and then using the Config object, go onto access the values in the .cfg
files...but If i want to use this elsewhere, how would i do a similar thing?
That was my question.

Thanks.
Vikas.

On 5/21/07, MK Tan <mk...@gmail.com> wrote:
>
> Hi,
>
> Just wonder why you need to do this in xml way?
> Wasn't it much simpler if you do it in properties (key value) format?
> Most of the time, I just found it people like to overuse xml :-p
>
> Best regards,
> MK Tan
>
> On 5/21/07, vikas rao <vi...@gmail.com> wrote:
> >
> > Hi,
> >
> > Can someone point out a tutorial or some place where I can learn how to
> > read
> > values from a .cfg file?
> > Say I have a Input.cfg file which goes like:
> > <com>
> > <MyCompany>
> >    <Common
> >      inputDir="C:/vray/xml_files/"
> >       outputDir="C:/xm_output"
> >    />
> > </MyCompany>
> > </com>
> >
> > Now, if I need the inputDir value inside my program, how do I do it?
> >
> > Sorry that this is off topic from struts, but i cant seem to find some
> > sensible help.
> >
> > Any help will be appreciated.
> > Thanks.
> > Vikas.
> >
>

Re: OT- need some help

Posted by MK Tan <mk...@gmail.com>.
Hi,

Just wonder why you need to do this in xml way?
Wasn't it much simpler if you do it in properties (key value) format?
Most of the time, I just found it people like to overuse xml :-p

Best regards,
MK Tan

On 5/21/07, vikas rao <vi...@gmail.com> wrote:
>
> Hi,
>
> Can someone point out a tutorial or some place where I can learn how to
> read
> values from a .cfg file?
> Say I have a Input.cfg file which goes like:
> <com>
> <MyCompany>
>    <Common
>      inputDir="C:/vray/xml_files/"
>       outputDir="C:/xm_output"
>    />
> </MyCompany>
> </com>
>
> Now, if I need the inputDir value inside my program, how do I do it?
>
> Sorry that this is off topic from struts, but i cant seem to find some
> sensible help.
>
> Any help will be appreciated.
> Thanks.
> Vikas.
>