XmlUtils.kt 2.81 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
package at.bitfire.dav4android;

import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory
Ricki Hirner's avatar
Ricki Hirner committed
14
import java.io.IOException
Ricki Hirner's avatar
Ricki Hirner committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import java.util.logging.Level

class XmlUtils {
    companion object {

        @JvmField val NS_WEBDAV = "DAV:"
        @JvmField val NS_CALDAV = "urn:ietf:params:xml:ns:caldav"
        @JvmField val NS_CARDDAV = "urn:ietf:params:xml:ns:carddav"
        @JvmField val NS_APPLE_ICAL = "http://apple.com/ns/ical/"
        @JvmField val NS_CALENDARSERVER = "http://calendarserver.org/ns/"

        private val factory: XmlPullParserFactory
        init {
            try {
                factory = XmlPullParserFactory.newInstance()
                factory.isNamespaceAware = true
            } catch (e: XmlPullParserException) {
                throw RuntimeException("Couldn't create XmlPullParserFactory", e)
            }
        }

        @JvmStatic
        fun newPullParser() = factory.newPullParser()!!

        @JvmStatic
        fun newSerializer() = factory.newSerializer()!!


Ricki Hirner's avatar
Ricki Hirner committed
43
        @Throws(IOException::class)
Ricki Hirner's avatar
Ricki Hirner committed
44 45 46 47 48 49 50 51 52 53 54 55
        fun readText(parser: XmlPullParser): String? {
            var text: String? = null

            try {
                val depth = parser.depth

                var eventType = parser.eventType
                while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
                    if (eventType == XmlPullParser.TEXT && parser.depth == depth)
                        text = parser.text
                    eventType = parser.next()
                }
Ricki Hirner's avatar
Ricki Hirner committed
56
            } catch(e: XmlPullParserException) {
Ricki Hirner's avatar
Ricki Hirner committed
57 58 59 60 61 62
                Constants.log.log(Level.SEVERE, "Couldn't parse text property", e);
            }

            return text
        }

Ricki Hirner's avatar
Ricki Hirner committed
63
        @Throws(IOException::class)
Ricki Hirner's avatar
Ricki Hirner committed
64 65 66 67 68 69 70 71 72 73 74
        fun readTextPropertyList(parser: XmlPullParser, name: Property.Name, list: MutableList<String>) {
            try {
                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 &&
                            Property.Name(parser.namespace, parser.name) == name)
                        list.add(parser.nextText())
                    eventType = parser.next()
                }
Ricki Hirner's avatar
Ricki Hirner committed
75
            } catch(e: XmlPullParserException) {
Ricki Hirner's avatar
Ricki Hirner committed
76 77 78 79 80 81
                Constants.log.log(Level.SEVERE, "Couldn't parse text property list", e)
            }
        }

    }
}