code: private String collapseWhitespaces(String src)
      {
          StringBuffer ret = new StringBuffer();
          boolean inws = false;
          for (int i = 0; i < src.length(); i++)
          {
              char ch = src.charAt(i);
              if (collapsews && isWhitespace(ch))
              {
                  if (!inws)
                  {
                      ret.append(' ');
                      inws = true;
                  }
              }
              else if (isLineBreak(ch))
              {
              	ret.append('\r'); //represent all line breaks as LF
              	//reduce eventual CR+LF to CR only
              	if (ch == '\r' && i+1 < src.length() && src.charAt(i+1) == '\n')
              		i++;
              }
              else
              {
                  inws = false;
                  ret.append(ch);
              }
          }
          return new String(ret);
      }
docs: Applies the whitespace removal rules used in HTML.
  @param src source string
  @return a new string with additional whitespaces removed