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.internal.util;
17  
18  import java.util.StringTokenizer;
19  
20  /**
21   * 
22   * @author Jeff Butler
23   */
24  public class StringUtility {
25  
26      /**
27       * Utility class. No instances allowed
28       */
29      private StringUtility() {
30          super();
31      }
32  
33      public static boolean stringHasValue(String s) {
34          return s != null && s.length() > 0;
35      }
36  
37      public static String composeFullyQualifiedTableName(String catalog,
38              String schema, String tableName, char separator) {
39          StringBuilder sb = new StringBuilder();
40  
41          if (stringHasValue(catalog)) {
42              sb.append(catalog);
43              sb.append(separator);
44          }
45  
46          if (stringHasValue(schema)) {
47              sb.append(schema);
48              sb.append(separator);
49          } else {
50              if (sb.length() > 0) {
51                  sb.append(separator);
52              }
53          }
54  
55          sb.append(tableName);
56  
57          return sb.toString();
58      }
59  
60      public static boolean stringContainsSpace(String s) {
61          return s != null && s.indexOf(' ') != -1;
62      }
63  
64      public static String escapeStringForJava(String s) {
65          StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
66          StringBuilder sb = new StringBuilder();
67          while (st.hasMoreTokens()) {
68              String token = st.nextToken();
69              if ("\"".equals(token)) { //$NON-NLS-1$
70                  sb.append("\\\""); //$NON-NLS-1$
71              } else {
72                  sb.append(token);
73              }
74          }
75  
76          return sb.toString();
77      }
78  
79      public static String escapeStringForXml(String s) {
80          StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
81          StringBuilder sb = new StringBuilder();
82          while (st.hasMoreTokens()) {
83              String token = st.nextToken();
84              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 }