1. /*
  2. * Copyright 2001-2002,2004 The Apache Software Foundation
  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. */
  17. package org.apache.tools.ant.util;
  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.util.Vector;
  21. /**
  22. * A set of helper methods related to string manipulation.
  23. *
  24. */
  25. public final class StringUtils {
  26. /** the line separator for this OS */
  27. public static final String LINE_SEP = System.getProperty("line.separator");
  28. /**
  29. * Splits up a string into a list of lines. It is equivalent
  30. * to <tt>split(data, '\n')</tt>.
  31. * @param data the string to split up into lines.
  32. * @return the list of lines available in the string.
  33. */
  34. public static Vector lineSplit(String data) {
  35. return split(data, '\n');
  36. }
  37. /**
  38. * Splits up a string where elements are separated by a specific
  39. * character and return all elements.
  40. * @param data the string to split up.
  41. * @param ch the separator character.
  42. * @return the list of elements.
  43. */
  44. public static Vector split(String data, int ch) {
  45. Vector elems = new Vector();
  46. int pos = -1;
  47. int i = 0;
  48. while ((pos = data.indexOf(ch, i)) != -1) {
  49. String elem = data.substring(i, pos);
  50. elems.addElement(elem);
  51. i = pos + 1;
  52. }
  53. elems.addElement(data.substring(i));
  54. return elems;
  55. }
  56. /**
  57. * Replace occurrences into a string.
  58. * @param data the string to replace occurrences into
  59. * @param from the occurrence to replace.
  60. * @param to the occurrence to be used as a replacement.
  61. * @return the new string with replaced occurrences.
  62. */
  63. public static String replace(String data, String from, String to) {
  64. StringBuffer buf = new StringBuffer(data.length());
  65. int pos = -1;
  66. int i = 0;
  67. while ((pos = data.indexOf(from, i)) != -1) {
  68. buf.append(data.substring(i, pos)).append(to);
  69. i = pos + from.length();
  70. }
  71. buf.append(data.substring(i));
  72. return buf.toString();
  73. }
  74. /**
  75. * Convenient method to retrieve the full stacktrace from a given exception.
  76. * @param t the exception to get the stacktrace from.
  77. * @return the stacktrace from the given exception.
  78. */
  79. public static String getStackTrace(Throwable t) {
  80. StringWriter sw = new StringWriter();
  81. PrintWriter pw = new PrintWriter(sw, true);
  82. t.printStackTrace(pw);
  83. pw.flush();
  84. pw.close();
  85. return sw.toString();
  86. }
  87. }