XmlParserContext.cs 5.9 KB

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