You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.util;
  18. import java.io.UnsupportedEncodingException;
  19. import java.security.MessageDigest;
  20. import java.security.NoSuchAlgorithmException;
  21. import java.util.ArrayList;
  22. /**
  23. * Common string utilities.
  24. *
  25. */
  26. public class StringUtils {
  27. /**
  28. * Replace all occurrences of the before string with the after string.
  29. *
  30. * @param s
  31. * the string
  32. * @param before
  33. * the old text
  34. * @param after
  35. * the new text
  36. * @return the string with the before string replaced
  37. */
  38. public static String replaceAll(String s, String before, String after) {
  39. int next = s.indexOf(before);
  40. if (next < 0) {
  41. return s;
  42. }
  43. StringBuilder buff = new StringBuilder(s.length() - before.length() + after.length());
  44. int index = 0;
  45. while (true) {
  46. buff.append(s.substring(index, next)).append(after);
  47. index = next + before.length();
  48. next = s.indexOf(before, index);
  49. if (next < 0) {
  50. buff.append(s.substring(index));
  51. break;
  52. }
  53. }
  54. return buff.toString();
  55. }
  56. /**
  57. * Check if a String is null or empty (the length is null).
  58. *
  59. * @param s
  60. * the string to check
  61. * @return true if it is null or empty
  62. */
  63. public static boolean isNullOrEmpty(String s) {
  64. return s == null || s.length() == 0;
  65. }
  66. /**
  67. * Convert a string to a Java literal using the correct escape sequences.
  68. * The literal is not enclosed in double quotes. The result can be used in
  69. * properties files or in Java source code.
  70. *
  71. * @param s
  72. * the text to convert
  73. * @return the Java representation
  74. */
  75. public static String javaEncode(String s) {
  76. int length = s.length();
  77. StringBuilder buff = new StringBuilder(length);
  78. for (int i = 0; i < length; i++) {
  79. char c = s.charAt(i);
  80. switch (c) {
  81. // case '\b':
  82. // // BS backspace
  83. // // not supported in properties files
  84. // buff.append("\\b");
  85. // break;
  86. case '\t':
  87. // HT horizontal tab
  88. buff.append("\\t");
  89. break;
  90. case '\n':
  91. // LF linefeed
  92. buff.append("\\n");
  93. break;
  94. case '\f':
  95. // FF form feed
  96. buff.append("\\f");
  97. break;
  98. case '\r':
  99. // CR carriage return
  100. buff.append("\\r");
  101. break;
  102. case '"':
  103. // double quote
  104. buff.append("\\\"");
  105. break;
  106. case '\\':
  107. // backslash
  108. buff.append("\\\\");
  109. break;
  110. default:
  111. int ch = c & 0xffff;
  112. if (ch >= ' ' && (ch < 0x80)) {
  113. buff.append(c);
  114. // not supported in properties files
  115. // } else if(ch < 0xff) {
  116. // buff.append("\\");
  117. // // make sure it's three characters (0x200 is octal 1000)
  118. // buff.append(Integer.toOctalString(0x200 |
  119. // ch).substring(1));
  120. } else {
  121. buff.append("\\u");
  122. // make sure it's four characters
  123. buff.append(Integer.toHexString(0x10000 | ch).substring(1));
  124. }
  125. }
  126. }
  127. return buff.toString();
  128. }
  129. /**
  130. * Pad a string. This method is used for the SQL function RPAD and LPAD.
  131. *
  132. * @param string
  133. * the original string
  134. * @param n
  135. * the target length
  136. * @param padding
  137. * the padding string
  138. * @param right
  139. * true if the padding should be appended at the end
  140. * @return the padded string
  141. */
  142. public static String pad(String string, int n, String padding, boolean right) {
  143. if (n < 0) {
  144. n = 0;
  145. }
  146. if (n < string.length()) {
  147. return string.substring(0, n);
  148. } else if (n == string.length()) {
  149. return string;
  150. }
  151. char paddingChar;
  152. if (padding == null || padding.length() == 0) {
  153. paddingChar = ' ';
  154. } else {
  155. paddingChar = padding.charAt(0);
  156. }
  157. StringBuilder buff = new StringBuilder(n);
  158. n -= string.length();
  159. if (right) {
  160. buff.append(string);
  161. }
  162. for (int i = 0; i < n; i++) {
  163. buff.append(paddingChar);
  164. }
  165. if (!right) {
  166. buff.append(string);
  167. }
  168. return buff.toString();
  169. }
  170. /**
  171. * Convert a string to a SQL literal. Null is converted to NULL. The text is
  172. * enclosed in single quotes. If there are any special characters, the
  173. * method STRINGDECODE is used.
  174. *
  175. * @param s
  176. * the text to convert.
  177. * @return the SQL literal
  178. */
  179. public static String quoteStringSQL(String s) {
  180. if (s == null) {
  181. return "NULL";
  182. }
  183. int length = s.length();
  184. StringBuilder buff = new StringBuilder(length + 2);
  185. buff.append('\'');
  186. for (int i = 0; i < length; i++) {
  187. char c = s.charAt(i);
  188. if (c == '\'') {
  189. buff.append(c);
  190. } else if (c < ' ' || c > 127) {
  191. // need to start from the beginning because maybe there was a \
  192. // that was not quoted
  193. return "STRINGDECODE(" + quoteStringSQL(javaEncode(s)) + ")";
  194. }
  195. buff.append(c);
  196. }
  197. buff.append('\'');
  198. return buff.toString();
  199. }
  200. /**
  201. * Split a string into an array of strings using the given separator. A null
  202. * string will result in a null array, and an empty string in a zero element
  203. * array.
  204. *
  205. * @param s
  206. * the string to split
  207. * @param separatorChar
  208. * the separator character
  209. * @param trim
  210. * whether each element should be trimmed
  211. * @return the array list
  212. */
  213. public static String[] arraySplit(String s, char separatorChar, boolean trim) {
  214. if (s == null) {
  215. return null;
  216. }
  217. int length = s.length();
  218. if (length == 0) {
  219. return new String[0];
  220. }
  221. ArrayList<String> list = Utils.newArrayList();
  222. StringBuilder buff = new StringBuilder(length);
  223. for (int i = 0; i < length; i++) {
  224. char c = s.charAt(i);
  225. if (c == separatorChar) {
  226. String e = buff.toString();
  227. list.add(trim ? e.trim() : e);
  228. buff.setLength(0);
  229. } else if (c == '\\' && i < length - 1) {
  230. buff.append(s.charAt(++i));
  231. } else {
  232. buff.append(c);
  233. }
  234. }
  235. String e = buff.toString();
  236. list.add(trim ? e.trim() : e);
  237. String[] array = new String[list.size()];
  238. list.toArray(array);
  239. return array;
  240. }
  241. /**
  242. * Calculates the SHA1 of the string.
  243. *
  244. * @param text
  245. * @return sha1 of the string
  246. */
  247. public static String calculateSHA1(String text) {
  248. try {
  249. byte[] bytes = text.getBytes("iso-8859-1");
  250. return calculateSHA1(bytes);
  251. } catch (UnsupportedEncodingException u) {
  252. throw new RuntimeException(u);
  253. }
  254. }
  255. /**
  256. * Calculates the SHA1 of the byte array.
  257. *
  258. * @param bytes
  259. * @return sha1 of the byte array
  260. */
  261. public static String calculateSHA1(byte[] bytes) {
  262. try {
  263. MessageDigest md = MessageDigest.getInstance("SHA-1");
  264. md.update(bytes, 0, bytes.length);
  265. byte[] digest = md.digest();
  266. StringBuilder sb = new StringBuilder(digest.length * 2);
  267. for (int i = 0; i < digest.length; i++) {
  268. if (((int) digest[i] & 0xff) < 0x10) {
  269. sb.append('0');
  270. }
  271. sb.append(Integer.toHexString((int) digest[i] & 0xff));
  272. }
  273. return sb.toString();
  274. } catch (NoSuchAlgorithmException t) {
  275. throw new RuntimeException(t);
  276. }
  277. }
  278. /**
  279. * Counts the occurrences of char c in the given string.
  280. *
  281. * @param c
  282. * the character to count
  283. * @param value
  284. * the source string
  285. * @return the count of c in value
  286. */
  287. public static int count(char c, String value) {
  288. int count = 0;
  289. for (char cv : value.toCharArray()) {
  290. if (cv == c) {
  291. count++;
  292. }
  293. }
  294. return count;
  295. }
  296. }