Advertisement
Guest User

examOOP

a guest
Mar 22nd, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.CodeDom.Compiler;
  5. using Microsoft.CSharp;
  6. using System.Reflection;
  7. using System.Collections.Generic;
  8.  
  9. namespace HTMLRenderer
  10. {
  11.     public interface IElement
  12.     {
  13.         string Name { get; }
  14.         string TextContent { get; set; }
  15.         IEnumerable<IElement> ChildElements { get; }
  16.         void AddElement(IElement element);
  17.         void Render(StringBuilder output);
  18.         string ToString();
  19.     }
  20.  
  21.     public interface ITable : IElement
  22.     {
  23.         int Rows { get; }
  24.         int Cols { get; }
  25.         IElement this[int row, int col] { get; set; }
  26.     }
  27.  
  28.     public interface IElementFactory
  29.     {
  30.         IElement CreateElement(string name);
  31.         IElement CreateElement(string name, string content);
  32.         ITable CreateTable(int rows, int cols);
  33.     }
  34.  
  35.     public class HTMLElementFactory : IElementFactory
  36.     {
  37.         public IElement CreateElement(string name)
  38.         {
  39.             return new HTMLElement(name);
  40.         }
  41.  
  42.         public IElement CreateElement(string name, string content)
  43.         {
  44.             return new HTMLElement(name, content);
  45.         }
  46.  
  47.         public ITable CreateTable(int rows, int cols)
  48.         {
  49.             return new HTMLTable(rows, cols);
  50.         }
  51.     }
  52.  
  53.     public abstract class Element : IElement
  54.     {
  55.         private string name;
  56.         private string textContent;
  57.         protected ICollection<IElement> childElements;
  58.  
  59.         public Element(string name, string textContent)
  60.         {
  61.             this.name = name;
  62.             this.textContent = textContent;
  63.         }
  64.  
  65.         public string Name
  66.         {
  67.             get { return this.name; }
  68.             private set
  69.             {
  70.                 if (!string.IsNullOrWhiteSpace(value))
  71.                     this.name = value;
  72.                 else
  73.                     this.name = null;
  74.             }
  75.         }
  76.  
  77.         public virtual string TextContent
  78.         {
  79.             get
  80.             {
  81.                 return this.textContent;
  82.             }
  83.             set
  84.             {
  85.                 if (!string.IsNullOrWhiteSpace(value))
  86.                     this.textContent = value;
  87.                 else
  88.                     this.textContent = null;
  89.             }
  90.         }
  91.  
  92.         public IEnumerable<IElement> ChildElements
  93.         {
  94.             get { return new List<IElement>(childElements); }
  95.         }
  96.  
  97.         public abstract void AddElement(IElement element);
  98.  
  99.         public void Render(StringBuilder output)
  100.         {
  101.             output.Append(this.ToString());
  102.  
  103.             Console.WriteLine(output.ToString());
  104.         }
  105.     }
  106.  
  107.     public class HTMLElement : Element
  108.     {
  109.         public HTMLElement(string name, string content)
  110.             : base(name, content)
  111.         {
  112.             base.childElements = new List<IElement>();
  113.         }
  114.  
  115.         public HTMLElement(string name)
  116.             : this(name, null)
  117.         {
  118.         }
  119.  
  120.         public override void AddElement(IElement element)
  121.         {
  122.             base.childElements.Add(element);
  123.         }
  124.  
  125.         public override string ToString()
  126.         {
  127.             StringBuilder output = new StringBuilder();
  128.  
  129.             if (!string.IsNullOrWhiteSpace(base.Name))
  130.             {
  131.                 output.Append(string.Format("<{0}>", base.Name));
  132.             }
  133.  
  134.             if (!string.IsNullOrWhiteSpace(base.TextContent))
  135.             {
  136.                 StringBuilder escapedContent = new StringBuilder();
  137.  
  138.                 foreach (char ch in base.TextContent)
  139.                 {
  140.                     if (ch == '<')
  141.                         escapedContent.Append("&lt;");
  142.                     else if (ch == '>')
  143.                         escapedContent.Append("&gt;");
  144.                     else
  145.                         escapedContent.Append(ch);
  146.                 }
  147.  
  148.                 output.Append(escapedContent.ToString());
  149.             }
  150.  
  151.             foreach (IElement element in base.ChildElements)
  152.             {
  153.                 output.Append(element.ToString());
  154.             }
  155.  
  156.             if (!string.IsNullOrWhiteSpace(base.Name))
  157.             {
  158.                 output.Append(string.Format("</{0}>", base.Name));
  159.             }
  160.  
  161.             return output.ToString();
  162.         }
  163.     }
  164.  
  165.     public class HTMLTable : Element, ITable
  166.     {
  167.         private int rows;
  168.         private int cols;
  169.         private IElement[,] matrix;
  170.  
  171.         public HTMLTable(int rows, int cols)
  172.             : base("table", null)
  173.         {
  174.             this.Rows = rows;
  175.             this.Cols = cols;
  176.             this.matrix = new IElement[this.Rows, this.Cols];
  177.         }
  178.  
  179.         public int Rows
  180.         {
  181.             get { return this.rows; }
  182.             private set
  183.             {
  184.                 if (value < 0)
  185.                     throw new ArgumentException("The rows of a table cannot be negative;");
  186.  
  187.                 this.rows = value;
  188.             }
  189.         }
  190.  
  191.         public int Cols
  192.         {
  193.             get { return this.cols; }
  194.             private set
  195.             {
  196.                 if (value < 0)
  197.                     throw new ArgumentException("The cols of a table cannot be negative;");
  198.  
  199.                 this.cols = value;
  200.             }
  201.         }
  202.  
  203.         public override string TextContent
  204.         {
  205.             get
  206.             { throw new ApplicationException("The table doesn't have text contnet");}
  207.             set
  208.             { throw new ApplicationException("The table doesn't have text contnet");}
  209.         }
  210.  
  211.         public IElement this[int row, int col]
  212.         {
  213.             get
  214.             {
  215.                 if (row < 0 || row >= this.Rows)
  216.                     throw new ArgumentException("The row you requested is outside the bounders of the table");
  217.  
  218.                 if (col < 0 || col >= this.Cols)
  219.                     throw new ArgumentException("The col you requested is outside the bounders of the table");
  220.  
  221.                 return this.matrix[row, col];
  222.             }
  223.             set
  224.             {
  225.                 if (row < 0 || row >= this.Rows)
  226.                     throw new ArgumentException("The row you requested to set is outside the bounders of the table");
  227.  
  228.                 if (col < 0 || col >= this.Cols)
  229.                     throw new ArgumentException("The col you requested to set is outside the bounders of the table");
  230.  
  231.                 this.matrix[row, col] = value;
  232.             }
  233.         }
  234.  
  235.         public override void AddElement(IElement element)
  236.         {
  237.             throw new ApplicationException("The tables cannot have child elements");
  238.         }
  239.  
  240.         public override string ToString()
  241.         {
  242.             StringBuilder output = new StringBuilder();
  243.  
  244.             output.Append("<table>");
  245.             for (int i = 0; i < this.Rows; i++)
  246.             {
  247.                 output.Append("<tr>");
  248.  
  249.                 for (int j = 0; j < this.Cols; j++)
  250.                 {
  251.                     output.Append("<td>");
  252.  
  253.                     if (this[i, j] != null)
  254.                         output.Append(this[i, j].ToString());
  255.  
  256.                     output.Append("</td>");
  257.                 }
  258.                 output.Append("</tr>");
  259.             }
  260.             output.Append("</table>");
  261.  
  262.             return output.ToString();
  263.         }
  264.     }
  265.  
  266.     public class HTMLRendererCommandExecutor
  267.     {
  268.         static void Main()
  269.         {
  270.             string csharpCode = ReadInputCSharpCode();
  271.             CompileAndRun(csharpCode);
  272.         }
  273.  
  274.         private static string ReadInputCSharpCode()
  275.         {
  276.             StringBuilder result = new StringBuilder();
  277.             string line;
  278.             while ((line = Console.ReadLine()) != "")
  279.             {
  280.                 result.AppendLine(line);
  281.             }
  282.             return result.ToString();
  283.         }
  284.  
  285.         static void CompileAndRun(string csharpCode)
  286.         {
  287.             // Prepare a C# program for compilation
  288.             string[] csharpClass =
  289.             {
  290.                 @"using System;
  291.                  using HTMLRenderer;
  292.  
  293.                  public class RuntimeCompiledClass
  294.                  {
  295.                     public static void Main()
  296.                     {"
  297.                         + csharpCode + @"
  298.                     }
  299.                  }"
  300.             };
  301.  
  302.             // Compile the C# program
  303.             CompilerParameters compilerParams = new CompilerParameters();
  304.             compilerParams.GenerateInMemory = true;
  305.             compilerParams.TempFiles = new TempFileCollection(".");
  306.             compilerParams.ReferencedAssemblies.Add("System.dll");
  307.             compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
  308.             CSharpCodeProvider csharpProvider = new CSharpCodeProvider();
  309.             CompilerResults compile = csharpProvider.CompileAssemblyFromSource(
  310.                 compilerParams, csharpClass);
  311.  
  312.             // Check for compilation errors
  313.             if (compile.Errors.HasErrors)
  314.             {
  315.                 string errorMsg = "Compilation error: ";
  316.                 foreach (CompilerError ce in compile.Errors)
  317.                 {
  318.                     errorMsg += "\r\n" + ce.ToString();
  319.                 }
  320.                 throw new Exception(errorMsg);
  321.             }
  322.  
  323.             // Invoke the Main() method of the compiled class
  324.             Assembly assembly = compile.CompiledAssembly;
  325.             Module module = assembly.GetModules()[0];
  326.             Type type = module.GetType("RuntimeCompiledClass");
  327.             MethodInfo methInfo = type.GetMethod("Main");
  328.             methInfo.Invoke(null, null);
  329.         }
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement