服务器端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,解析器
0
投稿
猜你喜欢
Springboot工具类FileCopyUtils使用教程
2023-10-15 18:56:36
关于通过java调用datax,返回任务执行的方法
2023-11-28 21:26:45
javaweb图书商城设计之用户模块(1)
2023-10-30 09:22:57
c# 如何使用 My 命名空间
2022-12-25 20:23:39
SpringBoot整合Dozer映射框架流程详解
2023-03-08 02:23:48
Android实现便于批量操作可多选的图片ListView实例
2021-07-17 19:24:54
java用户管理注册功能 含前后台代码
2022-08-01 12:05:11
分析并发编程之LongAdder原理
2023-05-11 17:19:30
spring boot与ktor整合的实现方法
2022-01-18 04:20:19
学习Java之如何正确地跳出循环结构
2021-10-21 11:21:53
Java并发之不可思议的死循环详解
2023-10-20 03:11:34
Java调取创蓝253短信验证码的实现代码
2021-11-05 00:48:10
Android实现上拉加载更多以及下拉刷新功能(ListView)
2022-03-02 15:50:41
Spring整合CXF webservice restful实例详解
2023-03-20 09:58:21
Android自带倒计时控件Chronometer使用方法详解
2022-09-18 13:11:33
SpringCloud eureka(server)微服务集群搭建过程
2023-05-22 15:08:55
Maven依赖管理的用法介绍
2021-07-13 18:07:35
Idea 搭建Spring源码环境的超详细教程
2023-09-30 16:13:17
Android数据传输中的参数加密代码示例
2021-05-25 23:54:43
C#接口在派生类和外部类中的调用方法示例
2021-06-13 07:54:43