Merge "Distributed Datastore integration with config subsystem Updated with the usage...
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / writer / OutFormatter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.netconf.cli.writer;
9
10 public class OutFormatter {
11
12     public static final String INDENT_STEP = "  ";
13     public static final String COMPOSITE_OPEN_NODE = " {";
14     public static final String COMPOSITE_CLOSE_NODE = "}";
15     public static final String NEW_LINE = "\n";
16
17     int indentLevel = -1;
18     private String currentIndent = "";
19
20     public OutFormatter indent(final StringBuilder buffer) {
21         buffer.append(currentIndent);
22         return this;
23     }
24
25     public OutFormatter openComposite(final StringBuilder buffer) {
26         buffer.append(COMPOSITE_OPEN_NODE);
27         return this;
28     }
29
30     public OutFormatter closeCompositeWithIndent(final StringBuilder buffer) {
31         buffer.append(currentIndent);
32         buffer.append(COMPOSITE_CLOSE_NODE);
33         return this;
34     }
35
36     public OutFormatter newLine(final StringBuilder buffer) {
37         buffer.append(NEW_LINE);
38         return this;
39     }
40
41     private void prepareIndent() {
42         final StringBuilder output = new StringBuilder();
43         for (int i = 0; i < indentLevel; i++) {
44             output.append(INDENT_STEP);
45         }
46         currentIndent = output.toString();
47     }
48
49     public OutFormatter increaseIndent() {
50         indentLevel++;
51         prepareIndent();
52         return this;
53     }
54
55     public OutFormatter decreaseIndent() {
56         indentLevel--;
57         prepareIndent();
58         return this;
59     }
60
61     public OutFormatter addStringWithIndent(final StringBuilder buffer, final String value) {
62         indent(buffer);
63         buffer.append(value);
64         return this;
65     }
66 }