PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/Utilities/HtmlBuilderElement.cs

https://bitbucket.org/VahidN/interlace
C# | 127 lines | 77 code | 25 blank | 25 comment | 6 complexity | 03a24e78bbbdba18f405d5b63ae61fdd MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Xml;
  32. #endregion
  33. namespace Interlace.Utilities
  34. {
  35. public class HtmlBuilderElement
  36. {
  37. string _name;
  38. string _text;
  39. Dictionary<string, string> _values;
  40. List<HtmlBuilderElement> _elements;
  41. public HtmlBuilderElement(string name)
  42. {
  43. _name = name;
  44. _values = new Dictionary<string, string>();
  45. _elements = new List<HtmlBuilderElement>();
  46. }
  47. public HtmlBuilderElement AddElement(string name)
  48. {
  49. HtmlBuilderElement element = new HtmlBuilderElement(name);
  50. _elements.Add(element);
  51. return element;
  52. }
  53. public void Add(string name, object value)
  54. {
  55. if (value == null) return;
  56. string stringValue = string.Format("{0}", value);
  57. if (string.IsNullOrEmpty(stringValue)) return;
  58. _values[name] = stringValue;
  59. }
  60. public void AddProperties(object obj, params string[] propertyNames)
  61. {
  62. Type objectType = obj.GetType();
  63. foreach (string propertyName in propertyNames)
  64. {
  65. PropertyInfo property = objectType.GetProperty(propertyName);
  66. if (property == null)
  67. {
  68. throw new ArgumentException(string.Format(
  69. "The property \"{0}\" is not available on the object of type \"{1}\".",
  70. propertyName, objectType.Name));
  71. }
  72. object value = property.GetValue(obj, null);
  73. Add(propertyName, string.Format("{0}", value));
  74. }
  75. }
  76. internal XmlElement Render(XmlDocument document)
  77. {
  78. XmlElement element = document.CreateElement(_name);
  79. foreach (KeyValuePair<string, string> pair in _values)
  80. {
  81. XmlElement valueElement = document.CreateElement(pair.Key);
  82. valueElement.InnerText = pair.Value;
  83. element.AppendChild(valueElement);
  84. }
  85. foreach (HtmlBuilderElement builder in _elements)
  86. {
  87. XmlElement subElement = builder.Render(document);
  88. element.AppendChild(subElement);
  89. }
  90. if (!string.IsNullOrEmpty(_text))
  91. {
  92. element.InnerText = Text;
  93. }
  94. return element;
  95. }
  96. public string Text
  97. {
  98. get { return _text; }
  99. set { _text = value; }
  100. }
  101. }
  102. }