Skip to content

Commit

Permalink
feat: streams overload for XmlFileHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 2, 2022
1 parent c8eedac commit 6f72c4c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/main/kotlin/app/revanced/patcher/data/impl/ResourceData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import app.revanced.patcher.data.Data
import org.w3c.dom.Document
import java.io.Closeable
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
Expand All @@ -17,14 +19,20 @@ class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable<Fi
override fun iterator() = resourceCacheDirectory.walkTopDown().iterator()

inner class XmlFileHolder {
operator fun get(inputStream: InputStream, outputStream: OutputStream) =
DomFileEditor(inputStream, outputStream)

operator fun get(path: String) = DomFileEditor(this@ResourceData[path])
}
}

class DomFileEditor internal constructor(private val domFile: File) : Closeable {
val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(domFile).also(Document::normalize)
class DomFileEditor internal constructor(inputStream: InputStream, private val outputStream: OutputStream) : Closeable {
constructor(file: File) : this(file.inputStream(), file.outputStream())

val file: Document =
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream).also(Document::normalize)

override fun close() =
TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), StreamResult(outputStream))

override fun close() = TransformerFactory.newInstance().newTransformer()
.transform(DOMSource(file), StreamResult(domFile.outputStream()))
}

0 comments on commit 6f72c4c

Please sign in to comment.