示例#1
0
        /// <summary>
        /// Creates an XML grammar which uses the base XML file passed in, and adds a rule for the list indicated.
        /// </summary>
        /// <param name="baseXmlFilePath">Absolute file path to the XML file tat we should use as the base.</param>
        /// <param name="ruleName">The name of the rule we are creating from the list.</param>
        /// <param name="list">List of values that the new rule may be. This list may be empty.</param>
        /// <returns>XmlGrammar instance which can be used to describe a grammar with the given base and the list of values in a rule.</returns>
        public static XmlGrammar CreateGrammarFromList(String baseXmlFilePath, String ruleName, List <String> list)
        {
            List <XmlElement> remoteElements = new List <XmlElement>();

            if (!list.Any())
            {
                // If the list is empty, the grammar file becomes invalid.
                // Therefore, I will add a nonsensical string that will never be detect
                // to prevent the grammar from erroring.
                // A better solution should be found for this, but it's not too important
                // right now.
                list.Add("XYZ123");
            }
            XmlDocument mainDoc = new XmlDocument();

            mainDoc.Load(baseXmlFilePath);
            XmlElement oneof = GrammarUtility.CreateListOfPossibleStrings(mainDoc, list);
            XmlElement rule  = GrammarUtility.CreateElement(mainDoc, "rule", new Dictionary <string, string>
            {
                { "id", ruleName },
                { "scope", "public" },
            });

            rule.AppendChild(oneof);
            mainDoc.LastChild.AppendChild(rule);
            return(new XmlGrammar(mainDoc));
        }
        /// <summary>
        /// Creates a grammar file representing all of the main grammars of the grammar providers
        /// to the file requested.
        /// </summary>
        /// <param name="fileToSaveTo">File to save the main grammar to.</param>
        public void UpdateMainGrammarFile(String fileToSaveTo)
        {
            List <String>     rules    = new List <string>();
            List <XmlElement> elements = new List <XmlElement>();

            // Add grammars from all of our grammar providers.
            foreach (GrammarProvider provider in _grammarProviders)
            {
                if (provider.MainGrammar != null)
                {
                    rules.Add(provider.MainGrammar.RuleName);
                    elements.AddRange(provider.MainGrammar.Rules);
                }
            }

            // Add utility rules.
            foreach (String fileName in _utilityFiles)
            {
                XmlDocument fileDoc = new XmlDocument();
                fileDoc.Load(fileName);
                foreach (XmlElement element in fileDoc.LastChild.ChildNodes)
                {
                    elements.Add(element);
                }
            }

            XmlDocument doc = new XmlDocument();

            doc.AppendChild(doc.CreateNode(XmlNodeType.XmlDeclaration, "", ""));

            XmlElement grammar = GrammarUtility.CreateElement(doc, "grammar", new Dictionary <String, String>
            {
                { "version", "1.0" },
                { "xml:lang", "en-US" },
                { "mode", "voice" },
                { "root", "overallCommand" },
                { "tag-format", "semantics/1.0" },
            });
            XmlElement overallRule = GrammarUtility.CreateElement(doc, "rule", new Dictionary <String, String>
            {
                { "id", "overallCommand" },
                { "scope", "public" },
            });
            XmlElement samiRule = GrammarUtility.CreateElement(doc, "item", new Dictionary <String, String>());

            samiRule.InnerText = "Sammie";
            overallRule.AppendChild(samiRule);

            overallRule.AppendChild(GrammarUtility.CreateListOfRuleRefs(doc, rules));
            grammar.AppendChild(overallRule);

            GrammarUtility.CopyElementsToNewRule(grammar, elements);
            doc.AppendChild(grammar);
            doc.Save(fileToSaveTo);
        }
示例#3
0
        /// <summary>
        /// Creates a rule which matches one of the given strings.
        /// The out value is then set to the value itself.
        /// </summary>
        /// <param name="doc">XMLDocument to add this rule to.</param>
        /// <param name="possibleVals">List of values that can be matched.</param>
        /// <returns>Element which represents the rule that has been created.</returns>
        public static XmlElement CreateListOfPossibleStrings(XmlDocument doc, IEnumerable <String> possibleVals)
        {
            XmlElement oneofRule = GrammarUtility.CreateElement(doc, "one-of", new Dictionary <String, String>());

            foreach (String possibleVal in possibleVals)
            {
                XmlElement item = GrammarUtility.CreateElement(doc, "item", new Dictionary <String, String>());
                item.InnerText = possibleVal;
                XmlElement tag = GrammarUtility.CreateElement(doc, "tag", new Dictionary <String, String>());
                tag.InnerText = "out = \"" + possibleVal + "\";";
                item.AppendChild(tag);
                oneofRule.AppendChild(item);
            }
            return(oneofRule);
        }
示例#4
0
        /// <summary>
        /// Creates a rule which matches one of the rule names given.
        /// </summary>
        /// <param name="doc">Document to which the rule should be added.</param>
        /// <param name="ruleNames">The list of names of the rules that might be matched.</param>
        /// <returns>Top level one-of element of the rule.</returns>
        public static XmlElement CreateListOfRuleRefs(XmlDocument doc, IEnumerable <String> ruleNames)
        {
            XmlElement oneofRule = GrammarUtility.CreateElement(doc, "one-of", new Dictionary <String, String>());

            foreach (String rule in ruleNames)
            {
                XmlElement item    = GrammarUtility.CreateElement(doc, "item", new Dictionary <String, String>());
                XmlElement ruleref = GrammarUtility.CreateElement(doc, "ruleref", new Dictionary <String, String>
                {
                    { "uri", String.Format("#{0}", rule) },
                });
                item.AppendChild(ruleref);
                oneofRule.AppendChild(item);
            }
            return(oneofRule);
        }