<commit_before>package org.rabix.bindings.draft3.resolver;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.rabix.bindings.BindingException;
import org.rabix.bindings.BindingWrongVersionException;
import org.rabix.bindings.ProtocolType;
import org.rabix.bindings.helper.URIHelper;
import org.rabix.common.helper.JSONHelper;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.base.Preconditions;

public class Draft3DocumentResolver {
  
public static Set<String> types = new HashSet<String>();
  
  static {
    types.add("null");
    types.add("boolean");
    types.add("int");
    types.add("long");
    types.add("float");
    types.add("double");
    types.add("string");
    types.add("File");
    types.add("record");
    types.add("enum");
    types.add("array");
    types.add("Any");
  }


  public static final String APP_STEP_KEY = "run";
  public static final String TYPE_KEY = "type";
  public static final String RESOLVER_REFERENCE_KEY = "$import";
  public static final String RESOLVER_REFERENCE_INCLUDE_KEY = "$include";
  public static final String GRAPH_KEY = "$graph";
  public static final String SCHEMA_KEY = "$schemas";
  public static final String NAMESPACES_KEY = "$namespaces";
  public static final String SCHEMADEF_KEY = "SchemaDefRequirement";
  public static final String CWL_VERSION_KEY = "cwlVersion";
  
  public static final String RESOLVER_JSON_POINTER_KEY = "$job";
  
  public static final String DOCUMENT_FRAGMENT_SEPARATOR = "#";
  
  private static final String DEFAULT_ENCODING = "UTF-8";

  private static boolean graphResolve = false;
  
  private static Map<String, String> namespaces = new HashMap<String, String>();
  private static Map<String, Map<String, Draft3DocumentResolverReference>> referenceCache = new HashMap<>();
  private static Map<String, LinkedHashSet<Draft3DocumentResolverReplacement>> replacements = new HashMap<>();
  
  public static String resolve(String appUrl) throws BindingException {
    String appUrlBase = appUrl;
    try {
      URI uri = URI.create(appUrl);
      if (uri.getScheme().equals(URIHelper.DATA_URI_SCHEME)) {
        appUrlBase = URIHelper.extractBase(appUrl);
      }
    } catch (IllegalArgumentException e) {

    }
    
    File file = null;
    JsonNode root = null;
    try {
      boolean isFile = URIHelper.isFile(appUrlBase);
      if (isFile) {
        file = new File(URIHelper.getURIInfo(appUrlBase));
      } else {
        file = new File(".");
      }
      root = JSONHelper.readJsonNode(URIHelper.getData(appUrlBase));
    } catch (IOException e) {
      throw new BindingException(e);
    }
    
    if(root.has(GRAPH_KEY)) {
      graphResolve = true;
    }
    
    if(root.has(NAMESPACES_KEY)) {
      populateNamespaces(root);
      ((ObjectNode) root).remove(NAMESPACES_KEY);
    }
    
    JsonNode cwlVersion = root.get(CWL_VERSION_KEY);
    if (cwlVersion == null || !(cwlVersion.asText().equals(ProtocolType.DRAFT3.appVersion))) {
      clearReplacements(appUrl);
      clearReferenceCache(appUrl);
      throw new BindingWrongVersionException("Document version is not " + ProtocolType.DRAFT3.appVersion);
    }
    
    traverse(appUrl, root, file, null, root);

    for (Draft3DocumentResolverReplacement replacement : getReplacements(appUrl)) {
      if (replacement.getParentNode().isArray()) {
        replaceArrayItem(appUrl, root, replacement);
      } else if (replacement.getParentNode().isObject()) {
        replaceObjectItem(appUrl, root, replacement);
      }
    }
    
    if(graphResolve) {
      String fragment = URIHelper.extractFragment(appUrl).substring(1);
      
      clearReplacements(appUrl);
      clearReferenceCache(appUrl);
      
      removeFragmentIdentifier(appUrl, root, file, null, root, fragment);
      
      for (Draft3DocumentResolverReplacement replacement : getReplacements(appUrl)) {
        if (replacement.getParentNode().isArray()) {
          replaceArrayItem(appUrl, root, replacement);
        } else if (replacement.getParentNode().isObject()) {
          replaceObjectItem(appUrl, root, replacement);
        }
      }
      
      for (final JsonNode elem : root.get(GRAPH_KEY)) {
        if (elem.get("id").asText().equals(fragment)) {
          Map<String, Object> result = JSONHelper.readMap(elem);
          result.put(CWL_VERSION_KEY, cwlVersion);
          root = JSONHelper.convertToJsonNode(result);
          break;
        }
      }
      graphResolve = false;
    }
    else {
      if (!(root.get(CWL_VERSION_KEY).asText().equals(ProtocolType.DRAFT3.appVersion))) {
        clearReplacements(appUrl);
        clearReferenceCache(appUrl);
        throw new BindingException("Document version is not cwl:draft-3");<commit_msg>return unsupported for  in draft3<commit_after>package org.rabix.bindings.draft3.resolver;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.rabix.bindings.BindingException;
import org.rabix.bindings.BindingWrongVersionException;
import org.rabix.bindings.ProtocolType;
import org.rabix.bindings.helper.URIHelper;
import org.rabix.common.helper.JSONHelper;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.base.Preconditions;

public class Draft3DocumentResolver {
  
public static Set<String> types = new HashSet<String>();
  
  static {
    types.add("null");
    types.add("boolean");
    types.add("int");
    types.add("long");
    types.add("float");
    types.add("double");
    types.add("string");
    types.add("File");
    types.add("record");
    types.add("enum");
    types.add("array");
    types.add("Any");
  }


  public static final String APP_STEP_KEY = "run";
  public static final String TYPE_KEY = "type";
  public static final String RESOLVER_REFERENCE_KEY = "$import";
  public static final String RESOLVER_REFERENCE_INCLUDE_KEY = "$include";
  public static final String GRAPH_KEY = "$graph";
  public static final String SCHEMA_KEY = "$schemas";
  public static final String NAMESPACES_KEY = "$namespaces";
  public static final String SCHEMADEF_KEY = "SchemaDefRequirement";
  public static final String CWL_VERSION_KEY = "cwlVersion";
  
  public static final String RESOLVER_JSON_POINTER_KEY = "$job";
  
  public static final String DOCUMENT_FRAGMENT_SEPARATOR = "#";
  
  private static final String DEFAULT_ENCODING = "UTF-8";

  private static boolean graphResolve = false;
  
  private static Map<String, String> namespaces = new HashMap<String, String>();
  private static Map<String, Map<String, Draft3DocumentResolverReference>> referenceCache = new HashMap<>();
  private static Map<String, LinkedHashSet<Draft3DocumentResolverReplacement>> replacements = new HashMap<>();
  
  public static String resolve(String appUrl) throws BindingException {
    String appUrlBase = appUrl;
    try {
      URI uri = URI.create(appUrl);
      if (uri.getScheme().equals(URIHelper.DATA_URI_SCHEME)) {
        appUrlBase = URIHelper.extractBase(appUrl);
      }
    } catch (IllegalArgumentException e) {

    }
    
    File file = null;
    JsonNode root = null;
    try {
      boolean isFile = URIHelper.isFile(appUrlBase);
      if (isFile) {
        file = new File(URIHelper.getURIInfo(appUrlBase));
      } else {
        file = new File(".");
      }
      root = JSONHelper.readJsonNode(URIHelper.getData(appUrlBase));
    } catch (IOException e) {
      throw new BindingException(e);
    }
    
    if(root.has(GRAPH_KEY)) {
      graphResolve = true;
    }
    
    if(root.has(NAMESPACES_KEY)) {
      populateNamespaces(root);
      ((ObjectNode) root).remove(NAMESPACES_KEY);
    }
    
    JsonNode cwlVersion = root.get(CWL_VERSION_KEY);
    if (cwlVersion == null || !(cwlVersion.asText().equals(ProtocolType.DRAFT3.appVersion))) {
      clearReplacements(appUrl);
      clearReferenceCache(appUrl);
      throw new BindingWrongVersionException("Document version is not " + ProtocolType.DRAFT3.appVersion);
    }
    
    traverse(appUrl, root, file, null, root);

    for (Draft3DocumentResolverReplacement replacement : getReplacements(appUrl)) {
      if (replacement.getParentNode().isArray()) {
        replaceArrayItem(appUrl, root, replacement);
      } else if (replacement.getParentNode().isObject()) {
        replaceObjectItem(appUrl, root, replacement);
      }
    }
    
    if (root.has(SCHEMA_KEY)) {
      throw new NotImplementedException("Feature not implemented");
    }
    
    if(graphResolve) {
      String fragment = URIHelper.extractFragment(appUrl).substring(1);
      
      clearReplacements(appUrl);
      clearReferenceCache(appUrl);
      
      removeFragmentIdentifier(appUrl, root, file, null, root, fragment);
      
      for (Draft3DocumentResolverReplacement replacement : getReplacements(appUrl)) {
        if (replacement.getParentNode().isArray()) {
          replaceArrayItem(appUrl, root, replacement);
        } else if (replacement.getParentNode().isObject()) {
          replaceObjectItem(appUrl, root, replacement);
        }
      }
      
      for (final JsonNode elem : root.get(GRAPH_KEY)) {
        if (elem.get("id").asText().equals(fragment)) {
          Map<String, Object> result = JSONHelper.readMap(elem);
          result.put(CWL_VERSION_KEY, cwlVersion);
          root = JSONHelper.convertToJsonNode(result);
          break;
        }
      }
      graphResolve = false;
    }
    else {
      if (!(root.get(CWL_VERSION_KEY).asText().equals(ProtocolType.DRAFT3.appVersion))) {
        clearReplacements(appUrl);
        clearReferenceCache(appUrl);
        throw new BindingException("Document version is not cwl:draft-3");
{'fraction_non_alphanumeric': 0.11436681622562037, 'fraction_numerical': 0.002289167658639319, 'mean_word_length': 3.6895663374838987, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '4828965', 'n_tokens_mistral': 3336, 'n_tokens_neox': 3256, 'n_words': 662}