Java 类org.apache.velocity.app.tools.VelocityFormatter 实例源码

项目:java-aws-mturk    文件:HITQuestion.java   
public String getQuestion(Map<String, String> input) {

    // If there is not velocity template associated with this question
    if (this.velocityTemplate == null) {
      // Return the explicitly set qeustion XML
      return this.question;
    }

    try {
      VelocityContext context = new VelocityContext();

      // Add some generic helpers just in case
      context.put("formatter", new VelocityFormatter(context));
      context.put("helper", new HITQuestionHelper());
      context.put("today", new Date());     

      if (input != null && input.values() != null) {        
        Iterator iter = input.keySet().iterator();
        while (iter.hasNext()) {
          String key = (String)iter.next();

          // Make a RAW version that's untouched
          context.put(key + RAW_KEY_SUFFIX, input.get(key));

          // Make the default version a QAP-cleaned version
          if (input.get(key) != null && input.get(key) instanceof String)
            context.put(key, cleanString((String)input.get(key)));
          else
            context.put(key, input.get(key));
        }
      }

      StringWriter writer = new StringWriter();

      this.velocityTemplate.merge(context, writer);
      this.question = writer.toString();
      return question;
    }
    catch (Exception e) {
      log.error("Could not read Question", e);
      return null;
    }
  }
项目:mturksdk-java-code-maven    文件:HITQuestion.java   
public String getQuestion(Map<String, String> input) {

    // If there is not velocity template associated with this question
    if (this.velocityTemplate == null) {
      // Return the explicitly set qeustion XML
      return this.question;
    }

    try {
      VelocityContext context = new VelocityContext();

      // Add some generic helpers just in case
      context.put("formatter", new VelocityFormatter(context));
      context.put("helper", new HITQuestionHelper());
      context.put("today", new Date());     

      if (input != null && input.values() != null) {        
        Iterator iter = input.keySet().iterator();
        while (iter.hasNext()) {
          String key = (String)iter.next();

          // Make a RAW version that's untouched
          context.put(key + RAW_KEY_SUFFIX, input.get(key));

          // Make the default version a QAP-cleaned version
          if (input.get(key) != null && input.get(key) instanceof String)
            context.put(key, cleanString((String)input.get(key)));
          else
            context.put(key, input.get(key));
        }
      }

      StringWriter writer = new StringWriter();

      this.velocityTemplate.merge(context, writer);
      this.question = writer.toString();
      return question;
    }
    catch (Exception e) {
      log.error("Could not read Question", e);
      return null;
    }
  }
项目:gort-public    文件:GortHITQuestion.java   
@Override
public String getQuestion(Map<String, String> input) {

  // If there is not velocity template associated with this question
  if (this.velocityTemplate == null) {
    // Return the explicitly set qeustion XML
    return this.question;
  }

  try {
    VelocityContext context = new VelocityContext();

    // Add some generic helpers just in case
    context.put("formatter", new VelocityFormatter(context));
    context.put("helper", new HITQuestionHelper());
    context.put("today", new Date());     

    if (input != null && input.values() != null) {        
      Iterator iter = input.keySet().iterator();
      while (iter.hasNext()) {
        String key = (String)iter.next();

        // Make a RAW version that's untouched
        context.put(key + RAW_KEY_SUFFIX, input.get(key));

        // Make the default version a QAP-cleaned version
        if (input.get(key) != null && input.get(key) instanceof String)
            // SMA: changed cleanString
          context.put(key, cleanString((String)input.get(key)));
        else
          context.put(key, input.get(key));
      }
    }

    StringWriter writer = new StringWriter();

    this.velocityTemplate.merge(context, writer);
    this.question = writer.toString();
    return question;
  }
  catch (Exception e) {
    log.error("Could not read Question", e);
    return null;
  }
}