View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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 "); //$NON-NLS-1$
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("}")) { //$NON-NLS-1$
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")) //$NON-NLS-1$ //$NON-NLS-2$
107                     || line.endsWith(":")) { //$NON-NLS-1$
108                 indentLevel++;
109             }
110 
111             if (line.startsWith("break")) { //$NON-NLS-1$
112                 // if the next line is '}', then don't outdent
113                 if (listIter.hasNext()) {
114                     String nextLine = listIter.next();
115                     if (nextLine.startsWith("}")) { //$NON-NLS-1$
116                         indentLevel++;
117                     }
118 
119                     // set back to the previous element
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 }