From f05e50292e8f2f1ae0ed529cbb61a444947f5189 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 10 Sep 2014 22:41:34 -0400 Subject: [PATCH] Move parser to Kotlin --- .../service/book/VerseLookupService.java | 2 +- .../service/format/osisparser/OsisParser.java | 83 ------------------- .../service/format/osisparser/OsisParser.kt | 36 ++++++++ 3 files changed, 37 insertions(+), 84 deletions(-) delete mode 100644 app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt 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 ad61eb4..a244ea6 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 @@ -83,7 +83,7 @@ public class VerseLookupService implements Action1 { SAXEventProvider provider = bookData.getSAXEventProvider(); OsisParser handler = new OsisParser(v); provider.provideSAXEvents(handler); - return handler.getContent().toJson(); + return handler.getVerseContent().toJson(); } catch (BookException e) { e.printStackTrace(); return "Unable to locate " + v.toString() + "!"; diff --git a/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java b/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java deleted file mode 100644 index 7690bf5..0000000 --- a/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.bspeice.minimalbible.service.format.osisparser; - -import android.util.Log; - -import org.crosswire.jsword.book.OSISUtil; -import org.crosswire.jsword.passage.Verse; -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - -import java.util.Stack; - -/** - * Parse a single verse into a VerseContent object. - * Need to benchmark if I should process ranges at a time... - */ -public class OsisParser extends DefaultHandler { - - VerseContent verseContent; - // ArrayDeque requires API 9 - Stack doWrite; - - public OsisParser(Verse v) { - verseContent = new VerseContent(v); - doWrite = new Stack(); - } - - @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) - throws SAXException { - String name = getName(localName, qName); - - if (name.equals(OSISUtil.OSIS_ELEMENT_VERSE)) { - doWrite.push(true); - } else { - doWrite.push(false); - } - } - - @Override - public void endElement(String uri, String localName, String qName) throws SAXException { - doWrite.pop(); - } - - @Override - public void characters(char[] ch, int start, int length) throws SAXException { - if (ch != null && doWrite.peek()) { - Log.e(getClass().getName(), new String(ch, start, length)); - verseContent.appendContent(new String(ch, start, length)); - } - } - - @Override - public void endDocument() throws SAXException { - super.endDocument(); - } - - public String getName(String eName, String qName) { - if (eName != null && eName.length() > 0) { - return eName; - } else { - return qName; - } - } - - public int getId(Attributes attributes) { - if (attributes == null) { - return 0; - } - - String osisId = attributes.getValue("", OSISUtil.OSIS_ELEMENT_VERSE); - if (osisId == null) { - return 0; - } - - String[] parts = osisId.split("\\."); - return Integer.valueOf(parts[parts.length - 1]); - } - - public VerseContent getContent() { - return this.verseContent; - } -} 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 new file mode 100644 index 0000000..1ace84c --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt @@ -0,0 +1,36 @@ +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 + +/** + * Created by bspeice on 9/10/14. + */ + +class OsisParser(v: Verse) : DefaultHandler() { + + val verseContent = VerseContent(v) + val doWrite = ArrayDeque() + + // Android Studio complains about compilation, but the method + // has @NotNull annotations, so we program for those. + override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) { + + if (localName.equals(OSISUtil.OSIS_ELEMENT_VERSE)) + doWrite.push(true) + else + doWrite.push(false) + } + // Android Studio complains about compilation, but the method + // has @NotNull annotations, so we program for those. + override fun endElement(uri: String?, localName: String, qName: String) { + doWrite.pop() + } + override fun characters(ch: CharArray?, start: Int, length: Int) { + if (doWrite.peek() as Boolean) + verseContent.appendContent(String(ch as CharArray)) + } +} \ No newline at end of file