服務(wù)器端C#實現(xiàn)的CSS解析器
更新時間:2008年09月09日 23:36:53 作者:
服務(wù)器端C#實現(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)啟動的方法
- c#實現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志
- c#多線程網(wǎng)絡(luò)聊天程序代碼分享(服務(wù)器端和客戶端)
- Docker容器啟動時初始化Mysql數(shù)據(jù)庫的方法
- C#實現(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實例
- C# FTP,GetResponse(),遠程服務(wù)器返回錯誤
- c# 連接字符串數(shù)據(jù)庫服務(wù)器端口號 .net狀態(tài)服務(wù)器端口號
- C#列出局域網(wǎng)中可用SQL Server服務(wù)器(續(xù))
- C#列出局域網(wǎng)中可用SQL Server服務(wù)器
- C#利用WMI操作DNS服務(wù)器(可遠程操作,需要相應(yīng)權(quán)限)
- c#批量上傳圖片到服務(wù)器示例分享
相關(guān)文章
C#實現(xiàn)計算一個點圍繞另一個點旋轉(zhuǎn)指定弧度后坐標值的方法
這篇文章主要介紹了C#實現(xiàn)計算一個點圍繞另一個點旋轉(zhuǎn)指定弧度后坐標值的方法,涉及C#針對坐標的數(shù)學(xué)運算相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
C#中調(diào)用SAPI實現(xiàn)語音合成的2種方法
這篇文章主要介紹了C#中調(diào)用SAPI實現(xiàn)語音合成的2種方法,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06
C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
這篇文章主要介紹了C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹,vshost.exe是一個宿主進程,主要用來提高調(diào)試效率,需要的朋友可以參考下2015-01-01

