Came across the need to replace tokens within files with the content of
another file, so I've produced a little patch for Ant.
The reasons for this patch are:
- although there is multi line replacetoken/replacevalue functionality
in ant, the actual tokens/values have to be specified within the build
file
- same problem with the properties file: properties file contains also
something other than the actual content that needs to replace the token
Here is the history of it:
My web sites are built using XSLT from the source XML files and XSL
stylesheets into XHTML. Ant facilitates the build via the style task.
However, there is the need to have certain site-wide files (ie. menus)
that are also XML but it is quite unclean to cut and paste them into the
actual source XML files, not to mention a huge mess when the number of
files are big. Placing the menus into the style sheets is even worse,
it's mixing style and data. At the same time, those menu files have to
be available as is because a web publishing system is plugged into the
whole thing and modifies the menus when the site is changed. After the
modification, ant builds the whole site, be that manually or
automatically.
So, in my case, this patch enables placement of XML files (well, their
well-formed fragments, really) within other XML files which are later
processed into HTML using XSLT.
If this makes any sense to any of you, feel free to use the attached
patch.
Bojan
--- jakarta-ant-1.3/src/main/org/apache/tools/ant/taskdefs/Replace.java Fri Mar
2 23:46:37 2001
+++ jakarta-ant-1.3-binarix/src/main/org/apache/tools/ant/taskdefs/Replace.java
Mon Apr 23 17:11:56 2001
@@ -98,6 +98,7 @@
{
private String token;
private String value;
+ private String valueFile;
private String property;
public void validate() throws BuildException {
@@ -112,9 +113,9 @@
throw new BuildException(message);
}
- //value and property are mutually exclusive attributes
- if ((value != null) && (property != null)) {
- String message = "Either value or property " + "can be
specified, but a replacefilter " + "element cannot have both.";
+ //value, valueFile and property are mutually exclusive attributes
+ if ((value != null) && (valueFile != null) && (property != null)) {
+ String message = "Either value, valueFile or property " + "can
be specified, but a replacefilter " + "element cannot have more then one.";
throw new BuildException(message);
}
@@ -142,6 +143,24 @@
else if (value != null) {
return value;
}
+ else if (valueFile != null) {
+ try{
+ FileReader fval=new FileReader(valueFile);
+ StringBuffer val=new StringBuffer();
+ int ch;
+
+ while ((ch=fval.read()) != -1)
+ val.append((char)ch);
+
+ return val.toString();
+ } catch (FileNotFoundException fnf) {
+ log("Value file " + valueFile + "not found.");
+ return new String("");
+ } catch (IOException ioe) {
+ log("Couldn't read value file " + valueFile + ".");
+ return new String("");
+ }
+ }
else if (Replace.this.value != null) {
return Replace.this.value.getText();
}
@@ -165,6 +184,14 @@
public String getValue() {
return value;
+ }
+
+ public void setValueFile(String valueFile) {
+ this.valueFile = valueFile;
+ }
+
+ public String getValueFile() {
+ return valueFile;
}
public void setProperty(String property) {