src/org/gege/caldavsyncadapter/caldav/xml/CalendarHomeHandler.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/CalendarHomeHandler.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,94 @@
     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.List;
    1.31 +
    1.32 +import org.gege.caldavsyncadapter.BuildConfig;
    1.33 +import org.xml.sax.Attributes;
    1.34 +import org.xml.sax.SAXException;
    1.35 +import org.xml.sax.helpers.DefaultHandler;
    1.36 +
    1.37 +import android.util.Log;
    1.38 +
    1.39 +public class CalendarHomeHandler extends DefaultHandler {
    1.40 +
    1.41 +	private static final String HREF = "href";
    1.42 +	private static final String CALENDAR_HOME_SET = "calendar-home-set";
    1.43 +	private boolean isInCalendarHomeSet = false;
    1.44 +	private StringBuilder stringBuilder = new StringBuilder();
    1.45 +	private String currentElement;
    1.46 +	private URI principalURI;
    1.47 +	
    1.48 +	public List<URI> calendarHomeSet = new ArrayList<URI>();
    1.49 +
    1.50 +	public CalendarHomeHandler(URI principalURI) {
    1.51 +		this.principalURI = principalURI;
    1.52 +	}
    1.53 +
    1.54 +	@Override
    1.55 +	public void startElement(String uri, String localName, String qName,
    1.56 +			Attributes attributes) throws SAXException {
    1.57 +		if (CALENDAR_HOME_SET.equals(localName)) {
    1.58 +			isInCalendarHomeSet = true;
    1.59 +		}
    1.60 +		currentElement = localName;
    1.61 +		stringBuilder.setLength(0);
    1.62 +	}
    1.63 +
    1.64 +	@Override
    1.65 +	public void characters(char[] ch, int start, int length)
    1.66 +			throws SAXException {
    1.67 +		if (HREF.equals(currentElement) && isInCalendarHomeSet) {
    1.68 +			stringBuilder.append(ch, start, length);
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	@Override
    1.73 +	public void endElement(String uri, String localName, String qName)
    1.74 +			throws SAXException {
    1.75 +		if (HREF.equals(localName) && isInCalendarHomeSet) {
    1.76 +			String calendarHomeSet = stringBuilder.toString();
    1.77 +			try {
    1.78 +				URI calendarHomeSetURI = new URI(calendarHomeSet);
    1.79 +				calendarHomeSetURI = principalURI.resolve(calendarHomeSetURI);
    1.80 +				this.calendarHomeSet.add(calendarHomeSetURI);
    1.81 +			} catch (URISyntaxException e) {
    1.82 +				if (BuildConfig.DEBUG) {
    1.83 +					Log.e(CalendarHomeHandler.class.getSimpleName(),
    1.84 +							"uri malformed: " + calendarHomeSet);
    1.85 +				} else {
    1.86 +					Log.e(CalendarHomeHandler.class.getSimpleName(),
    1.87 +							"uri malformed in calendar-home-set/href");
    1.88 +				}
    1.89 +			}
    1.90 +			//stringBuilder.setLength(0);
    1.91 +		}
    1.92 +		if (CALENDAR_HOME_SET.equals(localName)) {
    1.93 +			isInCalendarHomeSet = false;
    1.94 +		}
    1.95 +		currentElement=null;
    1.96 +	}
    1.97 +}

mercurial