'************************************************** ' FILE : BBCodeDictionary.vb ' AUTHOR : Paulo Santos ' CREATION : 4/29/2009 4:57:05 PM ' COPYRIGHT : Copyright © 2009 ' PJ on Development ' All Rights Reserved. ' ' Description: ' TODO: Add file description ' ' Change log: ' 0.1 4/29/2009 4:57:05 PM ' Paulo Santos ' Created. '*************************************************** Imports System.Xml.Serialization Imports System.Runtime.Serialization Imports System.Diagnostics.CodeAnalysis Imports System.Globalization ''' ''' Represents the dictionary of terms. ''' _ _ Public NotInheritable Class BBCodeElementDictionary Inherits Dictionary(Of String, BBCodeElementDefinition) Implements IXmlSerializable Private Const STR_DictionaryItem As String = "tag" ''' Initializes an instance of the class. ''' This is the default constructor for this class. Friend Sub New() End Sub ''' Initializes a new instance of the class with serialized data. ''' A object containing the information required to serialize the . ''' A structure containing the source and destination of the serialized stream associated with the . Private Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) MyBase.New(info, context) End Sub ''' ''' Adds the specified key and value to the dictionary. ''' ''' The key of the element to add. ''' The value of the element to add. Private Shadows Sub Add(ByVal tagName As String, ByVal value As BBCodeElementDefinition) ValidateTagName(tagName) MyBase.Add(tagName.ToUpperInvariant(), value) End Sub ''' ''' Adds a new tag definition to the . ''' ''' The name of the tag. ''' The text that the tag will generate, with placeholders for tag parameters. Public Shadows Sub Add(ByVal tagName As String, ByVal replacementFormat As String) Add(tagName, replacementFormat, False) End Sub ''' ''' Adds a new tag definition to the . ''' ''' The name of the tag. ''' The text that the tag will generate, with placeholders for tag parameters. ''' A value indicating wether or not the taq requires a closing tag. Public Shadows Sub Add(ByVal tagName As String, ByVal replacementFormat As String, ByVal requireClosingTag As Boolean) Add(tagName, New BBCodeElementDefinition() With {.TagName = tagName, _ .ReplacementFormat = replacementFormat, _ .RequireClosingTag = requireClosingTag}) End Sub ''' This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the to the class. ''' An that describes the XML representation of the object that is produced by the method and consumed by the method. Private Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema Return Nothing End Function ''' Generates an object from its XML representation. ''' The stream from which the object is deserialized. _ Private Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml '* '* Check the name of the element '* If (reader.NamespaceURI <> STR_BBCodeSchemaNamespace) OrElse (reader.LocalName <> STR_BBCodeDictionaryXmlElement) Then Exit Sub End If '* '* Move to the first item '* reader.Read() '* '* Reads the items '* Do While (reader.LocalName = STR_DictionaryItem AndAlso reader.NamespaceURI = STR_BBCodeSchemaNamespace) Dim definition As New BBCodeElementDefinition Dim escaped As Boolean With definition .TagName = reader.GetAttribute("name") Boolean.TryParse(reader.GetAttribute("requireClosingTag"), .RequireClosingTag) Boolean.TryParse(reader.GetAttribute("escaped"), escaped) If (escaped) Then .ReplacementFormat = reader.ReadElementContentAsString() Else .ReplacementFormat = reader.ReadInnerXml().Replace(" xmlns=""""", "") End If End With Me.Add(definition.TagName, definition) Loop If (reader.NodeType = Xml.XmlNodeType.EndElement) Then reader.Read() End If End Sub ''' Converts an object into its XML representation. ''' The stream to which the object is serialized. _ Private Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml Dim doc = New Xml.XmlDocument() Dim xml = doc.CreateElement(STR_DictionaryItem, STR_BBCodeSchemaNamespace) For Each it In Me xml.Attributes.RemoveAll() xml.SetAttribute("name", it.Key.ToLower(CultureInfo.InvariantCulture)) xml.SetAttribute("requireClosingTag", it.Value.RequireClosingTag.ToString()) Try '* '* Check if the Replacement format is valid XML '* xml.InnerXml = it.Value.ReplacementFormat Catch ex As Xml.XmlException '* '* It's not a valid XML, so adds it as a value '* xml.SetAttribute("escaped", True) xml.InnerText = it.Value.ReplacementFormat End Try '* '* OK, we attemp to write it of '* writer.WriteNode(xml.CreateNavigator(), True) Next End Sub End Class