解析 Cisco 路由器配置块

Parsing Cisco router configuration blocks

我正在尝试解析思科配置。它具有如下所示的缩进结构。有很多这样的块。有些块可以更长。 ! 符号表示上述缩进结束。如何解析块下的块和子块?

最简单的方法是将块数据存储在地图中:

class Block {
    private Map<String, String> data;
    private Block parent;
    private List<Block> children;
}

另外,您可以创建另一个数据结构来满足您的需要/解决您手头的任务。

然后,逐行读取文件,通过跟踪缩进数来检测是否需要新块,并填充上面的数据结构。

快速而肮脏的示例(免责声明:我没有 运行 这段代码,那只是为了让你开始,你应该添加验证,将它拆分成多个函数等,以便它干净,正确,很高兴阅读):

    try (BufferedReader bufferedReader = new BufferedReader(reader)) {
        Block block = new Block();
        String line, previousIndents = "";
        while (null != (line = bufferedReader.readLine()) {
            Matcher m = Pattern.compile("^(\s+)").matcher(line);
            if (m.find()) {
                String indents = m.group(1);
                if (previousIndents.equals(indents)) {
                     // update current block
                } else if (indents.length() > previousIndents.length()) {
                     // start a new block
                     Block newBlock = new Block();
                     newBlock.setParent(block);
                     block.getChildren().add(newBlock);

                     block = newBlock;
                } else {
                     // current block finished, return to parent
                     block = block.getParent();
                }

                previousIndents = indents;
            }
        } 
    }
    catch (IOException ioEx) {
        throw new ParseException(ioEx);
    }