XQueryContext.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // XQueryContext.cs - XQuery/XPath2 dynamic context
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Globalization;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Xml.XPath;
  37. using System.Xml.Query;
  38. namespace Mono.Xml.XPath2
  39. {
  40. internal class XQueryContextManager
  41. {
  42. XQueryStaticContext staticContext;
  43. // Fixed dynamic context during evaluation
  44. XmlArgumentList args;
  45. XmlResolver extDocResolver;
  46. Stack<XQueryContext> contextStack = new Stack<XQueryContext> ();
  47. XQueryContext currentContext;
  48. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  49. #else
  50. Stack<XPathSequence> contextSequenceStack = new Stack<XPathSequence> ();
  51. #endif
  52. XmlWriter currentWriter;
  53. XPathItem input; // source input item(node)
  54. XPathSequence currentSequence;
  55. XmlNamespaceManager namespaceManager;
  56. Hashtable localCollationCache = new Hashtable ();
  57. internal XQueryContextManager (XQueryStaticContext ctx, XPathItem input, XmlWriter writer, XmlResolver resolver, XmlArgumentList args)
  58. {
  59. this.input = input;
  60. this.staticContext = ctx;
  61. this.args = args;
  62. currentWriter = writer;
  63. this.extDocResolver = resolver;
  64. namespaceManager = new XmlNamespaceManager (ctx.NameTable);
  65. foreach (DictionaryEntry de in ctx.NSResolver.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml))
  66. namespaceManager.AddNamespace (de.Key.ToString (), de.Value.ToString ());
  67. namespaceManager.PushScope ();
  68. if (input != null) {
  69. currentSequence = new SingleItemIterator (input, currentContext);
  70. currentSequence.MoveNext ();
  71. }
  72. currentContext = new XQueryContext (this, currentSequence);
  73. }
  74. public bool Initialized {
  75. get { return currentContext != null; }
  76. }
  77. public XmlResolver ExtDocResolver {
  78. get { return extDocResolver; }
  79. }
  80. public XmlArgumentList Arguments {
  81. get { return args; }
  82. }
  83. public XmlWriter Writer {
  84. get { return currentWriter; }
  85. // FIXME: might be better avoid setter as public
  86. set { currentWriter = value; }
  87. }
  88. internal XQueryContext CurrentContext {
  89. get { return currentContext; }
  90. }
  91. internal XQueryStaticContext StaticContext {
  92. get { return staticContext; }
  93. }
  94. internal CultureInfo GetCulture (string collation)
  95. {
  96. CultureInfo ci = staticContext.GetCulture (collation);
  97. if (ci == null)
  98. ci = (CultureInfo) localCollationCache [collation];
  99. if (ci != null)
  100. return ci;
  101. ci = new CultureInfo (collation);
  102. localCollationCache [collation] = ci;
  103. return ci;
  104. }
  105. public void PushCurrentSequence (XPathSequence sequence)
  106. {
  107. if (sequence == null)
  108. throw new ArgumentNullException ();
  109. sequence = sequence.Clone ();
  110. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  111. contextStack.Push (currentContext);
  112. currentsequence = sequence;
  113. currentContext = new XQueryContext (this);
  114. #else
  115. contextSequenceStack.Push (currentSequence);
  116. currentSequence = sequence;
  117. #endif
  118. }
  119. public void PopCurrentSequence ()
  120. {
  121. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  122. PopContext ();
  123. #else
  124. currentSequence = contextSequenceStack.Pop ();
  125. #endif
  126. if (currentSequence == null)
  127. throw new SystemException ("XQuery error: should not happen.");
  128. }
  129. internal void PushContext ()
  130. {
  131. contextStack.Push (currentContext);
  132. currentContext = new XQueryContext (this);
  133. }
  134. internal void PopContext ()
  135. {
  136. currentContext = contextStack.Pop ();
  137. }
  138. internal XmlNamespaceManager NSManager {
  139. get { return namespaceManager; }
  140. }
  141. }
  142. public class XQueryContext : IXmlNamespaceResolver
  143. {
  144. XQueryContextManager contextManager;
  145. Hashtable currentVariables;
  146. XPathSequence currentSequence;
  147. internal XQueryContext (XQueryContextManager manager)
  148. : this (manager, manager.CurrentContext.CurrentSequence)
  149. {
  150. }
  151. internal XQueryContext (XQueryContextManager manager, XPathSequence currentSequence)
  152. {
  153. contextManager = manager;
  154. this.currentSequence = currentSequence;
  155. if (manager.CurrentContext != null)
  156. currentVariables = (Hashtable) manager.CurrentContext.currentVariables.Clone ();
  157. else
  158. currentVariables = new Hashtable ();
  159. }
  160. internal XmlWriter Writer {
  161. get { return contextManager.Writer; }
  162. // FIXME: might be better avoid public setter.
  163. set { contextManager.Writer = value; }
  164. }
  165. internal XQueryStaticContext StaticContext {
  166. get { return contextManager.StaticContext; }
  167. }
  168. internal CultureInfo DefaultCollation {
  169. get { return StaticContext.DefaultCollation; }
  170. }
  171. internal XQueryContextManager ContextManager {
  172. get { return contextManager; }
  173. }
  174. public XPathItem CurrentItem {
  175. get {
  176. if (currentSequence == null)
  177. throw new XmlQueryException ("This XQuery dynamic context has no context item.");
  178. return CurrentSequence.Current;
  179. }
  180. }
  181. public XPathNavigator CurrentNode {
  182. get { return CurrentItem as XPathNavigator; }
  183. }
  184. public XPathSequence CurrentSequence {
  185. get { return currentSequence; }
  186. }
  187. internal CultureInfo GetCulture (string collation)
  188. {
  189. return contextManager.GetCulture (collation);
  190. }
  191. internal void PushVariable (XmlQualifiedName name, object iter)
  192. {
  193. contextManager.PushContext ();
  194. currentVariables [name] = iter;
  195. }
  196. internal void PopVariable ()
  197. {
  198. contextManager.PopContext ();
  199. }
  200. internal XPathSequence ResolveVariable (XmlQualifiedName name)
  201. {
  202. object obj = currentVariables [name];
  203. if (obj == null && contextManager.Arguments != null)
  204. obj = contextManager.Arguments.GetParameter (name.Name, name.Namespace);
  205. if (obj == null)
  206. return new XPathEmptySequence (this);
  207. XPathSequence seq = obj as XPathSequence;
  208. if (seq != null)
  209. return seq;
  210. XPathItem item = obj as XPathItem;
  211. if (item == null)
  212. item = new XPathAtomicValue (obj, XmlSchemaType.GetBuiltInType (XPathAtomicValue.XmlTypeCodeFromRuntimeType (obj.GetType (), true)));
  213. return new SingleItemIterator (item, this);
  214. }
  215. internal XPathSequence ResolveCollection (string name)
  216. {
  217. // FIXME: support later.
  218. return new XPathEmptySequence (currentSequence.Context);
  219. }
  220. public IXmlNamespaceResolver NSResolver {
  221. get { return contextManager.NSManager; }
  222. }
  223. #region IXmlNamespaceResolver implementation
  224. public XmlNameTable NameTable {
  225. get { return contextManager.NSManager.NameTable; }
  226. }
  227. public string LookupPrefix (string ns)
  228. {
  229. return contextManager.NSManager.LookupPrefix (ns);
  230. }
  231. public string LookupPrefix (string ns, bool atomized)
  232. {
  233. return contextManager.NSManager.LookupPrefix (ns, atomized);
  234. }
  235. public string LookupNamespace (string prefix)
  236. {
  237. return contextManager.NSManager.LookupNamespace (prefix);
  238. }
  239. public string LookupNamespace (string prefix, bool atomized)
  240. {
  241. return contextManager.NSManager.LookupNamespace (prefix, atomized);
  242. }
  243. public IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  244. {
  245. return contextManager.NSManager.GetNamespacesInScope (scope);
  246. }
  247. #endregion
  248. }
  249. }
  250. #endif