Browse Source

Changes to <propertyfile>

1) documentation.

2) default to String type if type is omitted.

3) use Properties.store (or .save) to write the property file.

Submitted by:	Jesse Tilly <JTilly@hoteltools.com>

I've modified Jesse's original patch to make it compile on JDK 1.1.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268153 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
08eef4e8a6
3 changed files with 226 additions and 87 deletions
  1. +1
    -0
      docs/index.html
  2. +112
    -0
      docs/propertyfile.html
  3. +113
    -87
      src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java

+ 1
- 0
docs/index.html View File

@@ -4538,6 +4538,7 @@ it had been located at <code>htdocs/manual/ChangeLog.txt</code>.</p>
<li><a href="native2ascii.html">Native2Ascii</a></li>
<li><a href="#netrexxc">NetRexxC</a></li>
<li><a href="P4desc.html">Perforce</a></li>
<li><a href="propertyfile.html">PropertyFile</a></li>
<li><a href="#renameexts">RenameExtensions</a></li>
<li><a href="#script">Script</a></li>
<li><a href="#vssget">VssGet</a></li>


+ 112
- 0
docs/propertyfile.html View File

@@ -0,0 +1,112 @@
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Ant PropertyFile Task</title>

</head>

<body>

<h1>Ant PropertyFile Task User Manual</h1>
<p>by</p>
<!-- Names are in alphabetical order, on last name -->
<ul>
<li>Jeremy Mawson (<a href="mailto:jem@loftinspace.com.au">jem@loftinspace.com/au</a>)</li>
</ul>

<p>Version 1.0 - 2000/11/02</p>
<hr>
<h2>Table of Contents</h2>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#proptask">PropertyFile Task</a></li>
<li><a href="#entrytask">Entry Task</a></li>
</ul>

<hr>
<h2><a name="introduction">Introduction</a></h2>
<p>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".

<hr>
<h2><a name="tasks">PropertyFile Task</a></h2>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td width="12%" valign="top"><b>Attribute</b></td>
<td width="78%" valign="top"><b>Description</b></td>
<td width="10%" valign="top"><b>Required</b></td>
</tr>
<tr>
<td width="12%" valign="top">file</td>
<td width="78%" valign="top">Location of the property file to be edited</td>
<td width="10%" valign="top">Yes</td>
</tr>
<tr>
<td width="12%" valign="top">comment</td>
<td width="78%" valign="top">Header for the file itself</td>
<td width="10%" valign="top">no</td>
</tr>
</table>

<h3>Parameters specified as nested elements</h3>
<h4>Entry</h4>
<p>Use nested <code>&lt;entry&gt;</code>
elements to specify actual modifcations to the property file itself
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">key</td>
<td valign="top">Name of the property name/value pair</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">value</td>
<td valign="top">Value to set for the key the property name/value pair</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">type</td>
<td valign="top">Manipulate the value as type: int, date</td>
<td valign="top" align="center">No, but must be used if opertion attribute used</td>
</tr>
<tr>
<td valign="top">operation</td>
<td valign="top">If type is date, this cane be "now" or "never". If the type is int, only "-" and "+" may be used.</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Examples</h3>

<p>The following changes the my.properties file. Assume my.properties look like:<br>
<p>
<ul># A comment<br>
akey=novalue<br></ul>
</p>
After running, the file would now look like
<p>
<ul>#Thu Nov 02 23:41:47 EST 2000<br>
akey=avalue<br>
adate=2000/11/02 23\:41<br>
anint=1<br></ul>
</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.
</p>

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

</body>
</html>

+ 113
- 87
src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -50,70 +50,75 @@
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

/*
**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.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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 <jem@loftinspace.com.au>
*/
*/
public class PropertyFile extends Task
{

@@ -239,10 +244,6 @@ public class PropertyFile extends Task
m_comment = hdr;
}

/*
* Writes the properties to a file. Writes the file manually, rather than
* using Properties.store method, so that special characters are not escaped.
*/
private void writeFile() throws BuildException
{
BufferedOutputStream bos = null;
@@ -250,35 +251,52 @@ public class PropertyFile extends Task
{
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();
// }

// Properties.store is not available in JDK 1.1
Method m =
Properties.class.getMethod("store",
new Class[] {
OutputStream.class,
String.class}
);
m.invoke(m_properties, new Object[] {bos, m_comment});

} catch (NoSuchMethodException nsme) {
m_properties.save(bos, m_comment);
} catch (InvocationTargetException ite) {
Throwable t = ite.getTargetException();
throw new BuildException(t, location);
} catch (IllegalAccessException iae) {
// impossible
throw new BuildException(iae, location);
}
catch (IOException ioe)
{
throw new BuildException(ioe.toString());
throw new BuildException(ioe, location);
}
finally {
if (bos != null) {
@@ -308,7 +326,7 @@ public class PropertyFile extends Task
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 +368,9 @@ public class PropertyFile extends Task
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));
@@ -366,8 +387,13 @@ public class PropertyFile extends Task
throw new BuildException("Unknown operation type: "+m_type+"");
}
} catch (NullPointerException npe) {
// Default to string type
// which means do nothing
}
// Insert as a string by default
props.put(m_key, m_value);
}
/*
* Continue execution for Date values


Loading…
Cancel
Save