Esempio n. 1
0
  /** find parameters in the expanded job */
  public static Map<String, JobParameter> macroFindParameters(String expandedJob) {
    LinkedHashMap<String, JobParameter> params = new LinkedHashMap<>();
    if (expandedJob == null) {
      return params;
    }
    List<String> contents = new ArrayList<>();
    List<String> delimiters = new ArrayList<>();
    CommentTokenizer commentTokenizer = new CommentTokenizer(expandedJob);
    commentTokenizer.tokenize(contents, delimiters);
    int length = contents.size();

    macroFindParametersHelper(contents.get(0), params);
    for (int i = 1; i < length; i++) {
      String delimiter = delimiters.get(i - 1);
      // Ignore parameters inside of comments
      if (delimiter.equals("//") || delimiter.equals("/*")) {
        // do nothing
      } else if (delimiter.equals("%[") && delimiters.get(i).equals("]%")) {
        addParameter(contents.get(i), params);
      } else { // Delimiters such as double-quotes may contain parameters inside them
        macroFindParametersHelper(contents.get(i), params);
      }
    }
    return params;
  }
Esempio n. 2
0
    @Override
    public String replace(Region region, String label) {
      if (label.startsWith("http://")) {
        try {
          return new String(HttpUtil.httpGet(label, 0).getBody(), "UTF-8");
        } catch (Exception ex) {
          throw new RuntimeException(ex);
        }
      }
      JobMacro macro = spawn.getJobMacroManager().getEntity(label);
      String target = null;
      if (macro != null) {
        target = macro.getMacro();
      } else {
        List<String> aliases = spawn.getAliasManager().aliasToJobs(label);
        if (aliases != null) {
          target = joiner.join(aliases);
        }
      }
      if (target != null) {
        List<String> contents = new ArrayList<>();
        List<String> delimiters = new ArrayList<>();
        CommentTokenizer commentTokenizer = new CommentTokenizer(target);
        commentTokenizer.tokenize(contents, delimiters);
        StringBuilder builder = new StringBuilder();
        int length = contents.size();

        builder.append(contents.get(0));
        builder.append(delimiters.get(0));
        for (int i = 1; i < length; i++) {
          String delimiter = delimiters.get(i - 1);
          if (delimiter.equals("//") || delimiter.equals("/*")) {
            /* disable any macros inside comments so they don't get expanded */
            builder.append(macroPattern.matcher(contents.get(i)).replaceAll("%_{$1}_%"));
          } else {
            builder.append(contents.get(i));
          }
          builder.append(delimiters.get(i));
        }
        return builder.toString();
      } else {
        String msg = "non-existent macro referenced : " + label;
        log.warn(msg);
        throw new RuntimeException(msg);
      }
    }
Esempio n. 3
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;
  }
Esempio n. 4
0
  /**
   * recursively expand macros
   *
   * @throws IllegalStateException if expanded config exceeds the max length allowed.
   */
  public static String macroExpand(Spawn spawn, String rawtext)
      throws TokenReplacerOverflowException, IllegalStateException {
    MacroTokenReplacer replacer = new MacroTokenReplacer(spawn);
    List<String> contents = new ArrayList<>();
    List<String> delimiters = new ArrayList<>();
    CommentTokenizer commentTokenizer = new CommentTokenizer(rawtext);
    commentTokenizer.tokenize(contents, delimiters);
    StringBuilder builder = new StringBuilder();
    int length = contents.size();

    builder.append(replacer.process(contents.get(0)));
    builder.append(delimiters.get(0));
    for (int i = 1; i < length; i++) {
      if (builder.length() > Spawn.inputMaxNumberOfCharacters) {
        throw new IllegalStateException(
            "Expanded job config length of at least "
                + builder.length()
                + " characters is greater than max length of "
                + Spawn.inputMaxNumberOfCharacters);
      }
      String delimiter = delimiters.get(i - 1);
      if (delimiter.equals("//") || delimiter.equals("/*")) {
        builder.append(contents.get(i));
      } else {
        builder.append(replacer.process(contents.get(i)));
      }
      builder.append(delimiters.get(i));
    }
    if (builder.length() > Spawn.inputMaxNumberOfCharacters) {
      throw new IllegalStateException(
          "Expanded job config length of "
              + builder.length()
              + " characters is greater than max length of "
              + Spawn.inputMaxNumberOfCharacters);
    }
    return builder.toString();
  }