服務(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ù)庫(kù)服務(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ù)庫(kù)的方法
- C#實(shí)現(xiàn)HTTP協(xié)議迷你服務(wù)器(兩種方法)
- c# HttpWebRequest通過(guò)代理服務(wù)器抓取網(wǎng)頁(yè)內(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ù)庫(kù)服務(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# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼
這篇文章主要介紹了c# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用
這篇文章主要介紹了C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用,注意區(qū)分一下簡(jiǎn)單工廠模式、工廠方法模式和抽象工廠模式概念之間的區(qū)別,需要的朋友可以參考下2016-02-02C#基于正則表達(dá)式抓取a標(biāo)簽鏈接和innerhtml的方法
這篇文章主要介紹了C#基于正則表達(dá)式抓取a標(biāo)簽鏈接和innerhtml的方法,結(jié)合實(shí)例形式分析了C#使用正則表達(dá)式進(jìn)行頁(yè)面元素的匹配與抓取相關(guān)操作技巧,需要的朋友可以參考下2017-06-06C#使用ODBC與OLEDB連接數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了C#使用ODBC與OLEDB連接數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了C#基于ODBC與OLEDB實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接操作簡(jiǎn)單操作技巧,需要的朋友可以參考下2017-05-05C#使用Equals()方法比較兩個(gè)對(duì)象是否相等的方法
這篇文章主要介紹了C#使用Equals()方法比較兩個(gè)對(duì)象是否相等的方法,涉及C#操作對(duì)象的相關(guān)技巧,需要的朋友可以參考下2015-04-04Dictionary擴(kuò)展基礎(chǔ)類向字典中添加鍵和值
Dictionary<TKey, TValue> 類是常用的一個(gè)基礎(chǔ)類,但用起來(lái)有時(shí)確不是很方便。本文逐一討論,并使用擴(kuò)展方法解決2013-11-11