mirror of https://github.com/tasks/tasks
Add ShareAccess property
parent
364d9bc3ca
commit
c42b22ebe9
@ -0,0 +1,47 @@
|
||||
package org.tasks.caldav.property
|
||||
|
||||
import at.bitfire.dav4jvm.Property
|
||||
import at.bitfire.dav4jvm.PropertyFactory
|
||||
import at.bitfire.dav4jvm.XmlUtils
|
||||
import at.bitfire.dav4jvm.XmlUtils.propertyName
|
||||
import org.tasks.BuildConfig
|
||||
import org.xmlpull.v1.XmlPullParser
|
||||
|
||||
data class ShareAccess(val access: Property.Name): Property {
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val NAME = Property.Name(XmlUtils.NS_WEBDAV, "share-access")
|
||||
|
||||
val SHARED_OWNER = Property.Name(XmlUtils.NS_WEBDAV, "shared-owner")
|
||||
val READ_WRITE = Property.Name(XmlUtils.NS_WEBDAV, "read-write")
|
||||
val NOT_SHARED = Property.Name(XmlUtils.NS_WEBDAV, "not-shared")
|
||||
val READ = Property.Name(XmlUtils.NS_WEBDAV, "read")
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "ShareAccess(access=$access)"
|
||||
}
|
||||
|
||||
class Factory : PropertyFactory {
|
||||
|
||||
override fun getName() = NAME
|
||||
|
||||
override fun create(parser: XmlPullParser): ShareAccess? {
|
||||
// <!ELEMENT share-access #PCDATA>
|
||||
var result: Property.Name? = null
|
||||
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) {
|
||||
when (val name = parser.propertyName()) {
|
||||
SHARED_OWNER, READ_WRITE, NOT_SHARED, READ -> result = name
|
||||
}
|
||||
}
|
||||
eventType = parser.next()
|
||||
}
|
||||
if (BuildConfig.DEBUG && parser.depth != depth) { error("Assertion failed") }
|
||||
return result?.let { ShareAccess(it) }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package org.tasks.caldav.property
|
||||
|
||||
import at.bitfire.dav4jvm.Property
|
||||
import at.bitfire.dav4jvm.XmlUtils
|
||||
import java.io.StringReader
|
||||
|
||||
object PropertyUtils {
|
||||
fun <T: Property> String.toProperty(): T =
|
||||
toProperties()
|
||||
.apply { if (this.size != 1) throw IllegalStateException("${this.size} items") }
|
||||
.first()
|
||||
.let {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
it as T
|
||||
}
|
||||
|
||||
fun String.toProperties(): List<Property> =
|
||||
XmlUtils.newPullParser()
|
||||
.apply {
|
||||
setInput(
|
||||
StringReader("""
|
||||
<test xmlns:d="DAV:">
|
||||
${this@toProperties}
|
||||
</test>
|
||||
""".trimIndent()
|
||||
)
|
||||
)
|
||||
nextTag()
|
||||
}
|
||||
.let { Property.parse(it) }
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.tasks.caldav.property
|
||||
|
||||
import at.bitfire.dav4jvm.PropertyRegistry
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.tasks.caldav.property.PropertyUtils.toProperty
|
||||
import org.tasks.caldav.property.ShareAccess.Companion.SHARED_OWNER
|
||||
|
||||
class ShareAccessTest {
|
||||
@Before
|
||||
fun setUp() {
|
||||
PropertyRegistry.register(ShareAccess.Factory())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseShareAccess() {
|
||||
val access: ShareAccess = """
|
||||
<d:share-access>
|
||||
<d:shared-owner />
|
||||
</d:share-access>
|
||||
""".toProperty()
|
||||
|
||||
assertEquals(ShareAccess(SHARED_OWNER), access)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue