From 20e4dfe4c4525fff02f65a61a2a1f8e42b39e2ad Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 7 Sep 2014 21:27:41 -0400 Subject: [PATCH] Begin parsing OSIS on my own Also add Kotlin, since I'd like to do what I can to get away from Java. --- app/build.gradle | 11 ++++ app/src/main/assets/dist/book-bundle.js | 8 +-- app/src/main/assets/src/book.coffee | 6 +- .../activity/viewer/BibleViewerModules.java | 6 ++ .../activity/viewer/BookFragment.java | 3 +- .../service/book/VerseLookupService.java | 8 +-- .../service/osisparser/OsisParser.kt | 66 +++++++++++++++++++ .../service/osisparser/VerseContent.kt | 23 +++++++ .../service/osisparser/VerseReference.kt | 12 ++++ 9 files changed, 125 insertions(+), 18 deletions(-) create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/OsisParser.kt create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseContent.kt create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseReference.kt diff --git a/app/build.gradle b/app/build.gradle index aca9f12..650c4fc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,14 +1,17 @@ buildscript { + ext.kotlin_version = '0.8.11' repositories { mavenCentral() } dependencies { classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'android-sdk-manager' apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' android { compileSdkVersion 20 @@ -50,6 +53,9 @@ android { targetSdkVersion 20 } } + sourceSets { + main.java.srcDirs += 'src/main/kotlin' + } } dependencies { @@ -68,9 +74,14 @@ dependencies { compile 'com.android.support:appcompat-v7:20.+' compile 'org.apache.commons:commons-lang3:+' compile 'com.google.code.gson:gson:+' + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" androidTestCompile 'com.jayway.awaitility:awaitility:+' androidTestCompile 'org.mockito:mockito-core:+' androidTestCompile 'com.google.dexmaker:dexmaker:+' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:+' + } +repositories { + mavenCentral() +} \ No newline at end of file diff --git a/app/src/main/assets/dist/book-bundle.js b/app/src/main/assets/dist/book-bundle.js index 50a2b95..4589aa9 100755 --- a/app/src/main/assets/dist/book-bundle.js +++ b/app/src/main/assets/dist/book-bundle.js @@ -9,11 +9,7 @@ app = angular.module('bookApp', []); app.controller('BookCtrl', [ '$scope', function($scope) { - $scope.verses = [ - { - 'text': 'hello.' - } - ]; + $scope.verses = []; return $scope.appendVerse = function(text) { return $scope.verses.push({ 'text': text @@ -31,8 +27,6 @@ window.appendVerse = function(text) { return scope.$apply(); }; -console.log(Android.testReturn("Good morning.")); - /* Future reference: Get the controller scope like so: diff --git a/app/src/main/assets/src/book.coffee b/app/src/main/assets/src/book.coffee index c7fd49e..455b254 100644 --- a/app/src/main/assets/src/book.coffee +++ b/app/src/main/assets/src/book.coffee @@ -4,9 +4,7 @@ require 'angular' app = angular.module('bookApp', []) app.controller 'BookCtrl', ['$scope', ($scope) -> - $scope.verses = [ - {'text': 'hello.'} - ]; + $scope.verses = [] $scope.appendVerse = (text) -> $scope.verses.push {'text': text} @@ -23,8 +21,6 @@ window.appendVerse = (text) -> # Since we're calling outside of angular, we need to manually apply scope.$apply() -console.log Android.testReturn "Good morning." - ### Future reference: Get the controller scope like so: angular.element($("")).scope(). diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java index 4d3f769..62190dd 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java @@ -92,6 +92,12 @@ public class BibleViewerModules { } } + @Provides + @Singleton + BookManager bookManager() { + return new BookManager(); + } + @Provides VersificationUtil provideVersificationUtil() { return new VersificationUtil(); diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java index d5f257d..e868a55 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java @@ -114,8 +114,7 @@ public class BookFragment extends BaseFragment { Verse initial = new Verse(vUtil.getVersification(mBook.get()), BibleBook.GEN, 1, 1); super.onPageFinished(view, url); -// invokeJavascript("appendVerse", lookupService.getHTMLVerse(initial)); - invokeJavascript("appendVerse", "Testing string..."); + invokeJavascript("appendVerse", lookupService.getHTMLVerse(initial)); } @Override diff --git a/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java b/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java index 0da91ac..6a40538 100644 --- a/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java +++ b/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java @@ -4,8 +4,7 @@ import android.support.v4.util.LruCache; import android.util.Log; import org.bspeice.minimalbible.Injector; -import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlParameters; -import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlSaxHandler; +import org.bspeice.minimalbible.service.osisparser.OsisParser; import org.crosswire.common.xml.SAXEventProvider; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookData; @@ -81,10 +80,11 @@ public class VerseLookupService implements Action1 { BookData bookData = new BookData(book, v); try { SAXEventProvider provider = bookData.getSAXEventProvider(); - OsisToHtmlSaxHandler handler = new OsisToHtmlSaxHandler(new OsisToHtmlParameters()); +// OsisToHtmlSaxHandler handler = new OsisToHtmlSaxHandler(new OsisToHtmlParameters()); + OsisParser handler = new OsisParser(v); provider.provideSAXEvents(handler); Log.e(this.getClass().getName(), handler.toString()); - return handler.toString(); + return handler.getContent().getContent(); } catch (BookException e) { e.printStackTrace(); return "Unable to locate " + v.toString() + "!"; diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/OsisParser.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/OsisParser.kt new file mode 100644 index 0000000..eb7c4ec --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/OsisParser.kt @@ -0,0 +1,66 @@ +package org.bspeice.minimalbible.service.osisparser + +import org.crosswire.jsword.book.OSISUtil +import org.xml.sax.Attributes +import org.xml.sax.helpers.DefaultHandler +import java.util.ArrayDeque +import org.crosswire.jsword.passage.Verse + +/** + * Parse the OSIS SAX to an object we can actually use. + * Technically we don't need the verse reference currently, but it will make persisting + * everything easier later. + */ + +class OsisParser(verse: Verse) : DefaultHandler() { + + var content: VerseContent = VerseContent() + val doWrite: ArrayDeque = ArrayDeque() + val verse: Verse = verse + + // Android Studio complains, but the override below compiles since the java + // has a @NotNull contract + override fun startElement(uri: String, localName: String, + qName: String, attributes: Attributes) { + + val name = getName(localName, qName) + + if (name.equals(OSISUtil.OSIS_ELEMENT_VERSE)) { + doWrite.push(true) + content.id = getId(attributes) + } else { + doWrite.push(false) + } + } + + // Android Studio complains, but the override below compiles since the java + // has a @NotNull contract + override fun endElement(uri: String?, localName: String, qName: String) { + doWrite.pop() + } + + override fun characters(ch: CharArray?, start: Int, length: Int) { + if (ch != null && doWrite.peek() as Boolean) + content.appendContent(String(ch)) + } + + fun getName(eName: String?, qName: String): String { + if (eName != null && eName.length > 0) + return eName + else + return qName + } + + fun getId(attrs: Attributes?): Int { + if (attrs == null) + return 0 + + val osisId: String? = attrs.getValue("", OSISUtil.OSIS_ELEMENT_VERSE) + if (osisId == null) + return 0 + + val parts: Array = osisId.split("\\.") + return parts[parts.size - 1].toInt() + } +} + diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseContent.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseContent.kt new file mode 100644 index 0000000..477df88 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseContent.kt @@ -0,0 +1,23 @@ +package org.bspeice.minimalbible.service.osisparser + +import java.util.ArrayList + +/** + * Created by bspeice on 9/7/14. + */ + +class VerseContent() { + public var id: Int = 0 + public var content: String = "" + public var chapterTitle: String = "" + public var paraTitle: String = "" + public var references: List = ArrayList() + + public fun appendContent(content: String) { + this.content += content + } + + public fun toJson() { + + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseReference.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseReference.kt new file mode 100644 index 0000000..6a830f0 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/osisparser/VerseReference.kt @@ -0,0 +1,12 @@ +package org.bspeice.minimalbible.service.osisparser + +import org.crosswire.jsword.passage.Verse + +/** + * Created by bspeice on 9/7/14. + */ + +class VerseReference(verse: Verse, index: Int) { + public val verse: Verse = verse + public val index: Int = index +} \ No newline at end of file