1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.api.dom.java;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.ListIterator;
22
23 import org.mybatis.generator.api.dom.OutputUtilities;
24
25 public class InitializationBlock {
26
27 private boolean isStatic;
28 private List<String> bodyLines;
29 private List<String> javaDocLines;
30
31 public InitializationBlock() {
32 this(false);
33 }
34
35 public InitializationBlock(boolean isStatic) {
36 this.isStatic = isStatic;
37 bodyLines = new ArrayList<String>();
38 javaDocLines = new ArrayList<String>();
39 }
40
41 public boolean isStatic() {
42 return isStatic;
43 }
44
45 public void setStatic(boolean isStatic) {
46 this.isStatic = isStatic;
47 }
48
49 public List<String> getBodyLines() {
50 return bodyLines;
51 }
52
53 public void addBodyLine(String line) {
54 bodyLines.add(line);
55 }
56
57 public void addBodyLine(int index, String line) {
58 bodyLines.add(index, line);
59 }
60
61 public void addBodyLines(Collection<String> lines) {
62 bodyLines.addAll(lines);
63 }
64
65 public void addBodyLines(int index, Collection<String> lines) {
66 bodyLines.addAll(index, lines);
67 }
68
69 public List<String> getJavaDocLines() {
70 return javaDocLines;
71 }
72
73 public void addJavaDocLine(String javaDocLine) {
74 javaDocLines.add(javaDocLine);
75 }
76
77 public String getFormattedContent(int indentLevel) {
78 StringBuilder sb = new StringBuilder();
79
80 for (String javaDocLine : javaDocLines) {
81 OutputUtilities.javaIndent(sb, indentLevel);
82 sb.append(javaDocLine);
83 OutputUtilities.newLine(sb);
84 }
85
86 OutputUtilities.javaIndent(sb, indentLevel);
87
88 if (isStatic) {
89 sb.append("static ");
90 }
91
92 sb.append('{');
93 indentLevel++;
94
95 ListIterator<String> listIter = bodyLines.listIterator();
96 while (listIter.hasNext()) {
97 String line = listIter.next();
98 if (line.startsWith("}")) {
99 indentLevel--;
100 }
101
102 OutputUtilities.newLine(sb);
103 OutputUtilities.javaIndent(sb, indentLevel);
104 sb.append(line);
105
106 if ((line.endsWith("{") && !line.startsWith("switch"))
107 || line.endsWith(":")) {
108 indentLevel++;
109 }
110
111 if (line.startsWith("break")) {
112
113 if (listIter.hasNext()) {
114 String nextLine = listIter.next();
115 if (nextLine.startsWith("}")) {
116 indentLevel++;
117 }
118
119
120 listIter.previous();
121 }
122 indentLevel--;
123 }
124 }
125
126 indentLevel--;
127 OutputUtilities.newLine(sb);
128 OutputUtilities.javaIndent(sb, indentLevel);
129 sb.append('}');
130
131 return sb.toString();
132 }
133 }