服務(wù)器端C#實(shí)現(xiàn)的CSS解析器
更新時(shí)間:2008年09月09日 23:36:53 作者:
服務(wù)器端C#實(shí)現(xiàn)的CSS解析器
復(fù)制代碼 代碼如下:
using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace CSS
{
public class App
{
public static void Main(string[] args)
{
//初始化CSS解析器
CssDocument doc = new CssDocument();
//加載現(xiàn)有CSS文件
doc.Load(Directory.GetCurrentDirectory() + "/test.css");
//修改CSS
doc["body"].Attributes["font-size"] = "12px";
//保存CSS文件
doc.Save(Directory.GetCurrentDirectory() + "/a.css");
Console.Read();
}
}
public class CssParse
{
private string m_source;
private int m_idx;
public static bool IsWhiteSpace(char ch)
{
return( "\t\n\r ".IndexOf(ch) != -1 );
}
public void EatWhiteSpace()
{
while ( !Eof() )
{
if ( !IsWhiteSpace(GetCurrentChar()) )
return;
m_idx++;
}
}
public bool Eof()
{
return(m_idx>=m_source.Length );
}
public string ParseElementName()
{
StringBuilder element = new StringBuilder();
EatWhiteSpace();
while ( !Eof() )
{
if (GetCurrentChar()=='{')
{
m_idx++;
break;
}
element.Append(GetCurrentChar());
m_idx++;
}
EatWhiteSpace();
return element.ToString().Trim();
}
public string ParseAttributeName()
{
StringBuilder attribute = new StringBuilder();
EatWhiteSpace();
while ( !Eof() )
{
if (GetCurrentChar()==':')
{
m_idx++;
break;
}
attribute.Append(GetCurrentChar());
m_idx++;
}
EatWhiteSpace();
return attribute.ToString().Trim();
}
public string ParseAttributeValue()
{
StringBuilder attribute = new StringBuilder();
EatWhiteSpace();
while ( !Eof() )
{
if (GetCurrentChar()==';')
{
m_idx++;
break;
}
attribute.Append(GetCurrentChar());
m_idx++;
}
EatWhiteSpace();
return attribute.ToString().Trim();
}
public char GetCurrentChar()
{
return GetCurrentChar(0);
}
public char GetCurrentChar(int peek)
{
if( (m_idx+peek)<m_source.Length )
return m_source[m_idx+peek];
else
return (char)0;
}
public char AdvanceCurrentChar()
{
return m_source[m_idx++];
}
public void Advance()
{
m_idx++;
}
public string Source
{
get
{
return m_source;
}
set
{
m_source = value;
}
}
public ArrayList Parse()
{
ArrayList elements = new ArrayList();
while (!Eof())
{
string elementName = ParseElementName();
if (elementName == null)
break;
CssElement element = new CssElement(elementName);
string name = ParseAttributeName();
string value = ParseAttributeValue();
while (name != null && value != null)
{
element.Add(name, value);
EatWhiteSpace();
if (GetCurrentChar()=='}')
{
m_idx++;
break;
}
name = ParseAttributeName();
value = ParseAttributeValue();
}
elements.Add(element);
}
return elements;
}
}
public class CssDocument
{
private string _Text;
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
}
}
private ArrayList _Elements;
public ArrayList Elements
{
get
{
return _Elements;
}
set
{
_Elements = value;
}
}
public CssElement this[string name]
{
get
{
for (int i = 0; i < Elements.Count; i++)
{
if (((CssElement)Elements[i]).Name.Equals(name))
return (CssElement)Elements[i];
}
return null;
}
}
private string _File;
public string File
{
get
{
return _File;
}
set
{
_File = value;
}
}
public CssDocument()
{
}
public void Load(string file)
{
using (StreamReader sr = new StreamReader(file))
{
Text = sr.ReadToEnd();
sr.Close();
}
CssParse parse = new CssParse();
parse.Source = Regex.Replace(Text, @"/\*.*?\*/", "", RegexOptions.Compiled);
Elements = parse.Parse();
}
public void Add(CssElement element)
{
Elements.Add(element);
}
public void Save()
{
Save(this.File);
}
public void Save(string file)
{
using (StreamWriter sw = new StreamWriter(file, false))
{
for (int i = 0; i < Elements.Count; i++)
{
CssElement element = (CssElement)Elements[i];
sw.WriteLine(element.Name + " {");
foreach (string name in element.Attributes.AllKeys)
{
sw.WriteLine("\t{0}:{1};", name, element.Attributes[name]);
}
sw.WriteLine("}");
}
sw.Flush();
sw.Close();
}
}
}
public class CssElement
{
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
private NameValueCollection _Attributes;
public NameValueCollection Attributes
{
get
{
return _Attributes;
}
set
{
_Attributes = value;
}
}
public CssElement(string name)
{
this.Name = name;
Attributes = new NameValueCollection();
}
public void Add(string attribute, string value)
{
Attributes[attribute] = value;
}
}
}
您可能感興趣的文章:
- c#判斷數(shù)據(jù)庫服務(wù)器是否已經(jīng)啟動(dòng)的方法
- c#實(shí)現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志
- c#多線程網(wǎng)絡(luò)聊天程序代碼分享(服務(wù)器端和客戶端)
- Docker容器啟動(dòng)時(shí)初始化Mysql數(shù)據(jù)庫的方法
- C#實(shí)現(xiàn)HTTP協(xié)議迷你服務(wù)器(兩種方法)
- c# HttpWebRequest通過代理服務(wù)器抓取網(wǎng)頁內(nèi)容應(yīng)用介紹
- c# 服務(wù)器上傳木馬監(jiān)控代碼(包含可疑文件)
- Javascript 直接調(diào)用服務(wù)器C#代碼 ASP.NET Ajax實(shí)例
- C# FTP,GetResponse(),遠(yuǎn)程服務(wù)器返回錯(cuò)誤
- c# 連接字符串?dāng)?shù)據(jù)庫服務(wù)器端口號(hào) .net狀態(tài)服務(wù)器端口號(hào)
- C#列出局域網(wǎng)中可用SQL Server服務(wù)器(續(xù))
- C#列出局域網(wǎng)中可用SQL Server服務(wù)器
- C#利用WMI操作DNS服務(wù)器(可遠(yuǎn)程操作,需要相應(yīng)權(quán)限)
- c#批量上傳圖片到服務(wù)器示例分享
相關(guān)文章
C# .NET中Socket簡(jiǎn)單實(shí)用框架的使用教程
最近一個(gè)項(xiàng)目因?yàn)橐玫絊ocket傳輸問題,所以決定學(xué)習(xí)一下,將自己學(xué)習(xí)的內(nèi)容總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于C# .NET中Socket簡(jiǎn)單實(shí)用框架使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09
比Math類庫abs()方法性能更高的取絕對(duì)值方法介紹
這篇文章主要給大家介紹了一種比Math類庫abs()方法性能更高的取絕對(duì)值方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法
這篇文章主要介紹了C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法,涉及C#針對(duì)坐標(biāo)的數(shù)學(xué)運(yùn)算相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#中調(diào)用SAPI實(shí)現(xiàn)語音合成的2種方法
這篇文章主要介紹了C#中調(diào)用SAPI實(shí)現(xiàn)語音合成的2種方法,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06
C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
這篇文章主要介紹了C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹,vshost.exe是一個(gè)宿主進(jìn)程,主要用來提高調(diào)試效率,需要的朋友可以參考下2015-01-01

