Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import groovy.transform.Field
- @Field def dirPath = "D:\\test" //ДИРЕКТОРИЯ С ДЖСОН ФАЙЛАМИ
- @Field def outputDir = new File("$dirPath${File.separator}output")
- def void processJson(File f) {
- def outputFile = new File("$outputDir${File.separator}strings-${f.name.substring(0, f.name.indexOf("."))}")
- def resultingText = new StringBuilder()
- resultingText.append('<resources>').append('\n\t')
- def lines = f.text
- def pattern = ~/"ui": \{([^}]+)\}/
- def matcher = pattern.matcher(lines)
- matcher.find()
- resultingText.append(jsonToXML(matcher.group(), ""))
- resultingText.append('\n\t')
- pattern = ~/"locations": \{([^{]+)/
- matcher = pattern.matcher(lines)
- matcher.find()
- resultingText.append(jsonToXML(matcher.group(), ""))
- pattern = ~/"roles":(?s).+/
- matcher = pattern.matcher(lines)
- matcher.find()
- def roles = matcher.group()
- pattern = ~/"([^"]+)": \{([^}]+)/
- matcher = pattern.matcher(roles)
- while (matcher.find()) {
- resultingText.append(jsonToXML(matcher.group(2), "role_${matcher.group(1).replaceAll(" ", "_")}"))
- resultingText.append('\n\t')
- }
- resultingText.append('\r</resources>')
- outputFile.write(resultingText.toString())
- }
- private String jsonToXML(String json, String prefix) {
- def itemPattern = ~/"([^"]+)": "([^"]+)"/
- def resultingText = new StringBuilder()
- json.eachLine { line ->
- def itemMatcher = itemPattern.matcher(line)
- if (itemMatcher.find()) {
- def name = "$prefix${itemMatcher.group(1).replaceAll(" ", "_")}"
- resultingText.append("<string name=\"$name\">${itemMatcher.group(2)}</string>").append('\n\t')
- }
- }
- resultingText.toString()
- }
- if (!outputDir.exists()) {
- outputDir.mkdir()
- }
- def dir = new File(dirPath)
- dir.eachFile { f ->
- if (f.name.endsWith(".json")) {
- processJson(f)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement