English 中文(简体)
粗略印刷
原标题:Pretty print for a groovy ConfigObject?

我有这项农艺方案,它使用一个提纲,制作一个农业组合文件。 一旦立案,就用下列文字写成:

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

结果是,根据单一行文撰写了结晶。 例如,地图将印成:

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

which is quite unreadable if someone has to take a look at it. Is there a way to get a more friendly output? Something like that would be great:

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

我知道我可以创建自己的<条码>writeTo,但是在格罗维瓦里,没有哪儿?

最佳回答

不幸的是,你需要写自己的<条码>。 请注意。

如果你拥有像以下结构那样的组合文件:

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

然后是: 将用结构书写,但如果您的公文档案只是一份老大事清单,则将把它写成一个老大的清单。

问题回答

如果它帮助任何人,即有同样的问题,并写这......不是(ha),而是:

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

Based on mike s answer above:

def prettyPrint
prettyPrint = {obj, level = 0, sb = new StringBuilder() ->
    def indent = { lev -> sb.append("  " * lev) }
    if(obj instanceof Map){
        sb.append("{
")
        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("
")
        }
        indent(level).append("}")
    }
    else if(obj instanceof List){
        sb.append("[
")
        obj.each{ value ->
            indent(level+1)
            prettyPrint(value, level+1, sb).append(",")
            sb.append("
")
        }
        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",
            ]
          }
        }
      }
    }
  }
}

Since GreenGiant answer seems broken when I tried using it, here s a working version for future reference :

static String prettify(obj, level = 0, StringBuilder sb = new StringBuilder()) {
    def indent = { lev -> sb.append("  " * lev) }
    if (!obj) return sb
    if(obj instanceof Map<String, ?>){
        sb.append("{
")
        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(" = ")
            prettify(value, level+1, sb)
            sb.append("
")
        }
        indent(level).append("}")
    } else if(obj instanceof List){
        sb.append("[
")
        obj.each{ value ->
            indent(level+1)
            prettify(value, level+1, sb).append(",")
            sb.append("
")
        }
        indent(level).append("]")
    } else if(obj instanceof String){
        sb.append( " ).append(obj).append( " )
    } else {
        sb.append(obj)
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签