XQueryContext.cs 7.9 KB

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