|
1 /** |
|
2 * Copyright (c) 2012-2013, David Wiesner |
|
3 * |
|
4 * This file is part of Andoid Caldav Sync Adapter Free. |
|
5 * |
|
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute |
|
7 * it and/or modify it under the terms of the GNU General Public License |
|
8 * as published by the Free Software Foundation, either version 3 of the |
|
9 * License, or at your option any later version. |
|
10 * |
|
11 * Andoid Caldav Sync Adapter Free is distributed in the hope that |
|
12 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 * GNU General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License |
|
17 * along with Andoid Caldav Sync Adapter Free. |
|
18 * If not, see <http://www.gnu.org/licenses/>. |
|
19 * |
|
20 */ |
|
21 |
|
22 package org.gege.caldavsyncadapter.caldav.xml; |
|
23 |
|
24 import java.net.URI; |
|
25 import java.net.URISyntaxException; |
|
26 import java.util.ArrayList; |
|
27 import java.util.Arrays; |
|
28 import java.util.List; |
|
29 |
|
30 import org.gege.caldavsyncadapter.BuildConfig; |
|
31 import org.gege.caldavsyncadapter.caldav.entities.DavCalendar; |
|
32 import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource; |
|
33 import org.xml.sax.Attributes; |
|
34 import org.xml.sax.SAXException; |
|
35 import org.xml.sax.helpers.DefaultHandler; |
|
36 |
|
37 import android.util.Log; |
|
38 |
|
39 public class CalendarsHandler extends DefaultHandler { |
|
40 |
|
41 private static final String CALENDAR = "calendar"; |
|
42 private static final String RESOURCETYPE = "resourcetype"; |
|
43 private static final String CALENDAR_COLOR = "calendar-color"; |
|
44 private static final String GETCTAG = "getctag"; |
|
45 private static final String DISPLAYNAME = "displayname"; |
|
46 private URI homeURI; |
|
47 |
|
48 public CalendarsHandler(URI homeURI) { |
|
49 this.homeURI = homeURI; |
|
50 } |
|
51 |
|
52 private static final String RESPONSE = "response"; |
|
53 private static final String HREF = "href"; |
|
54 |
|
55 private StringBuilder stringBuilder = new StringBuilder(); |
|
56 private String currentElement; |
|
57 private DavCalendar calendar; |
|
58 public List<DavCalendar> calendars = new ArrayList<DavCalendar>(); |
|
59 private boolean isInResourceType = false; |
|
60 private boolean isCalendarResource; |
|
61 |
|
62 public final static List<String> TAGS = Arrays.asList(HREF, RESOURCETYPE, |
|
63 DISPLAYNAME, GETCTAG, CALENDAR_COLOR); |
|
64 |
|
65 @Override |
|
66 public void startElement(String uri, String localName, String qName, |
|
67 Attributes attributes) throws SAXException { |
|
68 if (RESPONSE.equals(localName)) { |
|
69 calendar = new DavCalendar(CalendarSource.CalDAV); |
|
70 isCalendarResource = false; |
|
71 } else if (RESOURCETYPE.equals(localName)) { |
|
72 isInResourceType = true; |
|
73 } else if (isInResourceType && CALENDAR.equals(localName)) { |
|
74 isCalendarResource = true; |
|
75 } |
|
76 currentElement = localName; |
|
77 stringBuilder.setLength(0); |
|
78 } |
|
79 |
|
80 @Override |
|
81 public void characters(char[] ch, int start, int length) |
|
82 throws SAXException { |
|
83 if (TAGS.contains(currentElement)) { |
|
84 stringBuilder.append(ch, start, length); |
|
85 } |
|
86 } |
|
87 |
|
88 @Override |
|
89 public void endElement(String uri, String localName, String qName) |
|
90 throws SAXException { |
|
91 if (TAGS.contains(localName)) { |
|
92 if (calendar != null) { |
|
93 if (HREF.equals(localName)) { |
|
94 String calendarUrl = stringBuilder.toString(); |
|
95 calendarUrl = calendarUrl.trim(); |
|
96 try { |
|
97 URI calendarURI = new URI(calendarUrl); |
|
98 calendar.setURI(homeURI.resolve(calendarURI)); |
|
99 } catch (URISyntaxException e) { |
|
100 if (BuildConfig.DEBUG) { |
|
101 Log.e(CalendarsHandler.class.getSimpleName(), |
|
102 "calendar-uri malformed: " + calendarUrl); |
|
103 } else { |
|
104 Log.e(CalendarsHandler.class.getSimpleName(), |
|
105 "uri malformed in href"); |
|
106 } |
|
107 } |
|
108 } else if (DISPLAYNAME.equals(localName)) { |
|
109 calendar.setCalendarDisplayName(stringBuilder.toString()); |
|
110 } else if (GETCTAG.equals(localName)) { |
|
111 calendar.setCTag(stringBuilder.toString(), false); |
|
112 } else if (CALENDAR_COLOR.equals(localName)) { |
|
113 calendar.setCalendarColorAsString(stringBuilder.toString()); |
|
114 } |
|
115 } |
|
116 //stringBuilder.setLength(0); |
|
117 } else if (RESPONSE.equals(localName)) { |
|
118 if (isCalendarResource && isValidCalendar(calendar)) { |
|
119 calendars.add(calendar); |
|
120 } |
|
121 } |
|
122 currentElement=null; |
|
123 } |
|
124 |
|
125 private boolean isValidCalendar(DavCalendar calendar) { |
|
126 return calendar != null && calendar.getURI() != null |
|
127 && calendar.getcTag() != null |
|
128 && calendar.getCalendarDisplayName() != null; |
|
129 } |
|
130 } |