缩进 Java 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3137058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:30:52  来源:igfitidea点击:

Indenting Java Output

javaindentationgraphviz

提问by meteoritepanama

I am generating a graphviz DOT file in a Java program (here is a example of what one looks like: http://www.graphviz.org/Gallery/directed/cluster.html). I want to automatically indent this file based on the braces. Is there an easy way to do this in Java? Thanks!

我在 Java 程序中生成了一个 graphviz DOT 文件(这是一个示例:http: //www.graphviz.org/Gallery/directed/cluster.html)。我想根据大括号自动缩进这个文件。有没有一种简单的方法可以在 Java 中做到这一点?谢谢!

采纳答案by Steve B.

Nested contexts like this are really best expressed by stacks, but you can do a cheap version of this kind of parsing just by counting - it's not really "correct" in that it's not a full parser (for one thing, this doesn't take comments into account, and there's probably a few other ways it could break, like a name that includes a bracket), but good enough for a one-off:

像这样的嵌套上下文真的最好用堆栈来表达,但是你可以通过计数来做这种解析的廉价版本——它不是真正“正确”的,因为它不是一个完整的解析器(一方面,这不需要考虑到评论,并且可能还有其他一些方法可能会破坏它,例如包含括号的名称),但足以一次性使用:

psuedocode

伪代码

int indent=0;
for (line):
    print ('\t' for each indent) + line
    if (line.contains('{'))indent++
    if (line.contains('}')} indent --;

If the lines are not already broken at the brackets as your sample output displays, iterate the lines by breaking the input on newlines, '{', or '}'.

如果示例输出显示的括号中的行尚未断开,请通过在换行符、'{' 或 '}' 上断开输入来迭代这些行。

回答by Jason Wang

I suppose there are various solutions for this question. Deponding on the requirement, you may want an elegent solution which may allow you to easily cumstimaze a template for the final output. Then you need to understand the graphviz file format and possibly identify code by blocks as nodes. Then the job is easy: output nodes(I mean the code reprenting the node) of depth i with indents = i*4 spaces.

我想这个问题有多种解决方案。根据要求,您可能需要一个优雅的解决方案,它可以让您轻松地为最终输出制作模板。然后你需要了解 graphviz 文件格式,并可能通过块作为节点来识别代码。然后工作很简单:输出节点(我的意思是代表节点的代码),缩进数为 i*4 空格。

Since I have no knowledge about the graphviz file, I probaly will simply treat it as a plain text file. So all you need to do is

由于我对 graphviz 文件一无所知,因此我可能会简单地将其视为纯文本文件。所以你需要做的就是

  1. open the graph file, 
  2. create a temp file for the final output
  3. set indent = 0.
  4. read one line, if not null, search the line for braces, for every opening brace, ++indent
     and --indent for every closing brace.
  NOTE:you need to escape the escaped braces if there is any. say "{" or \{


  5. write the line to the temp file with preceding spaces = 4*indent (assuming you want 4 spaces for every indent)
  6. close both files. if needed, replace the old file with the temp file.

As I probably do not understand your equestion correctly, the pseudocode could be useless functionwise. But you got the idea ;-)

由于我可能没有正确理解您的问题,因此伪代码在功能上可能毫无用处。但你明白了;-)