SecurityElement.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // System.Security.SecurityElement.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. using System.Globalization;
  9. using System.Collections;
  10. using System.Text;
  11. namespace System.Security {
  12. [MonoTODO ("See bottom of the class for missing methods")]
  13. public sealed class SecurityElement {
  14. string text;
  15. string tag;
  16. public SecurityElement (string tag, string text)
  17. {
  18. if (tag.IndexOfAny (invalid_chars) != -1)
  19. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  20. if (text.IndexOfAny (invalid_chars) != -1 ||
  21. tag.IndexOfAny (invalid_chars) != -1)
  22. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  23. this.tag = tag;
  24. this.text = text;
  25. }
  26. public SecurityElement (string tag)
  27. {
  28. if (tag.IndexOfAny (invalid_chars) != -1)
  29. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  30. this.tag = tag;
  31. }
  32. Hashtable attributes;
  33. public Hashtable Attributes {
  34. get {
  35. return attributes;
  36. }
  37. set {
  38. attributes = value;
  39. }
  40. }
  41. ArrayList children;
  42. public ArrayList Children {
  43. get {
  44. return children;
  45. }
  46. set {
  47. if (value != null){
  48. foreach (object o in children){
  49. if (o == null)
  50. throw new ArgumentNullException ();
  51. }
  52. }
  53. children = value;
  54. }
  55. }
  56. public string Tag {
  57. get {
  58. return tag;
  59. }
  60. set {
  61. if (value == null)
  62. throw new ArgumentNullException ();
  63. if (tag.IndexOfAny (invalid_chars) != -1)
  64. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  65. tag = value;
  66. }
  67. }
  68. public string Text {
  69. get {
  70. return text;
  71. }
  72. set {
  73. if (value != null && (value.IndexOfAny (invalid_chars) != -1))
  74. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  75. text = value;
  76. }
  77. }
  78. public void AddAttribute (string name, string value)
  79. {
  80. if (name == null || value == null)
  81. throw new ArgumentNullException ();
  82. if (attributes == null)
  83. attributes = new Hashtable ();
  84. //
  85. // The hashtable will throw ArgumentException if name is already there
  86. //
  87. if (name.IndexOfAny (invalid_chars) != -1)
  88. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  89. if (value.IndexOfAny (invalid_chars) != -1)
  90. throw new ArgumentException (Locale.GetText ("Invalid XML string"));
  91. attributes.Add (name, value);
  92. }
  93. public void AddChild (SecurityElement child)
  94. {
  95. if (child == null)
  96. throw new ArgumentNullException ();
  97. if (children == null)
  98. children = new ArrayList ();
  99. children.Add (child);
  100. }
  101. public string Attribute (string name)
  102. {
  103. if (name == null)
  104. throw new ArgumentNullException ();
  105. if (attributes != null)
  106. return (string) attributes [name];
  107. else
  108. return null;
  109. }
  110. public bool Equal (SecurityElement other)
  111. {
  112. if (other == null)
  113. return false;
  114. if (text != other.text)
  115. return false;
  116. if (tag != other.tag)
  117. return false;
  118. throw new Exception ("IMPLEMENT ME: Compare attributes and children");
  119. }
  120. static char [] invalid_chars = new char [] { '<', '>', '"', '\'', '&' };
  121. public static string Escape (string str)
  122. {
  123. StringBuilder sb;
  124. if (str.IndexOfAny (invalid_chars) == -1)
  125. return str;
  126. sb = new StringBuilder ();
  127. int len = str.Length;
  128. for (int i = 0; i < len; i++){
  129. char c = str [i];
  130. switch (c){
  131. case '<': sb.Append ("&lt;"); break;
  132. case '>': sb.Append ("&gt;"); break;
  133. case '"': sb.Append ("&quot;"); break;
  134. case '\'': sb.Append ("&apos;"); break;
  135. case '&': sb.Append ("&amp;"); break;
  136. default: sb.Append (c); break;
  137. }
  138. }
  139. return sb.ToString ();
  140. }
  141. public static bool IsInvalidAttributeName (string name)
  142. {
  143. return name.IndexOfAny (invalid_chars) != -1;
  144. }
  145. public static bool IsInvalidAttributeValue (string value)
  146. {
  147. return value.IndexOfAny (invalid_chars) != -1;
  148. }
  149. public static bool IsInvalidTag (string value)
  150. {
  151. return value.IndexOfAny (invalid_chars) != -1;
  152. }
  153. public static bool IsInvalidText (string value)
  154. {
  155. return value.IndexOfAny (invalid_chars) != -1;
  156. }
  157. //
  158. // TODO:
  159. //
  160. // SearchForChildByTag
  161. // SearchForTextOfTag
  162. // ToString
  163. }
  164. }