Ejemplo n.º 1
1
 /**
  * Given a string, replace all tabs with the appropriate number of spaces.
  *
  * @param tabLength the length of each tab.
  * @param s the String to scan.
  */
 public static void replaceTabs(int tabLength, StringBuilder s) {
   if (whitespace == null || whitespace.length() < tabLength)
     whitespace = String.format("%" + tabLength + "s", " ");
   int index = 0;
   while ((index = s.indexOf("\t", index)) != -1) {
     int spaceCount = tabLength - index % tabLength;
     s.replace(index, index + 1, whitespace.substring(0, spaceCount));
     index += spaceCount;
   }
 }
Ejemplo n.º 2
0
 private void addClassPathToScriptEngine(File jarFile) throws ConfigurationException {
   try {
     StringBuilder cmd = new StringBuilder(addClassPathCmd);
     int tagStartPos = cmd.indexOf(parameterTag);
     int tageEndPos = tagStartPos + parameterTag.length();
     cmd.replace(tagStartPos, tageEndPos, jarFile.getAbsolutePath().replace("\\", "/"));
     // System.out.println("cmd " + cmd.toString());
     engine.eval("add-classpath", 1, 1, cmd.toString()); // $NON-NLS-1$
   } catch (Exception ex) {
     String msg = String.format("Failed to load class path %s", jarFile.getName()); // $NON-NLS-1$
     System.err.println(msg + " " + ex);
     throw new ConfigurationException(msg, ex);
   }
 }
Ejemplo n.º 3
0
 /**
  * Imports a package into the script engine
  *
  * @param pkgName the name of the package
  * @throws ConfigurationException if the package cannot be imported
  */
 private void importPkgToScriptEngine(String pkgName) throws ConfigurationException {
   try {
     StringBuilder cmd = new StringBuilder(importPackageCmd);
     int tagStartPos = cmd.indexOf(parameterTag);
     int tageEndPos = tagStartPos + parameterTag.length();
     cmd.replace(tagStartPos, tageEndPos, pkgName);
     // System.out.println("cmd " + cmd.toString());
     engine.eval("load-packages", 1, 1, cmd.toString()); // $NON-NLS-1$
   } catch (Exception ex) {
     String msg = String.format("Failed to import package %s", pkgName); // $NON-NLS-1$
     System.err.println(msg + " " + ex);
     throw new ConfigurationException(msg, ex);
   }
 }
Ejemplo n.º 4
0
    @Override
    public void replace(
        DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset)
        throws BadLocationException {
      Document doc = fp.getDocument();
      String oldText = doc.getText(0, doc.getLength());
      StringBuilder sb = new StringBuilder(oldText);
      sb.replace(offset, offset + length, oldText);

      int len = string.length();
      boolean isValidInteger = true;

      for (int i = 0; i < len; i++) {
        if (!Character.isDigit(string.charAt(i))) {
          isValidInteger = false;
          break;
        }
      }
      if (isValidInteger && verifyText(sb.toString())) {
        super.replace(fp, offset, length, string, aset);
      } else Toolkit.getDefaultToolkit().beep();
    }
Ejemplo n.º 5
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();
  }