|
Imports System
Imports System.Collections
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Xml.Serialization
Namespace Config
' Represents the Page.config XML file as a .NET class
<XmlRoot()> _
Public Class PageConfig
' A constant denoting the Page.config file name
<XmlIgnore()> _
Public Const PageConfigFileName As String = "Page.config"
' A constant denoting the PageConfig Cache object key
<XmlIgnore()> _
Public Const PageConfigCacheKey As String = "__PAGECONFIG"
' A constant denoting the Page.config file path
<XmlIgnore()> _
Public Shared ReadOnly Property PageConfigFilePath() As String
Get
' Return the default path to the Page.config file
Return String.Format("~/{0}", PageConfig.PageConfigFileName)
End Get
End Property
' The Page Templates defined in the Page.config file
<XmlArray(), XmlArrayItem("PageTemplate", GetType(PageTemplate))> _
Public PageTemplates As PageTemplateCollection
' The Pages defined in the Page.config file
<XmlArray(), XmlArrayItem("Page", GetType(Page))> _
Public Pages As PageCollection
' Default Constructor
Public Sub New()
' Create a default PageTemplateCollection
Me.PageTemplates = New PageTemplateCollection
' Create a default PageCollection
Me.Pages = New PageCollection
End Sub
' Locates the first Page in the PageConfig with the specified path
' @pagePath: The path of the Page to locate
' returns: The Page found in the PageConfig instance
Public Function FindPage(ByVal pagePath As String) As Page
' Create a local variable to hold the Page when found
Dim pageFound As Page = Nothing
' Iterate over the Pages ArrayList
Dim _page As Page
For Each _page In Me.Pages
' Check to ensure the Page's Path is not a null value
If (_page.Path <> Nothing) Then
' Check to see if the current Page's Path matches the specified pagePath
If (_page.Path.Trim().ToUpper().Equals(pagePath.Trim().ToUpper())) Then
' The Page was found, obtain a reference to the Page
pageFound = _page
' Break from the loop
Exit For
End If
ElseIf (_page.Expression <> Nothing) Then
' Check to see if the current Page's Expression matches the specified pagePath
If (Regex.IsMatch(pagePath, _page.Expression, RegexOptions.IgnoreCase)) Then
' The Page matched the Expression, obtain a reference to the Page
pageFound = _page
' Break from the loop
Exit For
End If
End If
Next _page
' Return the found Page
Return pageFound
End Function
' Locates the first Page in the PageConfig based on the path of the specified Request
' @request: The Request object containing the current Page path
' returns: The Page found in the PageConfig instance
Public Function FindPage(ByVal request As System.Web.HttpRequest) As Page
' Call the FindPage method and specify the Request's path, replacing the ApplicationPath with a tilde
Return Me.FindPage(request.Path.ToUpper().Replace(request.ApplicationPath.ToUpper(), "~"))
End Function
' Locates the specified templateName in the Page's PageTemplates collection
' @templateName: The template to be located
' returns: A reference ot the specified PageTemplate
Public Function FindTemplate(ByVal templateName As String) As PageTemplate
' Create a local variable to hold the PageTemplate if found
Dim templateFound As PageTemplate = Nothing
' Iterate over the templates in the PageTemplates ArrayList
Dim template As PageTemplate
For Each template In Me.PageTemplates
' Check to see if the curren PageTemplate matches the specified template
If (template.Name.Trim().ToUpper().Equals(templateName.Trim().ToUpper())) Then
' The PageTemplate was found, obtain a reference to the template
templateFound = template
' Break from the loop
Exit For
End If
Next template
' Return the found PageTemplate
Return templateFound
End Function
' Locates the default PageTemplate specified for PageConfig
' returns: A reference to the default PageTemplate
Public Function FindDefaultTemplate() As PageTemplate
' Create a local variable to hold the default PageTemplate
Dim defaultTemplate As PageTemplate = Nothing
' Iterate over the templates in the PageTemplates ArrayList
For Each template As PageTemplate In Me.PageTemplates
' Check to see if the current template is the default template
If (template.IsDefault) Then
' Obtain a reference to the default template
defaultTemplate = template
End If
Next
' Return the default PageTemplate
Return defaultTemplate
End Function
' Saves the PageConfig instance to the specified path
' @configFilePath: The path to which the PageConfig will be saved
Public Sub Save(ByVal configFilePath As String)
' Create an XmlSerializer to save the Page.config file
Dim xs As New XmlSerializer(GetType(PageConfig))
' Create an XmlTextWriter for saving the serialized XML to a file
Dim writer As New XmlTextWriter(configFilePath, System.Text.Encoding.UTF8)
' Set the formatting mode for the writer
writer.Formatting = Formatting.Indented
' Serialize the PageConfig into the writer
xs.Serialize(writer, Me)
' Flush the writer
writer.Flush()
' Close the writer
writer.Close()
End Sub
' Returns an XML string representing the serialized PageConfig
' returns: An XML string representing the serialized PageConfig
Public Overrides Function ToString() As String
' Create an XmlSerializer to generate the Page.config XML string
Dim xs As New XmlSerializer(GetType(PageConfig))
' Create a MemoryStream to serialize the instance into
Dim ms As New MemoryStream
' Serialize the instance into the MemoryStream
xs.Serialize(ms, Me)
' Get the XML string from the MemoryStream
Return System.Text.Encoding.UTF8.GetString(ms.ToArray())
End Function
' Deserializes the specified configuration file into a new instance
' of the PageConfig class
' @configPath: The path to the configuration file
' returns: The configuration file as an instance of the PageConfig class
Public Shared Function Load(ByVal configFilePath As String) As PageConfig
' Load the Page.config file into an XmlTextReader
Dim reader As New XmlTextReader(configFilePath)
' Create an XmlSerializer to load the Page.config file
Dim xs As New XmlSerializer(GetType(PageConfig))
' Deserialize the Page.config file into a PageConfig object
Dim pc As PageConfig = CType(xs.Deserialize(reader), PageConfig)
' Close the XmlTextReader
reader.Close()
' Return the deserialized instance
Return pc
End Function
End Class
End Namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Scott Van Vliet is a Principal Consultant with Neudesic based in Irvine, CA. He has been designing and developing technology solutions for the past 8 years, and has worked with Microsoft .NET technologies for over 3 years. Scott is currently developing solutions with ASP.NET 2.0, C# 2.0, Windows Presentation Foundation (codename "Avalon"), SQL Server 2000/2005, Reporting Services and SharePoint.
Scott welcomes feedback and can be reached through his Weblog at
http://weblogs.asp.net/skillet/.