Exemple #1
0
 public XQueryDocumentBuilder(XQueryDocument doc)
 {            
     m_document = doc;
     if (doc.pagefile == null)
     {
         m_pageFile = new PageFile(false);
         m_pageFile.HasSchemaInfo = true;
         doc.pagefile = m_pageFile;
     }
     else
         m_pageFile = doc.pagefile;
     NameTable = doc.nameTable;
     xmlns = NameTable.Get(XmlReservedNs.NsXmlNs);
     _state = WriteState.Start;
     if (doc.input == null)
     {
         _normalize = true;
         hs = new HashSet<object>();
         NamespaceManager = new XmlNamespaceManager(NameTable);
     }
     SchemaInfo = null;
     DocumentRoot = new DmRoot();
     _parent = DocumentRoot;
     _text = null;
 }
Exemple #2
0
 public override object Execute(object context, object[] args, XmlDocument xmlResult)
 {
     if (context != null)
     {
         XmlNode node = context as XmlNode;
         if (node == null)
         {
             if (context is Resultset)
             {
                 XmlReader reader = new ResultsetReader((Resultset)context,
                     "context", _command.Context.GetSettings());
                 XQueryDocument tmp = new XQueryDocument(reader);
                 _command.ContextItem = tmp.CreateNavigator();
             }
             else
                 throw new ESQLException(
                     String.Format(Properties.Resources.XmlQueryContextMustBeANode, context), null);
         }
         else
             _command.ContextItem = node.CreateNavigator();
     }
     if (args != null)
     {
         for (int k = 0; k < args.Length; k++)
         {
             XQueryParameter param = _command.Parameters[k];
             object val = args[k];
             if (val is Resultset)
             {
                 XmlReader reader = new ResultsetReader((Resultset)val,
                     XmlConvert.EncodeName(param.LocalName), _command.Context.GetSettings());
                 XQueryDocument tmp = new XQueryDocument(reader);
                 param.Value = tmp.CreateNavigator();
             }
             else if (val is XmlNode)
                 param.Value = ((XmlNode)val).CreateNavigator();
             else if (val is XmlNodeList)
                 param.Value = new  XmlNodeListIterator((XmlNodeList)val);
             else
                 param.Value = val;
         }
     }
     XQueryNodeIterator iter = _command.Execute();
     DOMConverter converter = new DOMConverter(xmlResult);
     XmlDataAccessor.NodeList res = new XmlDataAccessor.NodeList();
     while (iter.MoveNext())
         res.Add(converter.ToXmlNode(iter.Current));
     if (res.Count == 1)
         return res[0];
     return res;
 }
Exemple #3
0
 private bool CompareFragment(XQueryContext context, string sourceFile, XQueryNodeIterator iter, XmlSpace space)
 {
     StringBuilder sb = new StringBuilder();
     sb.AppendLine("<?xml version='1.0'?>");
     sb.Append("<root>");
     TextReader textReader = new StreamReader(sourceFile, true);
     sb.Append(textReader.ReadToEnd());
     textReader.Close();
     sb.Append("</root>");
     XmlReaderSettings settings = context.GetSettings();
     XmlReader reader = XmlReader.Create(new StringReader(sb.ToString()), settings);
     XQueryDocument doc1 = new XQueryDocument(reader, space);
     doc1.Fill();
     context.AddDocument(doc1);
     XQueryDocument doc2 = context.CreateDocument();
     XQueryDocumentBuilder builder = new XQueryDocumentBuilder(doc2);
     builder.WriteStartDocument();
     builder.WriteStartElement("root", "");
     Core.WriteNode(context.Engine, builder, iter.Clone());
     builder.WriteEndElement();
     XQueryNodeIterator iter1 = new NodeIterator(new XPathItem[] { doc1.CreateNavigator() });
     XQueryNodeIterator iter2 = new NodeIterator(new XPathItem[] { doc2.CreateNavigator() });
     TreeComparer comparer = new TreeComparer();
     comparer.IgnoreWhitespace = true;
     bool res = comparer.DeepEqual(iter1, iter2);
     return res;
 }
Exemple #4
0
 private XQueryCommand CreateCommand(TextWriter tw, XmlElement node)
 {
     string fileName = GetFilePath(node);
     tw.Write("{0}: ", node.GetAttribute("name"));
     if (!File.Exists(fileName))
     {
         _out.WriteLine("File {0} not exists.", fileName);
         throw new ArgumentException();
     }
     XQueryCommand command = new XQueryCommand();
     try
     {
         TextReader textReader = new StreamReader(fileName, true);
         command.CommandText = textReader.ReadToEnd();
         textReader.Close();
         command.OnResolveCollection += new ResolveCollectionEvent(command_OnResolveCollection);
         foreach (XmlNode child in node.ChildNodes)
         {
             XmlElement curr = child as XmlElement;
             if (curr == null || curr.NamespaceURI != XQTSNamespace)
                 continue;
             if (curr.LocalName == "module")
             {
                 string ns = curr.GetAttribute("namespace");
                 string id = curr.InnerText;
                 command.DefineModuleNamespace(ns, _module[id]);
             }
         }
         foreach (string[] schema in _schema.Values)
             command.DefineSchemaNamespace(schema[0], schema[1]);
         command.Compile();
         foreach (XmlNode child in node.ChildNodes)
         {
             XmlElement curr = child as XmlElement;
             if (curr == null || curr.NamespaceURI != XQTSNamespace)
                 continue;
             if (curr.LocalName == "input-file")
             {
                 string var = curr.GetAttribute("variable");
                 string id = curr.InnerText;
                 IXPathNavigable doc = command.Context.OpenDocument(_sources[id]);
                 command.Parameters.Add(new XQueryParameter(var, doc.CreateNavigator()));
             }
             else if (curr.LocalName == "contextItem")
             {
                 string id = curr.InnerText;
                 XmlReader reader = XmlReader.Create(_sources[id], command.Context.GetSettings());
                 XQueryDocument doc = new XQueryDocument(reader);
                 command.ContextItem = doc.CreateNavigator();
             }
             else if (curr.LocalName == "defaultCollection")
             {
                 string id = curr.InnerText;
                 command.OnResolveCollection += new ResolveCollectionEvent(delegate(object sender, ResolveCollectionArgs args)
                     {
                         if (args.CollectionName == "")
                             args.Collection = CreateCollection(command, id);
                     });
             }
             else if (curr.LocalName == "input-URI")
             {
                 string var = curr.GetAttribute("variable");
                 string value = curr.InnerText;
                 string expandedUri;
                 if (!_sources.TryGetValue(value, out expandedUri))
                     expandedUri = value;
                 command.Parameters.Add(new XQueryParameter(var, expandedUri));
             }
             else if (curr.LocalName == "input-query")
             {
                 string var = curr.GetAttribute("variable");
                 using (XQueryCommand command2 = new XQueryCommand())
                 {
                     TextReader textReader2 = new StreamReader(Path.Combine(
                         Path.GetDirectoryName(fileName), curr.GetAttribute("name") + ".xq"), true);
                     command2.CommandText = textReader2.ReadToEnd();
                     textReader2.Close();
                     command.Parameters.Add(new XQueryParameter(var, command2.Execute()));
                 }
             }
         }
         return command;
     }
     catch (Exception)
     {
         command.Dispose();
         throw;
     }
 }
Exemple #5
0
 public ItemSegment(XQueryDocument document)
 {
     this.document = document;                
 }
Exemple #6
0
 public ElementsSegment(XQueryDocument document)
     : base(document)
 {
 }