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

changeset 0
fb9019fb1bf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/xml/CalendarsHandler.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,130 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012-2013, David Wiesner
     1.6 + * 
     1.7 + * This file is part of Andoid Caldav Sync Adapter Free.
     1.8 + *
     1.9 + * Andoid Caldav Sync Adapter Free is free software: you can redistribute 
    1.10 + * it and/or modify it under the terms of the GNU General Public License 
    1.11 + * as published by the Free Software Foundation, either version 3 of the 
    1.12 + * License, or at your option any later version.
    1.13 + *
    1.14 + * Andoid Caldav Sync Adapter Free is distributed in the hope that 
    1.15 + * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
    1.16 + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with Andoid Caldav Sync Adapter Free.  
    1.21 + * If not, see <http://www.gnu.org/licenses/>.
    1.22 + * 
    1.23 + */
    1.24 +
    1.25 +package org.gege.caldavsyncadapter.caldav.xml;
    1.26 +
    1.27 +import java.net.URI;
    1.28 +import java.net.URISyntaxException;
    1.29 +import java.util.ArrayList;
    1.30 +import java.util.Arrays;
    1.31 +import java.util.List;
    1.32 +
    1.33 +import org.gege.caldavsyncadapter.BuildConfig;
    1.34 +import org.gege.caldavsyncadapter.caldav.entities.DavCalendar;
    1.35 +import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource;
    1.36 +import org.xml.sax.Attributes;
    1.37 +import org.xml.sax.SAXException;
    1.38 +import org.xml.sax.helpers.DefaultHandler;
    1.39 +
    1.40 +import android.util.Log;
    1.41 +
    1.42 +public class CalendarsHandler extends DefaultHandler {
    1.43 +
    1.44 +	private static final String CALENDAR = "calendar";
    1.45 +	private static final String RESOURCETYPE = "resourcetype";
    1.46 +	private static final String CALENDAR_COLOR = "calendar-color";
    1.47 +	private static final String GETCTAG = "getctag";
    1.48 +	private static final String DISPLAYNAME = "displayname";
    1.49 +	private URI homeURI;
    1.50 +
    1.51 +	public CalendarsHandler(URI homeURI) {
    1.52 +		this.homeURI = homeURI;
    1.53 +	}
    1.54 +
    1.55 +	private static final String RESPONSE = "response";
    1.56 +	private static final String HREF = "href";
    1.57 +
    1.58 +	private StringBuilder stringBuilder = new StringBuilder();
    1.59 +	private String currentElement;
    1.60 +	private DavCalendar calendar;
    1.61 +	public List<DavCalendar> calendars = new ArrayList<DavCalendar>();
    1.62 +	private boolean isInResourceType = false;
    1.63 +	private boolean isCalendarResource;
    1.64 +
    1.65 +	public final static List<String> TAGS = Arrays.asList(HREF, RESOURCETYPE,
    1.66 +			DISPLAYNAME, GETCTAG, CALENDAR_COLOR);
    1.67 +
    1.68 +	@Override
    1.69 +	public void startElement(String uri, String localName, String qName,
    1.70 +			Attributes attributes) throws SAXException {
    1.71 +		if (RESPONSE.equals(localName)) {
    1.72 +			calendar = new DavCalendar(CalendarSource.CalDAV);
    1.73 +			isCalendarResource = false;
    1.74 +		} else if (RESOURCETYPE.equals(localName)) {
    1.75 +			isInResourceType = true;
    1.76 +		} else if (isInResourceType && CALENDAR.equals(localName)) {
    1.77 +			isCalendarResource = true;
    1.78 +		}
    1.79 +		currentElement = localName;
    1.80 +		stringBuilder.setLength(0);
    1.81 +	}
    1.82 +
    1.83 +	@Override
    1.84 +	public void characters(char[] ch, int start, int length)
    1.85 +			throws SAXException {
    1.86 +		if (TAGS.contains(currentElement)) {
    1.87 +			stringBuilder.append(ch, start, length);
    1.88 +		}
    1.89 +	}
    1.90 +
    1.91 +	@Override
    1.92 +	public void endElement(String uri, String localName, String qName)
    1.93 +			throws SAXException {
    1.94 +		if (TAGS.contains(localName)) {
    1.95 +			if (calendar != null) {
    1.96 +				if (HREF.equals(localName)) {
    1.97 +					String calendarUrl = stringBuilder.toString();
    1.98 +					calendarUrl = calendarUrl.trim();
    1.99 +					try {
   1.100 +						URI calendarURI = new URI(calendarUrl);
   1.101 +						calendar.setURI(homeURI.resolve(calendarURI));
   1.102 +					} catch (URISyntaxException e) {
   1.103 +						if (BuildConfig.DEBUG) {
   1.104 +							Log.e(CalendarsHandler.class.getSimpleName(),
   1.105 +									"calendar-uri malformed: " + calendarUrl);
   1.106 +						} else {
   1.107 +							Log.e(CalendarsHandler.class.getSimpleName(),
   1.108 +									"uri malformed in href");
   1.109 +						}
   1.110 +					}
   1.111 +				} else if (DISPLAYNAME.equals(localName)) {
   1.112 +					calendar.setCalendarDisplayName(stringBuilder.toString());
   1.113 +				} else if (GETCTAG.equals(localName)) {
   1.114 +					calendar.setCTag(stringBuilder.toString(), false);
   1.115 +				} else if (CALENDAR_COLOR.equals(localName)) {
   1.116 +					calendar.setCalendarColorAsString(stringBuilder.toString());
   1.117 +				}
   1.118 +			}
   1.119 +			//stringBuilder.setLength(0);
   1.120 +		} else if (RESPONSE.equals(localName)) {
   1.121 +			if (isCalendarResource && isValidCalendar(calendar)) {
   1.122 +				calendars.add(calendar);
   1.123 +			}
   1.124 +		}
   1.125 +		currentElement=null;
   1.126 +	}
   1.127 +
   1.128 +	private boolean isValidCalendar(DavCalendar calendar) {
   1.129 +		return calendar != null && calendar.getURI() != null
   1.130 +				&& calendar.getcTag() != null
   1.131 +				&& calendar.getCalendarDisplayName() != null;
   1.132 +	}
   1.133 +}

mercurial