1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.exchange;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30 public class VObject {
31
32
33
34 ArrayList<VProperty> properties;
35
36
37
38 ArrayList<VObject> vObjects;
39
40
41
42 public String type;
43
44
45
46
47
48
49
50
51 public VObject(VProperty beginProperty, BufferedReader reader) throws IOException {
52 if (!"BEGIN".equals(beginProperty.getKey())) {
53 throw new IOException("Invalid first line: " + beginProperty);
54 }
55 type = beginProperty.getValue();
56 String beginLine = "BEGIN:" + type;
57 String endLine = "END:" + type;
58 String line = reader.readLine();
59 while (line != null && !line.startsWith(endLine)) {
60
61 if (!beginLine.equals(line)) {
62 handleLine(line, reader);
63 }
64 line = reader.readLine();
65 }
66
67 }
68
69
70
71
72
73
74
75 public VObject(BufferedReader reader) throws IOException {
76 this(new VProperty(reader.readLine()), reader);
77 }
78
79
80
81
82
83
84
85 public VObject(String itemBody) throws IOException {
86 this(new ICSBufferedReader(new StringReader(itemBody)));
87 }
88
89
90
91
92 public VObject() {
93 }
94
95 public boolean isVTimezone() {
96 return "VTIMEZONE".equals(type);
97 }
98
99 public boolean isVEvent() {
100 return "VEVENT".equals(type);
101 }
102
103 public boolean isVTodo() {
104 return "VTODO".equals(type);
105 }
106
107 public boolean isVAlarm() {
108 return "VALARM".equals(type);
109 }
110
111
112 protected void handleLine(String line, BufferedReader reader) throws IOException {
113
114 if (line.length() > 0) {
115 VProperty property = new VProperty(line);
116
117 if ("BEGIN".equals(property.getKey())) {
118 addVObject(new VObject(property, reader));
119 } else if (property.getKey() != null) {
120 addProperty(property);
121 }
122 }
123 }
124
125
126
127
128
129
130 public void addVObject(VObject vObject) {
131 if (vObjects == null) {
132 vObjects = new ArrayList<>();
133 }
134 vObjects.add(vObject);
135 }
136
137
138
139
140
141
142 public void addProperty(VProperty property) {
143 if (property.getValue() != null) {
144 if (properties == null) {
145 properties = new ArrayList<>();
146 }
147 properties.add(property);
148 }
149 }
150
151
152
153
154
155
156 public void writeTo(ICSBufferedWriter writer) {
157 writer.write("BEGIN:");
158 writer.writeLine(type);
159 if (properties != null) {
160 for (VProperty property : properties) {
161 writer.writeLine(property.toString());
162 }
163 }
164 if (vObjects != null) {
165 for (VObject object : vObjects) {
166 object.writeTo(writer);
167 }
168 }
169 writer.write("END:");
170 writer.writeLine(type);
171 }
172
173 public String toString() {
174 ICSBufferedWriter writer = new ICSBufferedWriter();
175 writeTo(writer);
176 return writer.toString();
177 }
178
179
180
181
182
183
184 public List<VProperty> getProperties() {
185 return properties;
186 }
187
188
189
190
191
192
193
194 public VProperty getProperty(String name) {
195 if (properties != null) {
196 for (VProperty property : properties) {
197 if (property.getKey() != null && property.getKey().equalsIgnoreCase(name)) {
198 return property;
199 }
200 }
201
202 }
203 return null;
204 }
205
206
207
208
209
210
211
212 public List<VProperty> getProperties(String name) {
213 List<VProperty> result = null;
214 if (properties != null) {
215 for (VProperty property : properties) {
216 if (property.getKey() != null && property.getKey().equalsIgnoreCase(name)) {
217 if (result == null) {
218 result = new ArrayList<>();
219 }
220 result.add(property);
221 }
222 }
223
224 }
225 return result;
226 }
227
228
229
230
231
232
233
234 public String getPropertyValue(String name) {
235 VProperty property = getProperty(name);
236 if (property != null) {
237 return property.getValue();
238 } else {
239 return null;
240 }
241 }
242
243
244
245
246
247
248
249 public void setPropertyValue(String name, String value) {
250 if (value == null) {
251 removeProperty(name);
252 } else {
253 VProperty property = getProperty(name);
254 if (property == null) {
255 property = new VProperty(name, value);
256 addProperty(property);
257 } else {
258 property.setValue(value);
259 }
260 }
261 }
262
263
264
265
266
267
268
269 public void addPropertyValue(String name, String value) {
270 if (value != null) {
271 VProperty property = new VProperty(name, value);
272 addProperty(property);
273 }
274 }
275
276
277
278
279
280
281 public void removeProperty(String name) {
282 if (properties != null) {
283 VProperty property = getProperty(name);
284 if (property != null) {
285 properties.remove(property);
286 }
287 }
288 }
289
290
291
292
293
294
295 public void removeProperty(VProperty property) {
296 if (properties != null) {
297 properties.remove(property);
298 }
299 }
300
301 public void setType(String type) {
302 this.type = type;
303 }
304
305 }