服务器端C#实现的CSS解析器

 更新时间:2008年09月09日 23:36:53   作者:  
服务器端C#实现的CSS解析器

复制代码 代码如下:

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();
//加载现有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# .NET中Socket简单实用框架的使用教程

    C# .NET中Socket简单实用框架的使用教程

    最近一个项目因为要用到Socket传输问题,所以决定学习一下,将自己学习的内容总结分享出来,下面这篇文章主要给大家介绍了关于C# .NET中Socket简单实用框架使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-09-09
  • C#多线程之取消架构介绍

    C#多线程之取消架构介绍

    这篇文章介绍了C#多线程之取消架构,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • c# list部分操作实现代码

    c# list部分操作实现代码

    这篇文章主要介绍了c# list部分操作,需要的朋友可以参考下
    2013-09-09
  • 比Math类库abs()方法性能更高的取绝对值方法介绍

    比Math类库abs()方法性能更高的取绝对值方法介绍

    这篇文章主要给大家介绍了一种比Math类库abs()方法性能更高的取绝对值方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • Unity实现游戏存档框架

    Unity实现游戏存档框架

    这篇文章主要为大家详细介绍了Unity实现游戏存档框架,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法

    C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法

    这篇文章主要介绍了C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法,涉及C#针对坐标的数学运算相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • c#继承与多态使用示例

    c#继承与多态使用示例

    继承是面向对象程序设计的主要特征之一,允许重用现有类去创建新类的过程。下面使用示例学习一下c#继承与多态
    2014-01-01
  • 浅析C#异步中的Overlapped是如何寻址的

    浅析C#异步中的Overlapped是如何寻址的

    用ReadAsync做文件异步读取时,在Win32层面会传lpOverlapped到内核层,那在内核层回头时,它是如何通过这个lpOverlapped寻找到 ReadAsync这个异步的Task的呢,下面我们就来简单分析一下吧
    2025-01-01
  • C#中调用SAPI实现语音合成的2种方法

    C#中调用SAPI实现语音合成的2种方法

    这篇文章主要介绍了C#中调用SAPI实现语音合成的2种方法,本文直接给出示例代码,需要的朋友可以参考下
    2015-06-06
  • C# IDE VS2005中的Hosting Process (vshost.exe)作用介绍

    C# IDE VS2005中的Hosting Process (vshost.exe)作用介绍

    这篇文章主要介绍了C# IDE VS2005中的Hosting Process (vshost.exe)作用介绍,vshost.exe是一个宿主进程,主要用来提高调试效率,需要的朋友可以参考下
    2015-01-01

最新评论