Ejemplo n.º 1
0
  private Map<String, List<String>> decodeParams(String s) {
    Map<String, List<String>> params = new LinkedHashMap<String, List<String>>();
    String name = null;
    int pos = 0; // Beginning of the unprocessed region
    int i; // End of the unprocessed region
    char c = 0; // Current character
    for (i = 0; i < s.length(); i++) {
      c = s.charAt(i);
      if (c == '=' && name == null) {
        if (pos != i) {
          name = decodeComponent(s.substring(pos, i), charset);
        }
        pos = i + 1;
      } else if (c == '&') {
        if (name == null && pos != i) {
          // We haven't seen an `=' so far but moved forward.
          // Must be a param of the form '&a&' so add it with
          // an empty value.
          addParam(params, decodeComponent(s.substring(pos, i), charset), "");
        } else if (name != null) {
          addParam(params, name, decodeComponent(s.substring(pos, i), charset));
          name = null;
        }
        pos = i + 1;
      }
    }

    if (pos != i) { // Are there characters we haven't dealt with?
      if (name == null) { // Yes and we haven't seen any `='.
        addParam(params, decodeComponent(s.substring(pos, i), charset), "");
      } else { // Yes and this must be the last value.
        addParam(params, name, decodeComponent(s.substring(pos, i), charset));
      }
    } else if (name != null) { // Have we seen a name without value?
      addParam(params, name, "");
    }

    return params;
  }