<commit_before>package org.rabix.bindings.protocol.draft3.expression;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.rabix.bindings.protocol.draft3.bean.Draft3Job;
import org.rabix.bindings.protocol.draft3.bean.resource.requirement.Draft3InlineJavascriptRequirement;
import org.rabix.bindings.protocol.draft3.bean.resource.requirement.Draft3ResourceRequirement;
import org.rabix.bindings.protocol.draft3.expression.javascript.Draft3ExpressionJavascriptResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Draft3ExpressionResolver {

  private static Logger logger = LoggerFactory.getLogger(Draft3ExpressionResolver.class);
  
  public static String KEY_EXPRESSION_VALUE = "script";
  public static String KEY_EXPRESSION_LANGUAGE = "engine";
  
  private static String segSymbol = "\\w+";
  private static String segSingle = "\\['([^']|\\\')+'\\]";
  private static String segDouble = "\\[\"([^']|\\\")+\"\\]";
  private static String segIndex = "\\[[0-9]+\\]";

  private static String segments = String.format("(.%s|%s|%s|%s)", segSymbol, segSingle, segDouble, segIndex);

  private static String javascriptRe = "(\\$\\(.+\\)|\\$\\{.+\\})";
  private static String paramRe = String.format("\\$\\((%s)%s*\\)", segSymbol, segments);
    
  private static Pattern segPattern = Pattern.compile(segments);
  private static Pattern pattern = Pattern.compile(paramRe);
  private static Pattern javascriptPattern = Pattern.compile(javascriptRe);
  
  public static final ObjectMapper sortMapper = new ObjectMapper();
  
  static {
    sortMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
  }
  
  @SuppressWarnings({ "unchecked" })
  public static <T> T resolve(final Object expression, final Draft3Job job, final Object self) throws Draft3ExpressionException {
    if (expression == null) {
      return null;
    }
    if (isExpressionObject(expression)) {
      String script = (String) ((Map<?, ?>) expression).get(KEY_EXPRESSION_VALUE);
      return (T) Draft3ExpressionJavascriptResolver.evaluate(job.getInputs(), self, script, null);
    }
    if (expression instanceof String) {
      if (job.isInlineJavascriptEnabled()) {
        List<String> expressionLibs = Collections.<String>emptyList();
        Draft3InlineJavascriptRequirement inlineJavascriptRequirement = job.getApp().getInlineJavascriptRequirement();
        if (inlineJavascriptRequirement != null) {
          expressionLibs = inlineJavascriptRequirement.getExpressionLib();
        }
        Matcher m = javascriptPattern.matcher((CharSequence) expression);
        if (m.find()) {
          Object leaf = Draft3ExpressionJavascriptResolver.evaluate(job.getInputs(), self, m.group(0), expressionLibs);
          if (((String)expression).trim().length() == m.group(0).length()) {
            return (T) leaf;
          } else {
            try {
              String leafStr = sortMapper.writeValueAsString(leaf);
              if (leafStr.startsWith("\"")) {
                leafStr.substring(1, leafStr.length() - 1);
              }
              return (T) (((String)expression).substring(0, m.start(0)) + leafStr + ((String)expression).substring(m.end(0)));
            } catch (JsonProcessingException e) {
              logger.error("Failed to serialize {} to JSON.", leaf);
              throw new Draft3ExpressionException(e);
            }
          }
        }
        return (T) expression;
      } else {
        Map<String, Object> vars = new HashMap<>();
        vars.put("inputs", job.getInputs());
        vars.put("context", self);
        
        Draft3ResourceRequirement resourceRequirement = job.getApp().getResourceRequirement();
        if (resourceRequirement != null) {
          vars.put("runtime", resourceRequirement.build(job).toMap());
        }
        return (T) paramInterpolate((String) expression, vars, true);
      }
    }
    return (T) expression;
  }
  
  public static boolean isExpressionObject(Object expression) {
    return expression instanceof Map<?,?>  && ((Map<?,?>) expression).containsKey(KEY_EXPRESSION_VALUE)  && ((Map<?,?>) expression).containsKey(KEY_EXPRESSION_LANGUAGE);
  }
  <commit_msg>fix Javascript expression resolving<commit_after>package org.rabix.bindings.protocol.draft3.expression;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.rabix.bindings.protocol.draft3.bean.Draft3Job;
import org.rabix.bindings.protocol.draft3.bean.resource.requirement.Draft3InlineJavascriptRequirement;
import org.rabix.bindings.protocol.draft3.bean.resource.requirement.Draft3ResourceRequirement;
import org.rabix.bindings.protocol.draft3.expression.javascript.Draft3ExpressionJavascriptResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Draft3ExpressionResolver {

  private static Logger logger = LoggerFactory.getLogger(Draft3ExpressionResolver.class);
  
  public static String KEY_EXPRESSION_VALUE = "script";
  public static String KEY_EXPRESSION_LANGUAGE = "engine";
  
  private static String segSymbol = "\\w+";
  private static String segSingle = "\\['([^']|\\\')+'\\]";
  private static String segDouble = "\\[\"([^']|\\\")+\"\\]";
  private static String segIndex = "\\[[0-9]+\\]";

  private static String segments = String.format("(.%s|%s|%s|%s)", segSymbol, segSingle, segDouble, segIndex);

  private static String javascriptRe = "(\\$\\((.|\\r\\n|\\n|\\r)*\\)|\\$\\{(.|\\r\\n|\\n|\\r)*\\})";
  private static String paramRe = String.format("\\$\\((%s)%s*\\)", segSymbol, segments);
    
  private static Pattern segPattern = Pattern.compile(segments);
  private static Pattern pattern = Pattern.compile(paramRe);
  private static Pattern javascriptPattern = Pattern.compile(javascriptRe);
  
  public static final ObjectMapper sortMapper = new ObjectMapper();
  
  static {
    sortMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
  }
  
  @SuppressWarnings({ "unchecked" })
  public static <T> T resolve(final Object expression, final Draft3Job job, final Object self) throws Draft3ExpressionException {
    if (expression == null) {
      return null;
    }
    if (isExpressionObject(expression)) {
      String script = (String) ((Map<?, ?>) expression).get(KEY_EXPRESSION_VALUE);
      return (T) Draft3ExpressionJavascriptResolver.evaluate(job.getInputs(), self, script, null);
    }
    if (expression instanceof String) {
      if (job.isInlineJavascriptEnabled()) {
        List<String> expressionLibs = Collections.<String>emptyList();
        Draft3InlineJavascriptRequirement inlineJavascriptRequirement = job.getApp().getInlineJavascriptRequirement();
        if (inlineJavascriptRequirement != null) {
          expressionLibs = inlineJavascriptRequirement.getExpressionLib();
        }
        Matcher m = javascriptPattern.matcher((CharSequence) expression);
        if (m.find()) {
          Object leaf = Draft3ExpressionJavascriptResolver.evaluate(job.getInputs(), self, m.group(0), expressionLibs);
          if (((String)expression).trim().length() == m.group(0).length()) {
            return (T) leaf;
          } else {
            try {
              String leafStr = sortMapper.writeValueAsString(leaf);
              if (leafStr.startsWith("\"")) {
                leafStr.substring(1, leafStr.length() - 1);
              }
              return (T) (((String)expression).substring(0, m.start(0)) + leafStr + ((String)expression).substring(m.end(0)));
            } catch (JsonProcessingException e) {
              logger.error("Failed to serialize {} to JSON.", leaf);
              throw new Draft3ExpressionException(e);
            }
          }
        }
        return (T) expression;
      } else {
        Map<String, Object> vars = new HashMap<>();
        vars.put("inputs", job.getInputs());
        vars.put("self", self);
        
        Draft3ResourceRequirement resourceRequirement = job.getApp().getResourceRequirement();
        if (resourceRequirement != null) {
          vars.put("runtime", resourceRequirement.build(job).toMap());
        }
        return (T) paramInterpolate((String) expression, vars, true);
      }
    }
    return (T) expression;
  }
  
  public static boolean isExpressionObject(Object expression) {
    return expression instanceof Map<?,?>  && ((Map<?,?>) expression).containsKey(KEY_EXPRESSION_VALUE)  && ((Map<?,?>) expression).containsKey(KEY_EXPRESSION_LANGUAGE);
  }
  
{'fraction_non_alphanumeric': 0.13591805264897014, 'fraction_numerical': 0.0066086573411168635, 'mean_word_length': 3.6232179226069245, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1700567', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2569, 'n_words': 516}