| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- //
- // System.Security.SecurityElement.cs
- //
- // Author:
- // Miguel de Icaza ([email protected])
- // Lawrence Pit ([email protected])
- //
- // (C) Ximian, Inc. http://www.ximian.com
- using System.Globalization;
- using System.Collections;
- using System.Text;
- namespace System.Security
- {
- [Serializable]
- public sealed class SecurityElement
- {
- string text;
- string tag;
- Hashtable attributes;
- ArrayList children;
-
- // these values are determined by a simple test program against the MS.Net implementation:
- // for (int i = 0; i < 256; i++) {
- // if (!SecurityElement.IsValidTag ("" + ((char) i))) {
- // System.Console.WriteLine ("TAG: " + i);
- // }
- // }
- // note: this is actually an incorrect implementation of MS, as for example the &
- // character is not a valid character in tag names.
- private static char [] invalid_tag_chars = new char [] { ' ', '<', '>' };
- private static char [] invalid_text_chars = new char [] { '<', '>' };
- private static char [] invalid_attr_name_chars = new char [] { ' ', '<', '>' };
- private static char [] invalid_attr_value_chars = new char [] { '"', '<', '>' };
- private static char [] invalid_chars = new char [] { '<', '>', '"', '\'', '&' };
-
- public SecurityElement (string tag) : this (tag, null)
- {
- }
-
- public SecurityElement (string tag, string text)
- {
- this.Tag = tag;
- this.Text = (text == null) ? String.Empty : text;
- }
-
- public Hashtable Attributes {
- get {
- if (attributes == null)
- return null;
-
- Hashtable result = new Hashtable ();
- IDictionaryEnumerator e = attributes.GetEnumerator ();
- while (e.MoveNext ())
- result.Add (e.Key, e.Value);
- return result;
- }
- set {
- if (value == null || value.Count == 0) {
- attributes = null;
- return;
- }
-
- Hashtable result = new Hashtable ();
- IDictionaryEnumerator e = value.GetEnumerator ();
- while (e.MoveNext ()) {
- string key = (string) e.Key;
- string val = (string) e.Value;
- if (!IsValidAttributeName (key))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + key);
- if (!IsValidAttributeValue (val))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + key);
- result.Add (key, val);
- }
- attributes = result;
- }
- }
- public ArrayList Children {
- get {
- return children;
- }
- set {
- if (value != null) {
- foreach (object o in value) {
- if (o == null)
- throw new ArgumentNullException ();
- // shouldn't we also throw an exception
- // when o isn't an instance of SecurityElement?
- }
- }
- children = value;
- }
- }
- public string Tag {
- get {
- return tag;
- }
- set {
- if (value == null)
- throw new ArgumentNullException ();
- if (!IsValidTag (value))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + value);
- tag = value;
- }
- }
- public string Text {
- get {
- return text;
- }
- set {
- if (!IsValidText (value))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + text);
- text = value;
- }
- }
- public void AddAttribute (string name, string value)
- {
- if (name == null || value == null)
- throw new ArgumentNullException ();
- if (attributes == null)
- attributes = new Hashtable ();
- //
- // The hashtable will throw ArgumentException if name is already there
- //
- if (!IsValidAttributeName (name))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + name);
- if (!IsValidAttributeValue (value))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + value);
-
- attributes.Add (name, value);
- }
- public void AddChild (SecurityElement child)
- {
- if (child == null)
- throw new ArgumentNullException ();
- if (children == null)
- children = new ArrayList ();
- children.Add (child);
- }
- public string Attribute (string name)
- {
- if (name == null)
- throw new ArgumentNullException ();
- if (attributes != null)
- return (string) attributes [name];
- else
- return null;
- }
- public bool Equal (SecurityElement other)
- {
- if (other == null)
- return false;
-
- if (this == other)
- return true;
- if (this.text != other.text)
- return false;
- if (this.tag != other.tag)
- return false;
- if (this.attributes == null && other.attributes != null && other.attributes.Count != 0)
- return false;
-
- if (other.attributes == null && this.attributes != null && this.attributes.Count != 0)
- return false;
- if (this.attributes != null && other.attributes != null) {
- if (this.attributes.Count != other.attributes.Count)
- return false;
- IDictionaryEnumerator e = attributes.GetEnumerator ();
- while (e.MoveNext ())
- if (other.attributes [e.Key] != e.Value)
- return false;
- }
-
- if (this.children == null && other.children != null && other.children.Count != 0)
- return false;
-
- if (other.children == null && this.children != null && this.children.Count != 0)
- return false;
-
- if (this.children != null && other.children != null) {
- if (this.children.Count != other.children.Count)
- return false;
- for (int i = 0; i < this.children.Count; i++)
- if (!((SecurityElement) this.children [i]).Equal ((SecurityElement) other.children [i]))
- return false;
- }
-
- return true;
- }
- public static string Escape (string str)
- {
- StringBuilder sb;
-
- if (str.IndexOfAny (invalid_chars) == -1)
- return str;
- sb = new StringBuilder ();
- int len = str.Length;
-
- for (int i = 0; i < len; i++) {
- char c = str [i];
- switch (c) {
- case '<': sb.Append ("<"); break;
- case '>': sb.Append (">"); break;
- case '"': sb.Append ("""); break;
- case '\'': sb.Append ("'"); break;
- case '&': sb.Append ("&"); break;
- default: sb.Append (c); break;
- }
- }
- return sb.ToString ();
- }
- public static bool IsValidAttributeName (string name)
- {
- return name != null && name.IndexOfAny (invalid_attr_name_chars) == -1;
- }
- public static bool IsValidAttributeValue (string value)
- {
- return value != null && value.IndexOfAny (invalid_attr_value_chars) == -1;
- }
- public static bool IsValidTag (string value)
- {
- return value != null && value.IndexOfAny (invalid_tag_chars) == -1;
- }
- public static bool IsValidText (string value)
- {
- return value != null && value.IndexOfAny (invalid_text_chars) == -1;
- }
- public SecurityElement SearchForChildByTag (string tag)
- {
- if (tag == null)
- throw new ArgumentNullException ("tag");
-
- if (this.children == null)
- return null;
-
- for (int i = 0; i < children.Count; i++) {
- SecurityElement elem = (SecurityElement) children [i];
- if (elem.tag == tag)
- return elem;
- }
- return null;
- }
- public string SearchForTextOfTag (string tag)
- {
- if (tag == null)
- throw new ArgumentNullException ("tag");
-
- if (this.tag == tag)
- return this.text;
-
- if (this.children == null)
- return null;
-
- for (int i = 0; i < children.Count; i++) {
- string result = ((SecurityElement) children [i]).SearchForTextOfTag (tag);
- if (result != null)
- return result;
- }
- return null;
- }
-
- public override string ToString ()
- {
- StringBuilder s = new StringBuilder ();
- ToXml (ref s, 0);
- return s.ToString ();
- }
-
- private void ToXml(ref StringBuilder s, int level)
- {
- s.Append (' ', level << 2);
- s.Append ("<");
- s.Append (tag);
-
- if (attributes != null) {
- IDictionaryEnumerator e = attributes.GetEnumerator ();
- while (e.MoveNext ()) {
- s.Append (" ")
- .Append (e.Key)
- .Append ("=\"")
- .Append (e.Value)
- .Append ("\"");
- }
- }
-
- if ((text == null || text == String.Empty) &&
- (children == null || children.Count == 0))
- s.Append ("/>");
- else {
- s.Append (">").Append (text);
- if (children != null) {
- foreach (SecurityElement child in children) {
- s.Append (Environment.NewLine);
- child.ToXml (ref s, level + 1);
- }
- }
- s.Append (Environment.NewLine)
- .Append (' ', level << 2)
- .Append ("</")
- .Append (tag)
- .Append (">");
- }
- }
- }
- }
|