diff --git a/docs/manual/CoreTasks/loadfile.html b/docs/manual/CoreTasks/loadfile.html index e57a4a688..f3448c46a 100644 --- a/docs/manual/CoreTasks/loadfile.html +++ b/docs/manual/CoreTasks/loadfile.html @@ -5,50 +5,75 @@ +

LoadFile

Description

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 +

Parameters

- + - + - + - + - + + + + + + + + + + +
Attribute Description Required
srcFile source file Yes
property property to save to Yes
encoding encoding to use when loading the file No
failonerror Whether to halt the build on failure No, default "true"
evaluatePropertiesflag to enable property evalation in the fileNo, default "false"
makeOneLineflag to strip out newlines (but not spaces or + tabs) from the fileNo, default "false"
+

+The makeOneLine 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 +

a
+b
+c
+
would become "abc": you need spaces or +commas at the end/start of line to stop alphanumeric characters +being merged together.

Examples

-
    <loadfile property="mail.recipients"
-      srcFile="recipients.txt" / >
+
    <loadfile property="message"
+      srcFile="message.txt" / >
 
-Load file recipients.txt into property "mail.recipients" +Load file message.txt into property "message"; an <echo> +can print this.
    <loadfile property="encoded-file"
       srcFile="loadfile.xml"
@@ -56,16 +81,35 @@ Load file recipients.txt into property "mail.recipients"
 
Load a file using the latin-1 encoding -
    <loadfile 
-      property="optional.value" 
-      srcFile="optional.txt" 
+
    <loadfile
+      property="optional.value"
+      srcFile="optional.txt"
       failonerror="false" />
 
-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) + +
    <loadfile
+      property="mail.recipients"
+      srcFile="recipientlist.txt"
+      makeOneLine="true" />
+
+ +load a property which can be used as a parameter for another task (in this case mail), +merging lines to ensure this happens. + +
    <loadfile
+      property="system.configuration.xml"
+      srcFile="configuration.xml"
+      evaluateProperties="true" />
+
+ +load an XML file into a property, expanding all properties declared +in the file in the process. +
-

Copyright © 2001 Apache Software Foundation. All rights +

Copyright © 2001-2002 Apache Software Foundation. All rights Reserved.

diff --git a/src/etc/testcases/taskdefs/loadfile.xml b/src/etc/testcases/taskdefs/loadfile.xml index ba772204f..e57c02d07 100644 --- a/src/etc/testcases/taskdefs/loadfile.xml +++ b/src/etc/testcases/taskdefs/loadfile.xml @@ -1,19 +1,19 @@ - + - + - + @@ -22,15 +22,15 @@ - - - + + - @@ -44,9 +44,38 @@ srcFile="loadfile.xml" encoding="ISO-8859-1"/> - - + + + + + + ${testLoadAFile} + + + + + + + ${testLoadAFile} + + + + - + diff --git a/src/main/org/apache/tools/ant/taskdefs/LoadFile.java b/src/main/org/apache/tools/ant/taskdefs/LoadFile.java index 6695921a4..f78b3157b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/LoadFile.java +++ b/src/main/org/apache/tools/ant/taskdefs/LoadFile.java @@ -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.

@@ -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