XmlParserContext.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.IO;
  33. using System.Text;
  34. using Mono.Xml;
  35. #if NET_2_0
  36. using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
  37. #else
  38. using XmlTextReaderImpl = System.Xml.XmlTextReader;
  39. #endif
  40. namespace System.Xml
  41. {
  42. public class XmlParserContext
  43. {
  44. #region Class
  45. class ContextItem
  46. {
  47. public string BaseURI;
  48. public string XmlLang;
  49. public XmlSpace XmlSpace;
  50. }
  51. #endregion
  52. #region Constructors
  53. public XmlParserContext (
  54. XmlNameTable nt,
  55. XmlNamespaceManager nsMgr,
  56. string xmlLang,
  57. XmlSpace xmlSpace) :
  58. this (
  59. nt,
  60. nsMgr,
  61. null,
  62. null,
  63. null,
  64. null,
  65. null,
  66. xmlLang,
  67. xmlSpace,
  68. null
  69. )
  70. {
  71. }
  72. public XmlParserContext (
  73. XmlNameTable nt,
  74. XmlNamespaceManager nsMgr,
  75. string xmlLang,
  76. XmlSpace xmlSpace,
  77. Encoding enc) :
  78. this (
  79. nt,
  80. nsMgr,
  81. null,
  82. null,
  83. null,
  84. null,
  85. null,
  86. xmlLang,
  87. xmlSpace,
  88. enc
  89. )
  90. {
  91. }
  92. public XmlParserContext (
  93. XmlNameTable nt,
  94. XmlNamespaceManager nsMgr,
  95. string docTypeName,
  96. string pubId,
  97. string sysId,
  98. string internalSubset,
  99. string baseURI,
  100. string xmlLang,
  101. XmlSpace xmlSpace) :
  102. this (
  103. nt,
  104. nsMgr,
  105. docTypeName,
  106. pubId,
  107. sysId,
  108. internalSubset,
  109. baseURI,
  110. xmlLang,
  111. xmlSpace,
  112. null
  113. )
  114. {
  115. }
  116. public XmlParserContext (
  117. XmlNameTable nt,
  118. XmlNamespaceManager nsMgr,
  119. string docTypeName,
  120. string pubId,
  121. string sysId,
  122. string internalSubset,
  123. string baseURI,
  124. string xmlLang,
  125. XmlSpace xmlSpace,
  126. Encoding enc)
  127. : this (
  128. nt,
  129. nsMgr,
  130. (docTypeName != null && docTypeName != String.Empty) ?
  131. new XmlTextReaderImpl (TextReader.Null, nt).GenerateDTDObjectModel (
  132. docTypeName, pubId, sysId, internalSubset) : null,
  133. baseURI,
  134. xmlLang,
  135. xmlSpace,
  136. enc)
  137. {
  138. }
  139. internal XmlParserContext (XmlNameTable nt,
  140. XmlNamespaceManager nsMgr,
  141. DTDObjectModel dtd,
  142. string baseURI,
  143. string xmlLang,
  144. XmlSpace xmlSpace,
  145. Encoding enc)
  146. {
  147. this.namespaceManager = nsMgr;
  148. this.nameTable = nt != null ? nt : nsMgr != null ? nsMgr.NameTable : null;
  149. if (dtd != null) {
  150. this.DocTypeName = dtd.Name;
  151. this.PublicId = dtd.PublicId;
  152. this.SystemId = dtd.SystemId;
  153. this.InternalSubset = dtd.InternalSubset;
  154. this.dtd = dtd;
  155. }
  156. this.encoding = enc;
  157. this.BaseURI = baseURI;
  158. this.XmlLang = xmlLang;
  159. this.xmlSpace = xmlSpace;
  160. contextItems = new ArrayList ();
  161. }
  162. #endregion
  163. #region Fields
  164. private string baseURI = String.Empty;
  165. private string docTypeName = String.Empty;
  166. private Encoding encoding;
  167. private string internalSubset = String.Empty;
  168. private XmlNamespaceManager namespaceManager;
  169. private XmlNameTable nameTable;
  170. private string publicID = String.Empty;
  171. private string systemID = String.Empty;
  172. private string xmlLang = String.Empty;
  173. private XmlSpace xmlSpace;
  174. private ArrayList contextItems;
  175. private int contextItemCount;
  176. private DTDObjectModel dtd;
  177. #endregion
  178. #region Properties
  179. public string BaseURI {
  180. get { return baseURI; }
  181. set { baseURI = value != null ? value : String.Empty; }
  182. }
  183. public string DocTypeName {
  184. get { return docTypeName != null ? docTypeName : dtd != null ? dtd.Name : null; }
  185. set { docTypeName = value != null ? value : String.Empty; }
  186. }
  187. internal DTDObjectModel Dtd {
  188. get { return dtd; }
  189. set { dtd = value; }
  190. }
  191. public Encoding Encoding {
  192. get { return encoding; }
  193. set { encoding = value; }
  194. }
  195. public string InternalSubset {
  196. get { return internalSubset != null ? internalSubset : dtd != null ? dtd.InternalSubset : null; }
  197. set { internalSubset = value != null ? value : String.Empty; }
  198. }
  199. public XmlNamespaceManager NamespaceManager {
  200. get { return namespaceManager; }
  201. set { namespaceManager = value; }
  202. }
  203. public XmlNameTable NameTable {
  204. get { return nameTable; }
  205. set { nameTable = value; }
  206. }
  207. public string PublicId {
  208. get { return publicID != null ? publicID : dtd != null ? dtd.PublicId : null; }
  209. set { publicID = value != null ? value : String.Empty; }
  210. }
  211. public string SystemId {
  212. get { return systemID != null ? systemID : dtd != null ? dtd.SystemId : null; }
  213. set { systemID = value != null ? value : String.Empty; }
  214. }
  215. public string XmlLang {
  216. get { return xmlLang; }
  217. set { xmlLang = value != null ? value : String.Empty; }
  218. }
  219. public XmlSpace XmlSpace {
  220. get { return xmlSpace; }
  221. set { xmlSpace = value; }
  222. }
  223. #endregion
  224. #region Methods
  225. internal void PushScope ()
  226. {
  227. ContextItem item = null;
  228. if (contextItems.Count == contextItemCount) {
  229. item = new ContextItem ();
  230. contextItems.Add (item);
  231. }
  232. else
  233. item = (ContextItem) contextItems [contextItemCount];
  234. item.BaseURI = BaseURI;
  235. item.XmlLang = XmlLang;
  236. item.XmlSpace = XmlSpace;
  237. contextItemCount++;
  238. }
  239. internal void PopScope ()
  240. {
  241. if (contextItemCount == 0)
  242. throw new XmlException ("Unexpected end of element scope.");
  243. contextItemCount--;
  244. ContextItem prev = (ContextItem) contextItems [contextItemCount];
  245. baseURI = prev.BaseURI;
  246. xmlLang = prev.XmlLang;
  247. xmlSpace = prev.XmlSpace;
  248. }
  249. #endregion
  250. }
  251. }