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 #41 from readium/develop
Browse files Browse the repository at this point in the history
1.0.4
  • Loading branch information
aferditamuriqi authored Dec 9, 2018
2 parents bd8a7d5 + aef0585 commit 7f66e0a
Show file tree
Hide file tree
Showing 18 changed files with 640 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea
.DS_Store
/build
/captures
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

repositories {
google()
jcenter()
Expand Down
16 changes: 6 additions & 10 deletions r2-shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ android {
flavorDimensions "testapp"

compileSdkVersion 27
buildToolsVersion "27.0.3"

defaultConfig {
minSdkVersion 21
targetSdkVersion 27
Expand Down Expand Up @@ -52,24 +50,22 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.jakewharton.timber:timber:4.7.0'

implementation 'com.android.support:appcompat-v7:27.1.1'
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' //for JVM
implementation 'com.github.kittinunf.fuel:fuel-android:1.15.0' //for Android
implementation 'com.github.kittinunf.fuel:fuel:1.15.0'
implementation 'com.github.kittinunf.fuel:fuel-android:1.15.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'
implementation 'nl.komponents.kovenant:kovenant-combine:3.3.0'
implementation 'nl.komponents.kovenant:kovenant-jvm:3.3.0'
implementation 'nl.komponents.kovenant:kovenant-functional:3.3.0'
implementation 'joda-time:joda-time:2.9.9'

testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.12.0"
testImplementation 'org.mockito:mockito-core:2.12.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
Expand Down
161 changes: 82 additions & 79 deletions r2-shared/r2-shared.iml

Large diffs are not rendered by default.

20 changes: 17 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 @@ -24,7 +24,7 @@ class Contributor : JSONable, Serializable {
var name: String? = null
get() = multilanguageName.singleString

override fun getJSON(): JSONObject {
override fun toJSON(): JSONObject {
val obj = JSONObject()
obj.put("name", name)
if (roles.isNotEmpty()) {
Expand All @@ -44,6 +44,13 @@ fun parseContributors(contributors: Any): List<Contributor> {
c.multilanguageName.singleString = contributors
result.add(c)
}
is Array<*> -> {
for(i in 0 until contributors.size - 1) {
val c = Contributor()
c.multilanguageName.singleString = contributors[i] as String
result.add(c)
}
}
is JSONObject -> {
val c = parseContributor(contributors)
result.add(c)
Expand Down Expand Up @@ -79,8 +86,15 @@ fun parseContributor(cDict: JSONObject): Contributor {
c.roles.add(cDict.getString("role"))
}
if (cDict.has("links")) {
val linkDict = cDict.getJSONObject("links")
c.links.add(parseLink(linkDict))
cDict.get("links")?.let {
val links = it as? JSONArray
?: JSONArray()
for (i in 0..(links.length() - 1)) {
val linkDict = links.getJSONObject(i)
val link = parseLink(linkDict)
c.links.add(link)
}
}
}
return c
}
2 changes: 1 addition & 1 deletion r2-shared/src/main/java/org/readium/r2/shared/JSONable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import org.json.JSONObject

interface JSONable {

fun getJSON(): JSONObject
fun toJSON(): JSONObject

}
19 changes: 13 additions & 6 deletions r2-shared/src/main/java/org/readium/r2/shared/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ class Link : JSONable, Serializable {
return properties.encryption != null
}

override fun getJSON(): JSONObject {
override fun toJSON(): JSONObject {
val json = JSONObject()
json.putOpt("title", title)
json.putOpt("type", typeLink)
json.putOpt("href", href)
if (rel.isNotEmpty())
json.put("rel", getStringArray(rel))
json.putOpt("properties", properties)
tryPut(json, properties, "properties")
if (height != 0)
json.putOpt("height", height)
if (width != 0)
json.putOpt("width", width)
json.putOpt("duration", duration)
if (children.isNotEmpty())
json.put("children", getJSONArray(children))
return json
}

Expand Down Expand Up @@ -135,10 +137,15 @@ fun parseLink(linkDict: JSONObject, feedUrl: URL? = null): Link {
}
}
if (linkDict.has("children")) {
val childLinkDict = linkDict.getJSONObject("children")
?: throw Exception(LinkError.InvalidLink.name)
val childLink = parseLink(childLinkDict)
link.children.add(childLink)
linkDict.get("children")?.let {
val children = it as? JSONArray
?: throw Exception(LinkError.InvalidLink.name)
for (i in 0..(children.length() - 1)) {
val childLinkDict = children.getJSONObject(i)
val childLink = parseLink(childLinkDict)
link.children.add(childLink)
}
}
}
return link
}
177 changes: 177 additions & 0 deletions r2-shared/src/main/java/org/readium/r2/shared/Locator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Module: r2-shared-kotlin
* Developers: Aferdita Muriqi, Mostapha Idoubihi, Paul Stoica
*
* Copyright (c) 2018. 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

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

/**
* Locator model - https://github.com/readium/architecture/tree/master/locators
*
* @val href: String - The href of the resource the locator points at.
* @val created: Long - The datetime of creation of the locator.
* @val title: String - The title of the chapter or section which is more relevant in the context of this locator.
* @val location: Location - One or more alternative expressions of the location.
* @val text: LocatorText? - Textual context of the locator.
*/

open class Locator(val href: String,
val created: Long,
val title: String,
val locations: Locations,
val text: LocatorText?) : Serializable

class LocatorText(var after: String? = null,
var before: String? = null,
var hightlight: String? = null)
: JSONable, Serializable {

companion object {
fun fromJSON(json: JSONObject): LocatorText {

val location = LocatorText()
if (json.has("before")) {
location.before = json.getString("before")
}
if (json.has("hightlight")) {
location.hightlight = json.getString("hightlight")
}
if (json.has("after")) {
location.after = json.getString("after")
}

return location
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()

before?.let {
json.putOpt("before", before)
}
hightlight?.let {
json.putOpt("hightlight", hightlight)
}
after?.let {
json.putOpt("after", after)
}

return json
}

override fun toString(): String {
var jsonString = """{"""

if (before != null) {
before.let { jsonString += """ "before": "$before" ,""" }
}
if (hightlight != null) {
hightlight.let { jsonString += """ "before": "$hightlight" ,""" }
}
if (after != null) {
after.let { jsonString += """ "after": "$after" ,""" }
}
jsonString += """}"""
return jsonString
}
}

/**
* Location : Class that contain the different variables needed to localize a particular position
*
* @var id: Long? - Identifier of a specific fragment in the publication
* @var cfi: String? - String formatted to designed a particular place in an Publication
* @var cssSelector: String? - Css selector
* @var xpath: String? - An xpath in the resource
* @var progression: Double - A percentage ( between 0 and 1 ) of the progression in a Publication
* @var position: Long - Index of a segment in the resource / synthetic page number!!??
*
*/
class Locations(var cfi: String? = null, // 1 = highlight, annotation etc
var id: String? = null, // 2 = fragment identifier (toc, page lists, landmarks)
var cssSelector: String? = null, // 2 =
var xpath: String? = null, // 2 =
var progression: Double? = null, // 3 = bookmarks
var position: Long? = null // 4 = goto page
) : JSONable, Serializable {

companion object {
fun fromJSON(json: JSONObject): Locations {

val location = Locations()
if (json.has("id")) {
location.id = json.getString("id")
}
if (json.has("cfi")) {
location.cfi = json.getString("cfi")
}
if (json.has("cssSelector")) {
location.cssSelector = json.getString("cssSelector")
}
if (json.has("xpath")) {
location.xpath = json.getString("xpath")
}
if (json.has("progression")) {
location.progression = json.getDouble("progression")
}
if (json.has("position")) {
location.position = json.getLong("position")
}

return location
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()

id?.let {
json.putOpt("id", id)
}
cfi?.let {
json.putOpt("cfi", cfi)
}
cssSelector?.let {
json.putOpt("cssSelector", cssSelector)
}
xpath?.let {
json.putOpt("xpath", xpath)
}
progression?.let {
json.putOpt("progression", progression)
}
position?.let {
json.putOpt("position", position)
}

return json
}

override fun toString(): String {
var jsonString = """{"""
if (id != null) {
id.let { jsonString += """ "id": "$id" ,""" }
}
if (cfi != null) {
cfi.let { jsonString += """ "cfi": "$cfi" ,""" }
}
if (cssSelector != null) {
cssSelector.let { jsonString += """ "cssSelector": "$cssSelector" ,""" }
}
if (xpath != null) {
xpath.let { jsonString += """ "xpath": "$xpath" ,""" }
}
progression.let { jsonString += """ "progression": "$progression" ,""" }
position.let { jsonString += """ "position": "$position" """ }
jsonString += """}"""
return jsonString
}
}
Loading

0 comments on commit 7f66e0a

Please sign in to comment.