Printing.jrag 2.8 KB
Newer Older
René Schöne's avatar
*  
René Schöne committed
1
2
3
aspect Printing {
  String ASTNode.PRINT_INDENT = "  ";

4
5
6
7
8
9
10
11
  inh boolean KeyValuePair.isLast();
  inh boolean Element.isLast();
  eq MappingElement.getKeyValuePair(int i).isLast() = i == getNumKeyValuePair() - 1;
  eq ListElement.getElement(int i).isLast() = i == getNumElement() - 1;
  eq Document.getRootElement().isLast() = true;
  syn boolean KeyValuePair.needTrailingNewLine() = !this.isLast();
  syn boolean Element.needTrailingNewLine() = !this.isLast() || containingListElement() == null;

12
13
  public String Document.prettyPrint() {
    return prettyPrint(true);
René Schöne's avatar
*  
René Schöne committed
14
15
  }

16
  public String Document.prettyPrint(boolean prependCreationComment) {
René Schöne's avatar
*  
René Schöne committed
17
    StringBuilder sb = new StringBuilder();
18
    if (prependCreationComment) {
19
      sb.append("# Created at ").append(java.time.Instant.now()).append("\n");
René Schöne's avatar
*  
René Schöne committed
20
    }
21
22
23
24
    if (hasRootElement()) {
      getRootElement().prettyPrint(sb, false, "");
    }
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '\n') {
René Schöne's avatar
*  
René Schöne committed
25
26
27
28
      sb.append("\n");
    }
    return sb.toString();
  }
29
30
31
32
33

  abstract protected StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);

  @Override
  protected StringBuilder ValueElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
René Schöne's avatar
*  
René Schöne committed
34
35
36
    sb.append(getValue());
    return sb;
  }
37
38
39

  @Override
  protected StringBuilder StringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
René Schöne's avatar
*  
René Schöne committed
40
41
42
    sb.append("\"").append(getValue()).append("\"");
    return sb;
  }
43
44
45

  @Override
  protected StringBuilder ListElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
René Schöne's avatar
*  
René Schöne committed
46
47
48
49
50
51
    if (isEmpty()) {
      sb.append("[]");
    } else {
      for (Element element : getElementList()) {
        sb.append(indent).append("- ");
        element.prettyPrint(sb, false, indent + PRINT_INDENT);
52
53
54
        if (element.needTrailingNewLine()) {
          sb.append("\n");
        }
René Schöne's avatar
*  
René Schöne committed
55
56
57
58
      }
    }
    return sb;
  }
59
60

  protected StringBuilder KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
René Schöne's avatar
*  
René Schöne committed
61
    if (printIndent) sb.append(indent);
62
    sb.append(getKey()).append(":");
René Schöne's avatar
*  
René Schöne committed
63
64
    if (getValue().isComplex() && !getValue().isEmpty()) {
      sb.append("\n");
65
      getValue().prettyPrint(sb, true, indent + PRINT_INDENT);
René Schöne's avatar
*  
René Schöne committed
66
    } else {
67
      sb.append(" ");
René Schöne's avatar
*  
René Schöne committed
68
69
70
71
      getValue().prettyPrint(sb, false, indent);
    }
    return sb;
  }
72
73
74

  @Override
  protected StringBuilder MappingElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
René Schöne's avatar
*  
René Schöne committed
75
76
77
78
79
80
81
    if (isEmpty()) {
      sb.append("{}");
    } else {
      boolean first = true;
      for (KeyValuePair pair : getKeyValuePairList()) {
        if (!first || printIndent) sb.append(indent);
        first = false;
82
83
84
85
        pair.prettyPrint(sb, false, indent);
        if (pair.needTrailingNewLine()) {
          sb.append("\n");
        }
René Schöne's avatar
*  
René Schöne committed
86
87
88
89
90
      }
    }
    return sb;
  }
}