summaryrefslogtreecommitdiff
path: root/src/com/google/wireless/gdata/calendar/parser/xml/XmlCalendarsGDataParser.java
blob: 2879780cee002e26784d42afdeb818e5ca9b28b1 (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
// Copyright 2007 The Android Open Source Project

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

import com.google.wireless.gdata.calendar.data.CalendarEntry;
import com.google.wireless.gdata.calendar.data.CalendarsFeed;
import com.google.wireless.gdata.data.Entry;
import com.google.wireless.gdata.data.Feed;
import com.google.wireless.gdata.parser.ParseException;
import com.google.wireless.gdata.parser.xml.XmlGDataParser;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;

/**
 * GDataParser for the meta feed listing a user's calendars.
 */
public class XmlCalendarsGDataParser extends XmlGDataParser {

    /**
     * Creates a new XmlCalendarsGDataParser.
     * @param is The InputStream containing the calendars feed.
     * @throws ParseException Thrown if an XmlPullParser could not be created.
     */
    public XmlCalendarsGDataParser(InputStream is, XmlPullParser parser)
            throws ParseException {
        super(is, parser);
    }

    /*
     * (non-Javadoc)
     * @see com.google.wireless.gdata.parser.xml.XmlGDataParser#createFeed()
     */
    protected Feed createFeed() {
        return new CalendarsFeed();
    }

    /*
     * (non-Javadoc)
     * @see com.google.wireless.gdata.parser.xml.XmlGDataParser#createEntry()
     */
    protected Entry createEntry() {
        return new CalendarEntry();
    }

    /*
     * (non-Javadoc)
     * @see XmlGDataParser#handleExtraElementInEntry
     */
    protected void handleExtraElementInEntry(Entry entry)
        throws XmlPullParserException, IOException {

        XmlPullParser parser = getParser();

        if (!(entry instanceof CalendarEntry)) {
            throw new IllegalArgumentException("Expected CalendarEntry!");
        }
        CalendarEntry calendarEntry = (CalendarEntry) entry;

        // NOTE: all of these names are assumed to be in the "gcal" namespace.
        // we do not bother checking that here.
        String name = parser.getName();
        if ("accesslevel".equals(name)) {
            String accesslevelStr = parser.getAttributeValue(null /* ns */,
                    "value");
            byte accesslevel = CalendarEntry.ACCESS_READ;
            if ("none".equals(accesslevelStr)) {
                accesslevel = CalendarEntry.ACCESS_NONE;
            } else if ("read".equals(accesslevelStr)) {
                accesslevel = CalendarEntry.ACCESS_READ;
            } else if ("freebusy".equals(accesslevelStr)) {
                accesslevel = CalendarEntry.ACCESS_FREEBUSY;
            } else if ("contributor".equals(accesslevelStr)) {
                accesslevel = CalendarEntry.ACCESS_CONTRIBUTOR;
            } else if ("owner".equals(accesslevelStr)) {
                accesslevel = CalendarEntry.ACCESS_OWNER;
            }
            calendarEntry.setAccessLevel(accesslevel);
        } else if ("color".equals(name)) {
            String color =
                parser.getAttributeValue(null /* ns */, "value");
            calendarEntry.setColor(color);
        } else if ("hidden".equals(name)) {
            String hiddenStr =
                parser.getAttributeValue(null /* ns */, "value");
            boolean hidden = false;
            if ("false".equals(hiddenStr)) {
                hidden = false;
            } else if ("true".equals(hiddenStr)) {
                hidden = true;
            }
            calendarEntry.setHidden(hidden);
            // if the calendar is hidden, it cannot be selected.
            if (hidden) {
                calendarEntry.setSelected(false);
            }
        } else if ("selected".equals(name)) {
            String selectedStr =
                parser.getAttributeValue(null /* ns */, "value");
            boolean selected = false;
            if ("false".equals(selectedStr)) {
                selected = false;
            } else if ("true".equals(selectedStr)) {
                selected = true;
            }
            calendarEntry.setSelected(selected);
        } else if ("timezone".equals(name)) {
            String timezone =
                parser.getAttributeValue(null /* ns */, "value");
            calendarEntry.setTimezone(timezone);
        }
    }

    /*
     * (non-Javadoc)
     * @see XmlGDataParser#handleExtraLinkInEntry
     */
    protected void handleExtraLinkInEntry(String rel,
                                          String type,
                                          String href,
                                          Entry entry)
        throws XmlPullParserException, IOException {
        if (("alternate".equals(rel)) &&
            ("application/atom+xml".equals(type))) {
            CalendarEntry calendarEntry = (CalendarEntry) entry;
            calendarEntry.setAlternateLink(href);
        }
    }
}