Browse Source

allow tokens in replacetokens filter to be read from a resource. PR 42702

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@724610 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
f4bdfee52e
1 changed files with 28 additions and 10 deletions
  1. +28
    -10
      src/main/org/apache/tools/ant/filters/ReplaceTokens.java

+ 28
- 10
src/main/org/apache/tools/ant/filters/ReplaceTokens.java View File

@@ -17,7 +17,8 @@
*/ */
package org.apache.tools.ant.filters; package org.apache.tools.ant.filters;


import java.io.FileInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.Enumeration; import java.util.Enumeration;
@@ -25,6 +26,8 @@ import java.util.Hashtable;
import java.util.Properties; import java.util.Properties;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;


/** /**
@@ -213,6 +216,16 @@ public final class ReplaceTokens
return endToken; return endToken;
} }


/**
* A resource containing properties, each of which is interpreted
* as a token/value pair.
*
* @since Ant 1.8.0
*/
public void setPropertiesResource(Resource r) {
makeTokensFromProperties(r);
}

/** /**
* Adds a token element to the map of tokens to replace. * Adds a token element to the map of tokens to replace.
* *
@@ -228,11 +241,11 @@ public final class ReplaceTokens
* *
* @param fileName The file to load properties from. * @param fileName The file to load properties from.
*/ */
private Properties getPropertiesFromFile (String fileName) {
FileInputStream in = null;
private Properties getProperties(Resource r) {
InputStream in = null;
Properties props = new Properties(); Properties props = new Properties();
try { try {
in = new FileInputStream(fileName);
in = r.getInputStream();
props.load(in); props.load(in);
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
@@ -312,18 +325,23 @@ public final class ReplaceTokens
final String value = params[i].getValue(); final String value = params[i].getValue();
hash.put(name, value); hash.put(name, value);
} else if ("propertiesfile".equals(type)) { } else if ("propertiesfile".equals(type)) {
Properties props = getPropertiesFromFile(params[i].getValue());
for (Enumeration e = props.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
String value = props.getProperty(key);
hash.put(key, value);
}
makeTokensFromProperties(
new FileResource(new File(params[i].getValue())));
} }
} }
} }
} }
} }


private void makeTokensFromProperties(Resource r) {
Properties props = getProperties(r);
for (Enumeration e = props.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
String value = props.getProperty(key);
hash.put(key, value);
}
}

/** /**
* Holds a token * Holds a token
*/ */


Loading…
Cancel
Save