c#使用簡單工廠模式實(shí)現(xiàn)生成html文件的封裝類分享
由于這段時間比較輕松,于是想到很多的企業(yè)網(wǎng)站,新聞網(wǎng)站需要將頁面靜態(tài)化,于是寫了個封裝類來實(shí)現(xiàn)靜態(tài)文件的生成,思路比較簡單,但未完善,網(wǎng)友可根據(jù)自己的思路將此類擴(kuò)展,運(yùn)用了簡單工廠模式,先來看看靜態(tài)類的父類:StaticBase(抽象類)
public abstract class StaticBase : IDisposable
{
/// <summary>
/// 默認(rèn)編碼方式
/// </summary>
protected Encoding code = Encoding.GetEncoding("utf-8");
/// <summary>
/// 寫入頁面數(shù)據(jù)流
/// </summary>
protected StreamWriter sw = null;
/// <summary>
/// 讀取頁面數(shù)據(jù)流
/// </summary>
protected StreamReader sr = null;
/// <summary>
/// 生成的靜態(tài)頁面保存文件夾路徑
/// </summary>
protected string SavePath = "/Default/";
/// <summary>
/// 模板頁面的文件夾路徑
/// </summary>
protected string PagePath = "/Master/";
public abstract bool Osucess { set; get; }
public abstract string Errorstring { set; get; }
/// <summary>
/// 具體生成靜態(tài)方法
/// </summary>
protected abstract bool WriteFile();
/// <summary>
/// 不同模塊的文件名稱
/// </summary>
protected Dictionary<FlagsFileName, string> FileName
{
get
{
return new Dictionary<FlagsFileName, string>
{
{FlagsFileName.News,"article"},
{FlagsFileName.head,"head"},
{FlagsFileName.foot,"foot"},
};
}
}
// http://www.cnblogs.com/roucheng/
#region IDisposable 成員
public void Dispose()
{
sw.Dispose();
sr.Dispose();
}
#endregion
}
#region 對應(yīng)的頁面名稱
/// <summary>
/// 對應(yīng)的頁面名稱
/// </summary>
public enum FlagsFileName : byte
{
/// <summary>
/// 新聞
/// </summary>
[Description("新聞")]
News = 0,
/// <summary>
/// 頭部
/// </summary>
[Description("頭部")]
head=1,
/// <summary>
/// 腳部
/// </summary>
[Description("腳部")]
foot=2,
}
最后的一個枚舉用于定義不同位置或不同類別的靜態(tài)頁所對應(yīng)的子類,接下來看看其中一個子類的實(shí)現(xiàn)(該子類是用于所有單頁,如數(shù)據(jù)庫中有100條新聞記錄,那相應(yīng)的生成100個新聞html頁面,格式用模板定義的格式確定)
首先模板文件時靜態(tài)的html頁面,其中所有的需要從數(shù)據(jù)庫中替換的字段用一對$包含,如數(shù)據(jù)庫中的新聞標(biāo)題字段為titles,則模板頁中相應(yīng)的標(biāo)題位置用$titles$表示,頁面如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>$Titles$</title>
</script>
</head>
<body>
<div id="wrap">
$head$
<!--hd end-->
<div class="clear"></div>
<div id="wp">
<table border="0" cellpadding="0" cellspacing="0" width="980">
<tr>
<td rowspan="3" valign="top" id="main_box" class="index_box2">
<div class="subtitle subtitle4"></div>
<div class="article">
<div class="title">$Titles$</div>
$Contents_tw$
</div>
</td>
<td width="48" height="44" class="ri_top"> </td>
</tr>
<tr>
<td class="ri_mid" id="mid_box"> </td>
</tr>
<tr>
<td height="44" class="ri_bottom"> </td>
</tr>
</table>
</div>
<!--wp end-->
</div>
<!--wrap end-->
$foot$
<!--ft end-->
</body>
</html>
到這里知道個大概了吧,接下來就是這中頁面類型的子類實(shí)現(xiàn),我將它的名稱定義為ViewPage,因?yàn)樗锌梢詥为?dú)顯示的頁面都可以用這個子類,代碼如下
public class ViewPage : StaticBase
{
/// <summary>
/// 是否操作成功
/// </summary>
private bool o_sucess = true;
/// <summary>
/// 錯誤信息
/// </summary>
private string errorstring = string.Empty;
/// <summary>
/// 模板文件名稱
/// </summary>
private string masterhtml;
/// <summary>
/// 數(shù)據(jù)源
/// </summary>
private IEnumerable<DataRow> rowlist;
/// <summary>
/// 模塊類別
/// </summary>
private FlagsFileName fname;
/// <summary>
/// 指定命名文件的標(biāo)志列(從數(shù)據(jù)庫中的字段)
/// </summary>
private string thekey;
public override bool Osucess
{
get { return o_sucess; }
set { o_sucess = value; }
}
public override string Errorstring
{
get { return errorstring; }
set { errorstring = value; }
}
/// <summary>
/// 構(gòu)造器靜態(tài)生成對象
/// </summary>
/// <param name="rlist">需要生成靜態(tài)文件的數(shù)據(jù)源</param>
/// <param name="fn">文件類別枚舉</param>
/// <param name="myid">此字段為數(shù)據(jù)庫表中字段,由該字段指定生成的文件名字標(biāo)志 </param>
public ViewPage(DataRow[] rlist,FlagsFileName fn,string myid)
{
this.thekey = myid;
this.fname = fn;
this.rowlist = rlist;
this.masterhtml = FileName[fn] + ".html";
WriteFile();
}
protected override bool WriteFile()
{
string str = "";
try//從指定模板文件中讀取html代碼
{
sr = new StreamReader(HttpContext.Current.Server.MapPath(PagePath + this.masterhtml), code);
str = sr.ReadToEnd();
}
catch (Exception ex)//異常則指定返回的錯誤信息
{
sr.Close();
sr.Dispose();
this.o_sucess = false;
this.errorstring = ex.Message;
return this.o_sucess;
}
sr.Close();
sr.Dispose();
List<FlagsFileName> fn = new List<FlagsFileName>();
fn.Add(FlagsFileName.head);
fn.Add(FlagsFileName.foot);
PointPage pg = new PointPage(fn, str);
//要保存的文件名稱
string htmlfilename = string.Empty;
string changestring = "";//要更改的字符串
foreach (DataRow row in this.rowlist)//遍歷數(shù)據(jù)源數(shù)組中的每個數(shù)據(jù)表
{
string newString = str;
try
{
htmlfilename = FileName[fname] + "_" + row[thekey].ToString() + ".html";//給文件命名
foreach (DataColumn c in row.Table.Columns)//遍歷單個數(shù)據(jù)表的列名
{
changestring = "$" + c.ColumnName + "$";
newString = newString.Replace(changestring, row[c].ToString());
}
sw = new StreamWriter(HttpContext.Current.Server.MapPath(SavePath + htmlfilename), false, code);
sw.Write(newString);
sw.Flush();
}
catch (Exception ex)
{
this.o_sucess = false;
this.errorstring = ex.Message;
return this.o_sucess;
}
}
sw.Dispose();
sw.Close();
return true;
}
}
好,到這里實(shí)現(xiàn)了底層的思路設(shè)計(jì),那調(diào)用就很簡單了,某個aspx頁面,一個按鈕button,一個點(diǎn)擊事件Button_Click,點(diǎn)擊事件內(nèi)需要做的就是聲明一個基類StaticBase,將它實(shí)例化成一個子類ViewPage,傳遞的參數(shù)為一個數(shù)據(jù)項(xiàng)集合,DataRow[]為從數(shù)據(jù)表中讀取的集合,包含需要替換的字段,如select titles,contens,id from news(從新聞表中獲得標(biāo)識id,標(biāo)題,內(nèi)容),以及類型FlagsFileName.News為前天基類提到過的枚舉類型,為單獨(dú)頁面的生成方式,已經(jīng)重命名的標(biāo)識列,如此處為id,則生成的頁面格式為
news_1.html,news_2.html以此類推,代碼如下
protected void Create_Click(object sender, EventArgs e)
{
IEnumerable<DataRow> rowlist = TNotice_Command.SelectTNotice(-1);
using (StaticBase sb = new ViewPage((DataRow[])rowlist, FlagsFileName.News, "NID"))
{
if (!sb.Osucess)
{
Response.Write("<script language=javascript>alert('" + sb.Errorstring + "')</script>");
}
}
}
看到這里大家如果再從頭看一遍,相信就能知道靜態(tài)文件的生成的原理了。
相關(guān)文章
c#實(shí)現(xiàn)sunday算法實(shí)例
Sunday算法思想跟BM算法很相似,在匹配失敗時關(guān)注的是文本串中參加匹配的最末位字符的下一位字符,下面是用C#實(shí)現(xiàn)sunday的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08基于WPF實(shí)現(xiàn)一個簡單的音頻播放動畫控件
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)一個簡單的音頻播放動畫控件,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07C#中foreach循環(huán)對比for循環(huán)的優(yōu)勢和劣勢
循環(huán)語句是編程的基本語句,在C#中除了沿用C語言的循環(huán)語句外,還提供了foreach語句來實(shí)現(xiàn)循環(huán),下面這篇文章主要給大家介紹了關(guān)于C#中foreach循環(huán)對比for循環(huán)的優(yōu)勢和劣勢,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08WPF使用DrawingContext實(shí)現(xiàn)二維繪圖
這篇文章介紹了WPF使用DrawingContext實(shí)現(xiàn)二維繪圖的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06