| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- //
- // XQueryExpression.cs - abstract syntax tree for XQuery 1.0
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Collections;
- using System.Xml;
- using System.Xml.Query;
- using System.Xml.Schema;
- using System.Xml.XPath;
- using Mono.Xml.XPath2;
- namespace Mono.Xml.XQuery
- {
- internal abstract class XmlConstructorExpr : ExprSingle
- {
- public XmlConstructorExpr (ExprSequence content)
- {
- this.content = content;
- }
- ExprSequence content;
- public ExprSequence Content {
- get { return content; }
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- public void SerializeContent (XPathSequence iter)
- {
- if (Content != null)
- foreach (ExprSingle expr in Content)
- expr.Serialize (iter);
- }
- internal IXmlNamespaceResolver GetNSResolver (XPathSequence iter)
- {
- // FIXME: IXmlNamespaceResolver must be constructed
- // considering 1)static context and 2)in-scope element
- // construction.
- return iter.Context;
- }
- public XPathSequence EvaluateNode (XPathSequence iter)
- {
- return EvaluateNode (iter, XPathNodeType.All);
- }
-
- public XPathSequence EvaluateNode (XPathSequence iter, XPathNodeType moveAfterCreation)
- {
- XPathDocument doc = new XPathDocument ();
- XmlWriter w = iter.Context.Writer;
- try {
- iter.Context.Writer = doc.CreateEditor ().AppendChild ();
- Serialize (iter);
- iter.Context.Writer.Close ();
- } finally {
- iter.Context.Writer = w;
- }
- XPathNavigator nav = doc.CreateNavigator ();
- switch (moveAfterCreation) {
- case XPathNodeType.Attribute:
- nav.MoveToFirstAttribute ();
- break;
- case XPathNodeType.Root:
- break;
- default:
- nav.MoveToFirstChild ();
- break;
- }
- return new SingleItemIterator (nav, iter.Context);
- }
- #endregion
- }
- internal class XmlAttrConstructorList : CollectionBase
- {
- public XmlAttrConstructorList ()
- {
- }
- public void Add (XmlAttrConstructor item)
- {
- List.Add (item);
- }
- public void Insert (int pos, XmlAttrConstructor item)
- {
- List.Insert (pos, item);
- }
- }
- internal class XmlElemConstructor : XmlConstructorExpr
- {
- XmlQualifiedName name;
- ExprSequence nameExpr;
- public XmlElemConstructor (XmlQualifiedName name, ExprSequence content)
- : base (content)
- {
- this.name = name;
- }
- public XmlElemConstructor (ExprSequence name, ExprSequence content)
- : base (content)
- {
- this.name = XmlQualifiedName.Empty;
- this.nameExpr = name;
- }
- public XmlQualifiedName Name {
- get { return name; }
- }
- public ExprSequence NameExpr {
- get { return nameExpr; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (nameExpr != null)
- nameExpr.CheckReference (compiler);
- if (Content != null)
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (NameExpr != null)
- for (int i = 0; i < NameExpr.Count; i++)
- NameExpr [i] = NameExpr [i].Compile (compiler);
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.Element; }
- }
- public override void Serialize (XPathSequence iter)
- {
- XmlQualifiedName name = EvaluateName (iter);
- XmlWriter w = iter.Context.Writer;
- w.WriteStartElement (iter.Context.LookupPrefix (name.Namespace), name.Name, name.Namespace);
- SerializeContent (iter);
- w.WriteEndElement ();
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return EvaluateNode (iter);
- }
- private XmlQualifiedName EvaluateName (XPathSequence iter)
- {
- XmlQualifiedName name = Name;
- if (NameExpr != null) {
- XPathAtomicValue value = Atomize (new ExprSequenceIterator (iter, NameExpr));
- IXmlNamespaceResolver res = iter.Context.NSResolver;
- switch (value.XmlType.TypeCode) {
- case XmlTypeCode.QName:
- name = (XmlQualifiedName) value.ValueAs (typeof (XmlQualifiedName), res);
- break;
- case XmlTypeCode.String:
- try {
- name = XmlQualifiedName.Parse (value.Value, res);
- } catch (ArgumentException ex) {
- // FIXME: add more info
- 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));
- }
- break;
- default:
- // FIXME: add more info
- throw new XmlQueryException ("A name of an element constructor must be resolved to either a QName or string.");
- }
- }
- return name;
- }
- #endregion
- }
- internal class XmlAttrConstructor : XmlConstructorExpr
- {
- XmlQualifiedName name;
- ExprSequence nameExpr;
- public XmlAttrConstructor (XmlQualifiedName name, ExprSequence content)
- : base (content)
- {
- this.name = name;
- }
- public XmlAttrConstructor (ExprSequence name, ExprSequence content)
- : base (content)
- {
- this.nameExpr = name;
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (nameExpr != null)
- nameExpr.CheckReference (compiler);
- if (Content != null)
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (NameExpr != null)
- for (int i = 0; i < NameExpr.Count; i++)
- NameExpr [i] = NameExpr [i].Compile (compiler);
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- public XmlQualifiedName Name {
- get { return name; }
- }
- public ExprSequence NameExpr {
- get { return nameExpr; }
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.Attribute; }
- }
- public override void Serialize (XPathSequence iter)
- {
- XmlQualifiedName name = EvaluateName (iter);
- XmlWriter w = iter.Context.Writer;
- w.WriteStartAttribute (GetNSResolver (iter).LookupPrefix (name.Namespace), name.Name, name.Namespace);
- SerializeContent (iter);
- w.WriteEndAttribute ();
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return EvaluateNode (iter, XPathNodeType.Attribute);
- }
- private XmlQualifiedName EvaluateName (XPathSequence iter)
- {
- XmlQualifiedName name = Name;
- if (NameExpr != null) {
- XPathAtomicValue value = Atomize (new ExprSequenceIterator (iter, NameExpr));
- IXmlNamespaceResolver res = GetNSResolver (iter);
- switch (value.XmlType.TypeCode) {
- case XmlTypeCode.QName:
- name = (XmlQualifiedName) value.ValueAs (typeof (XmlQualifiedName), res);
- break;
- case XmlTypeCode.String:
- try {
- // nonprefixed attribute name == element's local namespace
- if (value.Value.IndexOf (':') < 0)
- name = new XmlQualifiedName (value.Value);
- else
- name = XmlQualifiedName.Parse (value.Value, res);
- } catch (ArgumentException ex) {
- // FIXME: add more info
- 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));
- }
- break;
- default:
- // FIXME: add more info
- throw new XmlQueryException ("A name of an attribute constructor must be resolved to either a QName or string.");
- }
- }
- return name;
- }
- #endregion
- }
- internal class XmlNSConstructor : XmlConstructorExpr
- {
- public XmlNSConstructor (string prefix, ExprSequence content)
- : base (content)
- {
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.Namespace; }
- }
- public override void Serialize (XPathSequence iter)
- {
- // TBD
- throw new NotImplementedException ();
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- // TBD
- throw new NotImplementedException ();
- }
- #endregion
- }
- internal class XmlDocConstructor : XmlConstructorExpr
- {
- public XmlDocConstructor (ExprSequence content)
- : base (content)
- {
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (Content != null)
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.Document; }
- }
- public override void Serialize (XPathSequence iter)
- {
- XmlWriter w = iter.Context.Writer;
- w.WriteStartDocument ();
- SerializeContent (iter);
- w.WriteEndDocument ();
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return EvaluateNode (iter, XPathNodeType.Root);
- }
- #endregion
- }
- internal class XmlTextConstructor : XmlConstructorExpr
- {
- public XmlTextConstructor (string text)
- : base (null)
- {
- this.text = text;
- }
- public XmlTextConstructor (ExprSequence content)
- : base (content)
- {
- }
- string text;
- public string LiteralText {
- get { return text; }
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (Content != null)
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- public override SequenceType StaticType {
- get { return SequenceType.Text; }
- }
- public override void Serialize (XPathSequence iter)
- {
- if (Content != null)
- iter.Context.Writer.WriteString (Atomize (new ExprSequenceIterator (iter, Content)).Value);
- else
- iter.Context.Writer.WriteString (LiteralText);
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return EvaluateNode (iter);
- }
- #endregion
- }
- internal class XmlCommentConstructor : XmlConstructorExpr
- {
- string contentLiteral;
- public XmlCommentConstructor (string content)
- : base (null)
- {
- this.contentLiteral = content;
- }
- public XmlCommentConstructor (ExprSequence content)
- : base (content)
- {
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (Content != null)
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.Comment; }
- }
- public override void Serialize (XPathSequence iter)
- {
- iter.Context.Writer.WriteComment (Atomize (new ExprSequenceIterator (iter, Content)).Value);
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return EvaluateNode (iter);
- }
- #endregion
- }
- internal class XmlPIConstructor : XmlConstructorExpr
- {
- string name;
- ExprSequence nameExpr;
- string contentLiteral;
- public XmlPIConstructor (string name, string content)
- : base (null)
- {
- this.name = name;
- this.contentLiteral = content;
- }
- public XmlPIConstructor (string name, ExprSequence content)
- : base (content)
- {
- this.name = name;
- }
- public XmlPIConstructor (ExprSequence name, ExprSequence content)
- : base (content)
- {
- this.nameExpr = name;
- }
- internal override void CheckReference (XQueryASTCompiler compiler)
- {
- if (nameExpr != null)
- nameExpr.CheckReference (compiler);
- if (Content != null)
- Content.CheckReference (compiler);
- }
- #region CompileAndEvaluate
- internal override ExprSingle CompileCore (XQueryASTCompiler compiler)
- {
- if (NameExpr != null)
- for (int i = 0; i < NameExpr.Count; i++)
- NameExpr [i] = NameExpr [i].Compile (compiler);
- if (Content != null)
- for (int i = 0; i < Content.Count; i++)
- Content [i] = Content [i].Compile (compiler);
- return this;
- }
- public string Name {
- get { return name; }
- }
- public ExprSequence NameExpr {
- get { return nameExpr; }
- }
- // FIXME: can be optimized by checking all items in Expr
- public override SequenceType StaticType {
- get { return SequenceType.XmlPI; }
- }
- public override void Serialize (XPathSequence iter)
- {
- iter.Context.Writer.WriteProcessingInstruction (
- GetName (iter),
- Atomize (new ExprSequenceIterator (iter, Content)).Value);
- }
- public override XPathSequence Evaluate (XPathSequence iter)
- {
- return EvaluateNode (iter);
- }
- private string GetName (XPathSequence iter)
- {
- if (Name != String.Empty)
- return Name;
- return Atomize (new ExprSequenceIterator (iter, NameExpr)).Value;
- }
- #endregion
- }
- }
- #endif
|