def to_compressed_string(val, max_length=0):
    """Converts val to a compressed string.

  A compressed string is one with no leading or trailing spaces.

  If val is None, or is blank (all spaces) None is returned.

  If max_length > 0 and  the stripped val is greater than max_length, val[:max_length] is returned.
  """
    if val is None or len(val) == 0:
        return None
    rval = ' '.join(val.split())
    if len(rval) == 0:
        return None
    if max_length == 0:
        return rval
    else:
        return rval[:max_length]
