diff --git a/r2-shared/src/main/java/org/readium/r2/shared/extensions/JSON.kt b/r2-shared/src/main/java/org/readium/r2/shared/extensions/JSON.kt index c5a8a222..d94e9299 100644 --- a/r2-shared/src/main/java/org/readium/r2/shared/extensions/JSON.kt +++ b/r2-shared/src/main/java/org/readium/r2/shared/extensions/JSON.kt @@ -178,6 +178,22 @@ fun JSONObject.optNullableInt(name: String, remove: Boolean = false): Int? { return value } +/** + * Returns the value mapped by [name] if it exists, coercing it if necessary, or [null] if no such + * mapping exists. + * If [remove] is true, then the mapping will be removed from the [JSONObject]. + */ +fun JSONObject.optNullableLong(name: String, remove: Boolean = false): Long? { + if (!has(name)) { + return null + } + val value = optLong(name) + if (remove) { + this.remove(name) + } + return value +} + /** * Returns the value mapped by [name] if it exists, coercing it if necessary, or [null] if no such * mapping exists. diff --git a/r2-shared/src/main/java/org/readium/r2/shared/publication/LocalizedString.kt b/r2-shared/src/main/java/org/readium/r2/shared/publication/LocalizedString.kt index ad4a0670..a330ce24 100644 --- a/r2-shared/src/main/java/org/readium/r2/shared/publication/LocalizedString.kt +++ b/r2-shared/src/main/java/org/readium/r2/shared/publication/LocalizedString.kt @@ -72,7 +72,7 @@ data class LocalizedString(val translations: Map = emptyMa * Returns a new [LocalizedString] after adding (or replacing) the translation with the given * [language]. */ - fun withString(language: String?, string: String): LocalizedString = + fun copyWithString(language: String?, string: String): LocalizedString = copy(translations = translations + Pair(language, Translation(string = string))) /** diff --git a/r2-shared/src/main/java/org/readium/r2/shared/publication/encryption/Encryption.kt b/r2-shared/src/main/java/org/readium/r2/shared/publication/encryption/Encryption.kt index 17e4ea74..71347560 100644 --- a/r2-shared/src/main/java/org/readium/r2/shared/publication/encryption/Encryption.kt +++ b/r2-shared/src/main/java/org/readium/r2/shared/publication/encryption/Encryption.kt @@ -15,6 +15,7 @@ import org.json.JSONObject import org.readium.r2.shared.JSONable import org.readium.r2.shared.util.logging.WarningLogger import org.readium.r2.shared.extensions.optNullableInt +import org.readium.r2.shared.extensions.optNullableLong import org.readium.r2.shared.extensions.optNullableString import org.readium.r2.shared.util.logging.JsonWarning import org.readium.r2.shared.util.logging.log @@ -34,7 +35,7 @@ import org.readium.r2.shared.util.logging.log data class Encryption( val algorithm: String, val compression: String? = null, - val originalLength: Int? = null, + val originalLength: Long? = null, val profile: String? = null, val scheme: String? = null ) : JSONable, Parcelable { @@ -68,8 +69,8 @@ data class Encryption( compression = json.optNullableString("compression"), // Fallback on [original-length] for legacy reasons // See https://github.com/readium/webpub-manifest/pull/43 - originalLength = json.optNullableInt("originalLength") - ?: json.optNullableInt("original-length"), + originalLength = json.optNullableLong("originalLength") + ?: json.optNullableLong("original-length"), profile = json.optNullableString("profile"), scheme = json.optNullableString("scheme") ) diff --git a/r2-shared/src/main/java/org/readium/r2/shared/publication/epub/Metadata.kt b/r2-shared/src/main/java/org/readium/r2/shared/publication/epub/Presentation.kt similarity index 51% rename from r2-shared/src/main/java/org/readium/r2/shared/publication/epub/Metadata.kt rename to r2-shared/src/main/java/org/readium/r2/shared/publication/epub/Presentation.kt index ee894b53..98bf50a7 100644 --- a/r2-shared/src/main/java/org/readium/r2/shared/publication/epub/Metadata.kt +++ b/r2-shared/src/main/java/org/readium/r2/shared/publication/epub/Presentation.kt @@ -9,13 +9,15 @@ package org.readium.r2.shared.publication.epub -import org.readium.r2.shared.publication.Metadata - -// EPUB extensions for [Metadata]. -// https://readium.org/webpub-manifest/schema/extensions/epub/metadata.schema.json +import org.readium.r2.shared.publication.Link +import org.readium.r2.shared.publication.presentation.Presentation /** - * Hints how the layout of the resource should be presented. + * Get the layout of the given resource in this publication. + * Falls back on REFLOWABLE. */ -val Metadata.layout: EpubLayout? - get() = EpubLayout(this["layout"] as? String) +fun Presentation.layoutOf(link: Link): EpubLayout { + return link.properties.layout + ?: layout + ?: EpubLayout.REFLOWABLE +} diff --git a/r2-shared/src/test/java/org/readium/r2/shared/publication/LocalizedStringTest.kt b/r2-shared/src/test/java/org/readium/r2/shared/publication/LocalizedStringTest.kt index 7ce2c53c..38a650df 100644 --- a/r2-shared/src/test/java/org/readium/r2/shared/publication/LocalizedStringTest.kt +++ b/r2-shared/src/test/java/org/readium/r2/shared/publication/LocalizedStringTest.kt @@ -196,7 +196,7 @@ class LocalizedStringTest { )), LocalizedString.fromStrings(mapOf( "en" to "a string" - )).withString("fr", "une chaîne") + )).copyWithString("fr", "une chaîne") ) } diff --git a/r2-shared/src/test/java/org/readium/r2/shared/publication/epub/MetadataTest.kt b/r2-shared/src/test/java/org/readium/r2/shared/publication/epub/MetadataTest.kt deleted file mode 100644 index e793e8f7..00000000 --- a/r2-shared/src/test/java/org/readium/r2/shared/publication/epub/MetadataTest.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Module: r2-shared-kotlin - * Developers: Mickaël Menu - * - * Copyright (c) 2020. Readium Foundation. All rights reserved. - * Use of this source code is governed by a BSD-style license which is detailed in the - * LICENSE file present in the project repository where this source code is maintained. - */ - -package org.readium.r2.shared.publication.epub - -import org.junit.Assert.* -import org.junit.Test -import org.readium.r2.shared.publication.LocalizedString -import org.readium.r2.shared.publication.Metadata - -class MetadataTest { - - @Test fun `get Metadata {layout} when available`() { - assertEquals( - EpubLayout.FIXED, - Metadata( - localizedTitle = LocalizedString("Title"), - otherMetadata = mapOf("layout" to "fixed") - ).layout - ) - } - - @Test fun `get Metadata {layout} when missing`() { - assertNull(Metadata( - localizedTitle = LocalizedString("Title") - ).layout) - } - -} \ No newline at end of file diff --git a/r2-shared/src/test/java/org/readium/r2/shared/publication/epub/PresentationTest.kt b/r2-shared/src/test/java/org/readium/r2/shared/publication/epub/PresentationTest.kt new file mode 100644 index 00000000..08747cb9 --- /dev/null +++ b/r2-shared/src/test/java/org/readium/r2/shared/publication/epub/PresentationTest.kt @@ -0,0 +1,68 @@ +/* + * Module: r2-shared-kotlin + * Developers: Mickaël Menu + * + * Copyright (c) 2020. Readium Foundation. All rights reserved. + * Use of this source code is governed by a BSD-style license which is detailed in the + * LICENSE file present in the project repository where this source code is maintained. + */ + +package org.readium.r2.shared.publication.epub + +import org.junit.Assert.* +import org.junit.Test +import org.readium.r2.shared.publication.Link +import org.readium.r2.shared.publication.Properties +import org.readium.r2.shared.publication.presentation.Presentation + +class PresentationTest { + + @Test + fun `Get the layout of a reflowable resource`() { + assertEquals( + EpubLayout.REFLOWABLE, + Presentation(layout = null).layoutOf(createLink(EpubLayout.REFLOWABLE)) + ) + } + + @Test + fun `Get the layout of a fixed resource`() { + assertEquals( + EpubLayout.FIXED, + Presentation(layout = null).layoutOf(createLink(EpubLayout.FIXED)) + ) + } + + @Test + fun `The layout of a resource takes precedence over the document layout`() { + assertEquals( + EpubLayout.FIXED, + Presentation(layout = EpubLayout.REFLOWABLE).layoutOf(createLink(EpubLayout.FIXED)) + ) + } + + @Test + fun `Get the layout falls back on the document layout`() { + assertEquals( + EpubLayout.FIXED, + Presentation(layout = EpubLayout.FIXED).layoutOf(createLink(null)) + ) + } + + @Test + fun `Get the layout falls back on REFLOWABLE`() { + assertEquals( + EpubLayout.REFLOWABLE, + Presentation(layout = null).layoutOf(createLink(null)) + ) + } + + private fun createLink(layout: EpubLayout?) = Link( + href = "res", + properties = Properties( + otherProperties = layout?.let { mapOf("layout" to layout.value) } + ?: emptyMap() + ) + ) + +} \ No newline at end of file