View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.internal.util;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  import java.util.StringTokenizer;
21  
22  public class StringUtility {
23  
24      /**
25       * Utility class. No instances allowed
26       */
27      private StringUtility() {
28          super();
29      }
30  
31      public static boolean stringHasValue(String s) {
32          return s != null && s.length() > 0;
33      }
34  
35      public static String composeFullyQualifiedTableName(String catalog,
36              String schema, String tableName, char separator) {
37          StringBuilder sb = new StringBuilder();
38  
39          if (stringHasValue(catalog)) {
40              sb.append(catalog);
41              sb.append(separator);
42          }
43  
44          if (stringHasValue(schema)) {
45              sb.append(schema);
46              sb.append(separator);
47          } else {
48              if (sb.length() > 0) {
49                  sb.append(separator);
50              }
51          }
52  
53          sb.append(tableName);
54  
55          return sb.toString();
56      }
57  
58      public static boolean stringContainsSpace(String s) {
59          return s != null && s.indexOf(' ') != -1;
60      }
61  
62      public static String escapeStringForJava(String s) {
63          StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
64          StringBuilder sb = new StringBuilder();
65          while (st.hasMoreTokens()) {
66              String token = st.nextToken();
67              if ("\"".equals(token)) { //$NON-NLS-1$
68                  sb.append("\\\""); //$NON-NLS-1$
69              } else {
70                  sb.append(token);
71              }
72          }
73  
74          return sb.toString();
75      }
76  
77      public static String escapeStringForKotlin(String s) {
78          StringTokenizer st = new StringTokenizer(s, "\"$", true); //$NON-NLS-1$
79          StringBuilder sb = new StringBuilder();
80          while (st.hasMoreTokens()) {
81              String token = st.nextToken();
82              if ("\"".equals(token)) { //$NON-NLS-1$
83                  sb.append("\\\""); //$NON-NLS-1$
84              } else if ("$".equals(token)) { //$NON-NLS-1$
85                  sb.append("\\$"); //$NON-NLS-1$
86              } else {
87                  sb.append(token);
88              }
89          }
90  
91          return sb.toString();
92      }
93  
94      public static boolean isTrue(String s) {
95          return "true".equalsIgnoreCase(s); //$NON-NLS-1$
96      }
97  
98      public static boolean stringContainsSQLWildcard(String s) {
99          if (s == null) {
100             return false;
101         }
102 
103         return s.indexOf('%') != -1 || s.indexOf('_') != -1;
104     }
105 
106     /**
107      * Given an input string, tokenize on the commas and trim all token. Returns an empty set if the input string is
108      * null.
109      *
110      * @param in
111      *            strong to tokenize.
112      *
113      * @return Set of tokens
114      */
115     public static Set<String> tokenize(String in) {
116         Set<String> answer = new HashSet<>();
117         if (StringUtility.stringHasValue(in)) {
118             StringTokenizer st = new StringTokenizer(in, ","); //$NON-NLS-1$
119             while (st.hasMoreTokens()) {
120                 String s = st.nextToken().trim();
121                 if (s.length() > 0) {
122                     answer.add(s);
123                 }
124             }
125         }
126         return answer;
127     }
128 
129 }