Beispiel #1
0
 private static void addParameter(String paramString, Map<String, JobParameter> params) {
   JobParameter param = new JobParameter();
   String[] tokens = paramString.split(":", 2);
   param.setName(tokens[0]);
   if (tokens.length > 1) {
     param.setDefaultValue(tokens[1]);
   }
   /** re-declarations not allowed -- iow, first instance wins (for defaulting values) */
   if (params.get(param.getName()) == null) {
     params.put(param.getName(), param);
   }
 }
Beispiel #2
0
  public static String macroTemplateParams(String expandedJob, Collection<JobParameter> params)
      throws TokenReplacerOverflowException {
    if (params != null && expandedJob != null) {
      final HashMap<String, String> map = new HashMap<>();
      for (JobParameter param : params) {
        String name = param.getName();
        String value = param.getValue();
        String defaultValue = param.getDefaultValue();
        map.put(name, LessStrings.isEmpty(value) ? defaultValue : value);
      }
      StringBuilder builder = new StringBuilder();
      List<String> contents = new ArrayList<>();
      List<String> delimiters = new ArrayList<>();
      CommentTokenizer commentTokenizer = new CommentTokenizer(expandedJob);
      commentTokenizer.tokenize(contents, delimiters);
      int length = contents.size();

      builder.append(macroTemplateParamsHelper(contents.get(0), map));
      String firstDelimiter = delimiters.get(0);
      if (firstDelimiter != "%[" && firstDelimiter != "]%") {
        builder.append(firstDelimiter);
      }
      for (int i = 1; i < length; i++) {
        String prevDelimiter = delimiters.get(i - 1);
        String nextDelimiter = delimiters.get(i);
        // Ignore parameters inside of comments
        if (prevDelimiter.equals("//") || prevDelimiter.equals("/*")) {
          builder.append(contents.get(i));
        } else if (prevDelimiter.equals("%[") && nextDelimiter.equals("]%")) {
          String value = map.get(LessStrings.splitArray(contents.get(i), ":")[0]);
          if (value != null) {
            builder.append(value);
          }
        } else { // Delimiters such as double-quotes may contain parameters inside them
          builder.append(macroTemplateParamsHelper(contents.get(i), map));
        }
        if (nextDelimiter != "%[" && nextDelimiter != "]%") {
          builder.append(nextDelimiter);
        }
      }
      return builder.toString();
    }
    return expandedJob;
  }