diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/OpmlParser.kt b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/OpmlParser.kt new file mode 100644 index 00000000..8beeb073 --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/OpmlParser.kt @@ -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 { + 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) + } + } + } + + +} \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Body.kt b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Body.kt new file mode 100644 index 00000000..b1cdd4ad --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Body.kt @@ -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?) { + + constructor() : this(null) +} \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Head.kt b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Head.kt new file mode 100644 index 00000000..d7f29086 --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Head.kt @@ -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) +} \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Opml.kt b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Opml.kt new file mode 100644 index 00000000..709369c6 --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Opml.kt @@ -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) + +} \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Outline.kt b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Outline.kt new file mode 100644 index 00000000..f59f4e0a --- /dev/null +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/opml/model/Outline.kt @@ -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?) { + + constructor() : this( + null, + null, + null, + null, + null, + null) + + fun hasSubElements(): Boolean { + return !outlines.isNullOrEmpty() + } +} \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/LibUtils.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/LibUtils.java index 0240c857..b0830e35 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/LibUtils.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/utils/LibUtils.java @@ -1,5 +1,9 @@ package com.readrops.readropslibrary.utils; +import android.content.Context; +import android.net.Uri; + +import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Scanner; @@ -28,6 +32,10 @@ public final class LibUtils { return scanner.hasNext() ? scanner.next() : ""; } + public static String fileToString(Uri uri, Context context) throws FileNotFoundException { + InputStream inputStream = context.getContentResolver().openInputStream(uri); + return inputStreamToString(inputStream); + } }