using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.IO;
using System.Reflection;
namespace Xsd2Code.Library.Helpers
{
///
/// Common utility methods
///
///
/// Revision history:
///
/// Created 2009-02-20 by Ruslan Urban
/// Moved some commonly used methods into this class and added new methods
///
///
public static class Utility
{
///
/// Transform string to GenerationLanguage
///
/// language string
/// GenerationLanguage object
public static GenerationLanguage GetGenerationLanguage(string input)
{
if (string.IsNullOrEmpty(input)) return default(GenerationLanguage);
switch (input.ToLower())
{
case "cs":
case "c#":
case "csharp":
case "visualcsharp":
return GenerationLanguage.CSharp;
case "c":
case "cpp":
case "c+":
case "c++":
case "cplus":
case "cplusplus":
case "visualc":
case "visualcplus":
case "visualcplusplus":
return GenerationLanguage.VisualCpp;
case "vb":
return GenerationLanguage.VisualBasic;
case "bas":
case "visualbasic":
return GenerationLanguage.VisualCpp;
}
return ToEnum(input);
}
///
/// Convert user input to target platform enumeration
///
/// User input
///
public static TargetFramework GetTargetPlatform(string input)
{
if (string.IsNullOrEmpty(input)) return default(TargetFramework);
switch (input.ToLower())
{
case "sl":
case "silverlight":
case "silverlight2":
case "silverlight20":
return TargetFramework.Silverlight;
case "2":
case "20":
case "2.0":
case "net2":
case "net20":
case "net2.0":
case "dotnet2":
case "dotnet20":
case "dotnet2.0":
return TargetFramework.Net35;
case "3":
case "30":
case "3.0":
case "net3":
case "net30":
case "net3.0":
case "dotnet3":
case "dotnet30":
case "dotnet3.0":
return TargetFramework.Net30;
case "35":
case "3.5":
case "net35":
case "net3.5":
case "dotnet35":
case "dotnet3.5":
return TargetFramework.Net35;
}
return ToEnum(input);
}
///
/// Get generation language
///
/// Code DOM provider
///
static private GenerationLanguage GetGenerationLanguage(CodeDomProvider provider)
{
return GetGenerationLanguage(provider.FileExtension.Replace(".", ""));
}
///
/// Get output file path
///
/// Input file path
///
///
public static string GetOutputFilePath(string xsdFilePath, CodeDomProvider provider)
{
/* DCM REMOVED: CodeDom Provider has FileExtension
var language = GetGenerationLanguage(provider);
return GetOutputFilePath(xsdFilePath, language);
*/
return Path.ChangeExtension(xsdFilePath, ".designer." + provider.FileExtension);
}
///
/// Get output file path
///
/// Input file path
/// Generation language
/// Generated file output path
public static string GetOutputFilePath(string xsdFilePath, GenerationLanguage language)
{
return GetOutputFilePath(xsdFilePath, CodeDomProviderFactory.GetProvider(language));
/* DCM REMOVED CodeDom Provider has FileExtension
switch (language)
{
case GenerationLanguage.VisualBasic:
return Path.ChangeExtension(xsdFilePath, ".Designer.vb");
case GenerationLanguage.VisualCpp:
return Path.ChangeExtension(xsdFilePath, ".Designer.cpp");
default:
return Path.ChangeExtension(xsdFilePath, ".Designer.cs");
}
*/
}
///
/// Gets the language extension based on the Passed language.
///
/// The language.
///
public static string GetLanguageExtension(GenerationLanguage language)
{
return CodeDomProviderFactory.GetProvider(language).FileExtension;
}
///
/// Convert string enumeration value name to actual enumeration value
///
/// Enumeration type
/// Name of the enumeration value
/// Enumeration value of type
public static T ToEnum(string name)
{
return ToEnum(name, true);
}
///
/// Convert string enumeration value name to actual enumeration value
///
/// Enumeration type
/// Name of the enumeration value
/// Flag that indicates that default enum value should be returned if value cannot be parsed
/// Enumeration value of type
public static T ToEnum(string name, bool returnDefault)
{
if (!Enum.IsDefined(typeof (T), name))
{
if (returnDefault) return default(T);
throw new ArgumentOutOfRangeException(string.Format("{0}...{1}", name, typeof (T)));
}
return (T) Enum.Parse(typeof (T), name, false);
}
///
/// Parse enum
///
/// Enumeration type
/// Name of the enumeration value
/// Default return value if cannot be parsed
/// Enumeration value of type
public static T ToEnum(string name, T defaultValue)
{
if (!Enum.IsDefined(typeof (T), name)) return defaultValue;
try
{
return (T) Enum.Parse(typeof (T), name, false);
}
catch
{
return defaultValue;
}
}
/// Get Enum description tag
/// Enumeration value
/// Value of the [Description] attribute, or string value of the Enum field
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
var attributes =
(DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
///
/// String to boolean static method
///
/// string to transform
/// if string is null use this default
/// booean result
public static bool ToBoolean(string p, bool defaultIfNull)
{
if (string.IsNullOrEmpty(p)) return defaultIfNull;
switch (p.Substring(0, 1).ToUpper())
{
case "T": // True in True|False (Most programming languages)
case "1": // 1 in 1|0 (XML)
case "Y": // Yes in Yes|No (Other cases)
return true;
}
return false;
}
///
/// String to boolean static method
///
/// string to transform
/// booean result
public static bool ToBoolean(string p)
{
return ToBoolean(p, false);
}
}
}