  public static String fixPath(String path) throws WebdavException {
    if (path == null) {
      return null;
    }
    String decoded;
    try {
      decoded = URLDecoder.decode(path, "UTF8");
    } catch (Throwable t) {
      throw new WebdavException(t);
    }
    if (decoded == null) {
      return (null);
    }
    if (decoded.indexOf('\\') >= 0) {
      decoded = decoded.replace('\\', '/');
    }
    if (!decoded.startsWith("/")) {
      decoded = "/" + decoded;
    }
    while (decoded.indexOf("//") >= 0) {
      decoded = decoded.replaceAll("//", "/");
    }
    if (decoded.indexOf("/.") < 0) {
      return decoded;
    }
    StringTokenizer st = new StringTokenizer(decoded, "/");
    ArrayList<String> al = new ArrayList<String>();
    while (st.hasMoreTokens()) {
      String s = st.nextToken();
      if (s.equals(".")) {
      } else if (s.equals("..")) {
        if (al.size() == 0) {
          return null;
        }
        al.remove(al.size() - 1);
      } else {
        al.add(s);
      }
    }
    StringBuffer sb = new StringBuffer();
    for (String s: al) {
      sb.append('/');
      sb.append(s);
    }
    return sb.toString();
  }

  public static String fixPath(String path) throws WebdavException {
    if (path == null) {
      return null;
    }
    String decoded;
    try {
      decoded = URLDecoder.decode(path, "UTF8");
    } catch (Throwable t) {
      throw new WebdavBadRequest("bad path: " + path);
    }
    if (decoded == null) {
      return (null);
    }
    if (decoded.indexOf('\\') >= 0) {
      decoded = decoded.replace('\\', '/');
    }
    if (!decoded.startsWith("/")) {
      decoded = "/" + decoded;
    }
    while (decoded.indexOf("//") >= 0) {
      decoded = decoded.replaceAll("//", "/");
    }
    if (decoded.indexOf("/.") < 0) {
      return decoded;
    }
    StringTokenizer st = new StringTokenizer(decoded, "/");
    ArrayList<String> al = new ArrayList<String>();
    while (st.hasMoreTokens()) {
      String s = st.nextToken();
      if (s.equals(".")) {
      } else if (s.equals("..")) {
        if (al.size() == 0) {
          return null;
        }
        al.remove(al.size() - 1);
      } else {
        al.add(s);
      }
    }
    StringBuffer sb = new StringBuffer();
    for (String s: al) {
      sb.append('/');
      sb.append(s);
    }
    return sb.toString();
  }
