using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; namespace Tango.CodeGeneration { /// /// Represents a code object base class. /// /// public abstract class CodeObject : ICodeObject { /// /// Initializes a new instance of the class. /// public CodeObject() { Attributes = new List(); IndentResult = true; } /// /// Gets or sets the code object modifier. /// public CodeObjectModifier Modifier { get; set; } /// /// Gets or sets the attributes associated with this code object. /// public List Attributes { get; set; } /// /// Gets or sets a value indicating whether to indent the resulting code. /// public bool IndentResult { get; set; } /// /// Generates the code represented by this code object. /// /// public virtual string GenerateCode() { var code = Helper.ParseTemplate(GetTemplate(), this); if (IndentResult) { code = Helper.IndentCSharpCode(code); code = RemoveDoubleLines(code); } code = code.Replace(""", "\""); code = HttpUtility.HtmlDecode(code); return code; } /// /// Removes the double lines. /// /// The string. /// private String RemoveDoubleLines(string str) { String result = String.Empty; using (var streamReader = new StringReader(str)) { string line = null; bool previousLineWasBlank = false; while ((line = streamReader.ReadLine()) != null) { if (!string.IsNullOrEmpty(line) || !previousLineWasBlank) { result += line +Environment.NewLine; } previousLineWasBlank = string.IsNullOrEmpty(line); } } return result; } /// /// Gets the template associated with this code object. /// /// public virtual string GetTemplate() { return Helper.GetTemplate(this.GetType()); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return GenerateCode(); } /// /// Gets the modifier string. /// /// public String GetModifierString() { return Modifier.ToString().ToLower(); } /// /// Gets the attributes string. /// /// public String GetAttributesString() { if (Attributes != null && Attributes.Count > 0) { return String.Join(Environment.NewLine, Attributes); } else { return null; } } } }