Move parser to Kotlin

This commit is contained in:
Bradlee Speice 2014-09-10 22:41:34 -04:00
parent 3d8ff65af4
commit f05e50292e
3 changed files with 37 additions and 84 deletions

View File

@ -83,7 +83,7 @@ public class VerseLookupService implements Action1<Verse> {
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() + "!";

View File

@ -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<Boolean> doWrite;
public OsisParser(Verse v) {
verseContent = new VerseContent(v);
doWrite = new Stack<Boolean>();
}
@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;
}
}

View File

@ -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<Boolean>()
// 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))
}
}