Skip to content

Commit 4b31d95

Browse files
committed
Only normalize when comparing
1 parent 96963bd commit 4b31d95

File tree

13 files changed

+39
-18
lines changed

13 files changed

+39
-18
lines changed

readium/lcp/src/main/java/org/readium/r2/lcp/LcpDecryptor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.readium.r2.shared.util.Try
2222
import org.readium.r2.shared.util.Url
2323
import org.readium.r2.shared.util.data.ReadError
2424
import org.readium.r2.shared.util.flatMap
25+
import org.readium.r2.shared.util.getEquivalent
2526
import org.readium.r2.shared.util.getOrElse
2627
import org.readium.r2.shared.util.resource.FailureResource
2728
import org.readium.r2.shared.util.resource.Resource
@@ -38,7 +39,7 @@ internal class LcpDecryptor(
3839

3940
fun transform(url: Url, resource: Resource): Resource {
4041
return resource.flatMap {
41-
val encryption = encryptionData[url]
42+
val encryption = encryptionData.getEquivalent(url)
4243

4344
// Checks if the resource is encrypted and whether the encryption schemes of the resource
4445
// and the DRM license are the same.

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,13 @@ public class EpubNavigatorFragment internal constructor(
615615

616616
listener?.onJumpToLocator(locator)
617617

618-
val href = locator.href.removeFragment().normalize()
618+
val href = locator.href.removeFragment()
619619

620620
fun setCurrent(resources: List<PageResource>) {
621621
val page = resources.withIndex().firstOrNull { (_, res) ->
622622
when (res) {
623623
is PageResource.EpubReflowable ->
624-
res.link.url() == href
624+
res.link.url().isEquivalent(href)
625625
is PageResource.EpubFxl ->
626626
res.leftUrl?.toString()?.endsWith(href.toString()) == true || res.rightUrl?.toString()?.endsWith(
627627
href.toString()

readium/navigator/src/main/java/org/readium/r2/navigator/extensions/Publication.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public fun Publication.normalizeLocator(locator: Locator): Locator {
3636

3737
return if (self == null) { // Packaged publication
3838
locator.copy(
39-
href = Url(locator.href.toString().removePrefix("/"))?.normalize()
39+
href = Url(locator.href.toString().removePrefix("/"))
4040
?: return locator
4141
)
4242
} else { // Remote publication
43-
// Check that the locator HREF relative to `self` exists int he manifest.
44-
val relativeHref = self.relativize(locator.href).normalize()
43+
// Check that the locator HREF relative to `self` exists in the manifest.
44+
val relativeHref = self.relativize(locator.href)
4545
if (linkWithHref(relativeHref) != null) {
4646
locator.copy(href = relativeHref)
4747
} else {

readium/shared/src/main/java/org/readium/r2/shared/publication/Href.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class Href private constructor(private val href: Url) : Parcelable {
4848
public fun resolve(
4949
base: SharedUrl? = null,
5050
parameters: Map<String, String> = emptyMap()
51-
): SharedUrl = href.resolve(base, parameters).normalize()
51+
): SharedUrl = href.resolve(base, parameters)
5252

5353
/**
5454
* Indicates whether this HREF is templated.

readium/shared/src/main/java/org/readium/r2/shared/publication/Link.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public data class Link(
9292
public fun url(
9393
base: Url? = null,
9494
parameters: Map<String, String> = emptyMap()
95-
): Url = href.resolve(base, parameters).normalize()
95+
): Url = href.resolve(base, parameters)
9696

9797
/**
9898
* List of URI template parameter keys, if the [Link] is templated.

readium/shared/src/main/java/org/readium/r2/shared/publication/Manifest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public data class Manifest(
6969
public fun linkWithHref(href: Url): Link? {
7070
fun List<Link>.deepLinkWithHref(href: Url): Link? {
7171
for (l in this) {
72-
if (l.url() == href) {
72+
if (l.url().normalize() == href) {
7373
return l
7474
} else {
7575
l.alternates.deepLinkWithHref(href)?.let { return it }

readium/shared/src/main/java/org/readium/r2/shared/util/Url.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ public sealed class Url : Parcelable {
210210
return true
211211
}
212212

213+
/**
214+
* Returns whether the receiver is equivalent to the given `url` after normalization.
215+
*/
216+
public fun isEquivalent(url: Url): Boolean =
217+
normalize() == url.normalize()
218+
213219
override fun hashCode(): Int =
214220
uri.toString().hashCode()
215221

@@ -347,9 +353,8 @@ public fun Url.Companion.fromLegacyHref(href: String): Url? =
347353
* if we can't parse the URL.
348354
*/
349355
@InternalReadiumApi
350-
public fun Url.Companion.fromEpubHref(href: String): Url? {
351-
return (Url(href) ?: fromDecodedPath(href))?.normalize()
352-
}
356+
public fun Url.Companion.fromEpubHref(href: String): Url? =
357+
Url(href) ?: fromDecodedPath(href)
353358

354359
public fun File.toUrl(): AbsoluteUrl =
355360
checkNotNull(AbsoluteUrl(Uri.fromFile(this)))
@@ -414,3 +419,13 @@ public value class FileExtension(
414419
*/
415420
public fun FileExtension?.appendToFilename(filename: String): String =
416421
this?.let { "$filename.$value" } ?: filename
422+
423+
/**
424+
* Returns the value of the first key matching `key` after normalization.
425+
*/
426+
public fun <T> Map<Url, T>.getEquivalent(key: Url): T? =
427+
get(key) ?: run {
428+
val url = key.normalize()
429+
keys.firstOrNull { it.normalize() == url }
430+
?.let { get(it) }
431+
}

readium/shared/src/main/java/org/readium/r2/shared/util/data/Caching.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.readium.r2.shared.util.data
88

99
import org.readium.r2.shared.util.Try
1010
import org.readium.r2.shared.util.Url
11+
import org.readium.r2.shared.util.getEquivalent
1112

1213
internal class CachingReadable(
1314
private val source: Readable
@@ -69,7 +70,7 @@ internal class CachingContainer(
6970
mutableMapOf()
7071

7172
override fun get(url: Url): Readable? {
72-
cache[url]?.let { return it }
73+
cache.getEquivalent(url)?.let { return it }
7374

7475
val entry = container[url]
7576
?: return null

readium/shared/src/main/java/org/readium/r2/shared/util/resource/SingleResourceContainer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SingleResourceContainer(
1818
override val entries: Set<Url> = setOf(entryUrl)
1919

2020
override fun get(url: Url): Resource? {
21-
if (url.removeFragment().removeQuery() != entryUrl) {
21+
if (!url.removeFragment().removeQuery().isEquivalent(entryUrl)) {
2222
return null
2323
}
2424

readium/shared/src/test/java/org/readium/r2/shared/util/format/TestContainer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.readium.r2.shared.util.format
88

99
import org.readium.r2.shared.util.Url
1010
import org.readium.r2.shared.util.data.Container
11+
import org.readium.r2.shared.util.getEquivalent
1112
import org.readium.r2.shared.util.resource.Resource
1213
import org.readium.r2.shared.util.resource.StringResource
1314

@@ -25,7 +26,7 @@ class TestContainer(
2526
resources.keys
2627

2728
override fun get(url: Url): Resource? =
28-
resources[url]?.let { StringResource(it) }
29+
resources.getEquivalent(url)?.let { StringResource(it) }
2930

3031
override fun close() {}
3132
}

0 commit comments

Comments
 (0)