Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SecurityElement.cs « System.Security « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4aeb033328bebde03f448b0395270ffc15dff980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// System.Security.SecurityElement.cs
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//   Lawrence Pit (loz@cable.a2000.nl)
//
// (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 ("&lt;"); break;
				case '>':  sb.Append ("&gt;"); break;
				case '"':  sb.Append ("&quot;"); break;
				case '\'': sb.Append ("&apos;"); break;
				case '&':  sb.Append ("&amp;"); 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 (">");
			}
		}
	}
}