using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
namespace LateBindingApi.CodeGenerator.ComponentAnalyzer
{
///
/// offers various helper functions
///
internal static class Utils
{
#region Ressource Methods
///
/// Read RessourceText
///
///
///
internal static string ReadTextFileFromRessource(string ressourcePath)
{
System.IO.Stream ressourceStream = null;
System.IO.StreamReader textStreamReader = null;
try
{
string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
ressourcePath = assemblyName + "." + ressourcePath;
ressourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourcePath);
if (ressourceStream == null)
throw (new System.IO.IOException("Error accessing resource Stream."));
textStreamReader = new System.IO.StreamReader(ressourceStream);
if (textStreamReader == null)
throw (new System.IO.IOException("Error accessing resource File."));
string text = textStreamReader.ReadToEnd();
return text;
}
catch (Exception exception)
{
throw (exception);
}
finally
{
if(null!=textStreamReader)
textStreamReader.Close();
if (null != ressourceStream)
ressourceStream.Close();
}
}
#endregion
#region Xml Methods
///
/// Gets friendly inner xml from node
///
///
///
public static String GetFormattedXml(XmlNode node)
{
string result = "";
MemoryStream memStream = new MemoryStream();
XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.UTF8);
textWriter.Formatting = Formatting.Indented;
node.WriteContentTo(textWriter);
textWriter.Flush();
memStream.Flush();
memStream.Position = 0;
StreamReader sReader = new StreamReader(memStream);
String FormattedXML = sReader.ReadToEnd();
result = FormattedXML;
memStream.Close();
textWriter.Close();
return result;
}
#endregion
#region Guid Methods
///
/// returns a new xml encoded guid as string
///
///
public static string NewEncodedGuid()
{
return XmlConvert.EncodeName(Guid.NewGuid().ToString());
}
///
/// xml encode guid and remove the chars"{}" if exists
///
///
///
public static string EncodeGuid(string guid)
{
return XmlConvert.EncodeName(guid.Replace("{", "").Replace("}", ""));
}
#endregion
#region IO Methods
///
/// Removes bad chars from filePath
///
///
///
public static string RemoveBadChars(string filePath)
{
if (filePath == null)
return "";
string validateFilePath = filePath.Replace("\0", "");
char[] badChars = System.IO.Path.GetInvalidPathChars();
char replaceChar = new char();
foreach (char item in badChars)
validateFilePath = validateFilePath.Replace(item, replaceChar);
return validateFilePath;
}
#endregion
}
}