62,046
社区成员
发帖
与我相关
我的任务
分享
Regex reg = new Regex(@"(?is)<asp:(\S+)\b(?=[^>]*?runat=(['""]?)server\2)(?=[^>]*?\bid=)[^>]*/?>((?:(?!</?asp:\1).)*</asp:\1>)?");
(?s)<asp:(\w+)[^>]+?(?i:runat)=([""'])(?i:server)\2(?:(?!/?>).)*(/)?>(?(3)|(?:(?!</asp:\1).)*</asp:\1>)
<asp:(\w+)[^>]*(?i:runat)=([""'])(?i:server)\2(?:(?!/?>).)*
(?>
>
(?>
<asp:\w+[^>]*/>
|
<asp:(?<level>\w+)[^>]*>
|
</asp:\k<level>>(?<-level>)
|
[^<>]+
)*
(?(level)(?!))
</asp:\1>
|
/>
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="PageBase.cs" Inherits="PageBase" Debug="True" Description="Template description here." %>
<%@ Property Name="NameSpace" Type="String" Optional="True" Category="Context" Default="ProjectManage" Description="The namespace to use for this class" %>
<%@ Property Name="IsInitValue" Type="System.Boolean" Default="True" Optional="True" Category="Booleans" Description="Init Value" %>
<%@ Property Name="IsSubmitValue" Type="System.Boolean" Default="True" Optional="True" Category="Booleans" Description="Init Value" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Src="Common.cs" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="TX.Template.Util" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% AnalysisPage();%>
using <%= NameSpace %>.Entity;
using <%= NameSpace %>.Logic;
protected void dataBind()
{
<%=CreateInitValue()%>
}
protected void btnSumit()
{
<%=CreateSubmitValue()%>
}
<script runat="template">
private string filename = string.Empty;
private IDictionary<string,string> controlList = new Dictionary<string,string>();
private IDictionary<string, string> dicInitControl = new Dictionary<string, string>();
private IDictionary<string, string> dicSubmitControl = new Dictionary<string, string>();
private IDictionary<string, int> dicControlHeadLen = new Dictionary<string, int>();
private Hashtable htTable = new Hashtable();
private TableSchemaCollection _sourceTables;
public string CreateInitValue()
{
StringBuilder sbInitText = new StringBuilder();
foreach(string tablename in htTable.Keys)
{
sbInitText.Append(tablename + "Entity " + GetCamelCaseName(tablename) + "Entity = new " + tablename + "Entity();\r\n");
}
foreach(KeyValuePair<string, string> kvp in ControlList)
{
//Debug.Print(kvp.Value.ToLower());
if(!dicInitControl.ContainsKey(kvp.Value.ToLower()))
{
continue;
}
else
{
foreach(string tablename in htTable.Keys)
{
string controlID = kvp.Key;
string columnLowName = kvp.Key.Substring(dicControlHeadLen[kvp.Value]).ToLower();
Debug.Print(columnLowName);
Hashtable htTemp = ((Hashtable)(htTable[tablename]));
if(htTemp.ContainsKey(columnLowName))
{
string templateString = dicInitControl[kvp.Value];
templateString = templateString.Replace("{$ID}",controlID);
templateString = templateString.Replace("{$Value}",GetCamelCaseName(tablename)+"Entity."+((ColumnSchema)(htTemp[columnLowName])).Name + ".ToString()");
sbInitText.Append(" " + templateString + "\r\n");
}
}
}
}
return sbInitText.ToString();
}
public string CreateSubmitValue()
{
StringBuilder sbSubmitText = new StringBuilder();
StringBuilder sbSubmitDataText = new StringBuilder();
foreach(string tablename in htTable.Keys)
{
sbSubmitText.Append(tablename + "Entity " + GetCamelCaseName(tablename) + "Entity = new " + tablename + "Entity();\r\n");
}
foreach(KeyValuePair<string, string> kvp in ControlList)
{
if(!dicSubmitControl.ContainsKey(kvp.Value))
{
continue;
}
else
{
foreach(string tablename in htTable.Keys)
{
string controlID = kvp.Key;
string columnLowName = kvp.Key.Substring(dicControlHeadLen[kvp.Value]).ToLower();
Hashtable htTemp = ((Hashtable)(htTable[tablename]));
if(htTemp.ContainsKey(columnLowName))
{
string templateString = dicSubmitControl[kvp.Value];
templateString = templateString.Replace("{$ID}",controlID);
templateString = templateString.Replace("{$Variable}","string " + GetCamelCaseName(((ColumnSchema)(htTemp[columnLowName])).Name));
sbSubmitText.Append(" " + templateString + "\r\n");
sbSubmitDataText.Append(" " + GetCamelCaseName(tablename)+"Entity."+((ColumnSchema)(htTemp[columnLowName])).Name + " = ");
sbSubmitDataText.Append(GetCSharpVariableType(((ColumnSchema)(htTemp[columnLowName])), GetCamelCaseName(((ColumnSchema)(htTemp[columnLowName])).Name)));
sbSubmitDataText.Append(";\r\n");
}
}
}
}
return sbSubmitText.ToString() + sbSubmitDataText.ToString();
}
private void Init()
{
dicInitControl.Add("label", "{$ID}.Text = {$Value};");
dicInitControl.Add("textbox", "{$ID}.Text = {$Value};");
dicInitControl.Add("checkboxlist", "{$ID}.SelectedValue = {$Value};");
dicInitControl.Add("radiobuttonlist", "{$ID}.SelectedValue = {$Value};");
dicInitControl.Add("dropdownlist", "{$ID}.SelectedValue = {$Value};");
//dicSubmitControl.Add("label", "{$Variable} = {$ID}.Text;");
dicSubmitControl.Add("textbox", "{$Variable} = {$ID}.Text;");
dicSubmitControl.Add("checkboxlist", "{$Variable} = {$ID}.SelectedValue;");
dicSubmitControl.Add("radiobuttonlist", "{$Variable} = {$ID}.SelectedValue;");
dicSubmitControl.Add("dropdownlist", "{$Variable} = {$ID}.SelectedValue;");
dicControlHeadLen.Add("label", 3);
dicControlHeadLen.Add("textbox", 3);
dicControlHeadLen.Add("checkboxlist", 3);
dicControlHeadLen.Add("radiobuttonlist", 3);
dicControlHeadLen.Add("dropdownlist", 3);
}
private void InitTable()
{
foreach ( TableSchema table in _sourceTables )
{
Hashtable htTemp = new Hashtable();
foreach(ColumnSchema column in table.Columns)
{
htTemp.Add(column.Name.ToLower(),column);
}
htTable.Add(table.Name,htTemp);
}
}
[Category("01b. Filter by Individual Objects - Optional")]
[Description("The tables you wish to generate, if you do not select any SourceTables or SourceViews, and SourceDatabase is completed, then the entire database will be generated.")]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
public TableSchemaCollection SourceTables
{
get
{
if (this._sourceTables != null && this._sourceTables.Count > 0 )
return this._sourceTables;
else
return null;
}
set
{
this._sourceTables = value;
}
}
[EditorAttribute(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Optional]
[Category("ChooseFile")]
[Description("choose file.")]
public string PageFilename
{
get
{
return filename;
}
set
{
filename = value;
}
}
public void CreatePage()
{
}
public IDictionary<string,string> ControlList
{
get
{
return controlList;
}
}
private void ChooseControls(string pageContent)
{
Regex r = new Regex(@"
<(?i:asp):(\w+)[^>]*(?i:runat)=([""'])(?i:server)\2(?:(?!/?>).)*
(?>
>
(?>
<asp:\w+[^>]*/>
|
<asp:(?<level>\w+)[^>]*>
|
</asp:\k<level>>(?<-level>)
|
[^<>]+
)*
(?(level)(?!))
</asp:\1>
|
/>
)
", RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
MatchCollection mc = r.Matches(pageContent);
foreach (Match i in mc)
{
string controlNameValue = GetID(i.Value);
string controlTypeValue = GetControlType(i.Value);
if(controlNameValue != string.Empty)
{
//Debug.Print(controlNameValue);
this.ControlList.Add(controlNameValue, controlTypeValue);
}
}
}
public string GetControlType(string controlHTML)
{
string retString=string.Empty;
Regex re = new Regex(@"(?<=asp:)([a-zA-Z0-9]+)(?<=\s*)", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
MatchCollection mc = re.Matches(controlHTML);
if(mc.Count>0)
{
retString = mc[0].ToString().ToLower();
}
return retString;
}
public string GetID(string controlHTML)
{
string retString=string.Empty;
Regex re = new Regex(@"(?<=id=\s*""?)[a-zA-Z0-9]+(?<=""*)", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
MatchCollection mc = re.Matches(controlHTML);
if(mc.Count>0)
{
retString = mc[0].ToString();
}
return retString;
}
public void AnalysisPage()
{
InitTable();
Init();
StreamReader sr=new StreamReader(filename);
string pageContent=sr.ReadToEnd();
ChooseControls(pageContent);
}
</script>