漂亮的ConfigObject打印?

时间:2011-10-26 02:18:23

标签: java groovy formatting configuration-files

我有这个groovy程序,使用ConfigObject创建一个groovy配置文件。设置ConfigObject后,使用以下命令将其写入文件:

myFile.withWriter {writer -> myConfigObject.writeTo(writer)}

这导致ConfigObject的每个属性都写在一行上。因此,例如地图将打印为:

graphs=[["type":"std", "host":"localhost", "name":"cpurawlinux"], ["type":"std", "host":"localhost", "name":"memory"], ["type":"std", "host":"localhost", "name":"udp"] ... ]

如果有人不得不看一眼,这是非常难以理解的。 有没有办法获得更友好的输出?这样的事情会很棒:

graphs=[
    ["type":"std", "host":"localhost", "name":"cpurawlinux"],
    ["type":"std", "host":"localhost", "name":"memory"],
    ["type":"std", "host":"localhost", "name":"udp"]
    ...
]

我知道我可以创建自己的writeTo,但Groovy中是不是已经有了这个呢?

3 个答案:

答案 0 :(得分:1)

不幸的是,您需要自己编写writeTo

如果您的配置文件的结构如下:

graphs {
  a=["type":"std", "host":"localhost", "name":"cpurawlinux"]
  b=["type":"std", "host":"localhost", "name":"memory"]
}

然后writeTo会用结构写出来,但是如果你的配置文件只是一个很旧的东西列表,它会把它写成一个很大的旧列表

答案 1 :(得分:1)

如果它对任何人都有帮助,我有同样的问题并且写了这个...不漂亮(哈哈),但是有效:

def prettyPrint(properties, level=1, stringBuilder = new StringBuilder()) {
    return properties.inject(stringBuilder) { sb, name, value ->
        sb.append("\n").append("\t" * level).append(name)
        if (!(value instanceof Map) && !(value instanceof List)) {
            return sb.append("=").append(value)
        } else {
            return prettyPrint(properties.getProperty(name), level+1, sb)
        }
    }
}

答案 2 :(得分:1)

基于迈克的答案:

def prettyPrint
prettyPrint = {obj, level = 0, sb = new StringBuilder() ->
    def indent = { lev -> sb.append("  " * lev) }
    if(obj instanceof Map){
        sb.append("{\n")
        obj.each{ name, value ->
            if(name.contains('.')) return // skip keys like "a.b.c", which are redundant
            indent(level+1).append(name)
            (value instanceof Map) ? sb.append(" ") : sb.append(" = ")
            prettyPrint(value, level+1, sb)
            sb.append("\n")
        }
        indent(level).append("}")
    }
    else if(obj instanceof List){
        sb.append("[\n")
        obj.each{ value ->
            indent(level+1)
            prettyPrint(value, level+1, sb).append(",")
            sb.append("\n")
        }
        indent(level).append("]")
    }
    else if(obj instanceof String){
        sb.append('"').append(obj).append('"')
    }
    else {
        sb.append(obj)
    }
}

输入如下:

{
  grails {
    scaffolding {
      templates.domainSuffix = "Instance"
    }
    enable {
      native2ascii = true
      blah = [ 1, 2, 3 ]
    }
    mime.disable.accept.header.userAgents = [ "WebKit", "Presto", "Trident" ]
  }
}

产地:

{
  grails {
    scaffolding {
      templates {
        domainSuffix = "Instance"
      }
    }
    enable {
      native2ascii = true
      blah = [
        1,
        2,
        3,
      ]
    }
    mime {
      disable {
        accept {
          header {
            userAgents = [
              "WebKit",
              "Presto",
              "Trident",
            ]
          }
        }
      }
    }
  }
}
相关问题