-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplateField.vb
227 lines (189 loc) · 7.04 KB
/
TemplateField.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'Copyright by Media Support - Didi Kunz [email protected]
'The same license conditions apply as CasparCG server has.
'
Imports System.Text
''' <summary>
''' TemplateField object
''' </summary>
''' <remarks>Used to send fields to templates or retrieve them</remarks>
<Serializable()> _
Public Class TemplateField
Implements IComparable(Of TemplateField)
Public Enum enumAttributeType
Text
Image
End Enum
#Region "Private vars"
Private _Attachements As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
#End Region
#Region "Properties"
''' <summary>
''' Field name
''' </summary>
Public Property Name As String = ""
''' <summary>
''' Field value
''' </summary>
Public Property Value As String = ""
''' <summary>
''' Attachements are additional non standard data that are added to the output as separate attributes. Currently only works in XML.
''' </summary>
''' <returns>A list of KeyValuePairs</returns>
Public ReadOnly Property Attachements As List(Of KeyValuePair(Of String, String))
Get
Return _Attachements
End Get
End Property
''' <summary>
''' Render the value as an XML Element instead of a Value attribute. Only works in XML.
''' </summary>
Public Property RenderAsElement As Boolean = False
''' <summary>
''' Set the type attribute for this value. Only works in XML.
''' </summary>
''' <returns></returns>
Public Property AttributeType As enumAttributeType = enumAttributeType.Text
#End Region
#Region "Shared"
Public Shared Function EncodeText(text As String, encodeAsHTML As Boolean) As String
If encodeAsHTML = False Then
Return text
Else 'HTML
Dim stringBuilder As New StringBuilder
Dim i As Integer
Dim c As Char
For i = 0 To text.Length - 1
c = text(i)
Select Case Asc(c)
Case 34 '"
stringBuilder.Append(""")
Case 38 '&
stringBuilder.Append("&")
Case 39 ''
stringBuilder.Append("'")
Case 60 '<
stringBuilder.Append("<")
Case 62 '>
stringBuilder.Append(">")
Case 92 '\
stringBuilder.Append("\")
Case Else
stringBuilder.Append(c)
End Select
Next
Return stringBuilder.ToString
End If
End Function
#End Region
#Region "Methods"
''' <summary>
''' Formated ToString function
''' </summary>
''' <returns>A string containing name and value</returns>
Public Overrides Function ToString() As String
Return String.Format("{0} = '{1}'", Me.Name, Me.Value)
End Function
''' <summary>
''' CompareTo for sorting
''' </summary>
''' <param name="other">A TemplateField object to compare this with</param>
''' <returns>An integer indicating if bigger, smaller or equal</returns>
Public Function CompareTo(other As TemplateField) As Integer Implements System.IComparable(Of TemplateField).CompareTo
Return Name.CompareTo(other.Name)
End Function
''' <summary>
''' Add a new Attachement to the list.
''' </summary>
''' <param name="Name">Name of the attachement => name of the attribute.</param>
''' <param name="Value">Value of the attachement => value of the attribute.</param>
Public Sub AddAttachement(Name As String, Value As String)
AddAttachement(Name, Value, False)
End Sub
''' <summary>
''' Add a new Attachement to the list.
''' </summary>
''' <param name="Name">Name of the attachement => name of the attribute.</param>
''' <param name="Value">Value of the attachement => value of the attribute.</param>
''' <param name="EncodeAsHTML">Text encoding for the value</param>
Public Sub AddAttachement(Name As String, Value As String, EncodeAsHTML As Boolean)
_Attachements.Add(New KeyValuePair(Of String, String)(Name, EncodeText(Value, EncodeAsHTML)))
End Sub
#End Region
#Region "Constructors"
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
Public Sub New(Name As String)
Me.Name = Name
End Sub
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
''' <param name="Value">The value of the field</param>
Public Sub New(Name As String, Value As String)
Me.Name = Name
Me.Value = Value
End Sub
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
''' <param name="Value">The value of the field</param>
''' <param name="Type">The type attribute</param>
Public Sub New(Name As String, Value As String, Type As enumAttributeType)
Me.Name = Name
Me.Value = Value
Me.AttributeType = Type
End Sub
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
''' <param name="Value">The value of the field</param>
''' <param name="EncodeAsHTML">Enables text encoding for the value</param>
Public Sub New(Name As String, Value As String, EncodeAsHTML As Boolean)
Me.Name = Name
Me.Value = EncodeText(Value, EncodeAsHTML)
End Sub
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
''' <param name="Value">The value of the field</param>
''' <param name="EncodeAsHTML">Enables text encoding for the value</param>
''' <param name="Type">The type attribute</param>
Public Sub New(Name As String, Value As String, EncodeAsHTML As Boolean, Type As enumAttributeType)
Me.Name = Name
Me.Value = EncodeText(Value, EncodeAsHTML)
Me.AttributeType = Type
End Sub
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
''' <param name="Value">The value of the field</param>
''' <param name="EncodeAsHTML">Enables text encoding for the value</param>
''' <param name="RenderAsElement">Render value as XML element insted of an attribute</param>
Public Sub New(Name As String, Value As String, EncodeAsHTML As Boolean, RenderAsElement As Boolean)
Me.Name = Name
Me.Value = EncodeText(Value, EncodeAsHTML)
Me.RenderAsElement = RenderAsElement
End Sub
''' <summary>
''' Constructor
''' </summary>
''' <param name="Name">The name of the field</param>
''' <param name="Value">The value of the field</param>
''' <param name="EncodeAsHTML">Enables text encoding for the value</param>
''' <param name="RenderAsElement">Render value as XML element insted of an attribute</param>
''' <param name="Type">The type attribute</param>
Public Sub New(Name As String, Value As String, EncodeAsHTML As Boolean, RenderAsElement As Boolean, Type As enumAttributeType)
Me.Name = Name
Me.Value = EncodeText(Value, EncodeAsHTML)
Me.RenderAsElement = RenderAsElement
Me.AttributeType = Type
End Sub
#End Region
End Class