Exemplo n.º 1
0
        /** Load full path name .st file relative to root by prefix */
        public virtual CompiledTemplate LoadTemplateFile(string prefix, string fileName)
        {
            if (Path.IsPathRooted(fileName))
            {
                throw new ArgumentException();
            }

            //System.out.println("load "+fileName+" from "+root+" prefix="+prefix);
            string templateName = Path.ChangeExtension(fileName, null);
            Uri    f            = null;

            try
            {
                f = new Uri(Path.Combine(root.LocalPath, fileName));
            }
            catch (UriFormatException me)
            {
                ErrorManager.RuntimeError(null, 0, ErrorType.INVALID_TEMPLATE_NAME, me, Path.Combine(root.LocalPath, fileName));
                return(null);
            }

            ANTLRReaderStream fs = null;

            try
            {
                fs = new ANTLRReaderStream(new StreamReader(f.LocalPath, Encoding ?? Encoding.UTF8));
            }
            catch (IOException)
            {
                // doesn't exist; just return null to say not found
                return(null);
            }

            GroupLexer lexer = new GroupLexer(fs);

            fs.name = fileName;
            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, Path.GetFileName(f.LocalPath), re, re.Message);
            }

            return(RawGetTemplate(templateName));
        }