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.

StringUtil.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.vaadin.sass.util;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. public class StringUtil {
  8. private static final String FOLDER_SEPARATOR = "/"; // folder separator
  9. private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; // Windows
  10. // folder
  11. // separator
  12. private static final String TOP_PATH = ".."; // top folder
  13. private static final String CURRENT_PATH = "."; // current folder
  14. public static String cleanPath(String path) {
  15. String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR,
  16. FOLDER_SEPARATOR);
  17. String[] pathArray = delimitedListToStringArray(pathToUse,
  18. FOLDER_SEPARATOR);
  19. List pathElements = new LinkedList();
  20. int tops = 0;
  21. for (int i = pathArray.length - 1; i >= 0; i--) {
  22. if (CURRENT_PATH.equals(pathArray[i])) {
  23. // do nothing
  24. } else if (TOP_PATH.equals(pathArray[i])) {
  25. tops++;
  26. } else {
  27. if (tops > 0) {
  28. tops--;
  29. } else {
  30. pathElements.add(0, pathArray[i]);
  31. }
  32. }
  33. }
  34. for (int i = 0; i < tops; i++) {
  35. pathElements.add(0, TOP_PATH);
  36. }
  37. return collectionToDelimitedString(pathElements, FOLDER_SEPARATOR);
  38. }
  39. public static String replace(String inString, String oldPattern,
  40. String newPattern) {
  41. if (inString == null) {
  42. return null;
  43. }
  44. if (oldPattern == null || newPattern == null) {
  45. return inString;
  46. }
  47. StringBuffer sbuf = new StringBuffer();
  48. // output StringBuffer we'll build up
  49. int pos = 0; // our position in the old string
  50. int index = inString.indexOf(oldPattern);
  51. // the index of an occurrence we've found, or -1
  52. int patLen = oldPattern.length();
  53. while (index >= 0) {
  54. sbuf.append(inString.substring(pos, index));
  55. sbuf.append(newPattern);
  56. pos = index + patLen;
  57. index = inString.indexOf(oldPattern, pos);
  58. }
  59. sbuf.append(inString.substring(pos));
  60. // remember to append any characters to the right of a match
  61. return sbuf.toString();
  62. }
  63. public static String[] delimitedListToStringArray(String str,
  64. String delimiter) {
  65. if (str == null) {
  66. return new String[0];
  67. }
  68. if (delimiter == null) {
  69. return new String[] { str };
  70. }
  71. List result = new ArrayList();
  72. int pos = 0;
  73. int delPos = 0;
  74. while ((delPos = str.indexOf(delimiter, pos)) != -1) {
  75. result.add(str.substring(pos, delPos));
  76. pos = delPos + delimiter.length();
  77. }
  78. if (str.length() > 0 && pos <= str.length()) {
  79. // Add rest of String, but not in case of empty input.
  80. result.add(str.substring(pos));
  81. }
  82. return (String[]) result.toArray(new String[result.size()]);
  83. }
  84. public static String collectionToDelimitedString(Collection coll,
  85. String delim, String prefix, String suffix) {
  86. if (coll == null) {
  87. return "";
  88. }
  89. StringBuffer sb = new StringBuffer();
  90. Iterator it = coll.iterator();
  91. int i = 0;
  92. while (it.hasNext()) {
  93. if (i > 0) {
  94. sb.append(delim);
  95. }
  96. sb.append(prefix).append(it.next()).append(suffix);
  97. i++;
  98. }
  99. return sb.toString();
  100. }
  101. public static String collectionToDelimitedString(Collection coll,
  102. String delim) {
  103. return collectionToDelimitedString(coll, delim, "", "");
  104. }
  105. }