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

StringUtils.java 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. 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. */
  18. package org.apache.tools.ant.util;
  19. import java.io.PrintWriter;
  20. import java.io.StringWriter;
  21. import java.util.Vector;
  22. /**
  23. * A set of helper methods related to string manipulation.
  24. *
  25. */
  26. public final class StringUtils {
  27. /**
  28. * constructor to stop anyone instantiating the class
  29. */
  30. private StringUtils() {
  31. }
  32. /** the line separator for this OS */
  33. public static final String LINE_SEP = System.getProperty("line.separator");
  34. /**
  35. * Splits up a string into a list of lines. It is equivalent
  36. * to <tt>split(data, '\n')</tt>.
  37. * @param data the string to split up into lines.
  38. * @return the list of lines available in the string.
  39. */
  40. public static Vector lineSplit(String data) {
  41. return split(data, '\n');
  42. }
  43. /**
  44. * Splits up a string where elements are separated by a specific
  45. * character and return all elements.
  46. * @param data the string to split up.
  47. * @param ch the separator character.
  48. * @return the list of elements.
  49. */
  50. public static Vector split(String data, int ch) {
  51. Vector elems = new Vector();
  52. int pos = -1;
  53. int i = 0;
  54. while ((pos = data.indexOf(ch, i)) != -1) {
  55. String elem = data.substring(i, pos);
  56. elems.addElement(elem);
  57. i = pos + 1;
  58. }
  59. elems.addElement(data.substring(i));
  60. return elems;
  61. }
  62. /**
  63. * Replace occurrences into a string.
  64. * @param data the string to replace occurrences into
  65. * @param from the occurrence to replace.
  66. * @param to the occurrence to be used as a replacement.
  67. * @return the new string with replaced occurrences.
  68. */
  69. public static String replace(String data, String from, String to) {
  70. StringBuffer buf = new StringBuffer(data.length());
  71. int pos = -1;
  72. int i = 0;
  73. while ((pos = data.indexOf(from, i)) != -1) {
  74. buf.append(data.substring(i, pos)).append(to);
  75. i = pos + from.length();
  76. }
  77. buf.append(data.substring(i));
  78. return buf.toString();
  79. }
  80. /**
  81. * Convenient method to retrieve the full stacktrace from a given exception.
  82. * @param t the exception to get the stacktrace from.
  83. * @return the stacktrace from the given exception.
  84. */
  85. public static String getStackTrace(Throwable t) {
  86. StringWriter sw = new StringWriter();
  87. PrintWriter pw = new PrintWriter(sw, true);
  88. t.printStackTrace(pw);
  89. pw.flush();
  90. pw.close();
  91. return sw.toString();
  92. }
  93. /**
  94. * Checks that a string buffer ends up with a given string. It may sound trivial with the existing
  95. * JDK API but the various implementation among JDKs can make those methods extremely resource intensive
  96. * and perform poorly due to massive memory allocation and copying. See
  97. * @param buffer the buffer to perform the check on
  98. * @param suffix the suffix
  99. * @return <code>true</code> if the character sequence represented by the
  100. * argument is a suffix of the character sequence represented by
  101. * the StringBuffer object; <code>false</code> otherwise. Note that the
  102. * result will be <code>true</code> if the argument is the
  103. * empty string.
  104. */
  105. public static boolean endsWith(StringBuffer buffer, String suffix) {
  106. if (suffix.length() > buffer.length()) {
  107. return false;
  108. }
  109. // this loop is done on purpose to avoid memory allocation performance
  110. // problems on various JDKs
  111. // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
  112. // implementation is ok though does allocation/copying
  113. // StringBuffer.toString().endsWith() does massive memory
  114. // allocation/copying on JDK 1.5
  115. // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
  116. int endIndex = suffix.length() - 1;
  117. int bufferIndex = buffer.length() - 1;
  118. while (endIndex >= 0) {
  119. if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
  120. return false;
  121. }
  122. bufferIndex--;
  123. endIndex--;
  124. }
  125. return true;
  126. }
  127. /**
  128. * xml does not do "c" like interpretation of strings.
  129. * i.e. \n\r\t etc.
  130. * this method processes \n, \r, \t, \f, \\
  131. * also subs \s -> " \n\r\t\f"
  132. * a trailing '\' will be ignored
  133. *
  134. * @param input raw string with possible embedded '\'s
  135. * @return converted string
  136. * @since Ant 1.7
  137. */
  138. public static String resolveBackSlash(String input) {
  139. StringBuffer b = new StringBuffer();
  140. boolean backSlashSeen = false;
  141. for (int i = 0; i < input.length(); ++i) {
  142. char c = input.charAt(i);
  143. if (!backSlashSeen) {
  144. if (c == '\\') {
  145. backSlashSeen = true;
  146. } else {
  147. b.append(c);
  148. }
  149. } else {
  150. switch (c) {
  151. case '\\':
  152. b.append((char) '\\');
  153. break;
  154. case 'n':
  155. b.append((char) '\n');
  156. break;
  157. case 'r':
  158. b.append((char) '\r');
  159. break;
  160. case 't':
  161. b.append((char) '\t');
  162. break;
  163. case 'f':
  164. b.append((char) '\f');
  165. break;
  166. case 's':
  167. b.append(" \t\n\r\f");
  168. break;
  169. default:
  170. b.append(c);
  171. }
  172. backSlashSeen = false;
  173. }
  174. }
  175. return b.toString();
  176. }
  177. }