public BlaiseInstrumentTreeWalker(CommonTree tree, CommonTokenStream tokens, BlaiseImportOptions options, string agencyId, string mainLanguage)
        {
            this.tree = tree;
            this.tokens = tokens;
            this.options = options;
            this.MainLanguage = mainLanguage;
            this.AgencyId = agencyId;

            Result = new XDocument();

            DdiInstance = Ddi.Element(Ddi.DdiInstance);
            Ddi.AddNamespaces(DdiInstance);

            ResourcePackage = Ddi.Element(Ddi.ResourcePackage);

            // Required in DDI 3.1
            var purpose = Ddi.Element(Ddi.Purpose);
            purpose.Add(Ddi.XmlLang(MainLanguage));
            purpose.Add(new XElement(Ddi.Content, "Not Specified"));
            ResourcePackage.Add(purpose);

            Instrument = Ddi.Element(Ddi.Instrument);
            ControlConstructScheme = Ddi.Element(Ddi.ControlConstructScheme);

            XElement groupDataCollection = Ddi.Element(Ddi.GroupDataCollection, false);
            XElement dataCollection = Ddi.Element(Ddi.DataCollection);
            groupDataCollection.Add(dataCollection);
            dataCollection.Add(Instrument);
            ResourcePackage.Add(groupDataCollection);
            ResourcePackage.Add(ControlConstructScheme);
            DdiInstance.Add(ResourcePackage);
        }
示例#2
0
        static int Main(string[] args)
        {
            if (args.Length != 2)
            {
                PrintUsage();
                return 1;
            }

            string fileName = args[0];
            string outputFileName = args[1];

            BlaiseImportOptions options = new BlaiseImportOptions();

            try
            {
                BlaiseConverter converter = new BlaiseConverter()
                {
                    AgencyId = Properties.Settings.Default.AgencyId,
                    MainLanguage = Properties.Settings.Default.MainLanguage
                };

                converter.ConvertPhase1(fileName);
                converter.ConvertPhase2(options, outputFileName);

                foreach (string msg in converter.Messages)
                {
                    Console.WriteLine(msg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 2;
            }

            return 0;
        }
        public BlaiseImportOptions ConvertPhase1(string fileName)
        {
            CaseInsensitiveFileStream stream = new CaseInsensitiveFileStream(fileName);
            stream.UpperCaseGrammar = true;

            // Run the lexer.
            BlaiseLexer lexer = new BlaiseLexer(stream);
            this.tokens = new CommonTokenStream(lexer, Lexer.DEFAULT_TOKEN_CHANNEL);

            // Create the parser and sign up for error events.
            BParser parser = new BParser(this.tokens);
            parser.TreeAdaptor = new CommonTreeAdaptor();
            parser.Error += new EventHandler<ParseErrorEventArgs>(parser_Error);
            parser.Warning += new EventHandler<ParseErrorEventArgs>(parser_Warning);
            parser.Message += new EventHandler<ParseErrorEventArgs>(parser_Message);

            // Run the parser.
            BParser.blaise_file_return r = null;
            try
            {
                r = parser.blaise_file();
            }
            catch (RecognitionException e)
            {
                TriggerError(e);
            }

            foreach (string missingFile in lexer.MissingFiles)
            {
                this.Messages.Add("Could not find " + missingFile);
            }

            if (r == null)
            {
                return null;
            }

            this.tree = r.Tree as CommonTree;

            if (this.tree == null ||
                this.tokens == null)
            {
                // TODO log this and/or send an error.
                return null;
            }

            // Prepare for the tree walkers.
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
            nodes.TokenStream = this.tokens;

            // Walk the tree to find language definitions.
            BlaiseImportOptions options = new BlaiseImportOptions() { BaseFilePath = fileName };
            LanguageTreeWalker ltw = new LanguageTreeWalker(tree);
            ltw.Parse();
            options.DefinedLanguages = ltw.DefinedLanguages;
            options.DefinedLanguagesOrder = ltw.DefinedLanguagesOrder;

            return options;
        }
        public void ConvertPhase2(BlaiseImportOptions options, string outputFileName)
        {
            if (this.tree == null || this.tokens == null)
            {
                throw new InvalidOperationException("Call ConvertPhase1 first");
            }

            this.options = options;

            BlaiseInstrumentTreeWalker itw = new BlaiseInstrumentTreeWalker(this.tree, this.tokens, options, this.AgencyId, this.MainLanguage);
            itw.ParseInstrument();

            //InstrumentShrinker shrinker = new InstrumentShrinker(itw.ActivityToTree, tokens);
            //this.Instrument.Accept(shrinker);

            itw.DdiInstance.Save(outputFileName, System.Xml.Linq.SaveOptions.None);
        }