XQueryExpression.cs 15 KB

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