示例#1
0
        /// <summary>
        /// Returns a stream object for a grammar.
        /// </summary>
        private MemoryStream LoadCfg(bool isImportedGrammar, bool stgInit)
        {
            // No parameters to the constructors
            Uri          uriGrammar = Uri;
            MemoryStream stream     = new();

            if (uriGrammar != null)
            {
                throw new PlatformNotSupportedException();
            }
            else if (_srgsDocument != null)
            {
                // If srgs, compile to a stream
                SrgsGrammarCompiler.Compile(_srgsDocument, stream);
                if (_baseUri == null && _srgsDocument.BaseUri != null)
                {
                    // If we loaded the SrgsDocument from a file then that should be used as the base path.
                    // But it should not override any baseUri supplied directly to the Grammar constructor or in the xmlBase attribute in the xml.
                    _baseUri = _srgsDocument.BaseUri;

                    // So the priority order for getting the base path is:
                    // 1. The xml:base attribute in the xml.
                    // 2. The baseUri passed to the Grammar constructor.
                    // 3. The path the xml was originally loaded from.
                }
            }
            else if (_grammarBuilder != null)
            {
                // If GrammarBuilder, compile to a stream
                _grammarBuilder.Compile(stream);
            }
            else
            {
                // If stream, load
                SrgsGrammarCompiler.CompileXmlOrCopyCfg(_appStream, stream, null);
            }

            stream.Position = 0;

            // Update the rule name
            _ruleName = CheckRuleName(stream, _ruleName, isImportedGrammar, stgInit, out _sapi53Only, out _semanticTag);

            // Create an app domain for the grammar code if any
            CreateSandbox(stream);

            stream.Position = 0;
            return(stream);
        }
示例#2
0
        private MemoryStream LoadCfg(bool isImportedGrammar, bool stgInit)
        {
            Uri          uri          = Uri;
            MemoryStream memoryStream = new MemoryStream();

            if (uri != null)
            {
                string mimeType;
                string localPath;
                using (Stream stream = _resourceLoader.LoadFile(uri, out mimeType, out _baseUri, out localPath))
                {
                    stream.Position = 0L;
                    SrgsGrammarCompiler.CompileXmlOrCopyCfg(stream, memoryStream, uri);
                }
                _resourceLoader.UnloadFile(localPath);
            }
            else if (_srgsDocument != null)
            {
                SrgsGrammarCompiler.Compile(_srgsDocument, memoryStream);
                if (_baseUri == null && _srgsDocument.BaseUri != null)
                {
                    _baseUri = _srgsDocument.BaseUri;
                }
            }
            else if (_grammarBuilder != null)
            {
                _grammarBuilder.Compile(memoryStream);
            }
            else
            {
                SrgsGrammarCompiler.CompileXmlOrCopyCfg(_appStream, memoryStream, null);
            }
            memoryStream.Position = 0L;
            _ruleName             = CheckRuleName(memoryStream, _ruleName, isImportedGrammar, stgInit, out _sapi53Only, out _semanticTag);
            CreateSandbox(memoryStream);
            memoryStream.Position = 0L;
            return(memoryStream);
        }