XQueryContext.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. currentContext = new XQueryContext (this, null, new Hashtable ());
  69. if (input != null) {
  70. currentSequence = new SingleItemIterator (input, currentContext);
  71. currentSequence.MoveNext ();
  72. }
  73. currentContext = new XQueryContext (this, currentSequence, new Hashtable ());
  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 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 (sequence == null)
  109. throw new ArgumentNullException ();
  110. // sequence = sequence.Clone ();
  111. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  112. contextStack.Push (currentContext);
  113. currentsequence = sequence;
  114. currentContext = new XQueryContext (this);
  115. #else
  116. contextSequenceStack.Push (currentSequence);
  117. currentSequence = sequence;
  118. #endif
  119. }
  120. public void PopCurrentSequence ()
  121. {
  122. #if SEEMS_CONTEXT_FOR_CURRENT_REQURED
  123. PopContext ();
  124. #else
  125. currentSequence = contextSequenceStack.Pop ();
  126. #endif
  127. if (currentSequence == null)
  128. throw new SystemException ("XQuery error: should not happen.");
  129. }
  130. internal void PushContext ()
  131. {
  132. contextStack.Push (currentContext);
  133. currentContext = new XQueryContext (this);
  134. }
  135. internal void PopContext ()
  136. {
  137. currentContext = contextStack.Pop ();
  138. }
  139. internal void PushVariable (XmlQualifiedName name, object iter)
  140. {
  141. PushContext ();
  142. CurrentContext.SetVariable (name, iter);
  143. }
  144. internal void PopVariable ()
  145. {
  146. PopContext ();
  147. }
  148. internal XmlNamespaceManager NSManager {
  149. get { return namespaceManager; }
  150. }
  151. internal XPathSequence CurrentSequence {
  152. get { return currentSequence; }
  153. }
  154. }
  155. public class XQueryContext : IXmlNamespaceResolver
  156. {
  157. XQueryContextManager contextManager;
  158. Hashtable currentVariables;
  159. XPathSequence currentSequence;
  160. internal XQueryContext (XQueryContextManager manager)
  161. : this (manager,
  162. manager.CurrentSequence,
  163. (Hashtable) manager.CurrentContext.currentVariables.Clone ())
  164. {
  165. }
  166. internal XQueryContext (XQueryContextManager manager, XPathSequence currentSequence, Hashtable currentVariables)
  167. {
  168. contextManager = manager;
  169. this.currentSequence = currentSequence;
  170. /*
  171. if (manager.CurrentContext != null)
  172. currentVariables = (Hashtable) manager.CurrentContext.currentVariables.Clone ();
  173. else
  174. currentVariables = new Hashtable ();
  175. */
  176. this.currentVariables = currentVariables;
  177. }
  178. internal XmlWriter Writer {
  179. get { return contextManager.Writer; }
  180. // FIXME: might be better avoid public setter.
  181. set { contextManager.Writer = value; }
  182. }
  183. internal XQueryStaticContext StaticContext {
  184. get { return contextManager.StaticContext; }
  185. }
  186. internal CultureInfo DefaultCollation {
  187. get { return StaticContext.DefaultCollation; }
  188. }
  189. internal XQueryContextManager ContextManager {
  190. get { return contextManager; }
  191. }
  192. public XPathItem CurrentItem {
  193. get {
  194. if (currentSequence == null)
  195. throw new XmlQueryException ("This XQuery dynamic context has no context item.");
  196. return CurrentSequence.Current;
  197. }
  198. }
  199. public XPathNavigator CurrentNode {
  200. get { return CurrentItem as XPathNavigator; }
  201. }
  202. public XPathSequence CurrentSequence {
  203. get { return currentSequence; }
  204. }
  205. internal CultureInfo GetCulture (string collation)
  206. {
  207. return contextManager.GetCulture (collation);
  208. }
  209. internal void PushVariable (XmlQualifiedName name, object iter)
  210. {
  211. contextManager.PushVariable (name, iter);
  212. }
  213. // FIXME: Hmm... this design is annoying.
  214. internal void SetVariable (XmlQualifiedName name, object iter)
  215. {
  216. currentVariables [name] = iter;
  217. }
  218. internal void PopVariable ()
  219. {
  220. contextManager.PopVariable ();
  221. }
  222. internal XPathSequence ResolveVariable (XmlQualifiedName name)
  223. {
  224. object obj = currentVariables [name];
  225. if (obj == null && contextManager.Arguments != null)
  226. obj = contextManager.Arguments.GetParameter (name.Name, name.Namespace);
  227. if (obj == null)
  228. return new XPathEmptySequence (this);
  229. XPathSequence seq = obj as XPathSequence;
  230. if (seq != null)
  231. return seq;
  232. XPathItem item = obj as XPathItem;
  233. if (item == null)
  234. item = new XPathAtomicValue (obj, XmlSchemaType.GetBuiltInType (XPathAtomicValue.XmlTypeCodeFromRuntimeType (obj.GetType (), true)));
  235. return new SingleItemIterator (item, this);
  236. }
  237. internal XPathSequence ResolveCollection (string name)
  238. {
  239. // FIXME: support later.
  240. return new XPathEmptySequence (currentSequence.Context);
  241. }
  242. public IXmlNamespaceResolver NSResolver {
  243. get { return contextManager.NSManager; }
  244. }
  245. #region IXmlNamespaceResolver implementation
  246. public XmlNameTable NameTable {
  247. get { return contextManager.NSManager.NameTable; }
  248. }
  249. public string LookupPrefix (string ns)
  250. {
  251. return contextManager.NSManager.LookupPrefix (ns);
  252. }
  253. public string LookupPrefix (string ns, bool atomized)
  254. {
  255. return contextManager.NSManager.LookupPrefix (ns, atomized);
  256. }
  257. public string LookupNamespace (string prefix)
  258. {
  259. return contextManager.NSManager.LookupNamespace (prefix);
  260. }
  261. public string LookupNamespace (string prefix, bool atomized)
  262. {
  263. return contextManager.NSManager.LookupNamespace (prefix, atomized);
  264. }
  265. public IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  266. {
  267. return contextManager.NSManager.GetNamespacesInScope (scope);
  268. }
  269. #endregion
  270. }
  271. }
  272. #endif