XmlParserContext.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // System.Xml.XmlParserContext
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2003 Atsushi Enomoto
  10. //
  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.Collections;
  32. using System.Text;
  33. using Mono.Xml;
  34. namespace System.Xml
  35. {
  36. public class XmlParserContext
  37. {
  38. #region Constructors
  39. public XmlParserContext (
  40. XmlNameTable nt,
  41. XmlNamespaceManager nsMgr,
  42. string xmlLang,
  43. XmlSpace xmlSpace) :
  44. this (
  45. nt,
  46. nsMgr,
  47. null,
  48. null,
  49. null,
  50. null,
  51. null,
  52. xmlLang,
  53. xmlSpace,
  54. null
  55. )
  56. {
  57. }
  58. public XmlParserContext (
  59. XmlNameTable nt,
  60. XmlNamespaceManager nsMgr,
  61. string xmlLang,
  62. XmlSpace xmlSpace,
  63. Encoding enc) :
  64. this (
  65. nt,
  66. nsMgr,
  67. null,
  68. null,
  69. null,
  70. null,
  71. null,
  72. xmlLang,
  73. xmlSpace,
  74. enc
  75. )
  76. {
  77. }
  78. public XmlParserContext (
  79. XmlNameTable nt,
  80. XmlNamespaceManager nsMgr,
  81. string docTypeName,
  82. string pubId,
  83. string sysId,
  84. string internalSubset,
  85. string baseURI,
  86. string xmlLang,
  87. XmlSpace xmlSpace) :
  88. this (
  89. nt,
  90. nsMgr,
  91. docTypeName,
  92. pubId,
  93. sysId,
  94. internalSubset,
  95. baseURI,
  96. xmlLang,
  97. xmlSpace,
  98. null
  99. )
  100. {
  101. }
  102. public XmlParserContext (
  103. XmlNameTable nt,
  104. XmlNamespaceManager nsMgr,
  105. string docTypeName,
  106. string pubId,
  107. string sysId,
  108. string internalSubset,
  109. string baseURI,
  110. string xmlLang,
  111. XmlSpace xmlSpace,
  112. Encoding enc)
  113. : this (
  114. nt,
  115. nsMgr,
  116. (docTypeName != null && docTypeName != String.Empty) ?
  117. new XmlTextReader ("", nt).GenerateDTDObjectModel (
  118. docTypeName, pubId, sysId, internalSubset) : null,
  119. baseURI,
  120. xmlLang,
  121. xmlSpace,
  122. enc)
  123. {
  124. }
  125. internal XmlParserContext (XmlNameTable nt,
  126. XmlNamespaceManager nsMgr,
  127. DTDObjectModel dtd,
  128. string baseURI,
  129. string xmlLang,
  130. XmlSpace xmlSpace,
  131. Encoding enc)
  132. {
  133. if (nt == null)
  134. this.nameTable = nsMgr == null ? new NameTable () : nsMgr.NameTable;
  135. else
  136. this.nameTable = nt;
  137. this.namespaceManager = nsMgr != null ? nsMgr : new XmlNamespaceManager (nameTable);
  138. if (dtd != null) {
  139. this.docTypeName = dtd.Name;
  140. this.publicID = dtd.PublicId;
  141. this.systemID = dtd.SystemId;
  142. this.internalSubset = dtd.InternalSubset;
  143. this.dtd = dtd;
  144. }
  145. this.encoding = enc;
  146. baseURIStack = new Stack ();
  147. xmlLangStack = new Stack ();
  148. xmlSpaceStack = new Stack ();
  149. baseURIStack.Push (baseURI != null ? baseURI : String.Empty);
  150. xmlLangStack.Push (xmlLang);
  151. xmlSpaceStack.Push (xmlSpace);
  152. }
  153. #endregion
  154. #region Fields
  155. private string baseURI;
  156. private string docTypeName;
  157. private Encoding encoding;
  158. private string internalSubset;
  159. private XmlNamespaceManager namespaceManager;
  160. private XmlNameTable nameTable;
  161. private string publicID;
  162. private string systemID;
  163. private string xmlLang;
  164. private XmlSpace xmlSpace;
  165. private Stack baseURIStack;
  166. private Stack xmlLangStack;
  167. private Stack xmlSpaceStack;
  168. private DTDObjectModel dtd;
  169. #endregion
  170. #region Properties
  171. public string BaseURI {
  172. get { return baseURI != null ? baseURI : baseURIStack.Peek () as string; }
  173. set { baseURI = value; }
  174. }
  175. public string DocTypeName {
  176. get { return docTypeName != null ? docTypeName : dtd != null ? dtd.Name : null; }
  177. set { docTypeName = value; }
  178. }
  179. internal DTDObjectModel Dtd {
  180. get { return dtd; }
  181. set { dtd = value; }
  182. }
  183. public Encoding Encoding {
  184. get { return encoding; }
  185. set { encoding = value; }
  186. }
  187. public string InternalSubset {
  188. get { return internalSubset != null ? internalSubset : dtd != null ? dtd.InternalSubset : null; }
  189. set { internalSubset = value; }
  190. }
  191. public XmlNamespaceManager NamespaceManager {
  192. get { return namespaceManager; }
  193. set { namespaceManager = value; }
  194. }
  195. public XmlNameTable NameTable {
  196. get { return nameTable; }
  197. set { nameTable = value; }
  198. }
  199. public string PublicId {
  200. get { return publicID != null ? publicID : dtd != null ? dtd.PublicId : null; }
  201. set { publicID = value; }
  202. }
  203. public string SystemId {
  204. get { return systemID != null ? systemID : dtd != null ? dtd.SystemId : null; }
  205. set { systemID = value; }
  206. }
  207. public string XmlLang {
  208. get { return xmlLang != null ? xmlLang : xmlLangStack.Peek () as string; }
  209. set { xmlLang = value; }
  210. }
  211. public XmlSpace XmlSpace {
  212. get { return xmlSpace != XmlSpace.None ? xmlSpace : (XmlSpace) xmlSpaceStack.Peek (); }
  213. set { xmlSpace = value; }
  214. }
  215. #endregion
  216. #region Methods
  217. internal void PushScope ()
  218. {
  219. baseURIStack.Push (BaseURI);
  220. xmlLangStack.Push (XmlLang);
  221. xmlSpaceStack.Push (XmlSpace);
  222. baseURI = null;
  223. xmlLang = null;
  224. xmlSpace = XmlSpace.None;
  225. }
  226. internal void PopScope ()
  227. {
  228. baseURIStack.Pop ();
  229. xmlLangStack.Pop ();
  230. xmlSpaceStack.Pop ();
  231. baseURI = null;
  232. xmlLang = null;
  233. xmlSpace = XmlSpace.None;
  234. }
  235. #endregion
  236. }
  237. }