SecurityParser.cs 3.4 KB

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