Start zip output
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package starter.utils
|
||||
|
||||
import com.mitchellbosecke.pebble.PebbleEngine
|
||||
import java.io.StringWriter
|
||||
|
||||
fun PebbleEngine.render(name: String, args: Map<String, Any?> = mapOf()): String {
|
||||
val template = getTemplate(name)
|
||||
val writer = StringWriter()
|
||||
template.evaluate(writer, args)
|
||||
return writer.toString()
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package starter.utils
|
||||
|
||||
import org.w3c.dom.Document
|
||||
import org.w3c.dom.NodeList
|
||||
import org.xml.sax.InputSource
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.StringWriter
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
import javax.xml.transform.OutputKeys
|
||||
import javax.xml.transform.TransformerFactory
|
||||
import javax.xml.transform.dom.DOMSource
|
||||
import javax.xml.transform.stream.StreamResult
|
||||
import javax.xml.xpath.XPathConstants
|
||||
import javax.xml.xpath.XPathFactory
|
||||
|
||||
|
||||
/*
|
||||
@see https://stackoverflow.com/a/33541820
|
||||
*/
|
||||
fun prettyPrintXml(xml: String, indent: Int = 4): String {
|
||||
// Turn xml string into a document
|
||||
val document: Document = DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder()
|
||||
.parse(InputSource(ByteArrayInputStream(xml.encodeToByteArray())))
|
||||
|
||||
// Remove whitespaces outside tags
|
||||
document.normalize()
|
||||
val xPath = XPathFactory.newInstance().newXPath()
|
||||
val nodeList = xPath.evaluate("//text()[normalize-space()='']",
|
||||
document,
|
||||
XPathConstants.NODESET) as NodeList
|
||||
for (i in 0 until nodeList.length) {
|
||||
val node = nodeList.item(i)
|
||||
node.parentNode.removeChild(node)
|
||||
}
|
||||
|
||||
// Setup pretty print options
|
||||
val transformerFactory = TransformerFactory.newInstance()
|
||||
transformerFactory.setAttribute("indent-number", indent)
|
||||
val transformer = transformerFactory.newTransformer()
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
|
||||
|
||||
// Return pretty print xml string
|
||||
val stringWriter = StringWriter()
|
||||
transformer.transform(DOMSource(document), StreamResult(stringWriter))
|
||||
return stringWriter.toString()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package starter.utils
|
||||
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
|
||||
import java.io.File
|
||||
import java.nio.file.Paths
|
||||
|
||||
class ZipOutput(name: String) : AutoCloseable {
|
||||
private val baseDir = File(".").canonicalFile
|
||||
private val zipPath = Paths.get(baseDir.path, "$name.zip")
|
||||
private val zipFile = zipPath.toAbsolutePath().normalize().toFile().apply {
|
||||
createNewFile()
|
||||
}
|
||||
private val outputStream = ZipArchiveOutputStream(zipFile.outputStream())
|
||||
|
||||
fun write(path: String, content: String) {
|
||||
val entry = ZipArchiveEntry(path)
|
||||
outputStream.putArchiveEntry(entry)
|
||||
outputStream.write(content.toByteArray())
|
||||
outputStream.closeArchiveEntry()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
outputStream.finish()
|
||||
outputStream.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user