Copy codeThe Code is 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)
{
// Initialize the CSS parser
CssDocument doc = new CssDocument ();
// Load the existing CSS file
Doc. Load (Directory. GetCurrentDirectory () + "/test.css ");
// Modify CSS
Doc ["body"]. Attributes ["font-size"] = "12px ";
// Save the 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)
{
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;
}
}
}