src/org/gege/caldavsyncadapter/caldav/xml/CalendarsHandler.java

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

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

mercurial