Implemented RequireClosingTag flag for custom-defined elements

main
Inga 🏳‍🌈 15 years ago
parent 971fcf4e38
commit a0340e7ebe
  1. 1
      ThirdParty/BBCode/BBCode.vbproj
  2. 1
      ThirdParty/BBCode/BBCodeConfiguration.xsd
  3. 2
      ThirdParty/BBCode/Classes/BBCodeElement.vb
  4. 2
      ThirdParty/BBCode/Classes/BBCodeElementFactory.vb
  5. 75
      ThirdParty/BBCode/Classes/BBCodeElementTypeDefinition.vb
  6. 30
      ThirdParty/BBCode/Classes/BBCodeElementTypeDictionary.vb

@ -69,6 +69,7 @@
<Compile Include="Classes\BBCodeElement.vb" /> <Compile Include="Classes\BBCodeElement.vb" />
<Compile Include="Classes\BBCodeElementDefinition.vb" /> <Compile Include="Classes\BBCodeElementDefinition.vb" />
<Compile Include="Classes\BBCodeElementFactory.vb" /> <Compile Include="Classes\BBCodeElementFactory.vb" />
<Compile Include="Classes\BBCodeElementTypeDefinition.vb" />
<Compile Include="Classes\BBCodeHtmlFormatter.vb" /> <Compile Include="Classes\BBCodeHtmlFormatter.vb" />
<Compile Include="Classes\BBCodeNode.vb" /> <Compile Include="Classes\BBCodeNode.vb" />
<Compile Include="Classes\BBCodeNodeCollection.vb" /> <Compile Include="Classes\BBCodeNodeCollection.vb" />

@ -50,6 +50,7 @@
<xs:element name="Element"> <xs:element name="Element">
<xs:complexType> <xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" /> <xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="requireClosingTag" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required"> <xs:attribute name="type" type="xs:string" use="required">
<xs:annotation> <xs:annotation>
<xs:documentation> <xs:documentation>

@ -100,6 +100,8 @@ Public Class BBCodeElement
If (__RequireClosingTag = TriState.UseDefault) Then If (__RequireClosingTag = TriState.UseDefault) Then
If (Not String.IsNullOrEmpty(Me.Name) AndAlso Me.Parser.Dictionary.ContainsKey(Me.Name)) 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) __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
End If End If
Return (__RequireClosingTag = TriState.True) Return (__RequireClosingTag = TriState.True)

@ -54,7 +54,7 @@ Friend NotInheritable Class BBCodeElementFactory
'* '*
'* Gets the type of the element '* 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 '* Check if the type IS BBCodeElement

@ -0,0 +1,75 @@
Imports System.Xml.Serialization
''' <summary>
''' Represents a way of describing an element.
''' </summary>
Public Structure BBCodeElementTypeDefinition
Private __TagName As String
Private __RequireClosingTag As Boolean
Private __Type As Type
''' <summary>
''' Gets the name of the element.
''' </summary>
<XmlAttribute()> _
Public Property TagName() As String
Get
Return __TagName
End Get
Set(ByVal value As String)
__TagName = value.ToUpperInvariant()
End Set
End Property
<XmlAttribute()> _
Public Property Type() As Type
Get
Return __Type
End Get
Set(ByVal value As Type)
__Type = value
End Set
End Property
''' <summary>
''' Gets or sets a value indicating if the element requires a closing tag.
''' </summary>
<XmlAttribute()> _
Public Property RequireClosingTag() As Boolean
Get
Return __RequireClosingTag
End Get
Set(ByVal value As Boolean)
__RequireClosingTag = value
End Set
End Property
''' <summary>Indicates whether this instance and a specified object are equal.</summary>
''' <returns>true if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, false.</returns>
''' <param name="obj">Another object to compare to.</param>
''' <filterpriority>2</filterpriority>
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
''' <summary>Returns the hash code for this instance.</summary>
''' <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
''' <filterpriority>2</filterpriority>
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

@ -25,7 +25,7 @@ Imports System.Diagnostics.CodeAnalysis
<Serializable()> _ <Serializable()> _
<XmlRoot(ElementName:=STR_BBCodeElementTypesXmlElement, [Namespace]:=STR_BBCodeSchemaNamespace)> _ <XmlRoot(ElementName:=STR_BBCodeElementTypesXmlElement, [Namespace]:=STR_BBCodeSchemaNamespace)> _
Public NotInheritable Class BBCodeElementTypeDictionary Public NotInheritable Class BBCodeElementTypeDictionary
Inherits Dictionary(Of String, Type) Inherits Dictionary(Of String, BBCodeElementTypeDefinition)
Implements IXmlSerializable Implements IXmlSerializable
Private Const STR_ConfigurationItem As String = "element" Private Const STR_ConfigurationItem As String = "element"
@ -47,12 +47,21 @@ Public NotInheritable Class BBCodeElementTypeDictionary
''' </summary> ''' </summary>
''' <param name="tagName">The key of the element to add.</param> ''' <param name="tagName">The key of the element to add.</param>
''' <param name="value">The value of the element to add.</param> ''' <param name="value">The value of the element to add.</param>
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) ValidateTagName(tagName)
ValidateBBCodeElementType(value) ValidateBBCodeElementType(value.Type)
MyBase.Add(tagName.ToUpperInvariant(), value) MyBase.Add(tagName.ToUpperInvariant(), value)
End Sub End Sub
''' <summary>
''' Adds the specified key and value to the dictionary.
''' </summary>
''' <param name="tagName">The key of the element to add.</param>
''' <param name="value">The value of the element to add.</param>
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
''' <summary>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 <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute" /> to the class.</summary> ''' <summary>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 <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute" /> to the class.</summary>
''' <returns>An <see cref="T:System.Xml.Schema.XmlSchema" /> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)" /> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)" /> method.</returns> ''' <returns>An <see cref="T:System.Xml.Schema.XmlSchema" /> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)" /> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)" /> method.</returns>
Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema 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 '* Reads the items
'* '*
Do While (reader.Read() AndAlso reader.LocalName = STR_ConfigurationItem AndAlso reader.NamespaceURI = STR_BBCodeSchemaNamespace) Do While (reader.Read() AndAlso reader.LocalName = STR_ConfigurationItem AndAlso reader.NamespaceURI = STR_BBCodeSchemaNamespace)
Dim name = reader.GetAttribute("name") Dim definition As New BBCodeElementTypeDefinition()
Dim type = System.Type.GetType(reader.GetAttribute("type")) With definition
If (type.IsSubclassOf(GetType(BBCodeElement))) Then .TagName = reader.GetAttribute("name")
Me.Add(name, type) 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 End If
Loop Loop
@ -95,7 +108,8 @@ Public NotInheritable Class BBCodeElementTypeDictionary
For Each it In Me For Each it In Me
writer.WriteStartElement(STR_ConfigurationItem, STR_BBCodeSchemaNamespace) writer.WriteStartElement(STR_ConfigurationItem, STR_BBCodeSchemaNamespace)
writer.WriteAttributeString("name", it.Key.ToLower(CultureInfo.InvariantCulture)) 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() writer.WriteEndElement()
Next Next

Loading…
Cancel
Save