'**************************************************
' FILE : BBCodeElementFactory.vb
' AUTHOR : Paulo Santos
' CREATION : 4/30/2009 10:48:21 PM
' COPYRIGHT : Copyright © 2009
' PJ on Development
' All Rights Reserved.
'
' Description:
' TODO: Add file description
'
' Change log:
' 0.1 4/30/2009 10:48:21 PM
' Paulo Santos
' Created.
'***************************************************
'''
''' Creates instances of .
'''
Friend NotInheritable Class BBCodeElementFactory(Of TContext As Class)
Private __Parser As BBCodeParser(Of TContext)
''' Initializes an instance of the class.
''' The that will utilize the new instance of the .
Friend Sub New(ByVal parser As BBCodeParser(Of TContext))
__Parser = parser
End Sub
'''
''' Gets the that utilizes this instance of the .
'''
Public ReadOnly Property Parser() As BBCodeParser(Of TContext)
Get
Return __Parser
End Get
End Property
'''
''' Creates a new .
'''
''' The name of the element.
''' The attributes of the element.
''' A new .
Public Function CreateElement(ByVal name As String, ByVal attributes As BBCodeAttributeDictionary) As BBCodeElement(Of TContext)
Dim el As BBCodeElement(Of TContext)
'*
'* Check if we have a different type to create
'*
If (Parser.ElementTypes.ContainsKey(name.ToUpperInvariant())) Then
'*
'* Gets the type of the element
'*
Dim type = Parser.ElementTypes(name.ToUpperInvariant()).Type
'*
'* Check if the type IS BBCodeElement
'*
If (type.Equals(GetType(BBCodeElement(Of TContext)))) Then
el = New BBCodeElement(Of TContext)()
Else
'*
'* Gets the default constructor
'*
Dim ctor = type.GetConstructor(New Type() {})
If (ctor Is Nothing) Then
Throw New InvalidOperationException("The type " & type.FullName & " does not have a default constructor.")
End If
'*
'* Creates the new element.
'*
el = TryCast(ctor.Invoke(New Object() {}), BBCodeElement(Of TContext))
If (el Is Nothing) Then
Throw New InvalidOperationException("The type " & type.FullName & " could not be assingned to BBCodeElement.")
End If
End If
Else
el = New BBCodeElement(Of TContext)()
End If
'*
'* Set the element properties
'*
el.SetName(name)
el.SetParser(Parser)
For Each attr In attributes
el.Attributes.Add(attr.Key, attr.Value)
Next
'*
'* Returns the created element.
'*
Return el
End Function
End Class