| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- //
- // System.Security.SecurityElement.cs
- //
- // Author:
- // Miguel de Icaza ([email protected])
- //
- // (C) Ximian, Inc. http://www.ximian.com
- using System.Globalization;
- using System.Collections;
- using System.Text;
- namespace System.Security {
- [MonoTODO ("See bottom of the class for missing methods")]
- public sealed class SecurityElement {
- string text;
- string tag;
-
- public SecurityElement (string tag, string text)
- {
- if (tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
- if (text.IndexOfAny (invalid_chars) != -1 ||
- tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
-
- this.tag = tag;
- this.text = text;
- }
- public SecurityElement (string tag)
- {
- if (tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
- this.tag = tag;
- }
- Hashtable attributes;
- public Hashtable Attributes {
- get {
- return attributes;
- }
- set {
- attributes = value;
- }
- }
- ArrayList children;
- public ArrayList Children {
- get {
- return children;
- }
- set {
- if (value != null){
- foreach (object o in children){
- if (o == null)
- throw new ArgumentNullException ();
- }
- }
- children = value;
- }
- }
- public string Tag {
- get {
- return tag;
- }
- set {
- if (value == null)
- throw new ArgumentNullException ();
- if (tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
- tag = value;
- }
- }
- public string Text {
- get {
- return text;
- }
- set {
- if (value != null && (value.IndexOfAny (invalid_chars) != -1))
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
-
- 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 (name.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
- if (value.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
-
- 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 (text != other.text)
- return false;
- if (tag != other.tag)
- return false;
- throw new Exception ("IMPLEMENT ME: Compare attributes and children");
- }
- static char [] invalid_chars = new char [] { '<', '>', '"', '\'', '&' };
-
- 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 IsInvalidAttributeName (string name)
- {
- return name.IndexOfAny (invalid_chars) != -1;
- }
- public static bool IsInvalidAttributeValue (string value)
- {
- return value.IndexOfAny (invalid_chars) != -1;
- }
- public static bool IsInvalidTag (string value)
- {
- return value.IndexOfAny (invalid_chars) != -1;
- }
- public static bool IsInvalidText (string value)
- {
- return value.IndexOfAny (invalid_chars) != -1;
- }
- //
- // TODO:
- //
- // SearchForChildByTag
- // SearchForTextOfTag
- // ToString
- }
- }
|