服务器端C#实现的CSS解析器
时间:2022-01-25 12:26:20
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#,CSS,解析器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Java中注解Annotation的定义、使用、解析
2021-07-29 04:07:52
Java Swing 多线程加载图片(保证顺序一致)
2023-10-26 10:10:36
![](https://img.aspxhome.com/file/2023/0/65210_0s.jpg)
Java中static变量作用和用法详解
2021-11-09 12:01:04
使用Postman传递arraylist数据给springboot方式
2022-08-27 01:13:01
![](https://img.aspxhome.com/file/2023/9/69359_0s.jpg)
JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别
2022-04-21 15:05:22
Unity Shader实现黑幕过场效果
2022-01-13 00:18:10
![](https://img.aspxhome.com/file/2023/5/69205_0s.gif)
Java抢红包的红包生成算法
2023-06-23 20:52:04
![](https://img.aspxhome.com/file/2023/2/57532_0s.jpg)
springBoot集成Elasticsearch 报错 Health check failed的解决
2022-12-07 05:18:16
![](https://img.aspxhome.com/file/2023/2/69872_0s.jpg)
Java8 Comparator: 列表排序的深入讲解
2022-12-11 08:30:01
深入浅析C# 11 对 ref 和 struct 的改进
2021-09-27 20:31:47
JAVAsynchronized原理详解
2023-05-17 12:18:32
![](https://img.aspxhome.com/file/2023/8/80318_0s.png)
Java读取Properties文件的七种方法的总结
2022-08-12 13:27:18
SpringBoot 如何实时刷新静态文件
2023-11-02 13:27:09
![](https://img.aspxhome.com/file/2023/5/58765_0s.png)
java 静态代理 动态代理深入学习
2022-07-24 21:46:25
Gradle修改本地仓库的位置方法实现
2022-01-17 21:25:52
![](https://img.aspxhome.com/file/2023/0/62720_0s.png)
C#中异步和多线程的区别介绍
2021-08-23 07:06:49
小菜编程成长记(一 面试受挫——代码无错就是好?)第1/3页
2023-06-08 19:15:26
Android完美实现平滑过渡的ViewPager广告条
2023-10-01 13:05:31
![](https://img.aspxhome.com/file/2023/2/87272_0s.jpg)
Spring中自动装配的4种方式
2021-08-01 16:44:41
java 多态与抽象类详解总结
2022-04-19 10:59:18
![](https://img.aspxhome.com/file/2023/5/67135_0s.jpg)