Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

StringUtils.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2011 gitblit.com.
  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 com.gitblit.utils;
  17. import java.io.UnsupportedEncodingException;
  18. import java.security.MessageDigest;
  19. import java.security.NoSuchAlgorithmException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.regex.PatternSyntaxException;
  23. public class StringUtils {
  24. public static final String MD5_TYPE = "MD5:";
  25. public static boolean isEmpty(String value) {
  26. return value == null || value.trim().length() == 0;
  27. }
  28. public static String breakLinesForHtml(String string) {
  29. return string.replace("\r\n", "<br/>").replace("\r", "<br/>").replace("\n", "<br/>");
  30. }
  31. public static String escapeForHtml(String inStr, boolean changeSpace) {
  32. StringBuffer retStr = new StringBuffer();
  33. int i = 0;
  34. while (i < inStr.length()) {
  35. if (inStr.charAt(i) == '&') {
  36. retStr.append("&amp;");
  37. } else if (inStr.charAt(i) == '<') {
  38. retStr.append("&lt;");
  39. } else if (inStr.charAt(i) == '>') {
  40. retStr.append("&gt;");
  41. } else if (inStr.charAt(i) == '\"') {
  42. retStr.append("&quot;");
  43. } else if (changeSpace && inStr.charAt(i) == ' ') {
  44. retStr.append("&nbsp;");
  45. } else if (changeSpace && inStr.charAt(i) == '\t') {
  46. retStr.append(" &nbsp; &nbsp;");
  47. } else {
  48. retStr.append(inStr.charAt(i));
  49. }
  50. i++;
  51. }
  52. return retStr.toString();
  53. }
  54. public static String encodeURL(String inStr) {
  55. StringBuffer retStr = new StringBuffer();
  56. int i = 0;
  57. while (i < inStr.length()) {
  58. if (inStr.charAt(i) == '/') {
  59. retStr.append("%2F");
  60. } else if (inStr.charAt(i) == ' ') {
  61. retStr.append("%20");
  62. } else {
  63. retStr.append(inStr.charAt(i));
  64. }
  65. i++;
  66. }
  67. return retStr.toString();
  68. }
  69. public static String flattenStrings(List<String> values) {
  70. return flattenStrings(values, " ");
  71. }
  72. public static String flattenStrings(List<String> values, String separator) {
  73. StringBuilder sb = new StringBuilder();
  74. for (String value : values) {
  75. sb.append(value).append(separator);
  76. }
  77. return sb.toString().trim();
  78. }
  79. public static String trimString(String value, int max) {
  80. if (value.length() <= max) {
  81. return value;
  82. }
  83. return value.substring(0, max - 3) + "...";
  84. }
  85. public static String trimShortLog(String string) {
  86. return trimString(string, 60);
  87. }
  88. public static String leftPad(String input, int length, char pad) {
  89. if (input.length() < length) {
  90. StringBuilder sb = new StringBuilder();
  91. for (int i = 0, len = length - input.length(); i < len; i++) {
  92. sb.append(pad);
  93. }
  94. sb.append(input);
  95. return sb.toString();
  96. }
  97. return input;
  98. }
  99. public static String rightPad(String input, int length, char pad) {
  100. if (input.length() < length) {
  101. StringBuilder sb = new StringBuilder();
  102. sb.append(input);
  103. for (int i = 0, len = length - input.length(); i < len; i++) {
  104. sb.append(pad);
  105. }
  106. return sb.toString();
  107. }
  108. return input;
  109. }
  110. public static String getSHA1(String text) {
  111. try {
  112. byte[] bytes = text.getBytes("iso-8859-1");
  113. return getSHA1(bytes);
  114. } catch (UnsupportedEncodingException u) {
  115. throw new RuntimeException(u);
  116. }
  117. }
  118. public static String getSHA1(byte[] bytes) {
  119. try {
  120. MessageDigest md = MessageDigest.getInstance("SHA-1");
  121. md.update(bytes, 0, bytes.length);
  122. byte[] digest = md.digest();
  123. return toHex(digest);
  124. } catch (NoSuchAlgorithmException t) {
  125. throw new RuntimeException(t);
  126. }
  127. }
  128. public static String getMD5(String string) {
  129. try {
  130. MessageDigest md = MessageDigest.getInstance("MD5");
  131. md.reset();
  132. md.update(string.getBytes("iso-8859-1"));
  133. byte[] digest = md.digest();
  134. return toHex(digest);
  135. } catch (UnsupportedEncodingException u) {
  136. throw new RuntimeException(u);
  137. } catch (NoSuchAlgorithmException t) {
  138. throw new RuntimeException(t);
  139. }
  140. }
  141. private static String toHex(byte[] bytes) {
  142. StringBuilder sb = new StringBuilder(bytes.length * 2);
  143. for (int i = 0; i < bytes.length; i++) {
  144. if (((int) bytes[i] & 0xff) < 0x10) {
  145. sb.append('0');
  146. }
  147. sb.append(Long.toString((int) bytes[i] & 0xff, 16));
  148. }
  149. return sb.toString();
  150. }
  151. public static String getRootPath(String path) {
  152. if (path.indexOf('/') > -1) {
  153. return path.substring(0, path.lastIndexOf('/'));
  154. }
  155. return "";
  156. }
  157. public static String getRelativePath(String basePath, String fullPath) {
  158. String relativePath = fullPath.substring(basePath.length()).replace('\\', '/');
  159. if (relativePath.charAt(0) == '/') {
  160. relativePath = relativePath.substring(1);
  161. }
  162. return relativePath;
  163. }
  164. public static List<String> getStringsFromValue(String value) {
  165. return getStringsFromValue(value, " ");
  166. }
  167. public static List<String> getStringsFromValue(String value, String separator) {
  168. List<String> strings = new ArrayList<String>();
  169. try {
  170. String[] chunks = value.split(separator);
  171. for (String chunk : chunks) {
  172. chunk = chunk.trim();
  173. if (chunk.length() > 0) {
  174. strings.add(chunk);
  175. }
  176. }
  177. } catch (PatternSyntaxException e) {
  178. throw new RuntimeException(e);
  179. }
  180. return strings;
  181. }
  182. }