2 // System.Security.SecurityElement.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
9 using System.Globalization;
10 using System.Collections;
13 namespace System.Security {
15 [MonoTODO ("See bottom of the class for missing methods")]
17 public sealed class SecurityElement {
21 public SecurityElement (string tag, string text)
23 if (tag.IndexOfAny (invalid_chars) != -1)
24 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
25 if (text.IndexOfAny (invalid_chars) != -1 ||
26 tag.IndexOfAny (invalid_chars) != -1)
27 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
32 public SecurityElement (string tag)
34 if (tag.IndexOfAny (invalid_chars) != -1)
35 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
41 public Hashtable Attributes {
52 public ArrayList Children {
59 foreach (object o in children){
61 throw new ArgumentNullException ();
74 throw new ArgumentNullException ();
75 if (tag.IndexOfAny (invalid_chars) != -1)
76 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
87 if (value != null && (value.IndexOfAny (invalid_chars) != -1))
88 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
94 public void AddAttribute (string name, string value)
96 if (name == null || value == null)
97 throw new ArgumentNullException ();
99 if (attributes == null)
100 attributes = new Hashtable ();
103 // The hashtable will throw ArgumentException if name is already there
106 if (name.IndexOfAny (invalid_chars) != -1)
107 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
109 if (value.IndexOfAny (invalid_chars) != -1)
110 throw new ArgumentException (Locale.GetText ("Invalid XML string"));
112 attributes.Add (name, value);
115 public void AddChild (SecurityElement child)
118 throw new ArgumentNullException ();
120 if (children == null)
121 children = new ArrayList ();
123 children.Add (child);
126 public string Attribute (string name)
129 throw new ArgumentNullException ();
131 if (attributes != null)
132 return (string) attributes [name];
137 public bool Equal (SecurityElement other)
142 if (text != other.text)
145 if (tag != other.tag)
148 throw new Exception ("IMPLEMENT ME: Compare attributes and children");
151 static char [] invalid_chars = new char [] { '<', '>', '"', '\'', '&' };
153 public static string Escape (string str)
157 if (str.IndexOfAny (invalid_chars) == -1)
160 sb = new StringBuilder ();
161 int len = str.Length;
163 for (int i = 0; i < len; i++){
167 case '<': sb.Append ("<"); break;
168 case '>': sb.Append (">"); break;
169 case '"': sb.Append ("""); break;
170 case '\'': sb.Append ("'"); break;
171 case '&': sb.Append ("&"); break;
172 default: sb.Append (c); break;
176 return sb.ToString ();
179 public static bool IsInvalidAttributeName (string name)
181 return name.IndexOfAny (invalid_chars) != -1;
184 public static bool IsInvalidAttributeValue (string value)
186 return value.IndexOfAny (invalid_chars) != -1;
189 public static bool IsInvalidTag (string value)
191 return value.IndexOfAny (invalid_chars) != -1;
194 public static bool IsInvalidText (string value)
196 return value.IndexOfAny (invalid_chars) != -1;
202 // SearchForChildByTag
203 // SearchForTextOfTag