a69408a0d9404fdd5ec081286465b06067f5081b
[mono.git] / mcs / class / corlib / Mono.Xml / SecurityParser.cs
1 //
2 // Mono.Xml.SecurityParser.cs class implementation
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Collections;
12 using System.Security;
13
14 namespace Mono.Xml {
15
16         // convert an XML document into SecurityElement objects
17         public class SecurityParser : MiniParser, MiniParser.IHandler, MiniParser.IReader {
18
19                 private SecurityElement root;
20
21                 public SecurityParser () : base () 
22                 {
23                         stack = new Stack ();
24                 }
25
26                 public void LoadXml (string xml) 
27                 {
28                         root = null;
29                         xmldoc = xml;
30                         pos = 0;
31                         stack.Clear ();
32                         Parse (this, this);
33                 }
34
35                 public SecurityElement ToXml () 
36                 {
37                         return root;
38                 }
39
40                 // IReader
41
42                 private string xmldoc;
43                 private int pos;
44
45                 public int Read () 
46                 {
47                         if (pos >= xmldoc.Length)
48                                 return -1;
49                         return (int) xmldoc [pos++];
50                 }
51
52                 // IHandler
53
54                 private SecurityElement current;
55                 private Stack stack;
56
57                 public void OnStartParsing (MiniParser parser) {}
58
59                 public void OnStartElement (string name, MiniParser.IAttrList attrs) 
60                 {
61                         SecurityElement newel = new SecurityElement (name); 
62                         if (root == null) {
63                                 root = newel;
64                                 current = newel;
65                         }
66                         else {
67                                 SecurityElement parent = (SecurityElement) stack.Peek ();
68                                 parent.AddChild (newel);
69                         }
70                         stack.Push (newel);
71                         current = newel;
72                         // attributes
73                         int n = attrs.Length;
74                         for (int i=0; i < n; i++)
75                                 current.AddAttribute (attrs.GetName (i), attrs.GetValue (i));
76                 }
77
78                 public void OnEndElement (string name) 
79                 {
80                         current = (SecurityElement) stack.Pop ();
81                 }
82
83                 public void OnChars (string ch) 
84                 {
85                         current.Text = ch;
86                 }
87
88                 public void OnEndParsing (MiniParser parser) {}
89         }
90 }