Exemple #1
0
 private void ProcessImportModule(Notation notation, Notation.Record rec)
 {
     Literal[] arr = null;
     Literal targetNamespace;
     if (rec.args.Length == 2)
     {
         targetNamespace = (Literal)rec.Arg0;
         if (targetNamespace.Data == "")
             throw new XQueryException(Properties.Resources.XQST0088);
         if (_moduleNs.Contains(targetNamespace.Data))
             throw new XQueryException(Properties.Resources.XQST0047, targetNamespace.Data);
         if (rec.args[1] != null)
             arr = Lisp.ToArray<Literal>(rec.args[1]);
         else
             arr = _context.ResolveModuleImport(String.Empty, targetNamespace.Data);
     }
     else
     {
         Qname prefix = (Qname)rec.Arg0;
         targetNamespace = (Literal)rec.Arg1;
         if (targetNamespace.Data == "")
             throw new XQueryException(Properties.Resources.XQST0088);
         if (_moduleNs.Contains(targetNamespace.Data))
             throw new XQueryException(Properties.Resources.XQST0047, targetNamespace.Data);
         if (prefix.Name == "xml" || prefix.Name == "xmlns")
             throw new XQueryException(Properties.Resources.XQST0070, targetNamespace.Data);
         if (rec.args[2] != null)
             arr = Lisp.ToArray<Literal>(rec.args[2]);
         else
             arr = _context.ResolveModuleImport(prefix.Name, targetNamespace.Data);
         if (_context.NamespaceManager.HasNamespace(prefix.Name))
             throw new XQueryException(Properties.Resources.XQST0033, prefix.Name);
         _context.AddNamespace(prefix.Name, targetNamespace.Data);
     }
     for (int k = 0; k < arr.Length; k++)
     {
         Literal filename = (Literal)arr[k];
         TextReader reader;
         string absolutePath;
         if (Uri.IsWellFormedUriString(filename.Data, UriKind.Absolute) ||
             (_context.BaseUri != null && Uri.IsWellFormedUriString(_context.BaseUri, UriKind.Absolute)))
         {
             Uri uri;
             WebClient client = new WebClient();
             client.Proxy.Credentials = CredentialCache.DefaultCredentials;
             if (!Uri.TryCreate(filename.Data, UriKind.Absolute, out uri))
                 uri = new Uri(new Uri(_context.BaseUri), filename.Data);
             absolutePath = uri.AbsoluteUri;
             reader = new StringReader(client.DownloadString(uri));
         }
         else
         {
             if (_context.BaseUri != null)
                 absolutePath = Path.Combine(_context.BaseUri, filename.Data);
             else
                 absolutePath = filename.Data;
             reader = new StreamReader(new FileStream(absolutePath,
                 FileMode.Open, FileAccess.Read));
         }
         TokenizerBase tok = new Tokenizer(reader.ReadToEnd());
         reader.Close();
         Notation notation2 = new Notation();
         YYParser parser = new YYParser(notation2);
         parser.yyparseSafe(tok);
         XQueryContext context2 = new XQueryContext(targetNamespace.Data, absolutePath, _context);
         Translator translator = new Translator(context2);
         translator.PreProcess(notation2);
         translator.Process(notation2);
         _varTable.ImportVariables(context2);
         _context.FunctionTable.CopyFrom(context2.FunctionTable, context2);
         context2.Close();
     }
     _moduleNs.Add(targetNamespace.Data);
 }
Exemple #2
0
 public void Compile()
 {
     CheckDisposed();
     if (String.IsNullOrEmpty(CommandText))
         throw new XQueryException("CommandText is empty string");
     TokenizerBase tok = new Tokenizer(CommandText);
     Notation notation = new Notation();
     YYParser parser = new YYParser(notation);
     parser.yyparseSafe(tok);
     if (BaseUri != null)
         m_context.BaseUri = BaseUri;
     if (SearchPath != null)
         m_context.SearchPath = SearchPath;
     m_context.SchemaProcessing = SchemaProcessing;
     m_context.Engine.Prepare();
     SymbolLink contextLink = new SymbolLink(typeof(IContextProvider));
     m_context.Engine.DefaultPool.Bind(contextLink);
     m_context.Engine.DefaultPool.SetData(contextLink, this);
     m_context.Resolver.SetValue(ID.Context, contextLink);
     Translator translator = new Translator(m_context);
     translator.PreProcess(notation);
     if (OnPreProcess != null) // Chance to process custom declare option statement
         OnPreProcess(this, EventArgs.Empty);
     m_res = translator.Process(notation);
     if (m_res == null)
         throw new XQueryException("Can't run XQuery function module");
     m_res.Bind(null, m_context.Engine.DefaultPool);
     // Compile variables and check it for XQST0054
     m_vars = new FunctionLink[m_context.variables.Count];
     Dictionary<SymbolLink, SymbolLink[]> dependences = new Dictionary<SymbolLink, SymbolLink[]>();
     for (int k = 0; k < m_context.variables.Count; k++)
     {
         XQueryContext.VariableRecord rec = m_context.variables[k];
         m_vars[k] = new FunctionLink();
         HashSet<Object> hs = new HashSet<object>();
         dependences.Add(rec.link, m_context.Engine.GetValueDependences(hs, null, rec.expr, m_vars[k], true));
     }
     int i = 0;
     foreach (KeyValuePair<SymbolLink, SymbolLink[]> kvp in dependences)
     {
         List<SymbolLink> closure = new List<SymbolLink>();
         HashSet<SymbolLink> hs = new HashSet<SymbolLink>();
         hs.Add(kvp.Key);
         closure.AddRange(kvp.Value);
         bool expand;
         do
         {
             expand = false;
             for (int k = 0; k < closure.Count; k++)
             {
                 SymbolLink[] values;
                 if (dependences.TryGetValue(closure[k], out values))
                 {
                     if (hs.Contains(closure[k]))
                         throw new XQueryException(Properties.Resources.XQST0054, m_context.variables[i].id);
                     hs.Add(closure[k]);
                     closure.RemoveAt(k);
                     closure.AddRange(values);
                     expand = true;
                     break;
                 }
             }
         }
         while (expand);
         i++;
     }
     m_compiled = true;
 }