Skip to content

Commit

Permalink
Make all properties data classes (#62)
Browse files Browse the repository at this point in the history
* Added hint for dataclasses

Signed-off-by: Arnau Mora Gras <[email protected]>

* Migrated caldav properties

Signed-off-by: Arnau Mora Gras <[email protected]>

* Migrated carddav properties

Signed-off-by: Arnau Mora Gras <[email protected]>

* Migrated webdav properties

Signed-off-by: Arnau Mora Gras <[email protected]>

* Improved syntax

Signed-off-by: Arnau Mora Gras <[email protected]>

* Improve comment

Signed-off-by: Arnau Mora Gras <[email protected]>

* Made HrefListProperty static

Signed-off-by: Arnau Mora Gras <[email protected]>

* Fixed constructors

Signed-off-by: Arnau Mora Gras <[email protected]>

---------

Signed-off-by: Arnau Mora Gras <[email protected]>
  • Loading branch information
ArnyminerZ authored Dec 5, 2024
1 parent 8b59413 commit a4f4068
Show file tree
Hide file tree
Showing 32 changed files with 206 additions and 107 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/at/bitfire/dav4jvm/Property.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
package at.bitfire.dav4jvm

import at.bitfire.dav4jvm.exception.InvalidPropertyException
import org.xmlpull.v1.XmlPullParser
import java.io.Serializable
import java.util.LinkedList
import java.util.logging.Level
import java.util.logging.Logger
import org.xmlpull.v1.XmlPullParser

/**
* Represents a WebDAV property.
*
* Every [Property] must define a static field (use `@JvmStatic`) called `NAME` of type [Property.Name],
* which will be accessed by reflection.
*
* Every [Property] should be a data class in order to be able to compare it against others, and convert to a useful
* string for debugging.
*/
interface Property {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.property.webdav.HrefListProperty
import org.xmlpull.v1.XmlPullParser

class CalendarHomeSet: HrefListProperty() {
data class CalendarHomeSet(
override val hrefs: List<String> = emptyList()
): HrefListProperty(hrefs) {

companion object {

@JvmField
val NAME = Property.Name(NS_CALDAV, "calendar-home-set")

}


object Factory: HrefListProperty.Factory() {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = create(parser, CalendarHomeSet())
override fun create(parser: XmlPullParser) = create(parser, ::CalendarHomeSet)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.property.webdav.HrefListProperty
import org.xmlpull.v1.XmlPullParser

class CalendarProxyReadFor: HrefListProperty() {
data class CalendarProxyReadFor(
override val hrefs: List<String> = emptyList()
): HrefListProperty(hrefs) {

companion object {
@JvmField
Expand All @@ -22,7 +24,7 @@ class CalendarProxyReadFor: HrefListProperty() {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = create(parser, CalendarProxyReadFor())
override fun create(parser: XmlPullParser) = create(parser, ::CalendarProxyReadFor)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.property.webdav.HrefListProperty
import org.xmlpull.v1.XmlPullParser

class CalendarProxyWriteFor: HrefListProperty() {
data class CalendarProxyWriteFor(
override val hrefs: List<String> = emptyList()
): HrefListProperty(hrefs) {

companion object {
@JvmField
Expand All @@ -22,7 +24,7 @@ class CalendarProxyWriteFor: HrefListProperty() {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = create(parser, CalendarProxyWriteFor())
override fun create(parser: XmlPullParser) = create(parser, ::CalendarProxyWriteFor)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

class CalendarTimezoneId(
data class CalendarTimezoneId(
val identifier: String?
): Property {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.property.webdav.HrefListProperty
import org.xmlpull.v1.XmlPullParser

class CalendarUserAddressSet: HrefListProperty() {
data class CalendarUserAddressSet(
override val hrefs: List<String> = emptyList()
): HrefListProperty(hrefs) {

companion object {
@JvmField
Expand All @@ -22,7 +24,7 @@ class CalendarUserAddressSet: HrefListProperty() {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = create(parser, CalendarUserAddressSet())
override fun create(parser: XmlPullParser) = create(parser, ::CalendarUserAddressSet)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import at.bitfire.dav4jvm.XmlReader
import okhttp3.Response
import org.xmlpull.v1.XmlPullParser

class ScheduleTag(
rawScheduleTag: String?
data class ScheduleTag(
val rawScheduleTag: String?
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_CALDAV, "schedule-tag")

fun fromResponse(response: Response) =
response.header("Schedule-Tag")?.let { ScheduleTag(it) }

}

/* Value: opaque-tag
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/at/bitfire/dav4jvm/property/caldav/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.property.webdav.HrefListProperty
import org.xmlpull.v1.XmlPullParser

class Source: HrefListProperty() {
class Source(
override val hrefs: List<String> = emptyList()
): HrefListProperty(hrefs) {

companion object {

@JvmField
val NAME = Property.Name(NS_CALENDARSERVER, "source")

}


object Factory: HrefListProperty.Factory() {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = create(parser, Source())
override fun create(parser: XmlPullParser) = create(parser, ::Source)

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import at.bitfire.dav4jvm.XmlUtils.propertyName
import org.xmlpull.v1.XmlPullParser

data class SupportedCalendarComponentSet(
var supportsEvents: Boolean,
var supportsTasks: Boolean,
var supportsJournal: Boolean
val supportsEvents: Boolean,
val supportsTasks: Boolean,
val supportsJournal: Boolean
): Property {

companion object {
Expand All @@ -37,23 +37,29 @@ data class SupportedCalendarComponentSet(
<!ELEMENT comp ((allprop | prop*), (allcomp | comp*))>
<!ATTLIST comp name CDATA #REQUIRED>
*/
val components = SupportedCalendarComponentSet(false, false, false)
var components = SupportedCalendarComponentSet(
supportsEvents = false,
supportsTasks = false,
supportsJournal = false
)

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 (parser.propertyName()) {
ALLCOMP -> {
components.supportsEvents = true
components.supportsTasks = true
components.supportsJournal = true
components = SupportedCalendarComponentSet(
supportsEvents = true,
supportsTasks = true,
supportsJournal = true
)
}
COMP ->
when (parser.getAttributeValue(null, "name")?.uppercase()) {
"VEVENT" -> components.supportsEvents = true
"VTODO" -> components.supportsTasks = true
"VJOURNAL" -> components.supportsJournal = true
"VEVENT" -> components = components.copy(supportsEvents = true)
"VTODO" -> components = components.copy(supportsTasks = true)
"VJOURNAL" -> components = components.copy(supportsJournal = true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import at.bitfire.dav4jvm.XmlReader
import okhttp3.MediaType
import org.xmlpull.v1.XmlPullParser

class SupportedCalendarData: Property {
data class SupportedCalendarData(
val types: Set<MediaType> = emptySet()
): Property {

companion object {

Expand All @@ -25,23 +27,19 @@ class SupportedCalendarData: Property {

}

val types = mutableSetOf<MediaType>()

fun hasJCal() = types.any { "application".equals(it.type, true) && "calendar+json".equals(it.subtype, true) }

override fun toString() = "[${types.joinToString(", ")}]"


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser): SupportedCalendarData {
val supported = SupportedCalendarData()
val supportedTypes = mutableSetOf<MediaType>()

XmlReader(parser).readContentTypes(CALENDAR_DATA_TYPE, supported.types::add)
XmlReader(parser).readContentTypes(CALENDAR_DATA_TYPE, supportedTypes::add)

return supported
return SupportedCalendarData(supportedTypes)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

data class AddressData(
val card: String?
val card: String?
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_CARDDAV, "address-data")

// attributes
const val CONTENT_TYPE = "content-type"
const val VERSION = "version"

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

data class AddressbookDescription(
var description: String? = null
val description: String? = null
): Property {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.property.webdav.HrefListProperty
import org.xmlpull.v1.XmlPullParser

class AddressbookHomeSet: HrefListProperty() {
class AddressbookHomeSet(
override val hrefs: List<String> = emptyList()
): HrefListProperty(hrefs) {

companion object {

@JvmField
val NAME = Property.Name(NS_CARDDAV, "addressbook-home-set")

}


object Factory: HrefListProperty.Factory() {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = create(parser, AddressbookHomeSet())
override fun create(parser: XmlPullParser) = create(parser, ::AddressbookHomeSet)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ import org.xmlpull.v1.XmlPullParser
data class MaxResourceSize(
val maxSize: Long?
) : Property {

companion object {

@JvmField
val NAME = Property.Name(NS_CARDDAV, "max-resource-size")

}

object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser) =
MaxResourceSize(XmlReader(parser).readLong())

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import at.bitfire.dav4jvm.XmlReader
import okhttp3.MediaType
import org.xmlpull.v1.XmlPullParser

class SupportedAddressData: Property {
class SupportedAddressData(
val types: Set<MediaType> = emptySet()
): Property {

companion object {

Expand All @@ -25,8 +27,6 @@ class SupportedAddressData: Property {

}

val types = mutableSetOf<MediaType>()

fun hasVCard4() = types.any { "text/vcard; version=4.0".equals(it.toString(), true) }
fun hasJCard() = types.any { "application".equals(it.type, true) && "vcard+json".equals(it.subtype, true) }

Expand All @@ -38,11 +38,11 @@ class SupportedAddressData: Property {
override fun getName() = NAME

override fun create(parser: XmlPullParser): SupportedAddressData {
val supported = SupportedAddressData()
val supportedTypes = mutableSetOf<MediaType>()

XmlReader(parser).readContentTypes(ADDRESS_DATA_TYPE, supported.types::add)
XmlReader(parser).readContentTypes(ADDRESS_DATA_TYPE, supportedTypes::add)

return supported
return SupportedAddressData(supportedTypes)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ import org.xmlpull.v1.XmlPullParser
data class AddMember(
val href: String?
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_WEBDAV, "add-member")

}

object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser) = AddMember(XmlReader(parser).readTextProperty(DavResource.HREF))

}
}

}
Loading

0 comments on commit a4f4068

Please sign in to comment.