/
CategoriesProperty.cs
195 lines (165 loc) · 7.11 KB
/
CategoriesProperty.cs
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
//===============================================================================================================
// System : Personal Data Interchange Classes
// File : CategoriesProperty.cs
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 01/03/2019
// Note : Copyright 2004-2019, Eric Woodruff, All rights reserved
// Compiler: Microsoft Visual C#
//
// This file contains the Categories property class used by the Personal Data Interchange (PDI) classes such as
// vCalendar, iCalendar, and vCard.
//
// This code is published under the Microsoft Public License (Ms-PL). A copy of the license should be
// distributed with the code and can be found at the project website: https://github.com/EWSoftware/PDI.
// This notice, the author's name, and all copyright notices must remain intact in all applications,
// documentation, and source files.
//
// Date Who Comments
// ==============================================================================================================
// 03/14/2004 EFW Created the code
// 08/19/2007 EFW Added support for vNote objects
//===============================================================================================================
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace EWSoftware.PDI.Properties
{
/// <summary>
/// This class is used to represent the Categories (CATEGORIES) property of a vCard, vCalendar, or iCalendar
/// object.
/// </summary>
/// <remarks>This property class parses the <see cref="Value"/> property and allows access to the component
/// parts. It is used to specify application category information about the object. This property is only
/// valid for use with the vCard 3.0 specification, vNote, vCalendar and iCalendar objects.</remarks>
public class CategoriesProperty : BaseProperty
{
#region Private data members
//=====================================================================
private static Regex reSplit = new Regex(@"(?:^[,;])|(?<=(?:[^\\]))[,;]");
private StringCollection categories;
#endregion
#region Properties
//=====================================================================
/// <summary>
/// This is used to establish the specification versions supported by the PDI object
/// </summary>
/// <value>Supports vCard 3.0 and 4.0, vCalendar 1.0, iCalendar 2.0, and IrMC 1.1.</value>
public override SpecificationVersions VersionsSupported => SpecificationVersions.vCard30 |
SpecificationVersions.vCard40 | SpecificationVersions.CalendarAll | SpecificationVersions.IrMC11;
/// <summary>
/// This read-only property defines the tag (CATEGORIES)
/// </summary>
public override string Tag => "CATEGORIES";
/// <summary>
/// This read-only property defines the default value type as TEXT
/// </summary>
public override string DefaultValueLocation => ValLocValue.Text;
/// <summary>
/// This property is used to get the categories string collection
/// </summary>
/// <value>Categories can be added to or removed from the returned collection reference</value>
public StringCollection Categories => categories;
/// <summary>
/// This property is used to set or get the categories as a string value
/// </summary>
/// <value>The string can contain one or more categories separated by commas or semi-colons. The string
/// will be split and loaded into the categories string collection.</value>
public string CategoriesString
{
get => String.Join(", ", categories);
set
{
string tempCat;
string[] entries;
categories.Clear();
if(value != null)
{
entries = value.Split(',', ';');
foreach(string s in entries)
{
tempCat = s.Trim();
if(tempCat.Length > 0)
categories.Add(tempCat);
}
}
}
}
/// <summary>
/// This property is overridden to handle parsing the categories and concatenating them when requested
/// </summary>
/// <value>The categories are escaped as needed</value>
public override string Value
{
get
{
// If empty, nothing will be saved
if(this.Categories.Count == 0)
return null;
StringBuilder sb = new StringBuilder(256);
foreach(string s in this.Categories)
{
// The vCalendar 1.0 and IrMC 1.1 spec use a semi-colon
if(this.Version == SpecificationVersions.vCalendar10 || this.Version == SpecificationVersions.IrMC11)
sb.Append(';');
else
sb.Append(',');
sb.Append(EncodingUtils.Escape(s));
}
sb.Remove(0, 1);
return sb.ToString();
}
set
{
string tempCat;
string[] entries;
this.Categories.Clear();
if(value != null && value.Length > 0)
{
// Split on all semi-colons and commas except escaped ones
entries = reSplit.Split(value);
foreach(string s in entries)
{
tempCat = EncodingUtils.Unescape(s.Trim());
if(tempCat.Length > 0)
categories.Add(tempCat);
}
}
}
}
/// <summary>
/// This property is overridden to handle parsing the categories and concatenating them when requested
/// </summary>
/// <value>The categories are escaped as needed</value>
public override string EncodedValue
{
get => this.Value;
set => this.Value = value;
}
#endregion
#region Constructor
//=====================================================================
/// <summary>
/// Constructor. Unless the version is changed, the object will conform to the vCard 3.0/iCalendar 2.0
/// specification.
/// </summary>
public CategoriesProperty()
{
this.Version = SpecificationVersions.iCalendar20;
categories = new StringCollection();
}
#endregion
#region Methods
//=====================================================================
/// <summary>
/// This is overridden to allow cloning of a PDI object
/// </summary>
/// <returns>A clone of the object</returns>
public override object Clone()
{
CategoriesProperty o = new CategoriesProperty();
o.Clone(this);
return o;
}
#endregion
}
}