Hi folks,
A little while ago I posted that I had code to grant
which would allow the PropertyFile task to retain the
layout of the properties file that it was editing
(mail subject was also "enhancement for PropertyFile,
have a patch").
I was busy for a while, so didn't get around to
posting the changes, but here they are. There are
changes to two files (java source for the task, and
the associated documentation), and a few associated
and test classes.
The changes listed below are contained in the patch
file. The other files are attached individually.
Any feedback or comments welcome. This is my first
submission, so I hope I am doing this right. Feel free
to point it out if I am not.
Changes:
========
src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java
The task now makes use of the
LayoutPreservingProperties (see below). Also, a new
attribute allows the user to select using a regular
java.util.Properties file instead, if they so choose.
docs/manual/OptionalTasks/propertyfile.html
Documentation changes to follow the task changes.
New files:
==========
src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java
A subclass of java.util.Properties that preserves the
layout of the input stream(s) from which properties
were loaded (if applicable)
src/testcases/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java
Unit tests for the new class.
src/etc/testcases/util/simple.properties
src/etc/testcases/util/unusual.properties
Test data for the unit tests.
Philip Hourihane
___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail
http://uk.messenger.yahoo.com
? src/etc/testcases/util
? src/etc/testcases/types/resources/work.jar
? src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java
? src/testcases/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java
Index: docs/manual/OptionalTasks/propertyfile.html
===================================================================
RCS file: /home/cvspublic/ant/docs/manual/OptionalTasks/propertyfile.html,v
retrieving revision 1.15
diff -u -r1.15 propertyfile.html
--- docs/manual/OptionalTasks/propertyfile.html 29 Apr 2005 18:58:09 -0000
1.15
+++ docs/manual/OptionalTasks/propertyfile.html 1 Oct 2005 22:12:28 -0000
@@ -28,8 +28,8 @@
<p>Ant provides an optional task for editing property files. This 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.</p>
+the ability to add properties or make changes to existing ones. Comments and
layout
+of the original properties file are preserved.</p>
<hr>
<h2><a name="proptask">PropertyFile Task</a></h2>
@@ -50,8 +50,15 @@
<td width="78%" valign="top">Header for the file itself</td>
<td width="10%" valign="top">no</td>
</tr>
+<tr>
+ <td width="12%" valign="top">jdkproperties</td>
+ <td width="78%" valign="top">Use java.lang.Properties, which will loose
comments and layout of file (default is 'false')</td>
+ <td width="10%" valign="top">no</td>
+</tr>
</table>
+<p>The boolean attribute 'jdkproperties' is provided to recover the previous
behaviour of the task, in which the layout and any comments in the properties
file were lost by the task.</p>
+
<h3>Parameters specified as nested elements</h3>
<h4><a name="entryElement">Entry</a></h4>
<p>Use nested <code><entry></code>
@@ -135,20 +142,35 @@
<p>The following changes the my.properties file. Assume my.properties look
like:</p>
-<pre># A comment
-akey=novalue</pre>
+<pre># A string value
+akey=original value
+
+# The following is a counter, which will be incremented by 1 for
+# each time the build is run.
+anint=1</pre>
<p>After running, the file would now look like
</p>
-<pre>#Thu Nov 02 23:41:47 EST 2000
+<pre>#My properties
+#Wed Aug 31 13:47:19 BST 2005
+# A string value
akey=avalue
-adate=2000/11/02 23\:41
-anint=1
+
+# The following is a counter, which will be incremented by 1 for
+# each time the build is run.
+anint=2
+
+adate=2005/08/31 13\:47
+
formated.int=0014
-formated.date=028 17\:34
-</pre>
+
+formated.date=243 13\:47</pre>
+<p>
+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.
+</p>
+
<p>
-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.
+The layout and comment of the original file is preserved. New properties are
added at the end of the file. Existing properties are overwritten in place.
</p>
<blockquote><pre><propertyfile
@@ -156,7 +178,7 @@
comment="My properties">
<entry key="akey" value="avalue"/>
<entry key="adate" type="date"
value="now"/>
- <entry key="anint" type="int"
operation="+"/>
+ <entry key="anint" type="int" default="0"
operation="+"/>
<entry key="formated.int" type="int"
default="0013" operation="+" pattern="0000"/>
<entry key="formated.date" type="date"
value="now" pattern="DDD HH:mm"/>
</propertyfile>
Index: src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java
===================================================================
RCS file:
/home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java,v
retrieving revision 1.37
diff -u -r1.37 PropertyFile.java
--- src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java 15 Nov
2004 14:52:27 -0000 1.37
+++ src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java 1 Oct
2005 22:12:44 -0000
@@ -36,72 +36,65 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.util.LayoutPreservingProperties;
/**
- *Modifies settings in a property file.
+ * <p>Modifies settings in a property file.</p>
*
- * <p>
- *The following is an example of its usage:
- * <ul><target name="setState"><br>
- * <ul><property<br>
- * <ul>name="header"<br>
- * value="##Generated file - do not modify!"/><br>
- * <propertyfile file="apropfile.properties"
comment="${header}"><br>
- * <entry key="product.version.major" type="int" value="5"/><br>
- * <entry key="product.version.minor" type="int" value="0"/><br>
- * <entry key="product.build.major" type="int" value="0" /><br>
- * <entry key="product.build.minor" type="int" operation="+"
/><br>
- * <entry key="product.build.date" type="date" value="now"
/><br>
- * <entry key="intSet" type="int" operation="=" value="681"/><br>
- * <entry key="intDec" type="int" operation="-"/><br>
- * <entry key="StringEquals" type="string" value="testValue"/><br>
- * </propertyfile><br></ul>
- * </target></ul><p>
+ * <p> The following is an example of its usage:</p>
+ * <pre><target name="setState">
+ * <propertyfile file="apropfile.properties" comment="header text">
+ * <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" value="now" />
+ * <entry key="intSet" type="int" operation="=" value="681"/>
+ * <entry key="intDec" type="int" operation="-"/>
+ * <entry key="StringEquals" type="string" value="testValue"/>
+ * </propertyfile>
+ * </target></pre>
*
- *The <propertyfile> task must have:<br>
+ *<p>The <propertyfile> 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>
+ * <ul><li>comment</li></ul></p>
*
- *The <entry> task must have:<br>
+ *<p>The <entry> task must have:<br>
* <ul><li>key</li></ul>
*Other parameters are:<br>
* <ul><li>operation</li>
* <li>type</li>
* <li>value</li>
* <li>default</li>
+ * <li>pattern</li>
* <li>unit</li>
- * </ul>
+ * </ul></p>
*
- *If type is unspecified, it defaults to string
+ *<p>If type is unspecified, it defaults to string.</p>
*
- *Parameter values:<br>
+ *<p>Parameter values:<br>
* <ul><li>operation:</li>
* <ul><li>"=" (set -- default)</li>
* <li>"-" (dec)</li>
- * <li>"+" (inc)</li>
+ * <li>"+" (inc)</li></ul>
*
* <li>type:</li>
* <ul><li>"int"</li>
* <li>"date"</li>
- * <li>"string"</li></ul></ul>
+ * <li>"string"</li></ul>
*
* <li>value:</li>
- * <ul><li>holds the default value, if the property
+ * <ul><li>holds the value to use, if the property
* was not found in property file</li>
* <li>"now" In case of type "date", the
* value "now" will be replaced by the current
* date/time and used even if a valid date was
- * found in the property file.</li></ul>
- *
- *
- *String property types can only use the "=" operation.
- *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 character.
+ * found in the property file.</li></ul></ul>
+ *</p>
*
+ *<p>All property types can use the "=" or "+" operations.
+ *Date and Int property types can also use "-" operation.</p>
*/
public class PropertyFile extends Task {
@@ -115,6 +108,7 @@
private Properties properties;
private File propertyfile;
+ private boolean useJDKProperties;
private Vector entries = new Vector();
@@ -149,8 +143,13 @@
}
private void readFile() throws BuildException {
- // Create the PropertyFile
- properties = new Properties();
+ if (useJDKProperties) {
+ // user chose to use standard Java properties, which loose
comments and layout
+ properties = new Properties();
+ }
+ else {
+ properties = new LayoutPreservingProperties();
+ }
try {
if (propertyfile.exists()) {
log("Updating property file: "
@@ -203,6 +202,13 @@
comment = hdr;
}
+ /**
+ * optional flag to use original Java properties (as opposed to layout
preserving properties)
+ */
+ public void setJDKProperties(boolean val) {
+ useJDKProperties = val;
+ }
+
private void writeFile() throws BuildException {
BufferedOutputStream bos = null;
try {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]