Copy Code code as follows:
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)
{
Initializing the CSS Parser
Cssdocument doc = new cssdocument ();
Load an existing CSS file
Doc. Load (Directory.GetCurrentDirectory () + "/test.css");
Modify CSS
doc["Body"]. attributes["font-size"] = "12px";
Save CSS File
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;
}
}
}