Merge pull request #2543 from ermshiperete/Xamarin-31021
[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 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.IO;
36 using System.Security;
37
38 namespace Mono.Xml {
39
40         // convert an XML document into SecurityElement objects
41 #if INSIDE_CORLIB
42         internal
43 #else
44         public
45 #endif
46         class SecurityParser : SmallXmlParser, SmallXmlParser.IContentHandler {
47
48                 private SecurityElement root;
49
50                 public SecurityParser () : base () 
51                 {
52                         stack = new Stack ();
53                 }
54
55                 public void LoadXml (string xml) 
56                 {
57                         root = null;
58                         stack.Clear ();
59                         Parse (new StringReader (xml), this);
60                 }
61
62                 public SecurityElement ToXml () 
63                 {
64                         return root;
65                 }
66
67                 // IContentHandler
68
69                 private SecurityElement current;
70                 private Stack stack;
71
72                 public void OnStartParsing (SmallXmlParser parser) {}
73
74                 public void OnProcessingInstruction (string name, string text) {}
75
76                 public void OnIgnorableWhitespace (string s) {}
77
78                 public void OnStartElement (string name, SmallXmlParser.IAttrList attrs) 
79                 {
80                         SecurityElement newel = new SecurityElement (name); 
81                         if (root == null) {
82                                 root = newel;
83                                 current = newel;
84                         }
85                         else {
86                                 SecurityElement parent = (SecurityElement) stack.Peek ();
87                                 parent.AddChild (newel);
88                         }
89                         stack.Push (newel);
90                         current = newel;
91                         // attributes
92                         int n = attrs.Length;
93                         for (int i=0; i < n; i++)
94                                 current.AddAttribute (attrs.GetName (i), SecurityElement.Escape (attrs.GetValue (i)));
95                 }
96
97                 public void OnEndElement (string name) 
98                 {
99                         current = (SecurityElement) stack.Pop ();
100                 }
101
102                 public void OnChars (string ch) 
103                 {
104                         current.Text = SecurityElement.Escape (ch);
105                 }
106
107                 public void OnEndParsing (SmallXmlParser parser) {}
108         }
109 }