src/org/gege/caldavsyncadapter/caldav/xml/CalendarHomeHandler.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.

     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  */
    22 package org.gege.caldavsyncadapter.caldav.xml;
    24 import java.net.URI;
    25 import java.net.URISyntaxException;
    26 import java.util.ArrayList;
    27 import java.util.List;
    29 import org.gege.caldavsyncadapter.BuildConfig;
    30 import org.xml.sax.Attributes;
    31 import org.xml.sax.SAXException;
    32 import org.xml.sax.helpers.DefaultHandler;
    34 import android.util.Log;
    36 public class CalendarHomeHandler extends DefaultHandler {
    38 	private static final String HREF = "href";
    39 	private static final String CALENDAR_HOME_SET = "calendar-home-set";
    40 	private boolean isInCalendarHomeSet = false;
    41 	private StringBuilder stringBuilder = new StringBuilder();
    42 	private String currentElement;
    43 	private URI principalURI;
    45 	public List<URI> calendarHomeSet = new ArrayList<URI>();
    47 	public CalendarHomeHandler(URI principalURI) {
    48 		this.principalURI = principalURI;
    49 	}
    51 	@Override
    52 	public void startElement(String uri, String localName, String qName,
    53 			Attributes attributes) throws SAXException {
    54 		if (CALENDAR_HOME_SET.equals(localName)) {
    55 			isInCalendarHomeSet = true;
    56 		}
    57 		currentElement = localName;
    58 		stringBuilder.setLength(0);
    59 	}
    61 	@Override
    62 	public void characters(char[] ch, int start, int length)
    63 			throws SAXException {
    64 		if (HREF.equals(currentElement) && isInCalendarHomeSet) {
    65 			stringBuilder.append(ch, start, length);
    66 		}
    67 	}
    69 	@Override
    70 	public void endElement(String uri, String localName, String qName)
    71 			throws SAXException {
    72 		if (HREF.equals(localName) && isInCalendarHomeSet) {
    73 			String calendarHomeSet = stringBuilder.toString();
    74 			try {
    75 				URI calendarHomeSetURI = new URI(calendarHomeSet);
    76 				calendarHomeSetURI = principalURI.resolve(calendarHomeSetURI);
    77 				this.calendarHomeSet.add(calendarHomeSetURI);
    78 			} catch (URISyntaxException e) {
    79 				if (BuildConfig.DEBUG) {
    80 					Log.e(CalendarHomeHandler.class.getSimpleName(),
    81 							"uri malformed: " + calendarHomeSet);
    82 				} else {
    83 					Log.e(CalendarHomeHandler.class.getSimpleName(),
    84 							"uri malformed in calendar-home-set/href");
    85 				}
    86 			}
    87 			//stringBuilder.setLength(0);
    88 		}
    89 		if (CALENDAR_HOME_SET.equals(localName)) {
    90 			isInCalendarHomeSet = false;
    91 		}
    92 		currentElement=null;
    93 	}
    94 }

mercurial