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(); }
public void TestFullyQualifiedGetInstanceOf() { string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); TemplateGroup group = new TemplateGroupDirectory(dir); Assert.AreEqual("foo", group.GetInstanceOf("a").Render()); Assert.AreEqual("foo", group.GetInstanceOf("/a").Render()); }
public void TestAbsoluteTemplateRefInExpr() { // /randomdir/a and /randomdir/subdir/b string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= << </subdir/b()> >>\n"); writeFile(Path.Combine(dir, "subdir"), "b.st", "b() ::= <<bar>>\n"); TemplateGroup group = new TemplateGroupDirectory(dir); Assert.AreEqual(" bar ", group.GetInstanceOf("a").Render()); }
public void TestFullyQualifiedTemplateRef() { // /randomdir/a and /randomdir/subdir/b string dir = tmpdir; writeFile(Path.Combine(dir, "subdir"), "a.st", "a() ::= << </subdir/b()> >>\n"); writeFile(Path.Combine(dir, "subdir"), "b.st", "b() ::= <<bar>>\n"); TemplateGroup group = new TemplateGroupDirectory(dir); Template template = group.GetInstanceOf("/subdir/a"); Assert.IsNotNull(template); Assert.AreEqual(" bar ", template.Render()); template = group.GetInstanceOf("subdir/a"); Assert.IsNotNull(template); Assert.AreEqual(" bar ", template.Render()); }
public void TestAnonymousTemplateInRegionInSubdir() { //fails since it makes region name /region__/g/a/_r string dir = tmpdir; string g = "a() ::= <<[<@r()>]>>\n" + "@a.r() ::= <<\n" + "<[\"foo\"]:{x|<x>}>\n" + ">>\n"; writeFile(dir, "g.stg", g); TemplateGroup group = new TemplateGroupDirectory(dir); group.Verbose = true; Template st = group.GetInstanceOf("g/a"); string expected = "[foo]"; string result = st.Render(); Assert.AreEqual(expected, result); }
/** Load group dir or file (if .stg suffix) and then import templates. Don't hold * an independent ref to the "supergroup". * * Override this if you want to look for groups elsewhere (database maybe?) * * ImportTemplates("org.foo.proj.G.stg") will try to find file org/foo/proj/G.stg * relative to current dir or in CLASSPATH. The name is not relative to this group. * Can use "/a/b/c/myfile.stg" also or "/a/b/c/mydir". * * Pass token so you can give good error if you want. */ public virtual void ImportTemplates(IToken fileNameToken) { string fileName = fileNameToken.Text; // do nothing upon syntax error if (fileName == null || fileName.Equals("<missing STRING>")) { return; } fileName = Utility.Strip(fileName, 1); TemplateGroup g = null; if (fileName.EndsWith(".stg")) { g = new TemplateGroupFile(fileName, delimiterStartChar, delimiterStopChar); } else { g = new TemplateGroupDirectory(fileName, delimiterStartChar, delimiterStopChar); } ImportTemplates(g); }
public void TestGroupFileInDir() { // /randomdir/a and /randomdir/group.stg with b and c templates string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); string groupFile = "b() ::= \"bar\"\n" + "c() ::= \"duh\"\n"; writeFile(dir, "group.stg", groupFile); TemplateGroup group = new TemplateGroupDirectory(dir); Assert.AreEqual("foo", group.GetInstanceOf("a").Render()); Assert.AreEqual("bar", group.GetInstanceOf("/group/b").Render()); Assert.AreEqual("duh", group.GetInstanceOf("/group/c").Render()); }
public void TestRefToAnotherTemplateInSameSubdir() { // /randomdir/a and /randomdir/subdir/b string dir = tmpdir; writeFile(Path.Combine(dir, "subdir"), "a.st", "a() ::= << <b()> >>\n"); writeFile(Path.Combine(dir, "subdir"), "b.st", "b() ::= <<bar>>\n"); TemplateGroup group = new TemplateGroupDirectory(dir); group.GetInstanceOf("/subdir/a").impl.Dump(); Assert.AreEqual(" bar ", group.GetInstanceOf("/subdir/a").Render()); }
public void TestFullyQualifiedTemplateRef2() { // /randomdir/a and /randomdir/group.stg with b and c templates string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= << </group/b()> >>\n"); string groupFile = "b() ::= \"bar\"\n" + "c() ::= \"</a()>\"\n"; writeFile(dir, "group.stg", groupFile); TemplateGroup group = new TemplateGroupDirectory(dir); Template st1 = group.GetInstanceOf("/a"); Assert.IsNotNull(st1); Assert.AreEqual(" bar ", st1.Render()); Template st2 = group.GetInstanceOf("/group/c"); // invokes /a Assert.IsNotNull(st2); Assert.AreEqual(" bar ", st2.Render()); }
public void TestImportTemplateFromSubdir() { // /randomdir/x/subdir/a and /randomdir/y/subdir/b string dir = tmpdir; string a = "a() ::= << </subdir/b()> >>\n"; string b = "b() ::= <<x's subdir/b>>\n"; writeFile(dir, Path.Combine("x", "subdir", "a.st"), a); writeFile(dir, Path.Combine("y", "subdir", "b.st"), b); TemplateGroup group1 = new TemplateGroupDirectory(Path.Combine(dir, "x")); TemplateGroup group2 = new TemplateGroupDirectory(Path.Combine(dir, "y")); group1.ImportTemplates(group2); Template st = group1.GetInstanceOf("subdir/a"); string expected = " x's subdir/b "; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestSimpleDefaultArg() { string dir = tmpdir; string a = "a() ::= << <b()> >>\n"; string b = "b(x=\"foo\") ::= \"<x>\"\n"; writeFile(dir, "a.st", a); writeFile(dir, "b.st", b); TemplateGroup group = new TemplateGroupDirectory(dir); Template st = group.GetInstanceOf("a"); string expected = " foo "; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestGroupFileInSubDir() { // /randomdir/a and /randomdir/group.stg with b and c templates string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); string groupFile = "b() ::= \"bar\"\n" + "c() ::= \"duh\"\n"; writeFile(dir, "subdir/group.stg", groupFile); TemplateGroup group = new TemplateGroupDirectory(dir); Template st1 = group.GetInstanceOf("a"); Template st2 = group.GetInstanceOf("subdir/group/b"); Template st3 = group.GetInstanceOf("subdir/group/c"); string expected = "foobarduh"; string result = st1.Render() + st2.Render() + st3.Render(); Assert.AreEqual(expected, result); }
public void TestImportTemplateFromAnotherGroupObject() { /* dir1 a.st b.st dir2 a.st */ string dir1 = tmpdir; string a = "a() ::= <<dir1 a>>\n"; string b = "b() ::= <<dir1 b>>\n"; writeFile(dir1, "a.st", a); writeFile(dir1, "b.st", b); string dir2 = tmpdir; a = "a() ::= << <b()> >>\n"; writeFile(dir2, "a.st", a); TemplateGroup group1 = new TemplateGroupDirectory(dir1); TemplateGroup group2 = new TemplateGroupDirectory(dir2); group2.ImportTemplates(group1); Template st = group2.GetInstanceOf("b"); string expected = "dir1 b"; string result = st.Render(); Assert.AreEqual(expected, result); // do it again, but make a template ref imported template st = group2.GetInstanceOf("a"); expected = " dir1 b "; result = st.Render(); Assert.AreEqual(expected, result); }
public void TestSubSubdir() { // /randomdir/a and /randomdir/subdir/b string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); writeFile(Path.Combine(dir, "sub1", "sub2"), "b.st", "b() ::= \"bar\""); TemplateGroup group = new TemplateGroupDirectory(dir); Template st1 = group.GetInstanceOf("a"); Template st2 = group.GetInstanceOf("/sub1/sub2/b"); string expected = "foobar"; string result = st1.Render() + st2.Render(); Assert.AreEqual(expected, result); }
public void TestUnloadingSimpleGroup() { string dir = tmpdir; string a = "a(x) ::= <<foo>>\n"; string b = "b() ::= <<bar>>\n"; writeFile(dir, "a.st", a); writeFile(dir, "b.st", b); TemplateGroup group = new TemplateGroupDirectory(dir); group.Load(); // force load Template st = group.GetInstanceOf("a"); int originalHashCode = RuntimeHelpers.GetHashCode(st); group.Unload(); // blast cache st = group.GetInstanceOf("a"); int newHashCode = RuntimeHelpers.GetHashCode(st); Assert.AreEqual(originalHashCode == newHashCode, false); // diff objects string expected = "foo"; string result = st.Render(); Assert.AreEqual(expected, result); st = group.GetInstanceOf("b"); expected = "bar"; result = st.Render(); Assert.AreEqual(expected, result); }
public void TestSubdirWithSubtemplate() { // /randomdir/a and /randomdir/subdir/b string dir = tmpdir; writeFile(Path.Combine(dir, "subdir"), "a.st", "a(x) ::= \"<x:{y|<y>}>\""); TemplateGroup group = new TemplateGroupDirectory(dir); Template st = group.GetInstanceOf("/subdir/a"); st.Add("x", new string[] { "a", "b" }); Assert.AreEqual("ab", st.Render()); }
public void TestSubdir() { // /randomdir/a and /randomdir/subdir/b string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); writeFile(Path.Combine(dir, "subdir"), "b.st", "b() ::= \"bar\""); TemplateGroup group = new TemplateGroupDirectory(dir); Assert.AreEqual("foo", group.GetInstanceOf("a").Render()); Assert.AreEqual("bar", group.GetInstanceOf("/subdir/b").Render()); Assert.AreEqual("bar", group.GetInstanceOf("subdir/b").Render()); }
public void TestSimpleGroup() { string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); TemplateGroup group = new TemplateGroupDirectory(dir); Template st = group.GetInstanceOf("a"); string expected = "foo"; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestEscapeOneRightAngle() { string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= << > >>"); TemplateGroup group = new TemplateGroupDirectory(dir); Template st = group.GetInstanceOf("a"); st.Add("x", "parrt"); string expected = " > "; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestPolymorphicTemplateReference() { string dir1 = Path.Combine(tmpdir, "d1"); string b = "b() ::= <<dir1 b>>\n"; writeFile(dir1, "b.st", b); string dir2 = Path.Combine(tmpdir, "d2"); string a = "a() ::= << <b()> >>\n"; b = "b() ::= <<dir2 b>>\n"; writeFile(dir2, "a.st", a); writeFile(dir2, "b.st", b); TemplateGroup group1 = new TemplateGroupDirectory(dir1); TemplateGroup group2 = new TemplateGroupDirectory(dir2); group1.ImportTemplates(group2); // normal lookup; a created from dir2 calls dir2.b Template st = group2.GetInstanceOf("a"); string expected = " dir2 b "; string result = st.Render(); Assert.AreEqual(expected, result); // polymorphic lookup; a created from dir1 calls dir2.a which calls dir1.b st = group1.GetInstanceOf("a"); expected = " dir1 b "; result = st.Render(); Assert.AreEqual(expected, result); }
public void TestGroupFileInDirImportsAnotherGroupFile() { // /randomdir/group.stg with a() imports /randomdir/imported.stg with b() // can't have groupdir then groupfile inside that imports string dir = tmpdir; string groupFile = "import \"imported.stg\"\n" + "a() ::= \"a: <b()>\"\n"; writeFile(dir, "group.stg", groupFile); string importedFile = "b() ::= \"b\"\n"; writeFile(dir, "imported.stg", importedFile); ITemplateErrorListener errors = new ErrorBuffer(); TemplateGroup group = new TemplateGroupDirectory(dir); group.Listener = errors; group.GetInstanceOf("/group/a"); string result = errors.ToString(); string substring = "import illegal in group files embedded in TemplateGroupDirectory; import \"imported.stg\" in TemplateGroupDirectory"; StringAssert.Contains(result, substring); }
public void TestUnloadImportedTemplate() { string dir1 = Path.Combine(tmpdir, "d1"); string a = "a() ::= <<dir1 a>>\n"; string b = "b() ::= <<dir1 b>>\n"; writeFile(dir1, "a.st", a); writeFile(dir1, "b.st", b); string dir2 = Path.Combine(tmpdir, "d2"); a = "a() ::= << <b()> >>\n"; writeFile(dir2, "a.st", a); TemplateGroup group1 = new TemplateGroupDirectory(dir1); TemplateGroup group2 = new TemplateGroupDirectory(dir2); group2.ImportTemplates(group1); Template st = group2.GetInstanceOf("a"); Template st2 = group2.GetInstanceOf("b"); int originalHashCode = RuntimeHelpers.GetHashCode(st); int originalHashCode2 = RuntimeHelpers.GetHashCode(st2); group1.Unload(); // blast cache st = group2.GetInstanceOf("a"); int newHashCode = RuntimeHelpers.GetHashCode(st); Assert.AreEqual(originalHashCode == newHashCode, false); // diff objects string expected = " dir1 b "; string result = st.Render(); Assert.AreEqual(expected, result); st = group2.GetInstanceOf("b"); int newHashCode2 = RuntimeHelpers.GetHashCode(st); Assert.AreEqual(originalHashCode2 == newHashCode2, false); // diff objects result = st.Render(); expected = "dir1 b"; Assert.AreEqual(expected, result); }
public void TestImportTemplateFromGroupFile() { // /randomdir/x/subdir/a and /randomdir/y/subdir.stg which has a and b string dir = tmpdir; string a = "a() ::= << </subdir/b()> >>\n"; // get b imported from subdir.stg writeFile(dir, Path.Combine("x", "subdir", "a.st"), a); string groupFile = "a() ::= \"group file: a\"\n" + "b() ::= \"group file: b\"\n"; writeFile(dir, Path.Combine("y", "subdir.stg"), groupFile); TemplateGroup group1 = new TemplateGroupDirectory(Path.Combine(dir, "x")); TemplateGroup group2 = new TemplateGroupDirectory(Path.Combine(dir, "y")); group1.ImportTemplates(group2); Template st = group1.GetInstanceOf("subdir/a"); Assert.IsNotNull(st); Assert.IsNotNull(group1.GetInstanceOf("subdir/b")); string expected = " group file: b "; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestCantSeeGroupDirIfGroupFileOfSameName() { string dir = tmpdir; string a = "a() ::= <<dir1 a>>\n"; writeFile(dir, "group/a.st", a); // can't see this file string groupFile = "b() ::= \"group file b\"\n"; writeFile(dir, "group.stg", groupFile); TemplateGroup group1 = new TemplateGroupDirectory(dir); Template st = group1.GetInstanceOf("group/a"); // can't see Assert.AreEqual(null, st); }
public void TestImportTemplateInGroupFileFromDir() { string dir = tmpdir; string a = "a() ::= << <b()> >>\n"; writeFile(dir, "x/a.st", a); string groupFile = "b() ::= \"group file b\"\n" + "c() ::= \"group file c\"\n"; writeFile(dir, Path.Combine("y", "group.stg"), groupFile); TemplateGroup group1 = new TemplateGroupDirectory(Path.Combine(dir, "x")); TemplateGroup group2 = new TemplateGroupFile(Path.Combine(dir, "y", "group.stg")); group1.ImportTemplates(group2); Template st = group1.GetInstanceOf("a"); st.impl.Dump(); string expected = " group file b "; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestGroupWithTwoTemplates() { string dir = tmpdir; writeFile(dir, "a.st", "a(x) ::= <<foo>>"); writeFile(dir, "b.st", "b() ::= \"bar\""); TemplateGroup group = new TemplateGroupDirectory(dir); Template st1 = group.GetInstanceOf("a"); Template st2 = group.GetInstanceOf("b"); string expected = "foobar"; string result = st1.Render() + st2.Render(); Assert.AreEqual(expected, result); }
public void TestSuper() { string dir1 = Path.Combine(tmpdir, "d1"); string a = "a() ::= <<dir1 a>>\n"; string b = "b() ::= <<dir1 b>>\n"; writeFile(dir1, "a.st", a); writeFile(dir1, "b.st", b); string dir2 = Path.Combine(tmpdir, "d2"); a = "a() ::= << [<super.a()>] >>\n"; writeFile(dir2, "a.st", a); TemplateGroup group1 = new TemplateGroupDirectory(dir1); TemplateGroup group2 = new TemplateGroupDirectory(dir2); group2.ImportTemplates(group1); Template st = group2.GetInstanceOf("a"); string expected = " [dir1 a] "; string result = st.Render(); Assert.AreEqual(expected, result); }
private void writeScenario() { if (alreadyGenerated) initialize(); TemplateGroup group = new TemplateGroupDirectory(this.templatePath, '$', '$'); ErrorBuffer errors = new ErrorBuffer(); group.Listener = errors; group.Load(); Template st = group.GetInstanceOf("MainTemplate"); this.TSFile = genTsStructFromTestContainer(this.outFile, sequence); st.Add("TestStandFile", this.TSFile); 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(this.outFile, false, Encoding.GetEncoding("windows-1251")); output.Write(result); output.Close(); CTsGenericInstr.resetIdCounter(); this.alreadyGenerated = true; }
public void TestRefToAnotherTemplateInSameGroup() { string dir = tmpdir; string a = "a() ::= << <$b()$> >>\n"; string b = "b() ::= <<bar>>\n"; writeFile(dir, "a.st", a); writeFile(dir, "b.st", b); TemplateGroup group = new TemplateGroupDirectory(dir, '$', '$'); Template st = group.GetInstanceOf("a"); string expected = " <bar> "; string result = st.Render(); Assert.AreEqual(expected, result); }
/** 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); } }