michael@0: /**
michael@0: * Copyright (c) 2012-2013, David Wiesner
michael@0: *
michael@0: * This file is part of Andoid Caldav Sync Adapter Free.
michael@0: *
michael@0: * Andoid Caldav Sync Adapter Free is free software: you can redistribute
michael@0: * it and/or modify it under the terms of the GNU General Public License
michael@0: * as published by the Free Software Foundation, either version 3 of the
michael@0: * License, or at your option any later version.
michael@0: *
michael@0: * Andoid Caldav Sync Adapter Free is distributed in the hope that
michael@0: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
michael@0: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
michael@0: * GNU General Public License for more details.
michael@0: *
michael@0: * You should have received a copy of the GNU General Public License
michael@0: * along with Andoid Caldav Sync Adapter Free.
michael@0: * If not, see .
michael@0: *
michael@0: */
michael@0:
michael@0: package org.gege.caldavsyncadapter.caldav.xml;
michael@0:
michael@0: import java.net.URI;
michael@0: import java.net.URISyntaxException;
michael@0: import java.util.ArrayList;
michael@0: import java.util.Arrays;
michael@0: import java.util.List;
michael@0:
michael@0: import org.gege.caldavsyncadapter.BuildConfig;
michael@0: import org.gege.caldavsyncadapter.caldav.entities.DavCalendar;
michael@0: import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource;
michael@0: import org.xml.sax.Attributes;
michael@0: import org.xml.sax.SAXException;
michael@0: import org.xml.sax.helpers.DefaultHandler;
michael@0:
michael@0: import android.util.Log;
michael@0:
michael@0: public class CalendarsHandler extends DefaultHandler {
michael@0:
michael@0: private static final String CALENDAR = "calendar";
michael@0: private static final String RESOURCETYPE = "resourcetype";
michael@0: private static final String CALENDAR_COLOR = "calendar-color";
michael@0: private static final String GETCTAG = "getctag";
michael@0: private static final String DISPLAYNAME = "displayname";
michael@0: private URI homeURI;
michael@0:
michael@0: public CalendarsHandler(URI homeURI) {
michael@0: this.homeURI = homeURI;
michael@0: }
michael@0:
michael@0: private static final String RESPONSE = "response";
michael@0: private static final String HREF = "href";
michael@0:
michael@0: private StringBuilder stringBuilder = new StringBuilder();
michael@0: private String currentElement;
michael@0: private DavCalendar calendar;
michael@0: public List calendars = new ArrayList();
michael@0: private boolean isInResourceType = false;
michael@0: private boolean isCalendarResource;
michael@0:
michael@0: public final static List TAGS = Arrays.asList(HREF, RESOURCETYPE,
michael@0: DISPLAYNAME, GETCTAG, CALENDAR_COLOR);
michael@0:
michael@0: @Override
michael@0: public void startElement(String uri, String localName, String qName,
michael@0: Attributes attributes) throws SAXException {
michael@0: if (RESPONSE.equals(localName)) {
michael@0: calendar = new DavCalendar(CalendarSource.CalDAV);
michael@0: isCalendarResource = false;
michael@0: } else if (RESOURCETYPE.equals(localName)) {
michael@0: isInResourceType = true;
michael@0: } else if (isInResourceType && CALENDAR.equals(localName)) {
michael@0: isCalendarResource = true;
michael@0: }
michael@0: currentElement = localName;
michael@0: stringBuilder.setLength(0);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void characters(char[] ch, int start, int length)
michael@0: throws SAXException {
michael@0: if (TAGS.contains(currentElement)) {
michael@0: stringBuilder.append(ch, start, length);
michael@0: }
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void endElement(String uri, String localName, String qName)
michael@0: throws SAXException {
michael@0: if (TAGS.contains(localName)) {
michael@0: if (calendar != null) {
michael@0: if (HREF.equals(localName)) {
michael@0: String calendarUrl = stringBuilder.toString();
michael@0: calendarUrl = calendarUrl.trim();
michael@0: try {
michael@0: URI calendarURI = new URI(calendarUrl);
michael@0: calendar.setURI(homeURI.resolve(calendarURI));
michael@0: } catch (URISyntaxException e) {
michael@0: if (BuildConfig.DEBUG) {
michael@0: Log.e(CalendarsHandler.class.getSimpleName(),
michael@0: "calendar-uri malformed: " + calendarUrl);
michael@0: } else {
michael@0: Log.e(CalendarsHandler.class.getSimpleName(),
michael@0: "uri malformed in href");
michael@0: }
michael@0: }
michael@0: } else if (DISPLAYNAME.equals(localName)) {
michael@0: calendar.setCalendarDisplayName(stringBuilder.toString());
michael@0: } else if (GETCTAG.equals(localName)) {
michael@0: calendar.setCTag(stringBuilder.toString(), false);
michael@0: } else if (CALENDAR_COLOR.equals(localName)) {
michael@0: calendar.setCalendarColorAsString(stringBuilder.toString());
michael@0: }
michael@0: }
michael@0: //stringBuilder.setLength(0);
michael@0: } else if (RESPONSE.equals(localName)) {
michael@0: if (isCalendarResource && isValidCalendar(calendar)) {
michael@0: calendars.add(calendar);
michael@0: }
michael@0: }
michael@0: currentElement=null;
michael@0: }
michael@0:
michael@0: private boolean isValidCalendar(DavCalendar calendar) {
michael@0: return calendar != null && calendar.getURI() != null
michael@0: && calendar.getcTag() != null
michael@0: && calendar.getCalendarDisplayName() != null;
michael@0: }
michael@0: }