// NAnt - A .NET build tool
// Copyright (C) 2001-2002 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Gert Driesen (gert.driesen@ardatis.com)
using System.Globalization;
using System.IO;
using System.Text;
using NAnt.Core.Attributes;
using NAnt.Core.Filters;
using NAnt.Core.Util;
namespace NAnt.Core.Tasks {
///
/// Load a text file into a single property.
///
///
///
/// Unless an encoding is specified, the encoding associated with the
/// system's current ANSI code page is used.
///
///
/// An UTF-8, little-endian Unicode, and big-endian Unicode encoded text
/// file is automatically recognized, if the file starts with the appropriate
/// byte order marks.
///
///
///
///
/// Load file message.txt into property "message".
///
///
///
/// ]]>
///
///
///
///
/// Load a file using the "latin-1" encoding.
///
///
///
/// ]]>
///
///
///
///
/// Load a file, replacing all @NOW@ tokens with the current
/// date/time.
///
///
///
///
///
///
///
///
///
/// ]]>
///
///
[TaskName("loadfile")]
public class LoadFileTask : Task {
#region Private Instance Fields
private FileInfo _file;
private Encoding _encoding;
private string _property;
private FilterChain _filterChain;
#endregion Private Instance Fields
#region Public Instance Properties
///
/// The file to load.
///
[TaskAttribute("file", Required=true)]
public FileInfo File {
get { return _file; }
set { _file = value; }
}
///
/// The name of the property to save the content to.
///
[TaskAttribute("property", Required=true)]
public string Property {
get { return _property; }
set { _property = value; }
}
///
/// The encoding to use when loading the file. The default is the encoding
/// associated with the system's current ANSI code page.
///
[TaskAttribute("encoding")]
public Encoding Encoding {
get { return _encoding; }
set { _encoding = value; }
}
///
/// The filterchain definition to use.
///
[BuildElement("filterchain")]
public FilterChain FilterChain {
get { return _filterChain; }
set { _filterChain = value; }
}
#endregion Public Instance Properties
#region Override implementation of Task
protected override void ExecuteTask() {
// make sure file actually exists
if (!File.Exists) {
throw new BuildException(string.Format(CultureInfo.InstalledUICulture,
"File '{0}' does not exist.", File.FullName), Location);
}
string content = null;
try {
// determine character encoding to use
Encoding encoding = (Encoding != null) ? Encoding : Encoding.Default;
// load file
using (StreamReader sr = new StreamReader(File.FullName, encoding, true)) {
if (FilterChain == null || FilterChain.Filters.Count == 0) {
content = sr.ReadToEnd();
} else {
Filter baseFilter = FilterChain.GetBaseFilter(
new PhysicalTextReader(sr));
StringWriter sw = new StringWriter();
while (true) {
int character = baseFilter.Read();
if (character > -1) {
sw.Write((char) character);
} else {
break;
}
}
content = sw.ToString();
}
}
} catch (IOException ex) {
throw new BuildException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1129"),
File.FullName), Location, ex);
}
// add/update property
Properties[Property] = content;
}
#endregion Override implementation of Task
}
}