git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271033 13f79535-47bb-0310-9956-ffa450edef68master
@@ -5,50 +5,75 @@ | |||
<body> | |||
<h2><a name="loadfile">LoadFile</a></h2> | |||
<h3>Description</h3> | |||
<p> | |||
Load a text file into a single property. Unless an encoding is specified, | |||
the encoding of the current locale is used. There is no explicit limit | |||
upon the size of the file which can be loaded, but loading very large | |||
files is not something anyone has yet explored. | |||
files is not something anyone has yet explored. Because the file is | |||
converted to text | |||
</p> | |||
<h3>Parameters</h3> | |||
<table border="1" cellpadding="2" cellspacing="0"> | |||
<tr> | |||
<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> | |||
<tr> | |||
<td valign="top">srcFile</td> | |||
<td valign="top">source file</td> | |||
<td valign="top" align="center">Yes</td> | |||
</tr> | |||
<tr> | |||
<tr> | |||
<td valign="top">property</td> | |||
<td valign="top">property to save to</td> | |||
<td valign="top" align="center">Yes</td> | |||
</tr> | |||
<tr> | |||
<tr> | |||
<td valign="top">encoding</td> | |||
<td valign="top">encoding to use when loading the file</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<tr> | |||
<td valign="top">failonerror</td> | |||
<td valign="top">Whether to halt the build on failure</td> | |||
<td align="center" valign="top">No, default "true"</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">evaluateProperties</td> | |||
<td valign="top">flag to enable property evalation in the file</td> | |||
<td align="center" valign="top">No, default "false"</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">makeOneLine</td> | |||
<td valign="top">flag to strip out newlines (but not spaces or | |||
tabs) from the file</td> | |||
<td align="center" valign="top">No, default "false"</td> | |||
</tr> | |||
</table> | |||
<p> | |||
The <tt>makeOneLine</tt> parameter enables you to use a file as an | |||
input to task parameters which expect single line input. It flattens | |||
the file by removing all carriage return and line feed characters, | |||
so that the file | |||
<pre>a | |||
b | |||
c | |||
</pre>would become "abc": you need spaces or | |||
commas at the end/start of line to stop alphanumeric characters | |||
being merged together. | |||
<h3>Examples</h3> | |||
<pre> <loadfile property="mail.recipients" | |||
srcFile="recipients.txt" / > | |||
<pre> <loadfile property="message" | |||
srcFile="message.txt" / > | |||
</pre> | |||
Load file recipients.txt into property "mail.recipients" | |||
Load file message.txt into property "message"; an <tt><echo></tt> | |||
can print this. | |||
<pre> <loadfile property="encoded-file" | |||
srcFile="loadfile.xml" | |||
@@ -56,16 +81,35 @@ Load file recipients.txt into property "mail.recipients" | |||
</pre> | |||
Load a file using the latin-1 encoding | |||
<pre> <loadfile | |||
property="optional.value" | |||
srcFile="optional.txt" | |||
<pre> <loadfile | |||
property="optional.value" | |||
srcFile="optional.txt" | |||
failonerror="false" /> | |||
</pre> | |||
Load a file, don't fail if it is missing (a message is printed, though) | |||
Load a file, don't fail if it is missing (a message is printed, though) | |||
<pre> <loadfile | |||
property="mail.recipients" | |||
srcFile="recipientlist.txt" | |||
makeOneLine="true" /> | |||
</pre> | |||
load a property which can be used as a parameter for another task (in this case mail), | |||
merging lines to ensure this happens. | |||
<pre> <loadfile | |||
property="system.configuration.xml" | |||
srcFile="configuration.xml" | |||
evaluateProperties="true" /> | |||
</pre> | |||
load an XML file into a property, expanding all properties declared | |||
in the file in the process. | |||
<hr> | |||
<p align="center">Copyright © 2001 Apache Software Foundation. All rights | |||
<p align="center">Copyright © 2001-2002 Apache Software Foundation. All rights | |||
Reserved.</p> | |||
</body> | |||
@@ -1,19 +1,19 @@ | |||
<?xml version="1.0" encoding="ISO-8859-1"?> | |||
<project name="loadfile-test" basedir="." default="testLoadAFile"> | |||
<target name="init"> | |||
</target> | |||
<target name="testNoSourcefileDefined" depends="init"> | |||
<loadfile property="foo" /> | |||
</target> | |||
<target name="testNoPropertyDefined" | |||
depends="init"> | |||
<loadfile srcFile="somefile" /> | |||
</target> | |||
<target name="testNoSourcefilefound" | |||
depends="init"> | |||
@@ -22,15 +22,15 @@ | |||
<target name="testFailOnError" | |||
depends="init"> | |||
<loadfile | |||
property="testFailOnError" | |||
srcFile="somefile" | |||
<loadfile | |||
property="testFailOnError" | |||
srcFile="somefile" | |||
failonerror="false"/> | |||
</target> | |||
</target> | |||
<target name="testLoadAFile" | |||
depends="init"> | |||
<echo | |||
<echo | |||
message="What's it going to be then, eh?" | |||
file="loadfile1.tmp" | |||
/> | |||
@@ -44,9 +44,38 @@ | |||
srcFile="loadfile.xml" | |||
encoding="ISO-8859-1"/> | |||
</target> | |||
<target name="cleanup"> | |||
<target name="testEvalProps" | |||
depends="init"> | |||
<property name="weather" value="rain" /> | |||
<echo | |||
message="All these moments will be lost in time, like teardrops in the ${weather}" | |||
file="loadfile1.tmp" | |||
/> | |||
<loadfile property="testEvalProps" | |||
srcFile="loadfile1.tmp" | |||
evaluateProperties="true" | |||
/> | |||
<echo>${testLoadAFile}</echo> | |||
</target> | |||
<target name="testOneLine" | |||
depends="init"> | |||
<echo | |||
message="1, 2, 3, 4" | |||
file="loadfile1.tmp" | |||
/> | |||
<loadfile property="testOneLine" | |||
srcFile="loadfile1.tmp" | |||
makeOneLine="true" | |||
/> | |||
<echo>${testLoadAFile}</echo> | |||
</target> | |||
<target name="cleanup"> | |||
<delete file="loadfile1.tmp"/> | |||
</target> | |||
</project> |
@@ -1,7 +1,7 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
@@ -56,14 +56,10 @@ package org.apache.tools.ant.taskdefs; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.ProjectHelper; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
import java.io.InputStreamReader; | |||
import java.io.IOException; | |||
import java.io.BufferedInputStream; | |||
import java.io.*; | |||
/** | |||
* Load a file into a property | |||
@@ -95,6 +91,16 @@ public class LoadFile extends Task { | |||
private String property = null; | |||
/** flag to control if we flatten the file or no' | |||
* | |||
*/ | |||
private boolean makeOneLine=false; | |||
/** | |||
* flag to control whether props get evaluated or not | |||
*/ | |||
private boolean evaluateProperties=false; | |||
/** | |||
* Encoding to use for filenames, defaults to the platform's default | |||
* encoding. <p> | |||
@@ -140,6 +146,22 @@ public class LoadFile extends Task { | |||
failOnError = fail; | |||
} | |||
/** | |||
* setter to flatten the file to a single line | |||
* @since 1.6 | |||
*/ | |||
public void setMakeOneLine(boolean makeOneLine) { | |||
this.makeOneLine=makeOneLine; | |||
} | |||
/** | |||
* setter to eval properties. | |||
* @since 1.6 | |||
*/ | |||
public void setEvaluateProperties(boolean evaluateProperties) { | |||
this.evaluateProperties=evaluateProperties; | |||
} | |||
/** | |||
* read in a source file to a property | |||
@@ -157,7 +179,7 @@ public class LoadFile extends Task { | |||
} | |||
FileInputStream fis = null; | |||
BufferedInputStream bis = null; | |||
InputStreamReader instream = null; | |||
Reader instream = null; | |||
log("loading "+srcFile+" into property "+property,Project.MSG_VERBOSE); | |||
try { | |||
long len = srcFile.length(); | |||
@@ -179,6 +201,12 @@ public class LoadFile extends Task { | |||
} | |||
instream.read(buffer); | |||
String text = new String(buffer); | |||
if (makeOneLine) { | |||
text=stripLineBreaks(text); | |||
} | |||
if(evaluateProperties) { | |||
text=ProjectHelper.replaceProperties(project,text); | |||
} | |||
project.setNewProperty(property, text); | |||
log("loaded "+buffer.length+" characters",Project.MSG_VERBOSE); | |||
log(property+" := "+text,Project.MSG_DEBUG); | |||
@@ -201,5 +229,28 @@ public class LoadFile extends Task { | |||
} | |||
} | |||
/** | |||
* strip out all line breaks from a string. | |||
* @param source source | |||
* This implementation always duplicates the string; it is nominally possible to probe | |||
* the string first looking for any line breaks before bothering to do a copy. But we assume if | |||
* the option is requested, then line breaks are probably in the source string. | |||
*/ | |||
protected String stripLineBreaks(String source) { | |||
//Linebreaks. What do to on funny IBM mainframes with odd line endings? | |||
String linebreaks="\r\n"; | |||
int len=source.length(); | |||
StringBuffer dest=new StringBuffer(len); | |||
for(int i=0;i<len;++i) { | |||
char ch=source.charAt(i); | |||
if(linebreaks.indexOf(ch)==-1) { | |||
dest.append(ch); | |||
} | |||
} | |||
return new String(dest); | |||
} | |||
//end class | |||
} |
@@ -1,7 +1,7 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
@@ -124,10 +124,7 @@ public class LoadFileTest extends BuildFileTest { | |||
*/ | |||
public void testFailOnError() | |||
throws BuildException { | |||
executeTarget("testFailOnError"); | |||
if(project.getProperty("testFailOnError")!=null) { | |||
fail("property should not have been defined"); | |||
} | |||
expectPropertyUnset("testFailOnError","testFailOnError"); | |||
} | |||
@@ -152,9 +149,27 @@ public class LoadFileTest extends BuildFileTest { | |||
if(project.getProperty("testLoadAFileEnc")==null) { | |||
fail("file load failed"); | |||
} | |||
} | |||
/** | |||
* A unit test for JUnit | |||
*/ | |||
public void testEvalProps() | |||
throws BuildException { | |||
executeTarget("testEvalProps"); | |||
if(project.getProperty("testEvalProps").indexOf("rain")<0) { | |||
fail("property eval broken"); | |||
} | |||
} | |||
/** | |||
* A unit test for JUnit | |||
*/ | |||
public void testOneLine() | |||
throws BuildException { | |||
expectPropertySet("testOneLine","testOneLine","1,2,3,4"); | |||
} | |||
} | |||