using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Everest.Library;
namespace Everest.Library.IO{
/// <summary>
///
/// </summary>
public static class ContentDocument
{
private static IList<IDocumentReader> documentReaders = new List<IDocumentReader>();
/// <summary>
/// Initializes the <see cref="Document"/> class.
/// </summary>
static ContentDocument()
{
var readers = UnityManager.ResolveAll<IDocumentReader>();
foreach (var reader in readers)
{
if (reader.SupportedExtensions == null || reader.SupportedExtensions.Count == 0)
{
throw new ArgumentException(string.Format(Resources.DocumentReaderIsInvalid, reader.GetType()), "SupportedExtensions");
}
foreach (var extension in reader.SupportedExtensions)
{
documentReaders.Add(reader);
}
}
}
/// <summary>
/// Reads as text.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns></returns>
public static string ReadAsText(string filePath)
{
if (!File.Exists(filePath))
{
return string.Empty;
}
var extension = Path.GetExtension(filePath);
string content = string.Empty;
foreach (var reader in documentReaders)
{
if (reader.SupportedExtensions.Contains(extension) || reader.SupportedExtensions.Contains("*.*"))
{
var readed = reader.ReadAsText(filePath, out content);
if (readed)
{
break;
}
}
}
return content;
}
}
}
|