code: public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail,
                                   boolean doublePunctuationSpaces)
    {
  
      int end = m_length + m_start;
      char[] buf = new char[m_length];
      FastStringBuffer fsb = fsb();
      boolean edit = false;
  
      /* replace S to ' '. and ' '+ -> single ' '. */
      int d = 0;
      boolean pres = false;
  
      for (int s = m_start; s < end; s++)
      {
        char c = fsb.charAt(s);
  
        if (isSpace(c))
        {
          if (!pres)
          {
            if (' ' != c)
            {
              edit = true;
            }
  
            buf[d++] = ' ';
  
            if (doublePunctuationSpaces && (d != 0))
            {
              char prevChar = buf[d - 1];
  
              if (!((prevChar == '.') || (prevChar == '!')
                    || (prevChar == '?')))
              {
                pres = true;
              }
            }
            else
            {
              pres = true;
            }
          }
          else
          {
            edit = true;
            pres = true;
          }
        }
        else
        {
          buf[d++] = c;
          pres = false;
        }
      }
  
      if (trimTail && 1 <= d && ' ' == buf[d - 1])
      {
        edit = true;
  
        d--;
      }
  
      int start = 0;
  
      if (trimHead && 0 < d && ' ' == buf[0])
      {
        edit = true;
  
        start++;
      }
  
      XMLStringFactory xsf = XMLStringFactoryImpl.getFactory();
  
      return edit ? xsf.newstr(buf, start, d - start) : this;
    }
docs: Conditionally trim all leading and trailing whitespace in the specified String.
  All strings of white space are
  replaced by a single space character (#x20), except spaces after punctuation which
  receive double spaces if doublePunctuationSpaces is true.
  This function may be useful to a formatter, but to get first class
  results, the formatter should probably do it's own white space handling
  based on the semantics of the formatting object.
  
  @param   trimHead    Trim leading whitespace?
  @param   trimTail    Trim trailing whitespace?
  @param   doublePunctuationSpaces    Use double spaces for punctuation?
  @return              The trimmed string.