Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #48 from readium/develop
Browse files Browse the repository at this point in the history
1.0.5
  • Loading branch information
aferditamuriqi authored Jan 15, 2019
2 parents 7f66e0a + 97044b8 commit f8292b6
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 78 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.61'
ext.kotlin_version = '1.3.11'
ext.support_version = '27.1.1'

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
4 changes: 2 additions & 2 deletions r2-shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ dependencies {
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation "com.android.support:appcompat-v7:$support_version"
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.github.kittinunf.fuel:fuel:1.15.0'
implementation 'com.github.kittinunf.fuel:fuel-android:1.15.0'
implementation 'com.github.kittinunf.fuel:fuel:1.16.0'
implementation 'com.github.kittinunf.fuel:fuel-android:1.16.0'
implementation 'nl.komponents.kovenant:kovenant:3.3.0'
implementation 'nl.komponents.kovenant:kovenant-core:3.3.0'
implementation 'nl.komponents.kovenant:kovenant-android:3.3.0'
Expand Down
91 changes: 53 additions & 38 deletions r2-shared/r2-shared.iml

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions r2-shared/src/main/java/org/readium/r2/shared/Contributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@ fun parseContributors(contributors: Any): List<Contributor> {
result.add(c)
}
is JSONArray -> for (i in 0..(contributors.length() - 1)) {
val obj = contributors.getJSONObject(i)
val c = parseContributor(obj)
result.add(c)
when (contributors.get(i)) {
is String -> {
val c = Contributor()
c.multilanguageName.singleString = contributors.getString(i)
result.add(c)
}
is JSONObject -> {
val obj = contributors.getJSONObject(i)
val c = parseContributor(obj)
result.add(c)
}
}
}
}
return result
Expand Down
1 change: 1 addition & 0 deletions r2-shared/src/main/java/org/readium/r2/shared/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fun parseLink(linkDict: JSONObject, feedUrl: URL? = null): Link {
val price = Price(currency = currency, value = value)
properties.price = price
}
link.properties = properties
}
if (linkDict.has("children")) {
linkDict.get("children")?.let {
Expand Down
38 changes: 24 additions & 14 deletions r2-shared/src/main/java/org/readium/r2/shared/Metadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,31 @@ fun parseMetadata(metadataDict: JSONObject): Metadata {
is JSONArray -> {
val subjDict = metadataDict.getJSONArray("subject")
for (i in 0..(subjDict.length() - 1)) {
val sub = subjDict.getJSONObject(i)
val subject = Subject()
if (sub.has("name")) {
subject.name = sub.getString("name")
}
if (sub.has("sort_as")) {
subject.sortAs = sub.getString("sort_as")
}
if (sub.has("scheme")) {
subject.scheme = sub.getString("scheme")
val subObject = subjDict.get(i)
when(subObject){
is String -> {
val subject = Subject()
subject.name = subObject
m.subjects.add(subject)
}
is JSONObject->{
val sub = subjDict.getJSONObject(i)
val subject = Subject()
if (sub.has("name")) {
subject.name = sub.getString("name")
}
if (sub.has("sort_as")) {
subject.sortAs = sub.getString("sort_as")
}
if (sub.has("scheme")) {
subject.scheme = sub.getString("scheme")
}
if (sub.has("code")) {
subject.code = sub.getString("code")
}
m.subjects.add(subject)
}
}
if (sub.has("code")) {
subject.code = sub.getString("code")
}
m.subjects.add(subject)
}
}
}
Expand Down
55 changes: 44 additions & 11 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 All @@ -106,7 +109,7 @@ class Publication : Serializable {
/// org.readium.r2shared.Publication.org.readium.r2shared.Link to special resources which are added to the publication.
var links: MutableList<Link> = mutableListOf()
/// Links of the spine items of the publication.
var spine: MutableList<Link> = mutableListOf()
var readingOrder: MutableList<Link> = mutableListOf()
/// Link to the resources of the publication.
var resources: MutableList<Link> = mutableListOf()
/// Table of content of the publication.
Expand Down Expand Up @@ -145,7 +148,7 @@ class Publication : Serializable {
val json = JSONObject()
json.put("metadata", metadata.writeJSON())
tryPut(json, links, "links")
tryPut(json, spine, "spine")
tryPut(json, readingOrder, "readingOrder")
tryPut(json, resources, "resources")
tryPut(json, tableOfContents, "toc")
tryPut(json, pageList, "page-list")
Expand All @@ -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? =
(readingOrder + 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) ?: readingOrder.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 Expand Up @@ -244,7 +277,7 @@ fun parsePublication(pubDict: JSONObject): Publication {
for (i in 0..(spine.length() - 1)) {
val linkDict = spine.getJSONObject(i)
val link = parseLink(linkDict)
p.spine.add(link)
p.readingOrder.add(link)
}
}
}
Expand All @@ -256,7 +289,7 @@ fun parsePublication(pubDict: JSONObject): Publication {
for (i in 0..(readingOrder.length() - 1)) {
val linkDict = readingOrder.getJSONObject(i)
val link = parseLink(linkDict)
p.spine.add(link)
p.readingOrder.add(link)
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions r2-shared/src/main/java/org/readium/r2/shared/URLHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@

package org.readium.r2.shared

import android.net.Uri
import java.net.URI

fun getAbsolute(href: String, base: String): String {
val baseURI = URI.create(base)
val relative = baseURI.resolve(href)
return relative.toString()
try {
val baseURI = URI.create(base)
val relative = baseURI.resolve(href)
return relative.toString()
}catch (e:IllegalArgumentException){
val hrefUri = Uri.parse(href)
if (hrefUri.isAbsolute){
return href
}else{
return base+href
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
package org.readium.r2.shared.metadata

import org.readium.r2.shared.Collection
import java.io.Serializable


class BelongsTo {
class BelongsTo: Serializable {

var series: MutableList<Collection> = mutableListOf()
var collection: MutableList<Collection> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.readium.r2.shared.opds

import org.json.JSONArray
import org.json.JSONObject
import java.io.Serializable

Expand Down Expand Up @@ -37,8 +38,21 @@ fun parseIndirectAcquisition(indirectAcquisitionDict: JSONObject): IndirectAcqui
val indirectAcquisitionType = indirectAcquisitionDict["type"] as? String
?: throw Exception(IndirectAcquisitionError.InvalidIndirectAcquisition.name)
val indirectAcquisition = IndirectAcquisition(typeAcquisition = indirectAcquisitionType)
val childDict = indirectAcquisitionDict.getJSONObject("child")
val child = parseIndirectAcquisition(indirectAcquisitionDict = childDict)
indirectAcquisition.child.add(child)
if (indirectAcquisitionDict.has("child")){
val childDictAny = indirectAcquisitionDict.get("child")
when (childDictAny) {
is JSONObject -> {
val child = parseIndirectAcquisition(indirectAcquisitionDict = childDictAny)
indirectAcquisition.child.add(child)
}
is JSONArray -> {
for (i in 0..(childDictAny.length() - 1)) {
val childDict = childDictAny.getJSONObject(i)
val child = parseIndirectAcquisition(indirectAcquisitionDict = childDict)
indirectAcquisition.child.add(child)
}
}
}
}
return indirectAcquisition
}

0 comments on commit f8292b6

Please sign in to comment.