public static String getSimpleQuery(Map<String, Object> params, String script, boolean noteForm) {
    String replaced = script;

    Pattern pattern = noteForm ? VAR_NOTE_PTN : VAR_PTN;

    Matcher match = pattern.matcher(replaced);
    while (match.find()) {
      int first = match.start();

      if (!noteForm && first > 0 && replaced.charAt(first - 1) == '$') {
        continue;
      }
      Input input = getInputForm(match);
      Object value;
      if (params.containsKey(input.name)) {
        value = params.get(input.name);
      } else {
        value = input.getDefaultValue();
      }

      String expanded;
      if (value instanceof Object[] || value instanceof Collection) {  // multi-selection
        OptionInput optionInput = (OptionInput) input;
        String delimiter = input.argument;
        if (delimiter == null) {
          delimiter = DEFAULT_DELIMITER;
        }
        Collection<Object> checked = value instanceof Collection ? (Collection<Object>) value
            : Arrays.asList((Object[]) value);
        List<Object> validChecked = new LinkedList<>();
        for (Object o : checked) {
          // filter out obsolete checked values
          if (optionInput.getOptions() != null) {
            for (ParamOption option : optionInput.getOptions()) {
              if (option.getValue().equals(o)) {
                validChecked.add(o);
                break;
              }
            }
          }
        }
        if (validChecked.isEmpty()) {
          expanded = StringUtils.join(checked, delimiter);
        } else {
          params.put(input.name, validChecked);
          expanded = StringUtils.join(validChecked, delimiter);
        }
      } else {
        // single-selection
        expanded = StringUtils.defaultString((String) value, "");
      }
      replaced = match.replaceFirst(expanded);
      match = pattern.matcher(replaced);
    }

    return replaced;
  }
@Test
  void testFormSubstitution() {
    // test form substitution without new forms
    String script = "INPUT=${input_form=}SELECTED=${select_form(Selection Form)=" +
        ",s_op1|s_op2|s_op3}\nCHECKED=${checkbox:checkbox_form=c_op1|c_op2,c_op1|c_op2|c_op3}";
    Map<String, Object> params = new HashMap<>();
    params.put("input_form", "some_input");
    params.put("select_form", "s_op2");
    params.put("checkbox_form", new String[]{"c_op1", "c_op3"});
    String replaced = Input.getSimpleQuery(params, script, false);
    assertEquals("INPUT=some_inputSELECTED=s_op2\nCHECKED=c_op1,c_op3", replaced);

    // test form substitution with new forms
    script = "INPUT=${input_form=}SELECTED=${select_form(Selection Form)=,s_op1|s_op2|s_op3}\n" +
        "CHECKED=${checkbox:checkbox_form=c_op1|c_op2,c_op1|c_op2|c_op3}\n" +
        "NEW_CHECKED=${checkbox( and ):new_check=nc_a|nc_c,nc_a|nc_b|nc_c}";
    replaced = Input.getSimpleQuery(params, script, false);
    assertEquals("INPUT=some_inputSELECTED=s_op2\nCHECKED=c_op1,c_op3\n" +
        "NEW_CHECKED=nc_a and nc_c", replaced);

    // test form substitution with obsoleted values
    script = "INPUT=${input_form=}SELECTED=${select_form(Selection Form)=,s_op1|s_op2|s_op3}\n" +
        "CHECKED=${checkbox:checkbox_form=c_op1|c_op2,c_op1|c_op2|c_op3_new}\n" +
        "NEW_CHECKED=${checkbox( and ):new_check=nc_a|nc_c,nc_a|nc_b|nc_c}";
    replaced = Input.getSimpleQuery(params, script, false);
    assertEquals("INPUT=some_inputSELECTED=s_op2\nCHECKED=c_op1\n" +
        "NEW_CHECKED=nc_a and nc_c", replaced);

    // textbox without param value provided
    script = "INPUT='${input_form}'";
    params = new HashMap<>();
    replaced = Input.getSimpleQuery(params, script, false);
    assertEquals("INPUT=''", replaced);
  }