-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcard-v3.go
161 lines (138 loc) · 3.48 KB
/
vcard-v3.go
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
package vcard
import (
"strings"
)
type VCardV3 struct {
properties []IProperty
/**
* possible values:
* - ignore - if a property with cardinality 1 or *1 is already set and is added for the second time, the new added is ignored (the first property is kept)
* - overwrite - if a property with cardinality 1 or *1 is already set and is added for the second time, the old one is overwritten.
* default: overwrite
*/
addPropertyScenario string
}
func (b *VCardV3) SetAddPropertyScenario(v string) {
if v != "ignore" {
v = "overwrite"
}
b.addPropertyScenario = v;
}
func (b *VCardV3) GetAddPropertyScenario() string {
v := b.addPropertyScenario
if v!= "ignore" {
v = "overwrite"
}
return v
}
func (vc *VCardV3) GetProperties() []IProperty {
return vc.properties
}
/**
* create property
*/
func (vc *VCardV3) CreateProperty(name string) IProperty {
p := NewProperty(name)
switch (p.GetName()) {
case "BEGIN", "END":
p.SetCardinality("1")
p.SetAllowMultipleValues(false)
p.AddValue(NewText("VCARD"))
case "PROFILE":
p.SetCardinality("*1")
p.SetAllowMultipleValues(false)
p.AddValue(NewText("VCARD"))
case "VERSION":
p.SetCardinality("1")
p.SetAllowMultipleValues(false)
p.AddValue(NewText("3.0"))
case "FN", "N":
p.SetCardinality("1")
p.SetAllowMultipleValues(false)
case "NICKNAME":
p.SetCardinality("*")
p.SetAllowMultipleValues(true)
case "PHOTO","ADR","LABEL","TEL","EMAIL","GEO", "TITLE", "ROLE", "LOGO", "AGENT", "ORG", "CATEGORIES", "NOTE", "PRODID", "SORT-STRING", "SOUND", "KEY":
p.SetCardinality("*")
p.SetAllowMultipleValues(false)
case "BDAY", "MAILER","TZ", "REV", "SOURCE", "UID", "CLASS":
p.SetCardinality("*1")
p.SetAllowMultipleValues(false)
default:
p.SetCardinality("*")
p.SetAllowMultipleValues(false)
}
return p
}
/**
* add a property
*/
func (b *VCardV3) AddProperty(p IProperty) {
if p.GetCardinality() == "1" || p.GetCardinality() == "*1" {
// only one property should exists
switch (b.GetAddPropertyScenario()) {
case "ignore":
propExists := b.GetProperty(p.GetName())
if len(propExists) > 0 {
// ignore item
return
}
case "overwrite":
// remove all existing properties of the same type (name)
b.DeleteProperty(p.GetName())
}
}
b.properties = append(b.properties, p)
}
/**
* return a list of properties
*/
func (b *VCardV3) GetProperty(name string) []IProperty {
var result []IProperty
for _ , p := range b.properties {
if (p.GetName() == strings.ToUpper(name)) {
result = append(result, p)
}
}
return result
}
/**
* delete a proprty by name
*/
func (b *VCardV3) DeleteProperty(name string) {
for idx, p := range b.properties {
if (p.GetName() == strings.ToUpper(name)) {
b.properties = append(b.properties[:idx], b.properties[idx+1:]...)
}
}
}
/**
* create a parameter and attachit to a property
* parameters & parameters values are specific to properties
*/
func (vc *VCardV3) AddPropertyParameter(p IProperty, name string, value []string) {
param := NewParameter(name)
switch (param.GetName()) {
case "ENCODING":
if len(value) > 0 {
if (strings.ToLower(value[0]) == "base64") {
value[0] = "b"
}
}
case "VALUE":
case "CHARSET":
case "LANGUAGE":
case "CONTEXT":
}
param.SetValue(value)
p.AddParameter(param)
}
func (vc *VCardV3) Build() string {
builder := NewBuilder(vc)
return builder.Build()
}
func NewVCardV3() *VCardV3{
v := VCardV3{}
v.SetAddPropertyScenario("overwrite")
return &v
}