Skip to content

Commit

Permalink
Add more logic to linkWithHref()
Browse files Browse the repository at this point in the history
For more explanation see commit with same commit message in
r2-streamer-kotlin
  • Loading branch information
hrishikesh-kadam committed Dec 31, 2018
1 parent c1f242f commit d2a7d85
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions r2-shared/src/main/java/org/readium/r2/shared/Publication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ package org.readium.r2.shared
import org.json.JSONArray
import org.json.JSONObject
import java.io.Serializable
import java.net.URI
import java.net.URL
import java.net.URLDecoder


fun URL.removeLastComponent(): URL {
Expand Down Expand Up @@ -93,6 +95,7 @@ class Publication : Serializable {
EPUB(".epub"),
CBZ(".cbz"),
JSON(".json");

companion object : EnumCompanion<String, EXTENSION>(EXTENSION.values().associateBy(EXTENSION::value))
}

Expand Down Expand Up @@ -157,18 +160,53 @@ class Publication : Serializable {
return str
}

fun resource(relativePath: String): Link? = (spine + resources).first { (it.href == relativePath) || (it.href == "/$relativePath") }
fun resource(href: String): Link? =
(spine + resources).firstOrNull {
isLinkWithHref(href, it) ||
isLinkWithHrefURIDecoded(href, it) ||
isLinkWithLinkHrefURLDecoded(href, it)
}

fun linkWithRel(rel: String): Link? {
val findLinkWithRel: (Link) -> Boolean = { it.rel.contains(rel) }
return findLinkInPublicationLinks(findLinkWithRel)
}

fun linkWithHref(href: String): Link? {
val findLinkWithHref: (Link) -> Boolean = { (href == it.href) || ("/$href" == it.href) }
val findLinkWithHref: (Link) -> Boolean = {
isLinkWithHref(href, it) ||
isLinkWithHrefURIDecoded(href, it) ||
isLinkWithLinkHrefURLDecoded(href, it)
}
return findLinkInPublicationLinks(findLinkWithHref)
}

private fun isLinkWithHref(href: String, link: Link): Boolean {
return href == link.href || "/$href" == link.href
}

private fun isLinkWithHrefURIDecoded(href: String, link: Link): Boolean {
return try {
val decodedHref = URI(null, null, href, null).toString()
decodedHref == link.href || "/$decodedHref" == link.href
} catch (e: Exception) {
false
}
}

private fun isLinkWithLinkHrefURLDecoded(href: String, link: Link): Boolean {
return try {
val decodedLinkHref = URLDecoder.decode(link.href, "UTF-8")
href == decodedLinkHref || "/$href" == decodedLinkHref
} catch (e: Exception) {
false
}
}

private fun findLinkInPublicationLinks(closure: (Link) -> Boolean) =
resources.firstOrNull(closure) ?: spine.firstOrNull(closure)
?: links.firstOrNull(closure) ?: pageList.firstOrNull(closure)

fun addSelfLink(endPoint: String, baseURL: URL) {
val publicationUrl: URL
val link = Link()
Expand All @@ -181,14 +219,9 @@ class Publication : Serializable {
links.add(link)
}

private fun findLinkInPublicationLinks(closure: (Link) -> Boolean) =
resources.firstOrNull(closure) ?: spine.firstOrNull(closure)
?: links.firstOrNull(closure) ?: pageList.firstOrNull(closure)

enum class PublicationError(var v: String) {
InvalidPublication("Invalid publication")
}

}


Expand Down

0 comments on commit d2a7d85

Please sign in to comment.