public static String compact(String str) {
    int right = str.length() - 1;
    int left = 0;
    while (left <= right &&
           Verifier.isXMLWhitespace(str.charAt(left))) {
      left++;
    }
    while (right > left &&
           Verifier.isXMLWhitespace(str.charAt(right))) {
      right--;
    }

    if (left > right) {
      return "";
    }

    boolean space = true;
    final StringBuilder buffer = new StringBuilder(right - left + 1);
    while (left <= right) {
      final char c = str.charAt(left);
      if (Verifier.isXMLWhitespace(c)) {
        if (space) {
          buffer.append(' ');
          space = false;
        }
      }
      else {
        buffer.append(c);
        space = true;
      }
      left++;
    }
    return buffer.toString();
  }
public static String compact(String str)