You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Pritesh saharey <pr...@sqatester.com> on 2004/08/01 18:52:45 UTC

Reading and Writing of String

Hi All,
       I need a help regarding reading and writing to a file. There is a file ui.properties which looks something like this

       build-date= AmarGeet_0098_20040812

       now i want to read this value of variable 'build-date' and write it to a 'String' variable in a java file
       
       String version_Date="";

       and this process has to be done before the compliation of java files, is there any way in ant to do this process.
       

       so what need to be done here can anybody help me out, I will be thankful to you.



Pritesh Saharey
BE(Mech)
<a href="http://www.ntsearch.com/search.php?q=Software&v=56">Software</a> Engineer


_____________________________________________________________
Join SQAtester.com Community  ---> http://www.sqatester.com/testersarea/joinus.htm

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


Re: Reading and Writing of String

Posted by Eduardo Andrés Alfonso Sierra <ed...@spymac.com>.
I would do it like this (however, other solutions must exist and could be
better, this is just my way)

load the properties file using

        <property file="ui.properties"/>

now you can access the value of you build-date property this way

        ${build-date}

then you can use replaceregexp to change the value of the version_Date
variable within the java file

        <replaceregexp  file="javaFile">
                <regexp pattern="String version_Date=(.*?);"/>
                <substitution expression="String
version_Date=&quot;${build-date}&quot;;"/>
        </replaceregexp>


This way could work, I think you can easily adapt it to you case.




----- Original Message ----- 
From: "Pritesh saharey" <pr...@sqatester.com>
To: <us...@ant.apache.org>
Sent: Sunday, August 01, 2004 11:52 AM
Subject: Reading and Writing of String


> Hi All,
>        I need a help regarding reading and writing to a file. There is a
file ui.properties which looks something like this
>
>        build-date= AmarGeet_0098_20040812
>
>        now i want to read this value of variable 'build-date' and write it
to a 'String' variable in a java file
>
>        String version_Date="";
>
>        and this process has to be done before the compliation of java
files, is there any way in ant to do this process.
>
>
>        so what need to be done here can anybody help me out, I will be
thankful to you.
>
>
>
> Pritesh Saharey
> BE(Mech)
> <a href="http://www.ntsearch.com/search.php?q=Software&v=56">Software</a>
Engineer
>
>
> _____________________________________________________________
> Join SQAtester.com Community  --->
http://www.sqatester.com/testersarea/joinus.htm
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>



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


Re: Reading and Writing of String

Posted by Rhino <rh...@sympatico.ca>.
I'm not sure if editing source files with build scripts is the best way to
handle this situation. It would probably be better for the Java program to
read the build-date value from the properties file itself. But you may have
a compelling reason to edit the source file; that's up to you.

I had never tried to do what you want to do but, based on the Ant manual, I
guessed that the 'loadproperties' and 'replace' tasks should do what you
want. I put together a simple script to make sure I understood the manual
correctly and found that it worked fine. My files and the script were as
follows:

ui.properties
--------------
prop1=foo

build-date=AmarGeet_0098_20040812

prop3=bar


pritesh.java
-------------

String version_Date="[xxx]";


pritesh.xml
-------------
<?xml version="1.0" ?>

<project name="Pritesh" default="Edit" basedir=".">

<description>Initialize a variable in a source file from a property in a
property file.

</description>

<target name="Read" description="Read build_date">

<loadproperties srcFile="ui.properties">

<filterchain>

<linecontains>

<contains value="build-date"/>

</linecontains>

</filterchain>

</loadproperties>

<echo message="The value of build-date is ${build-date}"/>

</target>

<target name="Edit" depends="Read" description="Initialize version_date">

<replace file="pritesh.java" token="[xxx]" value="${build-date}"/>

</target>

</project>

The "Read" target reads the ui.properties file via the loadproperties task
and finds the line containing the 'build-date' property. The value of the
build-date property in the ui.properties file is then used to initialize an
*Ant* property that is also named 'build-date' with the same value. The echo
task is used to verify that the *Ant* property 'build-date' is equal to
"AmarGeet_0098_20040812".

The "Edit" target uses the replace task to replace '[xxx]' in the source
file with "AmarGeet_0098_20040812". I checked the source file after
completion of the Ant script and it was correctly edited.

IMPORTANT NOTE:
I modified the source code slightly to make the solution to the problem
simpler. Specifically, I changed this:

String version_date="";

to

String version_date="[xxx]";

Since it is quite possible that you have initialized several different
variables in your program to "", I decided to put something distinctive in
the initialization for version_date. That is to ensure that you don't
initialize any value other than 'version_date' with your build-date.
Remember, the replace task will normally replace *ALL* occurrences of the
"token" with "value".

 It doesn't really matter what you use to initialize version_date in your
source file but I've found that it's usually less trouble if you avoid
imbedded blanks, quotes, or apostrophes. Naturally, you also have to set the
token parameter in the "replace" task with the same initialization value
that appears in the source file.


Rhino

----- Original Message ----- 
From: "Pritesh saharey" <pr...@sqatester.com>
To: <us...@ant.apache.org>
Sent: Sunday, August 01, 2004 12:52 PM
Subject: Reading and Writing of String


> Hi All,
>        I need a help regarding reading and writing to a file. There is a
file ui.properties which looks something like this
>
>        build-date= AmarGeet_0098_20040812
>
>        now i want to read this value of variable 'build-date' and write it
to a 'String' variable in a java file
>
>        String version_Date="";
>
>        and this process has to be done before the compliation of java
files, is there any way in ant to do this process.
>
>
>        so what need to be done here can anybody help me out, I will be
thankful to you.
>
>
>
> Pritesh Saharey
> BE(Mech)
> <a href="http://www.ntsearch.com/search.php?q=Software&v=56">Software</a>
Engineer
>
>
> _____________________________________________________________
> Join SQAtester.com Community  --->
http://www.sqatester.com/testersarea/joinus.htm
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>


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