XQueryExpression.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. //
  2. // XQueryExpression.cs - abstract syntax tree for XQuery 1.0
  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.Xml;
  33. using System.Xml.Query;
  34. using System.Xml.Schema;
  35. using System.Xml.XPath;
  36. using Mono.Xml.XPath2;
  37. namespace Mono.Xml.XQuery
  38. {
  39. internal abstract class XmlConstructorExpr : ExprSingle
  40. {
  41. public XmlConstructorExpr (ExprSequence content)
  42. {
  43. this.content = content;
  44. }
  45. ExprSequence content;
  46. public ExprSequence Content {
  47. get { return content; }
  48. }
  49. #region CompileAndEvaluate
  50. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  51. {
  52. if (Content != null)
  53. for (int i = 0; i < Content.Count; i++)
  54. Content [i] = Content [i].Compile (compiler);
  55. return this;
  56. }
  57. public void SerializeContent (XPathSequence iter)
  58. {
  59. if (Content != null)
  60. foreach (ExprSingle expr in Content)
  61. expr.Serialize (iter);
  62. }
  63. internal IXmlNamespaceResolver GetNSResolver (XPathSequence iter)
  64. {
  65. // FIXME: IXmlNamespaceResolver must be constructed
  66. // considering 1)static context and 2)in-scope element
  67. // construction.
  68. return iter.Context;
  69. }
  70. public XPathSequence EvaluateNode (XPathSequence iter)
  71. {
  72. return EvaluateNode (iter, XPathNodeType.All);
  73. }
  74. public XPathSequence EvaluateNode (XPathSequence iter, XPathNodeType moveAfterCreation)
  75. {
  76. XPathDocument doc = new XPathDocument ();
  77. XmlWriter w = iter.Context.Writer;
  78. try {
  79. iter.Context.Writer = doc.CreateEditor ().AppendChild ();
  80. Serialize (iter);
  81. iter.Context.Writer.Close ();
  82. } finally {
  83. iter.Context.Writer = w;
  84. }
  85. XPathNavigator nav = doc.CreateNavigator ();
  86. switch (moveAfterCreation) {
  87. case XPathNodeType.Attribute:
  88. nav.MoveToFirstAttribute ();
  89. break;
  90. case XPathNodeType.Root:
  91. break;
  92. default:
  93. nav.MoveToFirstChild ();
  94. break;
  95. }
  96. return new SingleItemIterator (nav, iter.Context);
  97. }
  98. #endregion
  99. }
  100. internal class XmlAttrConstructorList : CollectionBase
  101. {
  102. public XmlAttrConstructorList ()
  103. {
  104. }
  105. public void Add (XmlAttrConstructor item)
  106. {
  107. List.Add (item);
  108. }
  109. public void Insert (int pos, XmlAttrConstructor item)
  110. {
  111. List.Insert (pos, item);
  112. }
  113. }
  114. internal class XmlElemConstructor : XmlConstructorExpr
  115. {
  116. XmlQualifiedName name;
  117. ExprSequence nameExpr;
  118. public XmlElemConstructor (XmlQualifiedName name, ExprSequence content)
  119. : base (content)
  120. {
  121. this.name = name;
  122. }
  123. public XmlElemConstructor (ExprSequence name, ExprSequence content)
  124. : base (content)
  125. {
  126. this.name = XmlQualifiedName.Empty;
  127. this.nameExpr = name;
  128. }
  129. public XmlQualifiedName Name {
  130. get { return name; }
  131. }
  132. public ExprSequence NameExpr {
  133. get { return nameExpr; }
  134. }
  135. internal override void CheckReference (XQueryASTCompiler compiler)
  136. {
  137. if (nameExpr != null)
  138. nameExpr.CheckReference (compiler);
  139. if (Content != null)
  140. Content.CheckReference (compiler);
  141. }
  142. #region CompileAndEvaluate
  143. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  144. {
  145. if (NameExpr != null)
  146. for (int i = 0; i < NameExpr.Count; i++)
  147. NameExpr [i] = NameExpr [i].Compile (compiler);
  148. if (Content != null)
  149. for (int i = 0; i < Content.Count; i++)
  150. Content [i] = Content [i].Compile (compiler);
  151. return this;
  152. }
  153. // FIXME: can be optimized by checking all items in Expr
  154. public override SequenceType StaticType {
  155. get { return SequenceType.Element; }
  156. }
  157. public override void Serialize (XPathSequence iter)
  158. {
  159. XmlQualifiedName name = EvaluateName (iter);
  160. XmlWriter w = iter.Context.Writer;
  161. w.WriteStartElement (iter.Context.LookupPrefix (name.Namespace), name.Name, name.Namespace);
  162. SerializeContent (iter);
  163. w.WriteEndElement ();
  164. }
  165. public override XPathSequence Evaluate (XPathSequence iter)
  166. {
  167. return EvaluateNode (iter);
  168. }
  169. private XmlQualifiedName EvaluateName (XPathSequence iter)
  170. {
  171. XmlQualifiedName name = Name;
  172. if (NameExpr != null) {
  173. XPathAtomicValue value = Atomize (new ExprSequenceIterator (iter, NameExpr));
  174. IXmlNamespaceResolver res = iter.Context.NSResolver;
  175. switch (value.XmlType.TypeCode) {
  176. case XmlTypeCode.QName:
  177. name = (XmlQualifiedName) value.ValueAs (typeof (XmlQualifiedName), res);
  178. break;
  179. case XmlTypeCode.String:
  180. try {
  181. name = XmlQualifiedName.Parse (value.Value, res);
  182. } catch (ArgumentException ex) {
  183. // FIXME: add more info
  184. throw new XmlQueryException (String.Format ("The evaluation result of the name expression could not be resolved as a valid QName. Evaluation result string is '{0}'.", value.Value));
  185. }
  186. break;
  187. default:
  188. // FIXME: add more info
  189. throw new XmlQueryException ("A name of an element constructor must be resolved to either a QName or string.");
  190. }
  191. }
  192. return name;
  193. }
  194. #endregion
  195. }
  196. internal class XmlAttrConstructor : XmlConstructorExpr
  197. {
  198. XmlQualifiedName name;
  199. ExprSequence nameExpr;
  200. public XmlAttrConstructor (XmlQualifiedName name, ExprSequence content)
  201. : base (content)
  202. {
  203. this.name = name;
  204. }
  205. public XmlAttrConstructor (ExprSequence name, ExprSequence content)
  206. : base (content)
  207. {
  208. this.nameExpr = name;
  209. }
  210. internal override void CheckReference (XQueryASTCompiler compiler)
  211. {
  212. if (nameExpr != null)
  213. nameExpr.CheckReference (compiler);
  214. if (Content != null)
  215. Content.CheckReference (compiler);
  216. }
  217. #region CompileAndEvaluate
  218. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  219. {
  220. if (NameExpr != null)
  221. for (int i = 0; i < NameExpr.Count; i++)
  222. NameExpr [i] = NameExpr [i].Compile (compiler);
  223. if (Content != null)
  224. for (int i = 0; i < Content.Count; i++)
  225. Content [i] = Content [i].Compile (compiler);
  226. return this;
  227. }
  228. public XmlQualifiedName Name {
  229. get { return name; }
  230. }
  231. public ExprSequence NameExpr {
  232. get { return nameExpr; }
  233. }
  234. // FIXME: can be optimized by checking all items in Expr
  235. public override SequenceType StaticType {
  236. get { return SequenceType.Attribute; }
  237. }
  238. public override void Serialize (XPathSequence iter)
  239. {
  240. XmlQualifiedName name = EvaluateName (iter);
  241. XmlWriter w = iter.Context.Writer;
  242. w.WriteStartAttribute (GetNSResolver (iter).LookupPrefix (name.Namespace), name.Name, name.Namespace);
  243. SerializeContent (iter);
  244. w.WriteEndAttribute ();
  245. }
  246. public override XPathSequence Evaluate (XPathSequence iter)
  247. {
  248. return EvaluateNode (iter, XPathNodeType.Attribute);
  249. }
  250. private XmlQualifiedName EvaluateName (XPathSequence iter)
  251. {
  252. XmlQualifiedName name = Name;
  253. if (NameExpr != null) {
  254. XPathAtomicValue value = Atomize (new ExprSequenceIterator (iter, NameExpr));
  255. IXmlNamespaceResolver res = GetNSResolver (iter);
  256. switch (value.XmlType.TypeCode) {
  257. case XmlTypeCode.QName:
  258. name = (XmlQualifiedName) value.ValueAs (typeof (XmlQualifiedName), res);
  259. break;
  260. case XmlTypeCode.String:
  261. try {
  262. // nonprefixed attribute name == element's local namespace
  263. if (value.Value.IndexOf (':') < 0)
  264. name = new XmlQualifiedName (value.Value);
  265. else
  266. name = XmlQualifiedName.Parse (value.Value, res);
  267. } catch (ArgumentException ex) {
  268. // FIXME: add more info
  269. throw new XmlQueryException (String.Format ("The evaluation result of the name expression could not be resolved as a valid QName. Evaluation result string is '{0}'.", value.Value));
  270. }
  271. break;
  272. default:
  273. // FIXME: add more info
  274. throw new XmlQueryException ("A name of an attribute constructor must be resolved to either a QName or string.");
  275. }
  276. }
  277. return name;
  278. }
  279. #endregion
  280. }
  281. internal class XmlNSConstructor : XmlConstructorExpr
  282. {
  283. public XmlNSConstructor (string prefix, ExprSequence content)
  284. : base (content)
  285. {
  286. }
  287. internal override void CheckReference (XQueryASTCompiler compiler)
  288. {
  289. Content.CheckReference (compiler);
  290. }
  291. #region CompileAndEvaluate
  292. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  293. {
  294. if (Content != null)
  295. for (int i = 0; i < Content.Count; i++)
  296. Content [i] = Content [i].Compile (compiler);
  297. return this;
  298. }
  299. // FIXME: can be optimized by checking all items in Expr
  300. public override SequenceType StaticType {
  301. get { return SequenceType.Namespace; }
  302. }
  303. public override void Serialize (XPathSequence iter)
  304. {
  305. // TBD
  306. throw new NotImplementedException ();
  307. }
  308. public override XPathSequence Evaluate (XPathSequence iter)
  309. {
  310. // TBD
  311. throw new NotImplementedException ();
  312. }
  313. #endregion
  314. }
  315. internal class XmlDocConstructor : XmlConstructorExpr
  316. {
  317. public XmlDocConstructor (ExprSequence content)
  318. : base (content)
  319. {
  320. }
  321. internal override void CheckReference (XQueryASTCompiler compiler)
  322. {
  323. if (Content != null)
  324. Content.CheckReference (compiler);
  325. }
  326. #region CompileAndEvaluate
  327. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  328. {
  329. if (Content != null)
  330. for (int i = 0; i < Content.Count; i++)
  331. Content [i] = Content [i].Compile (compiler);
  332. return this;
  333. }
  334. // FIXME: can be optimized by checking all items in Expr
  335. public override SequenceType StaticType {
  336. get { return SequenceType.Document; }
  337. }
  338. public override void Serialize (XPathSequence iter)
  339. {
  340. XmlWriter w = iter.Context.Writer;
  341. w.WriteStartDocument ();
  342. SerializeContent (iter);
  343. w.WriteEndDocument ();
  344. }
  345. public override XPathSequence Evaluate (XPathSequence iter)
  346. {
  347. return EvaluateNode (iter, XPathNodeType.Root);
  348. }
  349. #endregion
  350. }
  351. internal class XmlTextConstructor : XmlConstructorExpr
  352. {
  353. public XmlTextConstructor (string text)
  354. : base (null)
  355. {
  356. this.text = text;
  357. }
  358. public XmlTextConstructor (ExprSequence content)
  359. : base (content)
  360. {
  361. }
  362. string text;
  363. public string LiteralText {
  364. get { return text; }
  365. }
  366. internal override void CheckReference (XQueryASTCompiler compiler)
  367. {
  368. if (Content != null)
  369. Content.CheckReference (compiler);
  370. }
  371. #region CompileAndEvaluate
  372. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  373. {
  374. if (Content != null)
  375. for (int i = 0; i < Content.Count; i++)
  376. Content [i] = Content [i].Compile (compiler);
  377. return this;
  378. }
  379. public override SequenceType StaticType {
  380. get { return SequenceType.Text; }
  381. }
  382. public override void Serialize (XPathSequence iter)
  383. {
  384. if (Content != null)
  385. iter.Context.Writer.WriteString (Atomize (new ExprSequenceIterator (iter, Content)).Value);
  386. else
  387. iter.Context.Writer.WriteString (LiteralText);
  388. }
  389. public override XPathSequence Evaluate (XPathSequence iter)
  390. {
  391. return EvaluateNode (iter);
  392. }
  393. #endregion
  394. }
  395. internal class XmlCommentConstructor : XmlConstructorExpr
  396. {
  397. string contentLiteral;
  398. public XmlCommentConstructor (string content)
  399. : base (null)
  400. {
  401. this.contentLiteral = content;
  402. }
  403. public XmlCommentConstructor (ExprSequence content)
  404. : base (content)
  405. {
  406. }
  407. internal override void CheckReference (XQueryASTCompiler compiler)
  408. {
  409. if (Content != null)
  410. Content.CheckReference (compiler);
  411. }
  412. #region CompileAndEvaluate
  413. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  414. {
  415. if (Content != null)
  416. for (int i = 0; i < Content.Count; i++)
  417. Content [i] = Content [i].Compile (compiler);
  418. return this;
  419. }
  420. // FIXME: can be optimized by checking all items in Expr
  421. public override SequenceType StaticType {
  422. get { return SequenceType.Comment; }
  423. }
  424. public override void Serialize (XPathSequence iter)
  425. {
  426. iter.Context.Writer.WriteComment (Atomize (new ExprSequenceIterator (iter, Content)).Value);
  427. }
  428. public override XPathSequence Evaluate (XPathSequence iter)
  429. {
  430. return EvaluateNode (iter);
  431. }
  432. #endregion
  433. }
  434. internal class XmlPIConstructor : XmlConstructorExpr
  435. {
  436. string name;
  437. ExprSequence nameExpr;
  438. string contentLiteral;
  439. public XmlPIConstructor (string name, string content)
  440. : base (null)
  441. {
  442. this.name = name;
  443. this.contentLiteral = content;
  444. }
  445. public XmlPIConstructor (string name, ExprSequence content)
  446. : base (content)
  447. {
  448. this.name = name;
  449. }
  450. public XmlPIConstructor (ExprSequence name, ExprSequence content)
  451. : base (content)
  452. {
  453. this.nameExpr = name;
  454. }
  455. internal override void CheckReference (XQueryASTCompiler compiler)
  456. {
  457. if (nameExpr != null)
  458. nameExpr.CheckReference (compiler);
  459. if (Content != null)
  460. Content.CheckReference (compiler);
  461. }
  462. #region CompileAndEvaluate
  463. internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
  464. {
  465. if (NameExpr != null)
  466. for (int i = 0; i < NameExpr.Count; i++)
  467. NameExpr [i] = NameExpr [i].Compile (compiler);
  468. if (Content != null)
  469. for (int i = 0; i < Content.Count; i++)
  470. Content [i] = Content [i].Compile (compiler);
  471. return this;
  472. }
  473. public string Name {
  474. get { return name; }
  475. }
  476. public ExprSequence NameExpr {
  477. get { return nameExpr; }
  478. }
  479. // FIXME: can be optimized by checking all items in Expr
  480. public override SequenceType StaticType {
  481. get { return SequenceType.XmlPI; }
  482. }
  483. public override void Serialize (XPathSequence iter)
  484. {
  485. iter.Context.Writer.WriteProcessingInstruction (
  486. GetName (iter),
  487. Atomize (new ExprSequenceIterator (iter, Content)).Value);
  488. }
  489. public override XPathSequence Evaluate (XPathSequence iter)
  490. {
  491. return EvaluateNode (iter);
  492. }
  493. private string GetName (XPathSequence iter)
  494. {
  495. if (Name != String.Empty)
  496. return Name;
  497. return Atomize (new ExprSequenceIterator (iter, NameExpr)).Value;
  498. }
  499. #endregion
  500. }
  501. }
  502. #endif