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;
17  
18  import java.util.Set;
19  import java.util.TreeSet;
20  
21  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
22  
23  /**
24   * The Class OutputUtilities.
25   *
26   * @author Jeff Butler
27   */
28  public class OutputUtilities {
29      
30      /** The Constant lineSeparator. */
31      private static final String lineSeparator;
32  
33      static {
34          String ls = System.getProperty("line.separator"); //$NON-NLS-1$
35          if (ls == null) {
36              ls = "\n"; //$NON-NLS-1$
37          }
38          lineSeparator = ls;
39      }
40  
41      /**
42       * Utility class - no instances allowed.
43       */
44      private OutputUtilities() {
45          super();
46      }
47  
48      /**
49       * Utility method that indents the buffer by the default amount for Java
50       * (four spaces per indent level).
51       * 
52       * @param sb
53       *            a StringBuilder to append to
54       * @param indentLevel
55       *            the required indent level
56       */
57      public static void javaIndent(StringBuilder sb, int indentLevel) {
58          for (int i = 0; i < indentLevel; i++) {
59              sb.append("    "); //$NON-NLS-1$
60          }
61      }
62  
63      /**
64       * Utility method that indents the buffer by the default amount for XML (two
65       * spaces per indent level).
66       * 
67       * @param sb
68       *            a StringBuilder to append to
69       * @param indentLevel
70       *            the required indent level
71       */
72      public static void xmlIndent(StringBuilder sb, int indentLevel) {
73          for (int i = 0; i < indentLevel; i++) {
74              sb.append("    "); //$NON-NLS-1$
75          }
76      }
77  
78      /**
79       * Utility method. Adds a newline character to a StringBuilder.
80       * 
81       * @param sb
82       *            the StringBuilder to be appended to
83       */
84      public static void newLine(StringBuilder sb) {
85          sb.append(lineSeparator);
86      }
87  
88      /**
89       * returns a unique set of "import xxx;" Strings for the set of types.
90       *
91       * @param importedTypes
92       *            the imported types
93       * @return the sets the
94       */
95      public static Set<String> calculateImports(
96              Set<FullyQualifiedJavaType> importedTypes) {
97          StringBuilder sb = new StringBuilder();
98          Set<String> importStrings = new TreeSet<String>();
99          for (FullyQualifiedJavaType fqjt : importedTypes) {
100             for (String importString : fqjt.getImportList()) {
101                 sb.setLength(0);
102                 sb.append("import "); //$NON-NLS-1$
103                 sb.append(importString);
104                 sb.append(';');
105                 importStrings.add(sb.toString());
106             }
107         }
108 
109         return importStrings;
110     }
111 }