XQueryContext.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. Hashtable currentVariables = new Hashtable ();
  56. XmlNamespaceManager namespaceManager;
  57. Hashtable localCollationCache = new Hashtable ();
  58. internal XQueryContextManager (XQueryStaticContext ctx, XPathItem input, XmlWriter writer, XmlResolver resolver, XmlArgumentList args)
  59. {
  60. this.input = input;
  61. this.staticContext = ctx;
  62. this.args = args;
  63. currentWriter = writer;
  64. this.extDocResolver = resolver;
  65. namespaceManager = new XmlNamespaceManager (ctx.NameTable);
  66. foreach (DictionaryEntry de in ctx.NSResolver.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml))
  67. namespaceManager.AddNamespace (de.Key.ToString (), de.Value.ToString ());
  68. namespaceManager.PushScope ();
  69. if (input != null) {
  70. currentSequence = new SingleItemIterator (input, currentContext);
  71. currentSequence.MoveNext ();
  72. }
  73. currentContext = new XQueryContext (this, currentSequence);
  74. }
  75. public bool Initialized {
  76. get { return currentContext != null; }
  77. }
  78. public XmlResolver ExtDocResolver {
  79. get { return extDocResolver; }
  80. }
  81. public XmlArgumentList Arguments {
  82. get { return args; }
  83. }
  84. public Hashtable LocalVariables {
  85. get { return currentVariables; }
  86. }
  87. public XmlWriter Writer {
  88. get { return currentWriter; }
  89. // FIXME: might be better avoid setter as public
  90. set { currentWriter = value; }
  91. }
  92. internal XQueryContext CurrentContext {
  93. get { return currentContext; }
  94. }
  95. internal XQueryStaticContext StaticContext {
  96. get { return staticContext; }
  97. }
  98. internal CultureInfo GetCulture (string collation)
  99. {
  100. CultureInfo ci = staticContext.GetCulture (collation);
  101. if (ci == null)
  102. ci = (CultureInfo) localCollationCache [collation];
  103. if (ci != null)
  104. return ci;
  105. ci = new CultureInfo (collation);
  106. localCollationCache [collation] = ci;
  107. return ci;
  108. }
  109. public void PushCurrentSequence (XPathSequence sequence)
  110. {
  111. if (sequence == null)
  112. throw new ArgumentNullException ();
  113. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  114. contextStack.Push (currentContext);
  115. currentsequence = sequence;
  116. currentContext = new XQueryContext (this);
  117. #else
  118. contextSequenceStack.Push (currentSequence);
  119. currentSequence = sequence;
  120. #endif
  121. }
  122. public void PopCurrentSequence ()
  123. {
  124. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  125. PopContext ();
  126. #else
  127. currentSequence = contextSequenceStack.Pop ();
  128. #endif
  129. if (currentSequence == null)
  130. throw new SystemException ("XQuery error: should not happen.");
  131. }
  132. // FIXME: According to the spec 3.8.1, variales bindings in
  133. // FLWOR is not necesarrily bound to the order of bindings.
  134. // Thus, we might not have to create every XQueryContext for
  135. // each variable binding (not sure for other kind of bindings).
  136. public void PushVariable (XmlQualifiedName name, XPathSequence iter)
  137. {
  138. contextStack.Push (currentContext);
  139. currentVariables.Add (name, iter);
  140. currentContext = new XQueryContext (this);
  141. }
  142. public void PopVariable ()
  143. {
  144. PopContext ();
  145. }
  146. private void PopContext ()
  147. {
  148. currentContext = contextStack.Pop ();
  149. }
  150. internal XmlNamespaceManager NSManager {
  151. get { return namespaceManager; }
  152. }
  153. }
  154. public class XQueryContext : IXmlNamespaceResolver
  155. {
  156. XQueryContextManager contextManager;
  157. Hashtable currentVariables;
  158. XPathSequence currentSequence;
  159. internal XQueryContext (XQueryContextManager manager)
  160. : this (manager, manager.CurrentContext.CurrentSequence)
  161. {
  162. }
  163. internal XQueryContext (XQueryContextManager manager, XPathSequence currentSequence)
  164. {
  165. contextManager = manager;
  166. // if (manager.Initialized) // this condition is not filled on initial creation.
  167. // currentSequence = manager.CurrentContext.currentSequence;
  168. this.currentSequence = currentSequence;
  169. currentVariables = (Hashtable) manager.LocalVariables.Clone ();
  170. }
  171. internal XmlWriter Writer {
  172. get { return contextManager.Writer; }
  173. // FIXME: might be better avoid public setter.
  174. set { contextManager.Writer = value; }
  175. }
  176. internal XQueryStaticContext StaticContext {
  177. get { return contextManager.StaticContext; }
  178. }
  179. internal CultureInfo DefaultCollation {
  180. get { return StaticContext.DefaultCollation; }
  181. }
  182. internal XQueryContextManager ContextManager {
  183. get { return contextManager; }
  184. }
  185. public XPathItem CurrentItem {
  186. get {
  187. if (currentSequence == null)
  188. throw new XmlQueryException ("This XQuery dynamic context has no context item.");
  189. return CurrentSequence.Current;
  190. }
  191. }
  192. public XPathNavigator CurrentNode {
  193. get { return CurrentItem as XPathNavigator; }
  194. }
  195. public XPathSequence CurrentSequence {
  196. get { return currentSequence; }
  197. }
  198. internal CultureInfo GetCulture (string collation)
  199. {
  200. return contextManager.GetCulture (collation);
  201. }
  202. internal void PushVariable (XmlQualifiedName name, XPathSequence iter)
  203. {
  204. contextManager.PushVariable (name, iter);
  205. }
  206. internal void PopVariable ()
  207. {
  208. contextManager.PopVariable ();
  209. }
  210. internal XPathSequence ResolveVariable (XmlQualifiedName name, XPathSequence context)
  211. {
  212. object obj = currentVariables [name];
  213. if (obj == null)
  214. obj = contextManager.Arguments.GetParameter (name.Name, name.Namespace);
  215. if (obj == null)
  216. // FIXME: location
  217. throw new XmlQueryException (String.Format ("Cannot resolve variable '{0}'.", name));
  218. XPathSequence seq = obj as XPathSequence;
  219. if (seq != null)
  220. return seq;
  221. XPathItem item = obj as XPathItem;
  222. if (item == null)
  223. item = new XPathAtomicValue (obj, null);
  224. return new SingleItemIterator (item, context);
  225. }
  226. internal XPathSequence ResolveCollection (string name)
  227. {
  228. // FIXME: support later.
  229. return new XPathEmptySequence (currentSequence);
  230. }
  231. public IXmlNamespaceResolver NSResolver {
  232. get { return contextManager.NSManager; }
  233. }
  234. #region IXmlNamespaceResolver implementation
  235. public XmlNameTable NameTable {
  236. get { return contextManager.NSManager.NameTable; }
  237. }
  238. public string LookupPrefix (string ns)
  239. {
  240. return contextManager.NSManager.LookupPrefix (ns);
  241. }
  242. public string LookupPrefix (string ns, bool atomized)
  243. {
  244. return contextManager.NSManager.LookupPrefix (ns, atomized);
  245. }
  246. public string LookupNamespace (string prefix)
  247. {
  248. return contextManager.NSManager.LookupNamespace (prefix);
  249. }
  250. public string LookupNamespace (string prefix, bool atomized)
  251. {
  252. return contextManager.NSManager.LookupNamespace (prefix, atomized);
  253. }
  254. public IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  255. {
  256. return contextManager.NSManager.GetNamespacesInScope (scope);
  257. }
  258. #endregion
  259. }
  260. }
  261. #endif