summaryrefslogtreecommitdiff
path: root/src/com/google/wireless/gdata/calendar/serializer/xml/XmlEventEntryGDataSerializer.java
blob: ccb701ffdecc67b9b1357773812b4b21242cfc1d (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2007 The Android Open Source Project

package com.google.wireless.gdata.calendar.serializer.xml;

import com.google.wireless.gdata.calendar.data.EventEntry;
import com.google.wireless.gdata.calendar.data.When;
import com.google.wireless.gdata.calendar.data.Reminder;
import com.google.wireless.gdata.calendar.data.Who;
import com.google.wireless.gdata.data.StringUtils;
import com.google.wireless.gdata.parser.xml.XmlGDataParser;
import com.google.wireless.gdata.parser.xml.XmlParserFactory;
import com.google.wireless.gdata.parser.ParseException;
import com.google.wireless.gdata.serializer.xml.XmlEntryGDataSerializer;

import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;


/**
 *  Serializes Google Calendar event entries into the Atom XML format.
 */
// TODO: move all strings into constants.  share with parser?
public class XmlEventEntryGDataSerializer extends XmlEntryGDataSerializer {

    public static final String NAMESPACE_GCAL = "gCal";
    public static final String NAMESPACE_GCAL_URI =
            "http://schemas.google.com/gCal/2005";

    public XmlEventEntryGDataSerializer(XmlParserFactory factory,
            EventEntry entry) {
        super(factory, entry);
    }

    protected EventEntry getEventEntry() {
        return (EventEntry) getEntry();
    }

    protected void declareExtraEntryNamespaces(XmlSerializer serializer)
            throws IOException {
        serializer.setPrefix(NAMESPACE_GCAL, NAMESPACE_GCAL_URI);
    }

    /* (non-Javadoc)
     * @see XmlEntryGDataSerializer#serializeExtraEntryContents
     */
    protected void serializeExtraEntryContents(XmlSerializer serializer,
            int format)
            throws IOException, ParseException {
        EventEntry entry = getEventEntry();

        serializeEventStatus(serializer, entry.getStatus());
        serializeTransparency(serializer, entry.getTransparency());
        serializeVisibility(serializer, entry.getVisibility());
        Enumeration attendees = entry.getAttendees().elements();
        while (attendees.hasMoreElements()) {
            Who attendee = (Who) attendees.nextElement();
            serializeWho(serializer, entry, attendee);
        }

        serializeRecurrence(serializer, entry.getRecurrence());
        // either serialize reminders directly under the entry, or serialize
        // whens (with reminders within the whens) -- should be just one.
        if (entry.getRecurrence() != null) {
            if (entry.getReminders() != null) {
                Enumeration reminders = entry.getReminders().elements();
                while (reminders.hasMoreElements()) {
                    Reminder reminder = (Reminder) reminders.nextElement();
                    serializeReminder(serializer, reminder);
                }
            }
        } else {
            Enumeration whens = entry.getWhens().elements();
            while (whens.hasMoreElements()) {
                When when = (When) whens.nextElement();
                serializeWhen(serializer, entry, when);
            }
        }
        serializeOriginalEvent(serializer,
                entry.getOriginalEventId(),
                entry.getOriginalEventStartTime());
        serializeWhere(serializer, entry.getWhere());

        serializeCommentsUri(serializer, entry.getCommentsUri());

        Hashtable extendedProperties = entry.getExtendedProperties();
        if (extendedProperties != null) {
            Enumeration propertyNames = extendedProperties.keys();
            while (propertyNames.hasMoreElements()) {
                String propertyName = (String) propertyNames.nextElement();
                String propertyValue = (String) extendedProperties.get(propertyName);
                serializeExtendedProperty(serializer, propertyName, propertyValue);
            }
        }
    }

    private static void serializeEventStatus(XmlSerializer serializer,
            byte status)
            throws IOException {

        String statusString;

        switch (status) {
            case EventEntry.STATUS_TENTATIVE:
                statusString = "http://schemas.google.com/g/2005#event.tentative";
                break;
            case EventEntry.STATUS_CANCELED:
                statusString = "http://schemas.google.com/g/2005#event.canceled";
                break;
            case EventEntry.STATUS_CONFIRMED:
                statusString = "http://schemas.google.com/g/2005#event.confirmed";
                break;
            default:
                // should not happen
                // TODO: log this
                statusString = "http://schemas.google.com/g/2005#event.tentative";
        }

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "eventStatus");
        serializer.attribute(null /* ns */, "value", statusString);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "eventStatus");
    }

    private static void serializeRecurrence(XmlSerializer serializer,
            String recurrence)
            throws IOException {
        if (StringUtils.isEmpty(recurrence)) {
            return;
        }
        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "recurrence");
        serializer.text(recurrence);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "recurrence");
    }

    private static void serializeTransparency(XmlSerializer serializer,
            byte transparency)
            throws IOException {

        String transparencyString;

        switch (transparency) {
            case EventEntry.TRANSPARENCY_OPAQUE:
                transparencyString =
                        "http://schemas.google.com/g/2005#event.opaque";
                break;
            case EventEntry.TRANSPARENCY_TRANSPARENT:
                transparencyString =
                        "http://schemas.google.com/g/2005#event.transparent";
                break;
            default:
                // should not happen
                // TODO: log this
                transparencyString =
                        "http://schemas.google.com/g/2005#event.transparent";
        }

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "transparency");
        serializer.attribute(null /* ns */, "value", transparencyString);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "transparency");
    }


    private static void serializeVisibility(XmlSerializer serializer,
            byte visibility)
            throws IOException {

        String visibilityString;

        switch (visibility) {
            case EventEntry.VISIBILITY_DEFAULT:
                visibilityString = "http://schemas.google.com/g/2005#event.default";
                break;
            case EventEntry.VISIBILITY_CONFIDENTIAL:
                visibilityString =
                        "http://schemas.google.com/g/2005#event.confidential";
                break;
            case EventEntry.VISIBILITY_PRIVATE:
                visibilityString = "http://schemas.google.com/g/2005#event.private";
                break;
            case EventEntry.VISIBILITY_PUBLIC:
                visibilityString = "http://schemas.google.com/g/2005#event.public";
                break;
            default:
                // should not happen
                // TODO: log this
                visibilityString = "http://schemas.google.com/g/2005#event.default";
        }

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "visibility");
        serializer.attribute(null /* ns */, "value", visibilityString);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "visibility");
    }

    private static void serializeWho(XmlSerializer serializer,
            EventEntry entry,
            Who who)
            throws IOException, ParseException {
        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "who");
        String email = who.getEmail();
        if (!StringUtils.isEmpty(email)) {
            serializer.attribute(null /* ns */, "email", email);
        }

        String value = who.getValue();
        if (!StringUtils.isEmpty(value)) {
            serializer.attribute(null /* ns */, "valueString", value);
        }

        String rel = null;
        switch (who.getRelationship()) {
            case Who.RELATIONSHIP_NONE:
                break;
            case Who.RELATIONSHIP_ATTENDEE:
                rel = "http://schemas.google.com/g/2005#event.attendee";
                break;
            case Who.RELATIONSHIP_ORGANIZER:
                rel = "http://schemas.google.com/g/2005#event.organizer";
                break;
            case Who.RELATIONSHIP_PERFORMER:
                rel = "http://schemas.google.com/g/2005#event.performer";
                break;
            case Who.RELATIONSHIP_SPEAKER:
                rel = "http://schemas.google.com/g/2005#event.speaker";
                break;
            default:
                throw new ParseException("Unexpected rel: " + who.getRelationship());
        }
        if (!StringUtils.isEmpty(rel)) {
            serializer.attribute(null /* ns */, "rel", rel);
        }

        String status = null;
        switch (who.getStatus()) {
            case Who.STATUS_NONE:
                break;
            case Who.STATUS_ACCEPTED:
                status = "http://schemas.google.com/g/2005#event.accepted";
                break;
            case Who.STATUS_DECLINED:
                status = "http://schemas.google.com/g/2005#event.declined";
                break;
            case Who.STATUS_INVITED:
                status = "http://schemas.google.com/g/2005#event.invited";
                break;
            case Who.STATUS_TENTATIVE:
                status = "http://schemas.google.com/g/2005#event.tentative";
                break;
            default:
                throw new ParseException("Unexpected status: " + who.getStatus());
        }
        if (!StringUtils.isEmpty(status)) {
            serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI,
                    "attendeeStatus");
            serializer.attribute(null /* ns */, "value", status);
            serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI,
                    "attendeeStatus");
        }

        String type = null;
        switch (who.getType()) {
            case Who.TYPE_NONE:
                break;
            case Who.TYPE_REQUIRED:
                type = "http://schemas.google.com/g/2005#event.required";
                break;
            case Who.TYPE_OPTIONAL:
                type = "http://schemas.google.com/g/2005#event.optional";
                break;
            default:
                throw new ParseException("Unexpected type: " + who.getType());
        }
        if (!StringUtils.isEmpty(type)) {
            serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI,
                    "attendeeType");
            serializer.attribute(null /* ns */, "value", type);
            serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "attendeeType");
        }
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "who");
    }

    private static void serializeWhen(XmlSerializer serializer,
            EventEntry entry,
            When when)
            throws IOException {
        // TODO: throw exn if startTime is empty but endTime is not?
        String startTime = when.getStartTime();
        String endTime = when.getEndTime();
        if (StringUtils.isEmpty(when.getStartTime())) {
            return;
        }

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "when");
        serializer.attribute(null /* ns */, "startTime", startTime);
        if (!StringUtils.isEmpty(endTime)) {
            serializer.attribute(null /* ns */, "endTime", endTime);
        }
        if (entry.getReminders() != null) {
            Enumeration reminders = entry.getReminders().elements();
            while (reminders.hasMoreElements()) {
                Reminder reminder = (Reminder) reminders.nextElement();
                serializeReminder(serializer, reminder);
            }
        }
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "when");
    }

    private static void serializeReminder(XmlSerializer serializer,
            Reminder reminder)
            throws IOException {

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "reminder");
        byte method = reminder.getMethod();
        String methodStr = null;
        switch (method) {
            case Reminder.METHOD_ALERT:
                methodStr = "alert";
                break;
            case Reminder.METHOD_EMAIL:
                methodStr = "email";
                break;
            case Reminder.METHOD_SMS:
                methodStr = "sms";
                break;
        }
        if (methodStr != null) {
            serializer.attribute(null /* ns */, "method", methodStr);
        }

        int minutes = reminder.getMinutes();
        if (minutes != Reminder.MINUTES_DEFAULT) {
            serializer.attribute(null /* ns */, "minutes",
                    Integer.toString(minutes));
        }

        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "reminder");
    }

    private static void serializeOriginalEvent(XmlSerializer serializer,
            String originalEventId,
            String originalEventTime)
            throws IOException {
        if (StringUtils.isEmpty(originalEventId) ||
                StringUtils.isEmpty(originalEventTime)) {
            return;
        }


        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "originalEvent");
        int index = originalEventId.lastIndexOf("/");
        if (index != -1) {
            String id = originalEventId.substring(index + 1);
            if (!StringUtils.isEmpty(id)) {
                serializer.attribute(null /* ns */, "id", id);
            }
        }
        serializer.attribute(null /* ns */, "href", originalEventId);
        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "when");
        serializer.attribute(null /* ns */, "startTime", originalEventTime);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "when");
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "originalEvent");
    }


    private static void serializeWhere(XmlSerializer serializer,
            String where)
            throws IOException {
        if (StringUtils.isEmpty(where)) {
            return;
        }

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "where");
        serializer.attribute(null /* ns */, "valueString", where);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "where");
    }

    private static void serializeCommentsUri(XmlSerializer serializer,
            String commentsUri)
            throws IOException {
        if (commentsUri == null) {
            return;
        }

        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "feedLink");
        serializer.attribute(null /* ns */, "href", commentsUri);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "feedLink");
    }

    private static void serializeExtendedProperty(XmlSerializer serializer,
            String name,
            String value)
            throws IOException {
        serializer.startTag(XmlGDataParser.NAMESPACE_GD_URI, "extendedProperty");
        serializer.attribute(null /* ns */, "name", name);
        serializer.attribute(null /* ns */, "value", value);
        serializer.endTag(XmlGDataParser.NAMESPACE_GD_URI, "extendedProperty");
    }
}