我的Asp.Net页面模板算法(New)
时间:2011-05-04 来源:再快一点
/// <summary>
/// 根据模板和一个DataTable得到一个解析字符串
/// </summary>
/// <param name="template">字符串模板</param>
/// <param name="dt">一个DataTable</param>
/// <returns>返回一个解析模板后的字符串</returns>
public static string Render(string template, DataTable dt)
{
if (dt == null || dt.Rows.Count < 1)
{
return "";
}
StringBuilder sb = new StringBuilder(1000);
List<string> arr;
List<string> columns;
Analyze(template, out arr, out columns);
int i;
foreach (DataRow row in dt.Rows)
{
i = 0;
for (; i < columns.Count; i++)
{
sb.Append(arr[i]);
sb.Append(row[columns[i]].ToString());
}
if (i < arr.Count)
{
sb.Append(arr[i]);
}
}
return sb.ToString();
}
/// <summary>
/// 根据模板和一个泛型实体集合得到一个解析字符串
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="template">字符串模板</param>
/// <param name="list">实体集合</param>
/// <returns>返回一个解析模板后的字符串</returns>
public static string Render<T>(string template, List<T> list)
{
if (list == null || list.Count < 1)
{
return "";
}
StringBuilder sb = new StringBuilder(1000);
List<string> arr;
List<string> columns;
Analyze(template, out arr, out columns);
int i;
Type type = typeof(T);
foreach (T item in list)
{
i = 0;
for (; i < columns.Count; i++)
{
sb.Append(arr[i]);
sb.Append(GetValue(type, item, columns[i]));
}
if (i < arr.Count)
{
sb.Append(arr[i]);
}
}
return sb.ToString();
}
/// <summary>
/// 对一个字符串模板进行分析
/// </summary>
/// <param name="template">字符串模板</param>
/// <param name="arr">存储除开列名的其他文本的集合</param>
/// <param name="columns">存储列名的集合</param>
static void Analyze(string template, out List<string> arr, out List<string> columns)
{
arr = new List<string>();
columns = new List<string>();
int previousEndIndex = 0;
//找到{xxx}
int startIndex = template.IndexOf('{');
int endIndex = template.IndexOf('}');
while (startIndex != -1)
{
//存储上一个}和现在搜索到的{之间的字符串,如果是第一次搜索到{,那么previousEndIndex=0则存储的是字符串起始到第一个{之间的字符串
arr.Add(template.Substring(previousEndIndex, startIndex - previousEndIndex));
//存储列名
columns.Add(template.Substring(startIndex + 1, endIndex - startIndex - 1));
startIndex++;
endIndex++;
previousEndIndex = endIndex;
startIndex = template.IndexOf('{', startIndex);
endIndex = template.IndexOf('}', endIndex);
}
//如果模板不是以}结尾,说明后面还有字符串
if (previousEndIndex < template.Length)
{
arr.Add(template.Substring(previousEndIndex));
}
//方法执行到此处,arr.Length==columns.Length或者arr.Length==columns.Length+1
}
/// <summary>
/// 根据一个实体类型,实体实例和属性名,获取属性值
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="item">实体实例</param>
/// <param name="attrName">属性吗</param>
/// <returns></returns>
static string GetValue(Type type, object item, string attrName)
{
PropertyInfo property = type.GetProperty(attrName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
object obj = property.GetValue(item, null);
if (obj != null)
{
return obj.ToString();
}
return "";
}
/// <summary>
/// 根据模板,使用正则匹配然后替换输出字符串
/// </summary>
/// <param name="template"></param>
/// <param name="dt"></param>
/// <returns></returns>
public static string RenderTemplate(string template, DataTable dt)
{
StringBuilder sb = new StringBuilder(1000);
MatchCollection matches = reg.Matches(template);
string rowStr = template;
foreach (DataRow row in dt.Rows)
{
rowStr = template;
foreach (Match match in matches)
{
rowStr = rowStr.Replace(match.Value, row[match.Groups[1].Value].ToString());
}
sb.Append(rowStr);
sb.Append("\n");
}
return sb.ToString();
}
/// 根据模板和一个DataTable得到一个解析字符串
/// </summary>
/// <param name="template">字符串模板</param>
/// <param name="dt">一个DataTable</param>
/// <returns>返回一个解析模板后的字符串</returns>
public static string Render(string template, DataTable dt)
{
if (dt == null || dt.Rows.Count < 1)
{
return "";
}
StringBuilder sb = new StringBuilder(1000);
List<string> arr;
List<string> columns;
Analyze(template, out arr, out columns);
int i;
foreach (DataRow row in dt.Rows)
{
i = 0;
for (; i < columns.Count; i++)
{
sb.Append(arr[i]);
sb.Append(row[columns[i]].ToString());
}
if (i < arr.Count)
{
sb.Append(arr[i]);
}
}
return sb.ToString();
}
/// <summary>
/// 根据模板和一个泛型实体集合得到一个解析字符串
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="template">字符串模板</param>
/// <param name="list">实体集合</param>
/// <returns>返回一个解析模板后的字符串</returns>
public static string Render<T>(string template, List<T> list)
{
if (list == null || list.Count < 1)
{
return "";
}
StringBuilder sb = new StringBuilder(1000);
List<string> arr;
List<string> columns;
Analyze(template, out arr, out columns);
int i;
Type type = typeof(T);
foreach (T item in list)
{
i = 0;
for (; i < columns.Count; i++)
{
sb.Append(arr[i]);
sb.Append(GetValue(type, item, columns[i]));
}
if (i < arr.Count)
{
sb.Append(arr[i]);
}
}
return sb.ToString();
}
/// <summary>
/// 对一个字符串模板进行分析
/// </summary>
/// <param name="template">字符串模板</param>
/// <param name="arr">存储除开列名的其他文本的集合</param>
/// <param name="columns">存储列名的集合</param>
static void Analyze(string template, out List<string> arr, out List<string> columns)
{
arr = new List<string>();
columns = new List<string>();
int previousEndIndex = 0;
//找到{xxx}
int startIndex = template.IndexOf('{');
int endIndex = template.IndexOf('}');
while (startIndex != -1)
{
//存储上一个}和现在搜索到的{之间的字符串,如果是第一次搜索到{,那么previousEndIndex=0则存储的是字符串起始到第一个{之间的字符串
arr.Add(template.Substring(previousEndIndex, startIndex - previousEndIndex));
//存储列名
columns.Add(template.Substring(startIndex + 1, endIndex - startIndex - 1));
startIndex++;
endIndex++;
previousEndIndex = endIndex;
startIndex = template.IndexOf('{', startIndex);
endIndex = template.IndexOf('}', endIndex);
}
//如果模板不是以}结尾,说明后面还有字符串
if (previousEndIndex < template.Length)
{
arr.Add(template.Substring(previousEndIndex));
}
//方法执行到此处,arr.Length==columns.Length或者arr.Length==columns.Length+1
}
/// <summary>
/// 根据一个实体类型,实体实例和属性名,获取属性值
/// </summary>
/// <param name="type">实体类型</param>
/// <param name="item">实体实例</param>
/// <param name="attrName">属性吗</param>
/// <returns></returns>
static string GetValue(Type type, object item, string attrName)
{
PropertyInfo property = type.GetProperty(attrName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
object obj = property.GetValue(item, null);
if (obj != null)
{
return obj.ToString();
}
return "";
}
/// <summary>
/// 根据模板,使用正则匹配然后替换输出字符串
/// </summary>
/// <param name="template"></param>
/// <param name="dt"></param>
/// <returns></returns>
public static string RenderTemplate(string template, DataTable dt)
{
StringBuilder sb = new StringBuilder(1000);
MatchCollection matches = reg.Matches(template);
string rowStr = template;
foreach (DataRow row in dt.Rows)
{
rowStr = template;
foreach (Match match in matches)
{
rowStr = rowStr.Replace(match.Value, row[match.Groups[1].Value].ToString());
}
sb.Append(rowStr);
sb.Append("\n");
}
return sb.ToString();
}
相关阅读 更多 +