Advertisement
Howtocode

Untitled

May 24th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import groovy.transform.Field
  2.  
  3. @Field def dirPath = "D:\\test" //ДИРЕКТОРИЯ С ДЖСОН ФАЙЛАМИ
  4. @Field def outputDir = new File("$dirPath${File.separator}output")
  5.  
  6. def void processJson(File f) {
  7.     def outputFile = new File("$outputDir${File.separator}strings-${f.name.substring(0, f.name.indexOf("."))}")
  8.     def resultingText = new StringBuilder()
  9.     resultingText.append('<resources>').append('\n\t')
  10.     def lines = f.text
  11.  
  12.     def pattern = ~/"ui": \{([^}]+)\}/
  13.     def matcher = pattern.matcher(lines)
  14.     matcher.find()
  15.     resultingText.append(jsonToXML(matcher.group(), ""))
  16.     resultingText.append('\n\t')
  17.  
  18.     pattern = ~/"locations": \{([^{]+)/
  19.     matcher = pattern.matcher(lines)
  20.     matcher.find()
  21.     resultingText.append(jsonToXML(matcher.group(), ""))
  22.  
  23.     pattern = ~/"roles":(?s).+/
  24.     matcher = pattern.matcher(lines)
  25.     matcher.find()
  26.     def roles = matcher.group()
  27.  
  28.     pattern = ~/"([^"]+)": \{([^}]+)/
  29.    matcher = pattern.matcher(roles)
  30.    while (matcher.find()) {
  31.        resultingText.append(jsonToXML(matcher.group(2), "role_${matcher.group(1).replaceAll(" ", "_")}"))
  32.        resultingText.append('\n\t')
  33.    }
  34.  
  35.    resultingText.append('\r</resources>')
  36.    outputFile.write(resultingText.toString())
  37. }
  38.  
  39. private String jsonToXML(String json, String prefix) {
  40.    def itemPattern = ~/"([^"]+)": "([^"]+)"/
  41.    def resultingText = new StringBuilder()
  42.    json.eachLine { line ->
  43.        def itemMatcher = itemPattern.matcher(line)
  44.        if (itemMatcher.find()) {
  45.            def name = "$prefix${itemMatcher.group(1).replaceAll(" ", "_")}"
  46.            resultingText.append("<string name=\"$name\">${itemMatcher.group(2)}</string>").append('\n\t')
  47.         }
  48.     }
  49.     resultingText.toString()
  50. }
  51.  
  52. if (!outputDir.exists()) {
  53.     outputDir.mkdir()
  54. }
  55. def dir = new File(dirPath)
  56. dir.eachFile { f ->
  57.     if (f.name.endsWith(".json")) {
  58.         processJson(f)
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement