Begin parsing OSIS on my own

Also add Kotlin, since I'd like to do what I can to get away from Java.
This commit is contained in:
Bradlee Speice 2014-09-07 21:27:41 -04:00
parent 80238f3cf2
commit 20e4dfe4c4
9 changed files with 125 additions and 18 deletions

View File

@ -1,14 +1,17 @@
buildscript { buildscript {
ext.kotlin_version = '0.8.11'
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+' classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }
apply plugin: 'android-sdk-manager' apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android { android {
compileSdkVersion 20 compileSdkVersion 20
@ -50,6 +53,9 @@ android {
targetSdkVersion 20 targetSdkVersion 20
} }
} }
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
} }
dependencies { dependencies {
@ -68,9 +74,14 @@ dependencies {
compile 'com.android.support:appcompat-v7:20.+' compile 'com.android.support:appcompat-v7:20.+'
compile 'org.apache.commons:commons-lang3:+' compile 'org.apache.commons:commons-lang3:+'
compile 'com.google.code.gson:gson:+' compile 'com.google.code.gson:gson:+'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
androidTestCompile 'com.jayway.awaitility:awaitility:+' androidTestCompile 'com.jayway.awaitility:awaitility:+'
androidTestCompile 'org.mockito:mockito-core:+' androidTestCompile 'org.mockito:mockito-core:+'
androidTestCompile 'com.google.dexmaker:dexmaker:+' androidTestCompile 'com.google.dexmaker:dexmaker:+'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:+' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:+'
}
repositories {
mavenCentral()
} }

View File

@ -9,11 +9,7 @@ app = angular.module('bookApp', []);
app.controller('BookCtrl', [ app.controller('BookCtrl', [
'$scope', function($scope) { '$scope', function($scope) {
$scope.verses = [ $scope.verses = [];
{
'text': 'hello.'
}
];
return $scope.appendVerse = function(text) { return $scope.appendVerse = function(text) {
return $scope.verses.push({ return $scope.verses.push({
'text': text 'text': text
@ -31,8 +27,6 @@ window.appendVerse = function(text) {
return scope.$apply(); return scope.$apply();
}; };
console.log(Android.testReturn("Good morning."));
/* /*
Future reference: Get the controller scope like so: Future reference: Get the controller scope like so:

View File

@ -4,9 +4,7 @@ require 'angular'
app = angular.module('bookApp', []) app = angular.module('bookApp', [])
app.controller 'BookCtrl', ['$scope', ($scope) -> app.controller 'BookCtrl', ['$scope', ($scope) ->
$scope.verses = [ $scope.verses = []
{'text': 'hello.'}
];
$scope.appendVerse = (text) -> $scope.appendVerse = (text) ->
$scope.verses.push {'text': 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 # Since we're calling outside of angular, we need to manually apply
scope.$apply() scope.$apply()
console.log Android.testReturn "Good morning."
### ###
Future reference: Get the controller scope like so: Future reference: Get the controller scope like so:
angular.element($("<controller-element>")).scope().<function> angular.element($("<controller-element>")).scope().<function>

View File

@ -92,6 +92,12 @@ public class BibleViewerModules {
} }
} }
@Provides
@Singleton
BookManager bookManager() {
return new BookManager();
}
@Provides @Provides
VersificationUtil provideVersificationUtil() { VersificationUtil provideVersificationUtil() {
return new VersificationUtil(); return new VersificationUtil();

View File

@ -114,8 +114,7 @@ public class BookFragment extends BaseFragment {
Verse initial = new Verse(vUtil.getVersification(mBook.get()), Verse initial = new Verse(vUtil.getVersification(mBook.get()),
BibleBook.GEN, 1, 1); BibleBook.GEN, 1, 1);
super.onPageFinished(view, url); super.onPageFinished(view, url);
// invokeJavascript("appendVerse", lookupService.getHTMLVerse(initial)); invokeJavascript("appendVerse", lookupService.getHTMLVerse(initial));
invokeJavascript("appendVerse", "Testing string...");
} }
@Override @Override

View File

@ -4,8 +4,7 @@ import android.support.v4.util.LruCache;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlParameters; import org.bspeice.minimalbible.service.osisparser.OsisParser;
import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlSaxHandler;
import org.crosswire.common.xml.SAXEventProvider; import org.crosswire.common.xml.SAXEventProvider;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookData; import org.crosswire.jsword.book.BookData;
@ -81,10 +80,11 @@ public class VerseLookupService implements Action1<Verse> {
BookData bookData = new BookData(book, v); BookData bookData = new BookData(book, v);
try { try {
SAXEventProvider provider = bookData.getSAXEventProvider(); SAXEventProvider provider = bookData.getSAXEventProvider();
OsisToHtmlSaxHandler handler = new OsisToHtmlSaxHandler(new OsisToHtmlParameters()); // OsisToHtmlSaxHandler handler = new OsisToHtmlSaxHandler(new OsisToHtmlParameters());
OsisParser handler = new OsisParser(v);
provider.provideSAXEvents(handler); provider.provideSAXEvents(handler);
Log.e(this.getClass().getName(), handler.toString()); Log.e(this.getClass().getName(), handler.toString());
return handler.toString(); return handler.getContent().getContent();
} catch (BookException e) { } catch (BookException e) {
e.printStackTrace(); e.printStackTrace();
return "Unable to locate " + v.toString() + "!"; return "Unable to locate " + v.toString() + "!";

View File

@ -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<Boolean> = 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<String> = osisId.split("\\.")
return parts[parts.size - 1].toInt()
}
}

View File

@ -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<VerseReference> = ArrayList()
public fun appendContent(content: String) {
this.content += content
}
public fun toJson() {
}
}

View File

@ -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
}