6c0d96591259f0523960fc109e97ad5041778721
[mono.git] / mcs / class / Mono.Security / 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         [CLSCompliant(false)]
18 #if INSIDE_CORLIB
19         internal
20 #else
21         public
22 #endif
23         class SecurityParser : MiniParser, MiniParser.IHandler, MiniParser.IReader {
24
25                 private SecurityElement root;
26
27                 public SecurityParser () : base () 
28                 {
29                         stack = new Stack ();
30                 }
31
32                 public void LoadXml (string xml) 
33                 {
34                         root = null;
35                         xmldoc = xml;
36                         pos = 0;
37                         stack.Clear ();
38                         Parse (this, this);
39                 }
40
41                 public SecurityElement ToXml () 
42                 {
43                         return root;
44                 }
45
46                 // IReader
47
48                 private string xmldoc;
49                 private int pos;
50
51                 public int Read () 
52                 {
53                         if (pos >= xmldoc.Length)
54                                 return -1;
55                         return (int) xmldoc [pos++];
56                 }
57
58                 // IHandler
59
60                 private SecurityElement current;
61                 private Stack stack;
62
63                 public void OnStartParsing (MiniParser parser) {}
64
65                 public void OnStartElement (string name, MiniParser.IAttrList attrs) 
66                 {
67                         SecurityElement newel = new SecurityElement (name); 
68                         if (root == null) {
69                                 root = newel;
70                                 current = newel;
71                         }
72                         else {
73                                 SecurityElement parent = (SecurityElement) stack.Peek ();
74                                 parent.AddChild (newel);
75                         }
76                         stack.Push (newel);
77                         current = newel;
78                         // attributes
79                         int n = attrs.Length;
80                         for (int i=0; i < n; i++)
81                                 current.AddAttribute (attrs.GetName (i), attrs.GetValue (i));
82                 }
83
84                 public void OnEndElement (string name) 
85                 {
86                         current = (SecurityElement) stack.Pop ();
87                 }
88
89                 public void OnChars (string ch) 
90                 {
91                         current.Text = ch;
92                 }
93
94                 public void OnEndParsing (MiniParser parser) {}
95         }
96 }