Antlr4.StringTemplate.TemplateGroup.LoadTemplateFile C# (CSharp) Method

LoadTemplateFile() public method

public LoadTemplateFile ( string prefix, string unqualifiedFileName, ICharStream templateStream ) : CompiledTemplate
prefix string
unqualifiedFileName string
templateStream ICharStream
return Antlr4.StringTemplate.Compiler.CompiledTemplate
        public virtual CompiledTemplate LoadTemplateFile(string prefix, string unqualifiedFileName, ICharStream templateStream)
        {
            GroupLexer lexer = new GroupLexer(templateStream);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            GroupParser parser = new GroupParser(tokens);
            parser.Group = this;
            lexer.group = this;
            try
            {
                parser.templateDef(prefix);
            }
            catch (RecognitionException re)
            {
                ErrorManager.GroupSyntaxError(ErrorType.SYNTAX_ERROR, unqualifiedFileName, re, re.Message);
            }

            string templateName = Path.GetFileNameWithoutExtension(unqualifiedFileName);
            if (!string.IsNullOrEmpty(prefix))
                templateName = prefix + templateName;

            CompiledTemplate impl = RawGetTemplate(templateName);
            impl.Prefix = prefix;
            return impl;
        }

Usage Example

Example #1
0
        /** Import template files, directories, and group files.
         *  Priority is given to templates defined in the current group;
         *  this, in effect, provides inheritance. Polymorphism is in effect so
         *  that if an inherited template references template t() then we
         *  search for t() in the subgroup first.
         *
         *  Templates are loaded on-demand from import dirs.  Imported groups are
         *  loaded on-demand when searching for a template.
         *
         *  The listener of this group is passed to the import group so errors
         *  found while loading imported element are sent to listener of this group.
         */
        public virtual void ImportTemplates(IToken fileNameToken)
        {
            string fileName = fileNameToken.Text;

            if (Verbose)
                Console.WriteLine("ImportTemplates({0})", fileName);

            // do nothing upon syntax error
            if (fileName == null || fileName.Equals("<missing STRING>"))
                return;

            fileName = Utility.Strip(fileName, 1);

            //Console.WriteLine("import {0}", fileName);
            bool isGroupFile = fileName.EndsWith(GroupFileExtension);
            bool isTemplateFile = fileName.EndsWith(TemplateFileExtension);
            bool isGroupDir = !(isGroupFile || isTemplateFile);

            TemplateGroup g = null;

            // search path is: working dir, g.stg's dir, CLASSPATH
            Uri thisRoot = RootDirUri;
            Uri fileUnderRoot = null;
            //Console.WriteLine("thisRoot={0}", thisRoot);
            try
            {
                fileUnderRoot = new Uri(thisRoot + "/" + fileName);
            }
            catch (UriFormatException mfe)
            {
                ErrorManager.InternalError(null, string.Format("can't build URL for {0}/{1}", thisRoot, fileName), mfe);
                return;
            }

            if (isTemplateFile)
            {
                g = new TemplateGroup();
                g.Listener = this.Listener;
                Uri fileURL = null;
                if (File.Exists(fileUnderRoot.LocalPath))
                    fileURL = fileUnderRoot;

                if (fileURL != null)
                {
                    try
                    {
                        Stream s = File.OpenRead(fileURL.LocalPath);
                        ANTLRInputStream templateStream = new ANTLRInputStream(s);
                        templateStream.name = fileName;
                        CompiledTemplate code = g.LoadTemplateFile("/", fileName, templateStream);
                        if (code == null)
                            g = null;
                    }
                    catch (IOException ioe)
                    {
                        ErrorManager.InternalError(null, string.Format("can't read from {0}", fileURL), ioe);
                        g = null;
                    }
                }
                else
                {
                    g = null;
                }
            }
            else if (isGroupFile)
            {
                //System.out.println("look for fileUnderRoot: "+fileUnderRoot);
                if (File.Exists(fileUnderRoot.LocalPath))
                {
                    g = new TemplateGroupFile(fileUnderRoot, Encoding, delimiterStartChar, delimiterStopChar);
                    g.Listener = this.Listener;
                }
                else
                {
                    g = new TemplateGroupFile(fileName, delimiterStartChar, delimiterStopChar);
                    g.Listener = this.Listener;
                }
            }
            else if (isGroupDir)
            {
                //			System.out.println("try dir "+fileUnderRoot);
                if (Directory.Exists(fileUnderRoot.LocalPath))
                {
                    g = new TemplateGroupDirectory(fileUnderRoot, Encoding, delimiterStartChar, delimiterStopChar);
                    g.Listener = this.Listener;
                }
                else
                {
                    // try in CLASSPATH
                    //				System.out.println("try dir in CLASSPATH "+fileName);
                    g = new TemplateGroupDirectory(fileName, delimiterStartChar, delimiterStopChar);
                    g.Listener = this.Listener;
                }
            }

            if (g == null)
            {
                ErrorManager.CompiletimeError(ErrorType.CANT_IMPORT, null, fileNameToken, fileName);
            }
            else
            {
                ImportTemplates(g, true);
            }
        }