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;
}