当前位置: 移动技术网 > IT编程>开发语言>c# > 服务器端C#实现的CSS解析器

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:using system; using system.collections; using system.text; using system.io;

复制代码 代码如下:

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;
}
}
}

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网