Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
缩进Java输出_Java_Indentation_Graphviz - Fatal编程技术网

缩进Java输出

缩进Java输出,java,indentation,graphviz,Java,Indentation,Graphviz,我正在一个Java程序中生成一个graphviz点文件(下面是一个类似的示例:)。我想根据大括号自动缩进此文件。在Java中有没有一种简单的方法可以做到这一点?谢谢 我想这个问题有多种解决方案。根据需求,您可能需要一个elegent解决方案,它可以让您轻松地为最终输出收集模板。然后,您需要了解graphviz文件格式,并可能将代码块标识为节点。然后工作就简单了:输出深度为I的节点(我是指代表节点的代码),缩进=I*4个空格 由于我不知道graphviz文件,我可能会简单地将其视为纯文本文件。 所

我正在一个Java程序中生成一个graphviz点文件(下面是一个类似的示例:)。我想根据大括号自动缩进此文件。在Java中有没有一种简单的方法可以做到这一点?谢谢

我想这个问题有多种解决方案。根据需求,您可能需要一个elegent解决方案,它可以让您轻松地为最终输出收集模板。然后,您需要了解graphviz文件格式,并可能将代码块标识为节点。然后工作就简单了:输出深度为I的节点(我是指代表节点的代码),缩进=I*4个空格

由于我不知道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.

由于我可能无法正确理解您的等式,伪代码在函数方面可能是无用的。但你有这个想法;-)

像这样的嵌套上下文实际上最好用堆栈来表示,但是你可以通过计数来实现这种解析的廉价版本——它不是真正的“正确”,因为它不是一个完整的解析器(一方面,它不考虑注释,可能还有其他一些方法,比如一个包含括号的名称),但足够一次性使用:

伪代码

int indent=0;
for (line):
    print ('\t' for each indent) + line
    if (line.contains('{'))indent++
    if (line.contains('}')} indent --;
如果在示例输出显示时括号中的行尚未断开,请通过在换行符“{”或“}”上断开输入来迭代这些行