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; }
/** <summary> * Where to report errors. All string templates in this group * use this error handler by default. * </summary> */ //class DefaultErrorListener : IStringTemplateErrorListener //{ // public virtual void Error(string s, Exception e) // { // Console.Error.WriteLine(s); // if (e != null) // { // Console.Error.WriteLine(e.Message); // } // } // public virtual void Warning(string s) // { // Console.Out.WriteLine(s); // } //} /// <summary>Convert a System.Data.DataTable using a StringTemplateGroup file</summary> /// <param name="table">DataTable to be converted</param> /// <param name="stgFileName">The name of the StringTemplateGroup file, including absolute or relative path as appropriate</param> /// <param name="outFileName">The name of the file to write the output to</param> /// <returns>The number of records processed</returns> /// <exception cref="System.ArgumentException">Throws an exception if stgFileName or outFileName are not supplied</exception> //public static int DT2STG_OLD(DataTable table, String stgFileName, String outFileName) //{ // //tidy up input parameters // stgFileName = stgFileName.Trim(); // outFileName = outFileName.Trim(); // //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"); // int rowCount = 0; // //Register group loader (using same directory as STG file) // String path = System.IO.Path.GetDirectoryName(stgFileName); // if (path == "") // path = "."; // IStringTemplateGroupLoader loader = new PathGroupLoader(path, new DefaultErrorListener()); // StringTemplateGroup.RegisterDefaultLexer(typeof(TemplateLexer)); // StringTemplateGroup.RegisterGroupLoader(loader); // //Read the template group from the file // //StringTemplateGroup stg = loader.LoadGroup(System.IO.Path.GetFileNameWithoutExtension(stgFileName)); // System.IO.TextReader tr = new System.IO.StreamReader(stgFileName); // StringTemplateGroup stg = new StringTemplateGroup(tr, typeof(TemplateLexer)); //lexer added to use $..$ in group templates instead of <..> // tr.Close(); // // Used to check which templates are present before calling them // ICollection<string> templateNames = stg.GetTemplateNames(); // System.Collections.ArrayList records = new System.Collections.ArrayList(); // //Write the results to the output file // 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 (templateNames.Contains("HEADER")) // { // StringTemplate stHeader = stg.GetInstanceOf("HEADER"); // sw.WriteLine(stHeader.ToString()); // } // 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().Trim(); // // Ensure any leading and trailing double quotes are removed.. // if (s.StartsWith("\"") && s.EndsWith("\"")) // s = s.Substring(1, s.Length - 2); // // Check for presence of properties in templates before attempting to use them // if (s != "") // { // record[dc.ColumnName.Trim()] = s; // } // } // // If the RECORD template is present, call it and write result to output file // if (templateNames.Contains("RECORD")) // { // StringTemplate stRecord = stg.GetInstanceOf("RECORD"); // stRecord.SetAttribute("data", record); // sw.WriteLine(stRecord.ToString()); // } // records.Add(record); // rowCount++; // } // // If the MAIN template is present, call it and write result to output file // //NOTE - deprecated; use HEADER, RECORD, FOOTER instead in templates // if (templateNames.Contains("MAIN")) // { // StringTemplate stMain = stg.GetInstanceOf("MAIN"); // stMain.SetAttribute("data", (object[])records.ToArray(typeof(IDictionary<string, object>))); // sw.WriteLine(stMain.ToString()); // } // // If the FOOTER template is present, call it and write result to output file // if (templateNames.Contains("FOOTER")) // { // StringTemplate stFooter = stg.GetInstanceOf("FOOTER"); // sw.WriteLine(stFooter.ToString()); // } // } // catch (Exception ex) // { // //worth catching this? // throw new Exception("Error during conversion: " + ex.Message, ex.InnerException); // } // finally // { // sw.Close(); // sw.Dispose(); // } // return rowCount; //} //new version 07/10/11 - using Antlr4.StringTemplate.. public static int DT2STG(DataTable table, String stgFileName, String outFileName) { //tidy up input parameters stgFileName = stgFileName.Trim(); outFileName = outFileName.Trim(); //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"); int rowCount = 0; //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 delimiters TemplateGroupFile stg = new TemplateGroupFile(stgFileName,'$','$'); //System.Collections.ArrayList records = new System.Collections.ArrayList(); //Write the results to the output file 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"); 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().Trim(); // Ensure any leading and trailing double quotes are removed.. if (s.StartsWith("\"") && s.EndsWith("\"")) s = s.Substring(1, s.Length - 2); // 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); 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"); 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; }