blob: dedfbbf25b7aa09dd874b7a9c51593167314a31e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package ca.allanwang.kau.xml
import android.content.Context
import android.content.res.XmlResourceParser
import android.support.annotation.XmlRes
import android.text.Html
import android.text.Spanned
import ca.allanwang.kau.utils.use
import org.xmlpull.v1.XmlPullParser
/**
* Created by Allan Wang on 2017-07-30.
*/
/**
* Parse an xml with two tags, <question>Text</question> and <answer>Text</answer>,
* and return them as a list of string pairs
*/
fun Context.kauParseFaq(@XmlRes xmlRes: Int, withNumbering: Boolean = true): List<Pair<Spanned, Spanned>> {
val items = mutableListOf<Pair<Spanned, Spanned>>()
resources.getXml(xmlRes).use {
parser: XmlResourceParser ->
var eventType = parser.eventType
var question: Spanned? = null
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (parser.name == "question") {
var q = parser.text.replace("\n", "<br/>")
if (withNumbering) q = "${items.size + 1}. $q"
question = Html.fromHtml(q)
} else if (parser.name == "answer") {
items.add(Pair(question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"),
Html.fromHtml(parser.text.replace("\n", "<br/>"))))
question = null
}
}
eventType = parser.next()
}
}
return items
}
|