name.remal.javax.xml.transform.Transformer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common Show documentation
Show all versions of common Show documentation
Java & Kotlin tools: common
package name.remal import org.w3c.dom.Document import org.w3c.dom.DocumentFragment import org.w3c.dom.Element import org.w3c.dom.Node import java.io.* import java.net.URI import java.net.URL import javax.xml.transform.Result import javax.xml.transform.Source import javax.xml.transform.Transformer fun Transformer.transform(source: Source) = TransformerWithSource(this, source) fun Transformer.transform(url: URL) = this.transform(newTransformSource(url)) fun Transformer.transform(file: File) = this.transform(newTransformSource(file)) fun Transformer.transform(inputStream: InputStream, location: URI? = null) = this.transform(newTransformSource(inputStream, location)) fun Transformer.transform(reader: Reader, location: URI? = null) = this.transform(newTransformSource(reader, location)) fun Transformer.transform(node: Node, location: URI? = null) = this.transform(newTransformSource(node, location)) fun Transformer.transform(string: String, location: URI? = null) = this.transform(newTransformSource(string, location)) class TransformerWithSource( private val transformer: Transformer, private val source: Source ) { fun into(result: Result) { transformer.transform(source, result) } fun into(outputStream: OutputStream, location: URI? = null) = into(newTransformResult(outputStream, location)) fun into(writer: Writer, location: URI? = null) = into(newTransformResult(writer, location)) fun into(document: Document, location: URI? = null) = into(newTransformResult(document, location)) fun into(documentFragment: DocumentFragment, location: URI? = null) = into(newTransformResult(documentFragment, location)) fun into(element: Element, location: URI? = null) = into(newTransformResult(element, location)) fun intoString(location: URI? = null): String = StringWriter().use { into(it, location); it.toString() } }