From a0340e7ebedf5e640a143d2e6b9b8a23fbbd3eed Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Sat, 3 Jul 2010 08:17:27 +0000 Subject: [PATCH] Implemented RequireClosingTag flag for custom-defined elements --- ThirdParty/BBCode/BBCode.vbproj | 1 + ThirdParty/BBCode/BBCodeConfiguration.xsd | 1 + ThirdParty/BBCode/Classes/BBCodeElement.vb | 2 + .../BBCode/Classes/BBCodeElementFactory.vb | 2 +- .../Classes/BBCodeElementTypeDefinition.vb | 75 +++++++++++++++++++ .../Classes/BBCodeElementTypeDictionary.vb | 30 ++++++-- 6 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 ThirdParty/BBCode/Classes/BBCodeElementTypeDefinition.vb diff --git a/ThirdParty/BBCode/BBCode.vbproj b/ThirdParty/BBCode/BBCode.vbproj index dca17e7..e30c3d3 100644 --- a/ThirdParty/BBCode/BBCode.vbproj +++ b/ThirdParty/BBCode/BBCode.vbproj @@ -69,6 +69,7 @@ + diff --git a/ThirdParty/BBCode/BBCodeConfiguration.xsd b/ThirdParty/BBCode/BBCodeConfiguration.xsd index daa6616..1eb176d 100644 --- a/ThirdParty/BBCode/BBCodeConfiguration.xsd +++ b/ThirdParty/BBCode/BBCodeConfiguration.xsd @@ -50,6 +50,7 @@ + diff --git a/ThirdParty/BBCode/Classes/BBCodeElement.vb b/ThirdParty/BBCode/Classes/BBCodeElement.vb index e5f5d7b..c63cde3 100644 --- a/ThirdParty/BBCode/Classes/BBCodeElement.vb +++ b/ThirdParty/BBCode/Classes/BBCodeElement.vb @@ -100,6 +100,8 @@ Public Class BBCodeElement If (__RequireClosingTag = TriState.UseDefault) Then If (Not String.IsNullOrEmpty(Me.Name) AndAlso Me.Parser.Dictionary.ContainsKey(Me.Name)) Then __RequireClosingTag = If(Me.Parser.Dictionary(Me.Name).RequireClosingTag, TriState.True, TriState.False) + ElseIf (Not String.IsNullOrEmpty(Me.Name) AndAlso Me.Parser.ElementTypes.ContainsKey(Me.Name)) Then + __RequireClosingTag = If(Me.Parser.ElementTypes(Me.Name).RequireClosingTag, TriState.True, TriState.False) End If End If Return (__RequireClosingTag = TriState.True) diff --git a/ThirdParty/BBCode/Classes/BBCodeElementFactory.vb b/ThirdParty/BBCode/Classes/BBCodeElementFactory.vb index 04e1eb1..4a18808 100644 --- a/ThirdParty/BBCode/Classes/BBCodeElementFactory.vb +++ b/ThirdParty/BBCode/Classes/BBCodeElementFactory.vb @@ -54,7 +54,7 @@ Friend NotInheritable Class BBCodeElementFactory '* '* Gets the type of the element '* - Dim type = Parser.ElementTypes(name.ToUpperInvariant()) + Dim type = Parser.ElementTypes(name.ToUpperInvariant()).Type '* '* Check if the type IS BBCodeElement diff --git a/ThirdParty/BBCode/Classes/BBCodeElementTypeDefinition.vb b/ThirdParty/BBCode/Classes/BBCodeElementTypeDefinition.vb new file mode 100644 index 0000000..6708c4a --- /dev/null +++ b/ThirdParty/BBCode/Classes/BBCodeElementTypeDefinition.vb @@ -0,0 +1,75 @@ +Imports System.Xml.Serialization + +''' +''' Represents a way of describing an element. +''' +Public Structure BBCodeElementTypeDefinition + + Private __TagName As String + Private __RequireClosingTag As Boolean + Private __Type As Type + + ''' + ''' Gets the name of the element. + ''' + _ + Public Property TagName() As String + Get + Return __TagName + End Get + Set(ByVal value As String) + __TagName = value.ToUpperInvariant() + End Set + End Property + + _ + Public Property Type() As Type + Get + Return __Type + End Get + Set(ByVal value As Type) + __Type = value + End Set + End Property + + ''' + ''' Gets or sets a value indicating if the element requires a closing tag. + ''' + _ + Public Property RequireClosingTag() As Boolean + Get + Return __RequireClosingTag + End Get + Set(ByVal value As Boolean) + __RequireClosingTag = value + End Set + End Property + + ''' Indicates whether this instance and a specified object are equal. + ''' true if and this instance are the same type and represent the same value; otherwise, false. + ''' Another object to compare to. + ''' 2 + Public Overrides Function Equals(ByVal obj As Object) As Boolean + If Not (TypeOf obj Is BBCodeElementTypeDefinition) Then + Return False + End If + Return (Me.TagName = obj.TagName AndAlso Me.Type.Equals(DirectCast(obj, BBCodeElementTypeDefinition).Type) AndAlso Me.RequireClosingTag = obj.RequireClosingTag) + End Function + + ''' Returns the hash code for this instance. + ''' A 32-bit signed integer that is the hash code for this instance. + ''' 2 + Public Overrides Function GetHashCode() As Integer + Dim hash As Long = Me.TagName.GetHashCode() + Me.Type.GetHashCode() + Me.RequireClosingTag.GetHashCode() + Return hash And Integer.MinValue + End Function + + Public Shared Operator =(ByVal left As BBCodeElementTypeDefinition, ByVal right As BBCodeElementTypeDefinition) As Boolean + Return left.Equals(right) + End Operator + + Public Shared Operator <>(ByVal left As BBCodeElementTypeDefinition, ByVal right As BBCodeElementTypeDefinition) As Boolean + Return Not left = right + End Operator + +End Structure diff --git a/ThirdParty/BBCode/Classes/BBCodeElementTypeDictionary.vb b/ThirdParty/BBCode/Classes/BBCodeElementTypeDictionary.vb index d4a5b54..4ea9ac9 100644 --- a/ThirdParty/BBCode/Classes/BBCodeElementTypeDictionary.vb +++ b/ThirdParty/BBCode/Classes/BBCodeElementTypeDictionary.vb @@ -25,7 +25,7 @@ Imports System.Diagnostics.CodeAnalysis _ _ Public NotInheritable Class BBCodeElementTypeDictionary - Inherits Dictionary(Of String, Type) + Inherits Dictionary(Of String, BBCodeElementTypeDefinition) Implements IXmlSerializable Private Const STR_ConfigurationItem As String = "element" @@ -47,12 +47,21 @@ Public NotInheritable Class BBCodeElementTypeDictionary ''' ''' The key of the element to add. ''' The value of the element to add. - Public Shadows Sub Add(ByVal tagName As String, ByVal value As Type) + Private Shadows Sub Add(ByVal tagName As String, ByVal value As BBCodeElementTypeDefinition) ValidateTagName(tagName) - ValidateBBCodeElementType(value) + ValidateBBCodeElementType(value.Type) MyBase.Add(tagName.ToUpperInvariant(), value) 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. + Public Shadows Sub Add(ByVal tagName As String, ByVal value As Type, ByVal requireClosingTag As Boolean) + Add(tagName.ToUpperInvariant(), New BBCodeElementTypeDefinition() With {.TagName = tagName, .Type = value, .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. Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema @@ -74,10 +83,14 @@ Public NotInheritable Class BBCodeElementTypeDictionary '* Reads the items '* Do While (reader.Read() AndAlso reader.LocalName = STR_ConfigurationItem AndAlso reader.NamespaceURI = STR_BBCodeSchemaNamespace) - Dim name = reader.GetAttribute("name") - Dim type = System.Type.GetType(reader.GetAttribute("type")) - If (type.IsSubclassOf(GetType(BBCodeElement))) Then - Me.Add(name, type) + Dim definition As New BBCodeElementTypeDefinition() + With definition + .TagName = reader.GetAttribute("name") + Boolean.TryParse(reader.GetAttribute("requireClosingTag"), .RequireClosingTag) + .Type = System.Type.GetType(reader.GetAttribute("type")) + End With + If (definition.Type.IsSubclassOf(GetType(BBCodeElement))) Then + Me.Add(definition.TagName, definition) End If Loop @@ -95,7 +108,8 @@ Public NotInheritable Class BBCodeElementTypeDictionary For Each it In Me writer.WriteStartElement(STR_ConfigurationItem, STR_BBCodeSchemaNamespace) writer.WriteAttributeString("name", it.Key.ToLower(CultureInfo.InvariantCulture)) - writer.WriteAttributeString("type", it.Value.AssemblyQualifiedName) + writer.WriteAttributeString("requireClosingTag", it.Value.RequireClosingTag.ToString()) + writer.WriteAttributeString("type", it.Value.Type.AssemblyQualifiedName) writer.WriteEndElement() Next