Hi this is a small CSS class I am working on to work with style sheets in your programs. You chould use this as a replacement for ini files or anything else you like to do with it. anyway hope you like first versions comments welcome.
Class code CssClass
Code:
'CSS Class By Ben Jones Made for VB 2010 Express
'Ver BETA 1.0.0
'About
'Hi this is a small class I am making to work with CSS (Cascading Style Sheets)
'The class at the moment only allows you to read values.
'Please note there are some rules first it does not support comments
'second you must follow the layout as shown below.
'This will work and you be able to read the value Ben from Name key.
'H1{
' Name : Ben;
'}
'This does not work with this class.
'H1
'{
' Name: Ben;
'}
'Also does not support multiple selections e.g. H1,H2,H3
Imports System.IO
Public Class CssClass
'Start and end tags
Const TagS As String = "{"
Const TagE As String = "}"
Const AssignChar As String = ":"
Const EndChar As String = ";"
Const Seperator As String = "|"
Private mFilename As String
Private TempLst As New List(Of String)
Public Function LoadFromFile() As Boolean
Dim sr As StreamReader
Dim sLine As String
Dim sSelection As String
Dim InTag As Boolean
'Init variables.
sSelection = ""
LoadFromFile = True
InTag = False
'Check if filename exists.
If Not File.Exists(Filename) Then
LoadFromFile = False
Exit Function
End If
'Clear list object.
TempLst.Clear()
'Create reader object.
sr = New StreamReader(Filename)
'While not end of file read lines.
While Not sr.EndOfStream
'Get current line and trim spaces
sLine = Trim(sr.ReadLine())
'Replace any tabs
sLine = Replace(sLine, vbTab, "")
If Not String.IsNullOrEmpty(sLine) Then
'Check for start tag {
If sLine.EndsWith(TagS) Then
'Store selection name.
sSelection = Trim(Mid(sLine, 1, sLine.Length - 1)).ToUpper()
sLine = ""
InTag = True
End If
'Check for end tag.
If sLine.EndsWith(TagE) Then
InTag = False
End If
'Check if in tag
If InTag Then
If Not String.IsNullOrEmpty(sLine) Then
'Build new string.
TempLst.Add(sSelection + Seperator + sLine)
End If
End If
Else
'
End If
End While
'Close file
sr.Close()
End Function
Public ReadOnly Property ReadString(ByVal Selection As String, ByVal Key As String, Optional ByVal IDefault As String = "") As String
'Return as string.
Get
ReadString = GetValue(Selection, Key, IDefault)
End Get
End Property
Public ReadOnly Property ReadBool(ByVal Selection As String, ByVal Key As String, Optional ByVal IDefault As Boolean = False) As Boolean
'Return as boolean.
Get
Dim TmpStr As String = GetValue(Selection, Key).ToUpper()
'If numeric return bool value.
If IsNumeric(TmpStr) Then
ReadBool = CBool(TmpStr)
Exit Property
End If
'Check for bool words.
If (TmpStr = "YES") Or (TmpStr = "ON") Or (TmpStr = "TRUE") Then
ReadBool = True
Exit Property
ElseIf (TmpStr = vbNullString) Then
ReadBool = IDefault
Exit Property
Else
ReadBool = False
Exit Property
End If
End Get
End Property
Private ReadOnly Property GetValue(ByVal Selection As String, ByVal Key As String, Optional ByVal iDefault As String = "") As String
Get
Dim sValue As String
Dim sName As String
Dim sLine As String
Dim idx As Integer
Dim aPos As Integer
sValue = vbNullString
'Return value
For Each sLine In TempLst
'Get position of seprator |
idx = sLine.IndexOf(Seperator)
'Check for index
If (idx > 0) Then
'Check if names match.
If Selection.ToUpper() = sLine.Substring(0, idx) Then
'Remove selection name from string
sLine = Trim(sLine.Remove(0, idx + 1))
'Check for end ;
If Not sLine.EndsWith(EndChar) Then
Exit For
Else
'Get position of assignment :
aPos = sLine.IndexOf(AssignChar)
'Check for assignment char.
If (aPos = 0) Then
Exit For
Else
'Extract name.
sName = Trim(sLine.Substring(0, aPos)).ToUpper()
'Check for name match
If sName = Key.ToUpper() Then
'Extract value.
sValue = Trim(sLine.Substring(aPos + 1, sLine.Length - aPos - 2))
'Return value.
If (sValue = "") And (iDefault <> "") Then
GetValue = iDefault
Else
GetValue = sValue
End If
Exit Property
End If
End If
End If
End If
End If
Next
'Return default value.
GetValue = iDefault
End Get
End Property
Public Property Filename As String
Get
Filename = mFilename
End Get
Set(ByVal value As String)
mFilename = value
End Set
End Property
End Class
Example
Code:
Public Class frmmain
Private MyCssReader As New CssClass
Private Sub frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyCssReader.Filename = "c:\out\test.css"
End Sub
Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
With MyCssReader
If Not .LoadFromFile() Then
MsgBox("Cannot LoadFromFile")
Else
MsgBox(.ReadString("H1", "TEXT-ALIGN", "left"))
MsgBox(.ReadBool("table", "border"))
End If
End With
End Sub
End Class
Example css style sheet.
Code:
table{
border : yes;
border_width : 5;
}
h1{
Color : red;
Text-align : center;
}