Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Nov 14, 2017
0 parents commit ab01957
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
Binary file added lib/dom4j-2.1.0-sources.jar
Binary file not shown.
Binary file added lib/dom4j-2.1.0.jar
Binary file not shown.
Binary file added lib/poi-3.17.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: tech.summerly.MainKt

80 changes: 80 additions & 0 deletions src/tech/summerly/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tech.summerly

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.dom4j.io.DOMReader
import java.io.File
import java.io.FileOutputStream
import javax.xml.parsers.DocumentBuilderFactory

private const val DIR_OUT = "./"
private const val DIR_INPUT = "./strings/"

fun main(args: Array<String>) {

val dir = File(DIR_INPUT).takeIf { it.exists() && it.isDirectory }
?: error("can not find strings directory")
val files = dir.listFiles().filter { it.name.endsWith(".xml") }
val names = files.map { it.nameWithoutExtension }

val dbf = DocumentBuilderFactory.newInstance()
val db = dbf.newDocumentBuilder()
val list = files.map { db.parse(it) }
.map { DOMReader().read(it) }
.map { it.rootElement }
.map { it.elements() }

val map = mutableMapOf<String, StringTag>()


list.forEachIndexed { index, element ->
val name = names[index]
element.forEach {
val id = it.attributeValue("name")
val tag = map[id]
if (tag == null) {
map[id] = StringTag(id).apply {
values.put(name, it.stringValue)
}
} else {
tag.values.put(name, it.stringValue)
}
}
}
val hssfWorkbook = HSSFWorkbook()
val sheet = hssfWorkbook.createSheet("strings")
val rowHeader = sheet.createRow(0)
rowHeader.createCell(0).setCellValue("id")
names.forEachIndexed { index, title ->
rowHeader.createCell(index + 1).setCellValue(title)
}
var rowNum = 1
map.forEach { key, stringTag ->
val row = sheet.createRow(rowNum)
row.createCell(0).setCellValue(key)
names.forEachIndexed { index, s ->
stringTag.values[s]?.let {
row.createCell(index + 1).setCellValue(it)
}
}
rowNum++
}
val output = File(DIR_OUT, "strings.xls")
if (output.exists()) {
println("detected strings.xls file exists, perform to delete it")
output.delete()
}
val outputStream = FileOutputStream(output)
hssfWorkbook.write(outputStream)
outputStream.close()

println("covert success !")
}

class StringTag(val name: String) {
val values: MutableMap<String, String> = mutableMapOf()
override fun toString(): String {
return "StringTag(name='$name', values=$values)"
}


}

0 comments on commit ab01957

Please sign in to comment.