Exemple #1
0
  private String genOptions() {
    /*
     * For multiplechoice
     * <input id="radio1" name="" value="" type="radio" />
     *     <label for="radio1">
     *      Streeful
     *     </label>
     * For checkBox:
     * <input id="checkbox1" name="" type="checkbox" />
     *      <label for="checkbox1">
     *       Apple
     *      </label>
     *
     * For chooselist:
     *
     * <option value="option1">
     *   Option 1
     * </option>
     */
    StringBuilder optHtml = new StringBuilder();
    String optMFormat = "<input id=\"radio%d\" name=\"ans\" value=\"%s\" type=\"radio\" />\n";
    String optMLableFormat = "<label for=\"radio%d\"> %s </label> \n";

    if (this.options.isEmpty()) {
      return optHtml.toString();
    }
    if (this.style.equals(this.MULTIPLECHOICE) || this.style.equals(this.YESNO)) {
      int i = 1;
      for (String option : this.options) {
        optHtml.append(String.format(optMFormat, i, option));
        optHtml.append(String.format(optMLableFormat, i, option));
        i++;
      }
    }

    String optCFormat = "<input id=\"checkbox%d\" name=\"ans\" value=\"%s\" type=\"checkbox\" />\n";
    String optCLableFormat = "<label for=\"checkbox%d\"> %s </label> \n";
    Log.i(TAG, "before entering checkbox");
    if (this.style.equals(this.CHECKBOX)) {
      int j = 1;
      for (String option : this.options) {
        optHtml.append(String.format(optCFormat, j, option));
        optHtml.append(String.format(optCLableFormat, j, option));
        j++;
      }
    }
    String optLFormt = "<option value=\"option%d\"> %s </option>";
    if (this.style.equals(this.CHOOSELIST)) {
      int k = 1;
      for (String option : this.options) {
        optHtml.append(String.format(optLFormt, k, option));

        k++;
      }
    }
    Log.i(TAG, "gen options for:(" + this.style + ")" + optHtml.toString());
    return optHtml.toString();
  }
Exemple #2
0
  /*
   * use webView.loadData() to load from an HTML string
   * 1. fetch the template according to survey style
   * 2. replace survey's question and survey's options in the template
   */
  private String genSurvey() throws IOException {
    Log.i(TAG, "the style is: " + style);
    //		String templatePath  = "component" + File.separator + getTemplatePath(this.style);
    String templatePath = "component" + java.io.File.separator + surveyTemplate.get(this.style);

    BufferedInputStream in =
        new BufferedInputStream(container.$context().getAssets().open(templatePath));

    // BufferedInputStream in = new BufferedInputStream(MediaUtil.openMedia(container.$form(),
    // templatePath));

    // read it with BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
      sb.append(line);
    }

    Log.i(TAG, "Before replace:" + sb.toString());

    // A. generate question
    // find question block start with <h1 id="_txt_qt">
    String questionBlk = "<h1 id=\"_txt_qt\">";
    int insertPos = sb.indexOf(questionBlk) + questionBlk.length();

    int insertQuestionPos = sb.indexOf("Question", insertPos);

    sb.replace(insertQuestionPos, insertQuestionPos + "Question".length(), this.question);

    Log.i(TAG, "after replace question:" + sb.toString());
    // B. generate options
    // 1. find options block (depends on style)
    // only "multipleChoice", "checkBox", and "chooseList" need to replace with options;
    // 2. For "scale" style, the first three options will be specifying the
    // min, max and default value of the scale

    if (this.style.equals(this.MULTIPLECHOICE) || this.style.equals(this.CHECKBOX)) {

      int startPos = sb.indexOf("</legend>") + "</legend>".length();
      int endPos = sb.indexOf("</fieldset>");
      Log.i(TAG, "before replace options:");
      sb.replace(startPos, endPos, genOptions()); // replace with the filled-in options
    }
    if (this.style.equals(this.CHOOSELIST)) {
      int startPos = sb.indexOf("<select name=\"\">") + "<select name=\"\">".length();
      int endPos = sb.indexOf("</select>");

      sb.replace(startPos, endPos, genOptions());
    }

    if (this.style.equals(this.SCALE)) {

      if (!this.options.isEmpty() && this.options.size() == 3) {
        // replace min
        int sliderPos = sb.indexOf("input name=\"slider\"");
        int startPosOfMin = sb.indexOf("min=\"1\"", sliderPos + "input name=\"slider\"".length());
        // example: min="1"
        sb.replace(startPosOfMin, startPosOfMin + 7, "min=\"" + this.options.get(0) + "\"");
        // replace max
        // example: replace max="10" to max="100"
        int startPosOfMax = sb.indexOf("max=\"10\"", sliderPos + "input name=\"slider\"".length());

        sb.replace(startPosOfMax, startPosOfMax + 8, "max=\"" + this.options.get(1) + "\"");

        // replace default initial scale
        // example: value="5"
        int startPosOfDefault =
            sb.indexOf("value=\"5\"", sliderPos + "input name=\"slider\"".length());

        sb.replace(
            startPosOfDefault, startPosOfDefault + 9, "value=\"" + this.options.get(2) + "\"");

      } else {; // do nothing, use the template
      }
    }

    return sb.toString();
  }