diff --git a/docs/index.html b/docs/index.html index 0f3f6ca90..214d5c136 100644 --- a/docs/index.html +++ b/docs/index.html @@ -4538,6 +4538,7 @@ it had been located at htdocs/manual/ChangeLog.txt.

  • Native2Ascii
  • NetRexxC
  • Perforce
  • +
  • PropertyFile
  • RenameExtensions
  • Script
  • VssGet
  • diff --git a/docs/propertyfile.html b/docs/propertyfile.html new file mode 100644 index 000000000..75cd861a9 --- /dev/null +++ b/docs/propertyfile.html @@ -0,0 +1,112 @@ + + +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

    + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    fileLocation of the property file to be editedYes
    commentHeader for the file itselfno
    + +

    Parameters specified as nested elements

    +

    Entry

    +

    Use nested <entry> +elements to specify actual modifcations to the property file itself + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    keyName of the property name/value pairYes
    valueValue to set for the key the property name/value pairYes
    typeManipulate the value as type: int, dateNo, but must be used if opertion attribute used
    operationIf 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:
    +

    +

    +

    +After running, the file would now look like +

    +

    +

    +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> +
    + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java b/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java index 65810895b..bc4655aa0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java @@ -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 * . + * */ -/* -**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: -** -** -** -** -** -** -** -** -** -** -** -** -** -** -** -** -**The 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.*; /** - * PropertyFile 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.

    + * + * + *The following is an example of its usage: + *

    + * + *The <propertyfile> task must have:
    + *

    + *Other parameters are:
    + * + * + *The <entry> task must have:
    + * + *Other parameters are:
    + * + * + *If type is unspecified, it defaults to string + * + *Parameter values:
    + * + * + *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. + * * @author Jeremy Mawson - */ +*/ 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