Antlr4.StringTemplate.TemplateGroupDirectory.Load C# (CSharp) Method

Load() protected method

* Load a template from dir or group file. Group file is given * precedence over dir with same name. name is * always fully qualified. *
protected Load ( string name ) : CompiledTemplate
name string
return Antlr4.StringTemplate.Compiler.CompiledTemplate
        protected override CompiledTemplate Load(string name)
        {
            if (Verbose)
                Console.WriteLine("STGroupDir.load(" + name + ")");

            string parent = Utility.GetParent(name); // must have parent; it's fully-qualified
            string prefix = Utility.GetPrefix(name);

            //    	if (parent.isEmpty()) {
            //    		// no need to check for a group file as name has no parent
            //            return loadTemplateFile("/", name+TemplateFileExtension); // load t.st file
            //    	}

            if (!Path.IsPathRooted(parent))
                throw new ArgumentException();

            Uri groupFileURL;
            try
            {
                // see if parent of template name is a group file
                groupFileURL = new Uri(TemplateName.GetTemplatePath(root.LocalPath, parent) + GroupFileExtension);
            }
            catch (UriFormatException e)
            {
                ErrorManager.InternalError(null, "bad URL: " + TemplateName.GetTemplatePath(root.LocalPath, parent) + GroupFileExtension, e);
                return null;
            }

            if (!File.Exists(groupFileURL.LocalPath))
            {
                string unqualifiedName = Path.GetFileName(name);
                return LoadTemplateFile(prefix, unqualifiedName + TemplateFileExtension); // load t.st file
            }
            #if false
            InputStream @is = null;
            try
            {
                @is = groupFileURL.openStream();
            }
            catch (FileNotFoundException fnfe)
            {
                // must not be in a group file
                return loadTemplateFile(parent, name + TemplateFileExtension); // load t.st file
            }
            catch (IOException ioe)
            {
                errMgr.internalError(null, "can't load template file " + name, ioe);
            }

            try
            {
                // clean up
                if (@is != null)
                    @is.close();
            }
            catch (IOException ioe)
            {
                errMgr.internalError(null, "can't close template file stream " + name, ioe);
            }
            #endif

            LoadGroupFile(prefix, groupFileURL.LocalPath);

            return RawGetTemplate(name);
        }

Usage Example

Example #1
0
        public void printReport(string filename)
        {
            string URIFilename = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + Path.DirectorySeparatorChar + "templates" + Path.DirectorySeparatorChar + "ST-HtmlReport" + Path.DirectorySeparatorChar;
            Uri uri = new Uri(URIFilename);
            logger.Debug("Defining Template directory for HTML report to " + uri.LocalPath);
            TemplateGroup group = new TemplateGroupDirectory(uri.LocalPath, '$', '$');

            ErrorBuffer errors = new ErrorBuffer();
            group.Listener = errors;
            group.Load();

            Template st = group.GetInstanceOf("MainTemplate");

            st.Add("Report", this);

            string result = st.Render();

            if (errors.Errors.Count > 0)
            {
                foreach (TemplateMessage m in errors.Errors)
               {
                    logger.Error(m);
                    throw new Exception(m.ToString());
                }
            }

            StreamWriter output = new StreamWriter(filename, false, Encoding.GetEncoding("UTF-8"));

            output.Write(result);
            output.Close();
        }
All Usage Examples Of Antlr4.StringTemplate.TemplateGroupDirectory::Load