This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
=
committed
Nov 14, 2017
0 parents
commit ab01957
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: tech.summerly.MainKt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
|
||
|
||
} |