<<PropertyFile-patch.txt>>  <<propertyfile.html>> 
Included are a minor patch to the PropertyFile code and a documentation HTML
file.  The code has been modified to default to a "string" property if no
type is specified rather than error out.  It also uses the
Properties.store() method instead of the writing directly to the output
stream.  This is in preparation of a better implementation that would allow
for maintaining comments placed in a properties file.  I don't have the time
tonight to finish it.  Maybe tomorrow or Saturday.  The javadoc has been
completely revamped, however, and I think the Javadoc and HTML doc
improvements are worthwhile to be in the next build.

My plans:  allow for the retention of comments in the original properties
file
                 limit the propertyfile task to file and comment parameters
forcing the use of entry tags
                 allow for a srcfile and destfile parameter set (maybe
eliminating the comment change since that would be harder than
srcfile/destfile)


> J E S S E  E  T I L L Y
> Senior Programmer, Development
> ____________________________________________
> HotelTools, Inc.
> 100 Galleria Parkway, Suite 1020
> Atlanta, GA  30339
> ph: 770.956.4080 x5361
> fax: 770.956.4077
> cell: 678.524.5764
> http://www.hoteltools.com
> 
 <<Jesse Tilly.vcf>> 
Index: PropertyFile.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java,v
retrieving revision 1.2
diff -u -r1.2 PropertyFile.java
--- PropertyFile.java   2000/10/25 12:05:06     1.2
+++ PropertyFile.java   2000/11/03 04:36:28
@@ -50,70 +50,123 @@
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
+ *
  */
+/**
+ *The following is an example of its usage (it is duplicated below in javadoc 
readible form:
+ *    <target name="setState">
+ *    <property 
+ *        name="header" 
+ *        value="##Generated file - do not modify!"/>
+ *      <propertyfile file="apropfile.properties" comment="${header}">
+ *        <entry key="product.version.major" type="int"  value="5"/>
+ *        <entry key="product.version.minor" type="int"  value="0"/>
+ *        <entry key="product.build.major"   type="int"  value="0" />
+ *        <entry key="product.build.minor"   type="int"  operation="+" />
+ *        <entry key="product.build.date"    type="date" operation="now" />
+ *        <entry key="intInc" type="int" operation="=" value="681"/>
+ *        <entry key="intDec" type="int" operation="-"/>
+ *        <entry key="NeverDate" type="date" operation="never"/>
+ *        <entry key="StringEquals" type="string" value="testValue"/>
+ *        <entry key="NowDate" type="date" operation="now"/>
+ *     </propertyfile>
+ *   </target>
+ *
+ *The <propertyfile> task must have:
+ *    file
+ *Other parameters are:
+ *    comment, operation, key, value, type (the final four being removed 
shortly)
+ *
+ *The <entry> task must have:
+ *    key
+ *Other parameters are:
+ *    value, operation, type
+ *
+ *Parameter values:
+ *    operation: 
+ *        "=" (set -- default)
+ *        "-" (decrement)
+ *        "+" (increment)
+ *        "now" (date and time)
+ *        "never" (empty string)
+ *
+ *    type:
+ *        "int"
+ *        "date"
+ *        "string"
+ *
+ *String property types can only use the "=" operation.
+ *Date property types can only use the "never" or "now" operations.
+ *Int property types can only use the "=", "-" or "+" operations.
+ *
+ *The message property is used for the property file header, with "\\" being
+ *a newline delimiter charater. (This should be '\n' but I couldn't quite get
+ *it right).
+ */
 
-/*
-**PropertyFile task uses java.util.Properties to modify integer, String and
-**Date settings in a property file.
-**
-**
-**The following is an example of its usage:
-**    <target name="setState">
-**    <property 
-**        name="header" 
-**        value="##Generated file - do not modify!"/>
-**      <propertyfile file="${state.file}" comment="${header}">
-**        <entry key="product.version.major" type="int"  value="5"/>
-**        <entry key="product.version.minor" type="int"  value="0"/>
-**        <entry key="product.build.major"   type="int"  value="0" />
-**        <entry key="product.build.minor"   type="int"  operation="+" />
-**        <entry key="product.build.date"    type="date" operation="now" />
-**        <entry key="intInc" type="int" operation="=" value="681"/>
-**        <entry key="intDec" type="int" operation="-"/>
-**        <entry key="NeverDate" type="date" operation="never"/>
-**        <entry key="StringEquals" type="string" value="testValue"/>
-**        <entry key="NowDate" type="date" operation="now"/>
-**     </propertyfile>
-**   </target>
-**
-**The <propertyfile> task must have:
-**    key, type, file
-**Other parameters are:
-**    operation, value, message
-**
-**Parameter values:
-**    operation: 
-**        "=" (set -- default)
-**        "-" (dec)
-**        "+" (inc)
-**        "now" (date and time)
-**        "never" (empty string)
-**
-**    type:
-**        "int"
-**        "date"
-**        "string"
-**
-**String property types can only use the "=" operation.
-**Date property types can only use the "never" or "now" operations.
-**Int property types can only use the "=", "-" or "+" operations.
-**
-**The message property is used for the property file header, with "\\" being
-**a newline delimiter charater. (This should be '\n' but I couldn't quite get
-**it right).
-**
-*/
 package org.apache.tools.ant.taskdefs.optional;
 
 import org.apache.tools.ant.*;
 import java.io.*;
 import java.util.*;
-
 /**
- *    <code>PropertyFile</code> provides a mechanism for updating a
- *    java.util.Properties object via Ant.
+ *PropertyFile task uses java.util.Properties to modify integer, String and
+ *Date settings in a property file.<p>
+ *
+ *
+ *The following is an example of its usage:
+ *    <ul>&lt;target name="setState"&gt;<br>
+ *    <ul>&lt;property<br>
+ *        <ul>name="header"<br> 
+ *        value="##Generated file - do not modify!"/&gt;<br>
+ *      &lt;propertyfile file="apropfile.properties" 
comment="${header}"&gt;<br>
+ *        &lt;entry key="product.version.major" type="int"  value="5"/&gt;<br>
+ *        &lt;entry key="product.version.minor" type="int"  value="0"/&gt;<br>
+ *        &lt;entry key="product.build.major"   type="int"  value="0" /&gt;<br>
+ *        &lt;entry key="product.build.minor"   type="int"  operation="+" 
/&gt;<br>
+ *        &lt;entry key="product.build.date"    type="date" operation="now" 
/&gt;<br>
+ *        &lt;entry key="intInc" type="int" operation="=" value="681"/&gt;<br>
+ *        &lt;entry key="intDec" type="int" operation="-"/&gt;<br>
+ *        &lt;entry key="NeverDate" type="date" operation="never"/&gt;<br>
+ *        &lt;entry key="StringEquals" type="string" value="testValue"/&gt;<br>
+ *        &lt;entry key="NowDate" type="date" operation="now"/&gt;<br></ul>
+ *     &lt;/propertyfile&gt;<br></ul>
+ *   &lt;/target&gt;</ul><p>
+ *
+ *The &lt;propertyfile&gt; task must have:<br>
+ *    <ul><li>file</li></ul>
+ *Other parameters are:<br>
+ *    <ul><li>comment, key, operation, type and value (the final four being 
eliminated shortly)</li></ul>
+ *
+ *The &lt;entry&gt; task must have:<br>
+ *    <ul><li>key</li></ul>
+ *Other parameters are:<br>
+ *    <ul><li>key, operation, type</li></ul>
+ *
+ *If type is unspecified, it defaults to string
+ *
+ *Parameter values:<br>
+ *    <ul><li>operation:</li>
+ *        <ul><li>"=" (set -- default)</li>
+ *        <li>"-" (dec)</li>
+ *        <li>"+" (inc)</li>
+ *        <li>"now" (date and time)</li>
+ *        <li>"never" (empty string)</li></ul>
+ *
+ *    <li>type:</li>
+ *        <ul><li>"int"</li>
+ *        <li>"date"</li>
+ *        <li>"string"</li></ul></ul>
+ *
+ *String property types can only use the "=" operation.
+ *Date property types can only use the "never" or "now" operations.
+ *Int property types can only use the "=", "-" or "+" operations.<p>
+ *
+ *The message property is used for the property file header, with "\\" being
+ *a newline delimiter charater.
+ *
  *    @author     Jeremy Mawson <[EMAIL PROTECTED]>
- */
+*/
 public class PropertyFile extends Task 
 {
 
@@ -249,32 +302,32 @@
         try 
         {
             bos = new BufferedOutputStream(new 
FileOutputStream(m_propertyfile));
-
-            // Write the message if we have one.
-            if (m_comment != null) 
-            {
-                // FIXME: would like to use \n as the newline rather than \\.
-                StringTokenizer tok = new StringTokenizer(m_comment, "\\");
-                while (tok.hasMoreTokens()) 
-                {
-                    bos.write("# ".getBytes());
-                    bos.write(((String)tok.nextToken()).getBytes());
-                    bos.write(NEWLINE.getBytes());
-                }
-                bos.write(NEWLINE.getBytes());
-                bos.flush();
-            }
 
-            Enumeration enumValues = m_properties.elements();
-            Enumeration enumKeys = m_properties.keys();
-            while (enumKeys.hasMoreElements()) 
-            {
-                bos.write(((String)enumKeys.nextElement()).getBytes());
-                bos.write("=".getBytes());
-                bos.write(((String)enumValues.nextElement()).getBytes());
-                bos.write(NEWLINE.getBytes());
-                bos.flush();
-            }
+// Write the message if we have one.
+//            if (m_comment != null) 
+//            {
+//                // FIXME: would like to use \n as the newline rather than \\.
+//                StringTokenizer tok = new StringTokenizer(m_comment, "\\");
+//                  while (tok.hasMoreTokens()) 
+//                  {
+//                      bos.write("# ".getBytes());
+//                      bos.write(((String)tok.nextToken()).getBytes());
+//                      bos.write(NEWLINE.getBytes());
+//                  }
+//                  bos.write(NEWLINE.getBytes());
+//                  bos.flush();
+//            }
+//            Enumeration enumValues = m_properties.elements();
+//            Enumeration enumKeys = m_properties.keys();
+//            while (enumKeys.hasMoreElements()) 
+//            {
+//                bos.write(((String)enumKeys.nextElement()).getBytes());
+//                bos.write("=".getBytes());
+//                bos.write(((String)enumValues.nextElement()).getBytes());
+//                bos.write(NEWLINE.getBytes());
+//                bos.flush();
+//            }
+            m_properties.store(bos, m_comment);
         }
         catch (IOException ioe) 
         {
@@ -308,7 +361,7 @@
         private static final String INTEGER_TYPE =          "int";
         private static final String DATE_TYPE =             "date";
         private static final String STRING_TYPE =           "string";
-
+        
         // Property type operations
         private static final String INCREMENT_OPER =        "+";
         private static final String DECREMENT_OPER =        "-";
@@ -350,6 +403,9 @@
         protected void executeOn(Properties props) throws BuildException 
         {
             // Fork off process per the operation type requested
+
+            // m_type may be null because it wasn't set
+            try {
             if (m_type.equals(INTEGER_TYPE)) 
             {
                 executeInteger((String)props.get(m_key));
@@ -365,9 +421,14 @@
             {
                 throw new BuildException("Unknown operation type: "+m_type+"");
             }
-            
-            props.put(m_key, m_value);
             
+            } catch (NullPointerException npe) {
+                // Default to string type
+                // which means do nothing
+            }
+            // Insert as a string by default
+            props.setProperty(m_key, m_value);
+
         }
         /*
         * Continue execution for Date values
Title: Ant PropertyFile Task

Ant PropertyFile Task User Manual

by

Version 1.0 - 2000/11/02


Table of Contents


Introduction

Ant provides an optional task for editing property files. Thes is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. However, any comments are lost. Work is being done to make this task a bit more "human friendly".


PropertyFile Task

Parameters

Attribute Description Required
file Location of the property file to be edited Yes
comment Header for the file itself no

Parameters specified as nested elements

Entry

Use nested <entry> elements to specify actual modifcations to the property file itself

Attribute Description Required
key Name of the property name/value pair Yes
value Value to set for the key the property name/value pair Yes
type Manipulate the value as type: int, date No, but must be used if opertion attribute used
operation If type is date, this cane be "now" or "never". If the type is int, only "-" and "+" may be used. No

Examples

The following changes the my.properties file. Assume my.properties look like:

    # A comment
    akey=novalue

After running, the file would now look like

    #Thu Nov 02 23:41:47 EST 2000
    akey=avalue
    adate=2000/11/02 23\:41
    anint=1

The slashes conform to the expectations of the Properties class. The file will be stored in a manner so that each character is examined and escaped if necessary. Note that the original comment is now lost. Please keep this in mind when running this task against heavily commented properties files. It may be best to have a commented version in the source tree, copy it to a deployment area, and then run the modifications on the copy. Future versions of PropertyFile will hopefully eliminate this shortcoming.

<propertyfile file="my.properties" comment"My properties" > <entry key="akey" value="avalue" /> <entry key="adate" type="date" operation="now"/> <entry key="anint" type="int" operation="+"/> </propertyfile>

Attachment: Jesse Tilly.vcf
Description: Binary data

Reply via email to