Add model for OPML parsing

This commit is contained in:
Shinokuni 2019-10-27 15:29:52 +01:00
parent 13f3132238
commit fbb75073ef
6 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package com.readrops.readropslibrary.opml
import android.content.Context
import android.net.Uri
import com.readrops.readropslibrary.opml.model.Opml
import com.readrops.readropslibrary.utils.LibUtils
import io.reactivex.Single
import org.simpleframework.xml.Serializer
import org.simpleframework.xml.core.Persister
class OpmlParser {
companion object {
@JvmStatic
fun parse(uri: Uri, context: Context): Single<Opml> {
return Single.create { emitter ->
val fileString = LibUtils.fileToString(uri, context)
val serializer: Serializer = Persister()
val opml: Opml = serializer.read(Opml::class.java, fileString)
emitter.onSuccess(opml)
}
}
}
}

View File

@ -0,0 +1,10 @@
package com.readrops.readropslibrary.opml.model
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
@Root(name = "body", strict = false)
data class Body(@field:ElementList(inline = true, required = true) var outlines: List<Outline>?) {
constructor() : this(null)
}

View File

@ -0,0 +1,10 @@
package com.readrops.readropslibrary.opml.model
import org.simpleframework.xml.Element
import org.simpleframework.xml.Root
@Root(name = "head", strict = false)
data class Head(@field:Element(required = false) var title: String?) {
constructor() : this(null)
}

View File

@ -0,0 +1,14 @@
package com.readrops.readropslibrary.opml.model
import org.simpleframework.xml.Attribute
import org.simpleframework.xml.Element
import org.simpleframework.xml.Root
@Root(name = "opml", strict = false)
data class Opml(@field:Attribute(required = true) var version: String?,
@field:Element(required = true) var head: Head?,
@field:Element(required = true) var body: Body?) {
constructor() : this(null, null, null)
}

View File

@ -0,0 +1,26 @@
package com.readrops.readropslibrary.opml.model
import org.simpleframework.xml.Attribute
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
@Root(name = "outline", strict = false)
data class Outline(@field:Attribute(required = false) var title: String?,
@field:Attribute(required = false) var text: String?,
@field:Attribute(required = false) var type: String?,
@field:Attribute(required = false) var xmlUrl: String?,
@field:Attribute(required = false) var htmlUrl: String?,
@field:ElementList(inline = true, required = false) var outlines: List<Outline>?) {
constructor() : this(
null,
null,
null,
null,
null,
null)
fun hasSubElements(): Boolean {
return !outlines.isNullOrEmpty()
}
}

View File

@ -1,5 +1,9 @@
package com.readrops.readropslibrary.utils; package com.readrops.readropslibrary.utils;
import android.content.Context;
import android.net.Uri;
import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Scanner; import java.util.Scanner;
@ -28,6 +32,10 @@ public final class LibUtils {
return scanner.hasNext() ? scanner.next() : ""; return scanner.hasNext() ? scanner.next() : "";
} }
public static String fileToString(Uri uri, Context context) throws FileNotFoundException {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
return inputStreamToString(inputStream);
}
} }