本文整理汇总了C#中Antlr4.StringTemplate.TemplateGroupFile.IsDefined方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateGroupFile.IsDefined方法的具体用法?C# TemplateGroupFile.IsDefined怎么用?C# TemplateGroupFile.IsDefined使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Antlr4.StringTemplate.TemplateGroupFile
的用法示例。
在下文中一共展示了TemplateGroupFile.IsDefined方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DT2STG
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)
//.........这里部分代码省略.........
示例2: DT2STG
//.........这里部分代码省略.........
// }
// }
// 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;
}