ResourceType.kt 2.25 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright © Ricki Hirner (bitfire web engineering).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

Ricki Hirner's avatar
Ricki Hirner committed
9 10 11 12 13 14 15 16 17 18 19 20 21
package at.bitfire.dav4android.property

import at.bitfire.dav4android.Property
import at.bitfire.dav4android.PropertyFactory
import at.bitfire.dav4android.XmlUtils
import org.xmlpull.v1.XmlPullParser

class ResourceType: Property {

    companion object {
        @JvmField
        val NAME = Property.Name(XmlUtils.NS_WEBDAV, "resourcetype")

22 23 24 25 26
        val COLLECTION = Property.Name(XmlUtils.NS_WEBDAV, "collection")    // WebDAV
        val PRINCIPAL = Property.Name(XmlUtils.NS_WEBDAV, "principal")      // WebDAV ACL
        val ADDRESSBOOK = Property.Name(XmlUtils.NS_CARDDAV, "addressbook") // CardDAV
        val CALENDAR = Property.Name(XmlUtils.NS_CALDAV, "calendar")        // CalDAV
        val SUBSCRIBED = Property.Name(XmlUtils.NS_CALENDARSERVER, "subscribed")
Ricki Hirner's avatar
Ricki Hirner committed
27 28 29 30
    }

    val types = mutableSetOf<Property.Name>()

Ricki Hirner's avatar
Ricki Hirner committed
31
    override fun toString() = "[${types.joinToString(", ")}]"
Ricki Hirner's avatar
Ricki Hirner committed
32 33 34 35 36 37 38 39 40


    class Factory: PropertyFactory {

        override fun getName() = NAME

        override fun create(parser: XmlPullParser): ResourceType? {
            val type = ResourceType()

41 42 43 44 45 46 47 48 49 50 51 52
            val depth = parser.depth
            var eventType = parser.eventType
            while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
                if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
                    // use static objects to allow types.contains()
                    var typeName = Property.Name(parser.namespace, parser.name)
                    when (typeName) {
                        COLLECTION -> typeName = COLLECTION
                        PRINCIPAL -> typeName = PRINCIPAL
                        ADDRESSBOOK -> typeName = ADDRESSBOOK
                        CALENDAR -> typeName = CALENDAR
                        SUBSCRIBED -> typeName = SUBSCRIBED
Ricki Hirner's avatar
Ricki Hirner committed
53
                    }
54
                    type.types.add(typeName)
Ricki Hirner's avatar
Ricki Hirner committed
55
                }
56
                eventType = parser.next()
Ricki Hirner's avatar
Ricki Hirner committed
57 58 59 60 61 62 63 64
            }

            return type
        }

    }

}