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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.List;
  21. public class StringUtils {
  22. public static boolean isEmpty(String value) {
  23. return value == null || value.trim().length() == 0;
  24. }
  25. public static String breakLinesForHtml(String string) {
  26. return string.replace("\r\n", "<br/>").replace("\r", "<br/>").replace("\n", "<br/>");
  27. }
  28. public static String escapeForHtml(String inStr, boolean changeSpace) {
  29. StringBuffer retStr = new StringBuffer();
  30. int i = 0;
  31. while (i < inStr.length()) {
  32. if (inStr.charAt(i) == '&') {
  33. retStr.append("&amp;");
  34. } else if (inStr.charAt(i) == '<') {
  35. retStr.append("&lt;");
  36. } else if (inStr.charAt(i) == '>') {
  37. retStr.append("&gt;");
  38. } else if (inStr.charAt(i) == '\"') {
  39. retStr.append("&quot;");
  40. } else if (changeSpace && inStr.charAt(i) == ' ') {
  41. retStr.append("&nbsp;");
  42. } else if (changeSpace && inStr.charAt(i) == '\t') {
  43. retStr.append(" &nbsp; &nbsp;");
  44. } else
  45. retStr.append(inStr.charAt(i));
  46. i++;
  47. }
  48. return retStr.toString();
  49. }
  50. public static String flattenStrings(List<String> values) {
  51. return flattenStrings(values, " ");
  52. }
  53. public static String flattenStrings(List<String> values, String separator) {
  54. StringBuilder sb = new StringBuilder();
  55. for (String value : values) {
  56. sb.append(value).append(separator);
  57. }
  58. return sb.toString().trim();
  59. }
  60. public static String trimString(String value, int max) {
  61. if (value.length() <= max) {
  62. return value;
  63. }
  64. return value.substring(0, max - 3) + "...";
  65. }
  66. public static String trimShortLog(String string) {
  67. return trimString(string, 60);
  68. }
  69. public static String leftPad(String input, int length, char pad) {
  70. if (input.length() < length) {
  71. StringBuilder sb = new StringBuilder();
  72. for (int i = 0, len = length - input.length(); i < len; i++) {
  73. sb.append(pad);
  74. }
  75. sb.append(input);
  76. return sb.toString();
  77. }
  78. return input;
  79. }
  80. public static String rightPad(String input, int length, char pad) {
  81. if (input.length() < length) {
  82. StringBuilder sb = new StringBuilder();
  83. sb.append(input);
  84. for (int i = 0, len = length - input.length(); i < len; i++) {
  85. sb.append(pad);
  86. }
  87. return sb.toString();
  88. }
  89. return input;
  90. }
  91. public static String getSHA1(String text) {
  92. try {
  93. byte[] bytes = text.getBytes("iso-8859-1");
  94. return getSHA1(bytes);
  95. } catch (UnsupportedEncodingException u) {
  96. throw new RuntimeException(u);
  97. }
  98. }
  99. public static String getSHA1(byte[] bytes) {
  100. try {
  101. MessageDigest md = MessageDigest.getInstance("SHA-1");
  102. md.update(bytes, 0, bytes.length);
  103. byte[] sha1hash = md.digest();
  104. StringBuilder sb = new StringBuilder(sha1hash.length * 2);
  105. for (int i = 0; i < sha1hash.length; i++) {
  106. if (((int) sha1hash[i] & 0xff) < 0x10)
  107. sb.append("0");
  108. sb.append(Long.toString((int) sha1hash[i] & 0xff, 16));
  109. }
  110. return sb.toString();
  111. } catch (NoSuchAlgorithmException t) {
  112. throw new RuntimeException(t);
  113. }
  114. }
  115. public static String getRootPath(String path) {
  116. if (path.indexOf('/') > -1) {
  117. return path.substring(0, path.indexOf('/'));
  118. }
  119. return "";
  120. }
  121. }