XmlParserContext.cs 6.4 KB

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