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