Exemple #1
0
 public void TestInstanceofRenderer()
 {
     string templates =
             "numberThing(x,y,z) ::= \"numbers: <x>, <y>; <z>\"\n";
     writeFile(tmpdir, "t.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
     group.RegisterRenderer(typeof(int), new NumberRenderer());
     group.RegisterRenderer(typeof(double), new NumberRenderer());
     Template st = group.GetInstanceOf("numberThing");
     st.Add("x", -2100);
     st.Add("y", 3.14159);
     st.Add("z", "hi");
     string expecting = "numbers: -2100, 3.14159; hi";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Exemple #2
0
        public void TestLocaleWithNumberRenderer()
        {
            //string templates = "foo(x,y) ::= << <x; format=\"%,d\"> <y; format=\"%,2.3f\"> >>\n";
            string templates = "foo(x,y) ::= << <x; format=\"{0:#,#}\"> <y; format=\"{0:0.000}\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(int), new NumberRenderer());
            group.RegisterRenderer(typeof(double), new NumberRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", -2100);
            st.Add("y", 3.14159);
            // Polish uses ' ' (ASCII 160) for ',' and ',' for '.'
            string expecting = " -2 100 3,142 "; // Ê
            string result = st.Render(new CultureInfo("pl"));
            Assert.AreEqual(expecting, result);
        }
Exemple #3
0
        protected virtual TemplateGroup LoadTemplates()
        {
            TemplateGroup result = new TemplateGroupFile(
                Path.Combine(
                    Path.GetDirectoryName(typeof(AntlrTool).GetTypeInfo().Assembly.Location),
                    Path.Combine(CodeGenerator.TEMPLATE_ROOT, GetLanguage(), GetLanguage() + TemplateGroup.GroupFileExtension)),
                Encoding.UTF8);
            result.RegisterRenderer(typeof(int), new NumberRenderer());
            result.RegisterRenderer(typeof(string), new StringRenderer());
            result.Listener = new ErrorListener(this);

            return result;
        }
Exemple #4
0
        public void TestNumberRendererWithPrintfFormat()
        {
            //string templates = "foo(x,y) ::= << <x; format=\"%d\"> <y; format=\"%2.3f\"> >>\n";
            string templates = "foo(x,y) ::= << <x; format=\"{0}\"> <y; format=\"{0:0.000}\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(int), new NumberRenderer());
            group.RegisterRenderer(typeof(double), new NumberRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", -2100);
            st.Add("y", 3.14159);
            string expecting = " -2100 3.142 ";
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemple #5
0
        public void TestStringRendererWithPrintfFormat()
        {
            string templates =
                    "foo(x) ::= << <x; format=\"{0,6}\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(string), new StringRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", "hi");
            string expecting = "     hi ";
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemple #6
0
        public void TestStringRendererWithTemplateInclude_cap()
        {
            // must toString the t() ref before applying format
            string templates =
                    "foo(x) ::= << <(t()); format=\"cap\"> >>\n" +
                    "t() ::= <<ack>>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            //Interpreter.trace = true;
            group.RegisterRenderer(typeof(string), new StringRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", "hi");
            string expecting = " Ack ";
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemple #7
0
        public void TestStringRendererWithFormat_cap_emptyValue()
        {
            string templates =
                    "foo(x) ::= << <x; format=\"cap\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(string), new StringRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", "");
            string expecting = " ";//FIXME: why not two spaces?
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemple #8
0
        public void TestStringRendererWithFormat_xml_encode()
        {
            string templates =
                    "foo(x) ::= << <x; format=\"xml-encode\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(string), new StringRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", "a<b> &\t\b");
            string expecting = " a&lt;b&gt; &amp;\t\b ";
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemple #9
0
        public void TestRendererWithPredefinedFormat4()
        {
            string templates =
                    "dateThing(created) ::= << time: <created; format=\"time:medium\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(DateTime), new DateRenderer());
            group.RegisterRenderer(typeof(DateTimeOffset), new DateRenderer());
            Template st = group.GetInstanceOf("dateThing");
            st.Add("created", new DateTime(2005, 7, 5));
            string expecting = " time: 12:00:00 AM ";
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemple #10
0
 public void TestRendererForGroup()
 {
     string templates =
             "dateThing(created) ::= \"datetime: <created>\"\n";
     writeFile(tmpdir, "t.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
     group.RegisterRenderer(typeof(DateTime), new DateRenderer());
     group.RegisterRenderer(typeof(DateTimeOffset), new DateRenderer());
     Template st = group.GetInstanceOf("dateThing");
     st.Add("created", new DateTime(2005, 7, 5));
     string expecting = "datetime: 7/5/2005 12:00 AM";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Exemple #11
0
        public static int DT2STG(DataTable table, String stgFileName, String outFileName, String optFileName)
        {
            //tidy up input parameters
            stgFileName = stgFileName.Trim(); // StringTemplateGroup file
            outFileName = outFileName.Trim(); // Output file
            optFileName = optFileName.Trim(); // Options file (new 20/10/11)

            //Fail if stgFileName not passed in
            if (stgFileName == String.Empty)
                throw new ArgumentException("template file name required", "stgFileName");
            //Fail if outFileName not passed in
            if (outFileName == String.Empty)
                throw new ArgumentException("output file name required", "outFileName");

            // Get options - these will be passed to HEADER, RECORD and FOOTER templates
            // Note - Options file is optional - may not be present
            IDictionary<string, object> options = new Dictionary<string, object>();
            if (optFileName != String.Empty)
            {
                DataTable dtOptions = Delimited2DT(optFileName, true);
                if (dtOptions.Rows.Count > 0)
                {
                    foreach (DataColumn dc in dtOptions.Columns)
                    {
                        String s = dtOptions.Rows[0][dc].ToString();
                        // Ensure any leading and trailing double quotes are removed..
                        s = trimQuotes(s);
                        // Add cleaned value to the array (if not blank)
                        if (s != "")
                        {
                            options[dc.ColumnName.Trim()] = s;
                        }
                    }
                }
            }

            //Get full path to the STG file, if not already passed in
            String path = System.IO.Path.GetDirectoryName(stgFileName);
            if (path == "")
                stgFileName = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), stgFileName);

            //Revised for Antlr4...Read the template group from the file, define default delimiters
            TemplateGroupFile stg = new TemplateGroupFile(stgFileName,'$','$');

            //Register renderer for performing Url/Xml Encoding
            stg.RegisterRenderer(typeof(String), new BasicFormatRenderer());

            //System.Collections.ArrayList records = new System.Collections.ArrayList();

            //Write the results to the output file
            int rowCount = 0;
            System.IO.StreamWriter sw = null;
            try
            {
                sw = new System.IO.StreamWriter(outFileName, false);

                // If the HEADER template is present, call it and write result to output file
                if (stg.IsDefined("HEADER"))
                {
                    Template stHeader = stg.GetInstanceOf("HEADER");
                    stHeader.Add("options", options);
                    sw.WriteLine(stHeader.Render());
                }

                foreach (DataRow dr in table.Rows)
                {
                    IDictionary<string, object> record = new Dictionary<string, object>();
                    foreach (DataColumn dc in table.Columns)
                    {
                        String s = dr[dc].ToString();
                        // Ensure any leading and trailing double quotes are removed..
                        s = trimQuotes(s);
                        // Add cleaned value to the array (if not blank)
                        if (s != "")
                        {
                            record[dc.ColumnName.Trim()] = s;
                        }
                    }
                    // If the RECORD template is present, call it and write result to output file
                    if (stg.IsDefined("RECORD"))
                    {
                        Template stRecord = stg.GetInstanceOf("RECORD");
                        stRecord.Add("data", record);
                        stRecord.Add("options", options);
                        sw.WriteLine(stRecord.Render());
                    }

                    //records.Add(record);
                    rowCount++;
                }

                // If the FOOTER template is present, call it and write result to output file
                if (stg.IsDefined("FOOTER"))
                {
                    Template stFooter = stg.GetInstanceOf("FOOTER");
                    stFooter.Add("options", options);
                    sw.WriteLine(stFooter.Render());
                }
            }
            catch (Exception ex)
            {
                //worth catching this?
                throw new Exception("Error during conversion: " + ex.Message, ex.InnerException);
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
            return rowCount;
        }
Exemple #12
0
 public void TestRendererWithFormat()
 {
     string templates =
             "dateThing(created) ::= << date: <created; format=\"yyyy.MM.dd\"> >>\n";
     writeFile(tmpdir, "t.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
     group.RegisterRenderer(typeof(DateTime), new DateRenderer());
     group.RegisterRenderer(typeof(DateTimeOffset), new DateRenderer());
     Template st = group.GetInstanceOf("dateThing");
     st.Add("created", new DateTime(2005, 07, 05));
     string expecting = " date: 2005.07.05 ";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Exemple #13
0
        protected override TemplateGroup LoadTemplates()
        {
            // override the superclass behavior to put all C# templates in the same folder
            TemplateGroup result = new TemplateGroupFile(
                Path.Combine(
                    Path.GetDirectoryName(typeof(AntlrTool).GetTypeInfo().Assembly.Location),
                    Path.Combine(CodeGenerator.TEMPLATE_ROOT, "CSharp", GetLanguage() + TemplateGroup.GroupFileExtension)),
                Encoding.UTF8);
            result.RegisterRenderer(typeof(int), new NumberRenderer());
            result.RegisterRenderer(typeof(string), new StringRenderer());
            result.Listener = new ErrorListener(this);

            return result;
        }