From 7bcb6765321b20a91e90ef6778aa37601f5d4bcd Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 5 Apr 2015 22:33:19 -0400 Subject: [PATCH] Basic quote handling Handling quotation marks and spacing are going to be interesting --- .../bspeice/minimalbible/MinimalBible.java | 12 +++++-- .../service/format/osisparser/OsisParser.kt | 30 +++++++++------- .../osisparser/handler/DivineHandler.kt | 13 ++++--- .../format/osisparser/handler/QHandler.kt | 20 +++++++++++ .../format/osisparser/handler/TagHandler.kt | 15 ++++---- .../osisparser/handler/UnknownHandler.kt | 11 ++++-- .../format/osisparser/handler/VerseHandler.kt | 36 +++++++++---------- app/src/main/res/values/colors.xml | 2 ++ 8 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/QHandler.kt diff --git a/app/src/main/java/org/bspeice/minimalbible/MinimalBible.java b/app/src/main/java/org/bspeice/minimalbible/MinimalBible.java index e13d845..475f209 100644 --- a/app/src/main/java/org/bspeice/minimalbible/MinimalBible.java +++ b/app/src/main/java/org/bspeice/minimalbible/MinimalBible.java @@ -16,16 +16,22 @@ import dagger.ObjectGraph; * Set up the application! */ public class MinimalBible extends Application implements Injector { - private String TAG = "MinimalBible"; + private static Context mContext; private ObjectGraph mObjectGraph; public static MinimalBible get(Context ctx) { return (MinimalBible) ctx.getApplicationContext(); } + public static Context getAppContext() { + Logger.w("Statically accessing context, please refactor that."); + return mContext; + } + @Override public void onCreate() { super.onCreate(); + mContext = this; Logger.init().setLogLevel(LogLevel.NONE); buildObjGraph(); setJswordHome(); @@ -52,10 +58,10 @@ public class MinimalBible extends Application implements Injector { // We need to set the download directory for jSword to stick with // Android. String home = this.getFilesDir().toString(); - Logger.d(TAG, "Setting jsword.home to: " + home); + Logger.d("Setting jsword.home to: " + home); System.setProperty("jsword.home", home); System.setProperty("sword.home", home); SwordBookPath.setDownloadDir(new File(home)); - Logger.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir()); + Logger.d("Sword download path: " + SwordBookPath.getSwordDownloadDir()); } } \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt index 7fd4453..0bb02f3 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt @@ -1,18 +1,17 @@ package org.bspeice.minimalbible.service.format.osisparser -import org.xml.sax.helpers.DefaultHandler -import org.crosswire.jsword.passage.Verse -import java.util.ArrayDeque -import org.xml.sax.Attributes -import org.crosswire.jsword.book.OSISUtil -import org.crosswire.jsword.book.BookData -import org.crosswire.jsword.book.Book -import kotlin.properties.Delegates -import org.bspeice.minimalbible.service.format.osisparser.handler.TagHandler -import org.bspeice.minimalbible.service.format.osisparser.handler.VerseHandler -import org.bspeice.minimalbible.service.format.osisparser.handler.UnknownHandler import android.text.SpannableStringBuilder -import org.bspeice.minimalbible.service.format.osisparser.handler.DivineHandler +import org.bspeice.minimalbible.MinimalBible +import org.bspeice.minimalbible.R +import org.bspeice.minimalbible.service.format.osisparser.handler.* +import org.crosswire.jsword.book.Book +import org.crosswire.jsword.book.BookData +import org.crosswire.jsword.book.OSISUtil +import org.crosswire.jsword.passage.Verse +import org.xml.sax.Attributes +import org.xml.sax.helpers.DefaultHandler +import java.util.ArrayDeque +import kotlin.properties.Delegates /** * Parse out the OSIS XML into whatever we want! @@ -56,12 +55,17 @@ class OsisParser() : DefaultHandler() { when (localName) { OSISUtil.OSIS_ELEMENT_VERSE -> handlerStack push VerseHandler() "divineName" -> handlerStack push DivineHandler() + "q" -> handlerStack push QHandler(MinimalBible.getAppContext() + .getResources().getColor(R.color.divineSpeech)) else -> handlerStack push UnknownHandler(localName) } + + handlerStack.peek().start(attributes, verseContent, builder) } override fun endElement(uri: String, localName: String, qName: String) { - handlerStack.pop() + val tagHandler = handlerStack.pop() + tagHandler.end(verseContent, builder) } override fun characters(ch: CharArray, start: Int, length: Int) { diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/DivineHandler.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/DivineHandler.kt index 10eb62a..e1d25a3 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/DivineHandler.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/DivineHandler.kt @@ -1,13 +1,18 @@ package org.bspeice.minimalbible.service.format.osisparser.handler import android.text.SpannableStringBuilder -import org.bspeice.minimalbible.service.format.osisparser.VerseContent import android.text.style.RelativeSizeSpan +import org.bspeice.minimalbible.service.format.osisparser.VerseContent +import org.xml.sax.Attributes -/** - * Created by bspeice on 12/1/14. - */ class DivineHandler() : TagHandler { + override fun end(info: VerseContent, builder: SpannableStringBuilder) { + } + + override fun start(attrs: Attributes, info: VerseContent, + builder: SpannableStringBuilder) { + } + override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { this buildDivineName chars forEach { it apply builder } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/QHandler.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/QHandler.kt new file mode 100644 index 0000000..17e5c1d --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/QHandler.kt @@ -0,0 +1,20 @@ +package org.bspeice.minimalbible.service.format.osisparser.handler + +import android.text.SpannableStringBuilder +import android.text.style.ForegroundColorSpan +import org.bspeice.minimalbible.service.format.osisparser.VerseContent +import org.xml.sax.Attributes + +class QHandler(val color: Int) : TagHandler { + + override fun start(attrs: Attributes, info: VerseContent, builder: SpannableStringBuilder) { + } + + override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { + AppendArgs(chars, ForegroundColorSpan(color)) apply builder + } + + override fun end(info: VerseContent, builder: SpannableStringBuilder) { + AppendArgs(" ", null) apply builder + } +} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/TagHandler.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/TagHandler.kt index 08d7f94..beee1a9 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/TagHandler.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/TagHandler.kt @@ -1,15 +1,14 @@ package org.bspeice.minimalbible.service.format.osisparser.handler import android.text.SpannableStringBuilder -import org.bspeice.minimalbible.service.format.osisparser.VerseContent import android.text.style.CharacterStyle - -/** - * Created by bspeice on 12/1/14. - */ +import org.bspeice.minimalbible.service.format.osisparser.VerseContent +import org.xml.sax.Attributes trait TagHandler { + fun start(attrs: Attributes, info: VerseContent, builder: SpannableStringBuilder) fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) + fun end(info: VerseContent, builder: SpannableStringBuilder) } data class AppendArgs(val text: String, val span: Any?) { @@ -17,9 +16,9 @@ data class AppendArgs(val text: String, val span: Any?) { val offset = builder.length() builder.append(text) when (span) { - is List<*> -> span.forEach { builder.setSpan(it, offset, offset + text.length, 0) } - is CharacterStyle -> builder.setSpan(span, offset, offset + text.length, 0) + is List<*> -> span.forEach { builder.setSpan(it, offset, offset + text.length(), 0) } + is CharacterStyle -> builder.setSpan(span, offset, offset + text.length(), 0) } - builder.setSpan(span, offset, offset + text.length, 0) + builder.setSpan(span, offset, offset + text.length(), 0) } } \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/UnknownHandler.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/UnknownHandler.kt index f078cc2..bd20678 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/UnknownHandler.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/UnknownHandler.kt @@ -3,11 +3,16 @@ package org.bspeice.minimalbible.service.format.osisparser.handler import android.text.SpannableStringBuilder import com.orhanobut.logger.Logger import org.bspeice.minimalbible.service.format.osisparser.VerseContent +import org.xml.sax.Attributes -/** - * Created by bspeice on 12/1/14. - */ class UnknownHandler(val tagName: String) : TagHandler { + override fun end(info: VerseContent, builder: SpannableStringBuilder) { + } + + override fun start(attrs: Attributes, info: VerseContent, + builder: SpannableStringBuilder) { + } + override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { Logger.w("Unknown tag '$tagName' received text: '$chars'") } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/VerseHandler.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/VerseHandler.kt index 4e5994b..21fa91b 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/VerseHandler.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/handler/VerseHandler.kt @@ -1,28 +1,28 @@ package org.bspeice.minimalbible.service.format.osisparser.handler -import android.text.SpannableStringBuilder -import org.bspeice.minimalbible.service.format.osisparser.VerseContent -import android.text.style.StyleSpan import android.graphics.Typeface -import android.text.style.SuperscriptSpan +import android.text.SpannableStringBuilder import android.text.style.RelativeSizeSpan +import android.text.style.StyleSpan +import android.text.style.SuperscriptSpan +import org.bspeice.minimalbible.service.format.osisparser.VerseContent +import org.xml.sax.Attributes -/** - * Created by bspeice on 12/1/14. - */ class VerseHandler() : TagHandler { - var isVerseStart = true - override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { - buildVerseHeader(info.chapter, info.verseNum, isVerseStart) apply builder - builder append chars - isVerseStart = false + override fun end(info: VerseContent, builder: SpannableStringBuilder) { } - fun buildVerseHeader(chapter: Int, verseNum: Int, verseStart: Boolean): AppendArgs = - when { - !verseStart -> AppendArgs("", null) - verseNum == 1 -> AppendArgs("$chapter ", StyleSpan(Typeface.BOLD)) - else -> AppendArgs("${verseNum}", listOf(SuperscriptSpan(), RelativeSizeSpan(.75f))) - } + override fun start(attrs: Attributes, info: VerseContent, + builder: SpannableStringBuilder) { + when { + info.verseNum == 1 -> AppendArgs("${info.chapter} ", StyleSpan(Typeface.BOLD)) + else -> AppendArgs("${info.verseNum}", + listOf(SuperscriptSpan(), RelativeSizeSpan(.75f))) + } apply builder + } + + override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { + builder append chars + } } \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 97569cf..27a7407 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -13,4 +13,6 @@ @color/textColor @color/textColorSecondary + + @color/colorPrimary