<commit_before>package org.oasis.datacore.rest.api.util;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.oasis.datacore.rest.api.DatacoreApi;


/**
 * TODO or in UriService ? BUT also required on client side !
 * 
 * @author mdutoo
 *
 */
public class UriHelper {

   public static int typeIndexInType = DatacoreApi.DC_TYPE_PATH.length() + 1; // TODO use Pattern & DC_TYPE_PATH
   
   /** to detect whether relative (rather than absolute) uri
    groups are delimited by () see http://stackoverflow.com/questions/6865377/java-regex-capture-group
    URI scheme : see http://stackoverflow.com/questions/3641722/valid-characters-for-uri-schemes */
   private static Pattern anyBaseUrlPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9\\.\\-\\+]*)://[^/]+/"); // TODO or "^http[s]?://data\\.oasis-eu\\.org/" ?
   private static Pattern multiSlashPattern = Pattern.compile("/+");
   

   /**
    * Only for client, not used by server. Does new DCURI(...).toString()
    * and also checks syntax & normalizes URL.
    * @param containerUrl
    * @param modelType
    * @param iri
    * @return
    */
   public static String buildUri(String containerUrl, String modelType, String iri) {
      String uri = new DCURI(containerUrl, modelType, iri, false, false, false).toString();
      try {
         // cannonicalize
         // TODO reuse getUriNormalizedContainerAndPathWithoutSlash's code...
         return new URL(uri).toURI().normalize().toString();
      } catch (MalformedURLException | URISyntaxException e) {
         throw new IllegalArgumentException("Bad URL : " + uri, e);
      }
   }
   
   /**
    * Adapts the given URI (URL) to the given container.
    * In given (endpoint URL) URI, replaces container URL part by the given one.
    * @param endpointUri ex. http://localhost:8080/dc/type/city.sample.country/France
    * @param containerUrl must have ending slash
    * @return ex.http://data.oasis-eu.org/dc/type/city.sample.country/France
    * @throws URISyntaxException 
    * @throws MalformedURLException 
    */
   public static String toContainerUrl(String endpointUri, String containerUrl)
         throws MalformedURLException, URISyntaxException {
      String[] containerAndPathWithoutSlash = getUriNormalizedContainerAndPathWithoutSlash(
            endpointUri, containerUrl, true, true);
      String urlPathWithoutSlash = containerAndPathWithoutSlash[1];
      return containerUrl + urlPathWithoutSlash;
   }
   
   /**
    * Parses any URI, even external
    * @param uri
    * @param containerUrl used only for relative URIs, which require it
    * @return
    * @throws MalformedURLException
    * @throws URISyntaxException
    */
   public static DCURI parseUri(String uri, String containerUrl) throws MalformedURLException, URISyntaxException {
      String[] containerAndPathWithoutSlash = getUriNormalizedContainerAndPathWithoutSlash(
            uri, null, true, false);
      String urlContainer = containerAndPathWithoutSlash[0];

      boolean isRelativeUri = urlContainer == null;
      ///boolean isExternalDatacoreUri = false;
      ///boolean isExternalWebUri = false;
      if (isRelativeUri) {
         if (containerUrl == null) {
            throw new MalformedURLException("Can't parse URI because is relative "
                  + "and no provided container URL " + uri);
         }
         urlContainer = containerUrl;
         // TODO LATER OPT also accept local type-relative uri (type-less iri) ???
      } else if (containerUrl != null && !containerUrl.equals(urlContainer)) {
         // external resource (another Datacore or any Web resource) :
         // NB. can't know if it's another Datacore
         // so Web resource :
         ///isExternalWebUri = true;
         // doesn't know how to parse modelType out of containerAndPathWithoutSlash
         // TODO LATER knownWebContainerUrlSet allowing such parsing & auth schemes
         // therefore return :
         return new DCURI(urlContainer, null, containerAndPathWithoutSlash[1], false, false, true);
      }
      
      String[] iri = parseIri(containerAndPathWithoutSlash[1]);
      return new DCURI(containerUrl, iri[0], iri[1], isRelativeUri, false, false);
   }
   /**<commit_msg>resource : patched UriHelper URI parsing<commit_after>package org.oasis.datacore.rest.api.util;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.oasis.datacore.rest.api.DatacoreApi;


/**
 * TODO or in UriService ? BUT also required on client side !
 * 
 * @author mdutoo
 *
 */
public class UriHelper {

   public static int typeIndexInType = DatacoreApi.DC_TYPE_PATH.length() + 1; // TODO use Pattern & DC_TYPE_PATH
   
   /** to detect whether relative (rather than absolute) uri
    groups are delimited by () see http://stackoverflow.com/questions/6865377/java-regex-capture-group
    URI scheme : see http://stackoverflow.com/questions/3641722/valid-characters-for-uri-schemes */
   private static Pattern anyBaseUrlPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9\\.\\-\\+]*)://[^/]+/"); // TODO or "^http[s]?://data\\.oasis-eu\\.org/" ?
   private static Pattern multiSlashPattern = Pattern.compile("/+");
   

   /**
    * Only for client, not used by server. Does new DCURI(...).toString()
    * and also checks syntax & normalizes URL.
    * @param containerUrl
    * @param modelType
    * @param iri
    * @return
    */
   public static String buildUri(String containerUrl, String modelType, String iri) {
      String uri = new DCURI(containerUrl, modelType, iri, false, false, false).toString();
      try {
         // cannonicalize
         // TODO reuse getUriNormalizedContainerAndPathWithoutSlash's code...
         return new URL(uri).toURI().normalize().toString();
      } catch (MalformedURLException | URISyntaxException e) {
         throw new IllegalArgumentException("Bad URL : " + uri, e);
      }
   }
   
   /**
    * Adapts the given URI (URL) to the given container.
    * In given (endpoint URL) URI, replaces container URL part by the given one.
    * @param endpointUri ex. http://localhost:8080/dc/type/city.sample.country/France
    * @param containerUrl must have ending slash
    * @return ex.http://data.oasis-eu.org/dc/type/city.sample.country/France
    * @throws URISyntaxException 
    * @throws MalformedURLException 
    */
   public static String toContainerUrl(String endpointUri, String containerUrl)
         throws MalformedURLException, URISyntaxException {
      String[] containerAndPathWithoutSlash = getUriNormalizedContainerAndPathWithoutSlash(
            endpointUri, containerUrl, true, true);
      String urlPathWithoutSlash = containerAndPathWithoutSlash[1];
      return containerUrl + urlPathWithoutSlash;
   }
   
   /**
    * Parses any URI, even external
    * @param uri
    * @param containerUrl used only for relative URIs, which require it
    * @return
    * @throws MalformedURLException
    * @throws URISyntaxException
    */
   public static DCURI parseUri(String uri, String containerUrl) throws MalformedURLException, URISyntaxException {
      String[] containerAndPathWithoutSlash = getUriNormalizedContainerAndPathWithoutSlash(
            uri, null, true, false);
      String urlContainer = containerAndPathWithoutSlash[0];

      boolean isRelativeUri = urlContainer == null;
      ///boolean isExternalDatacoreUri = false;
      ///boolean isExternalWebUri = false;
      if (isRelativeUri) {
         if (containerUrl == null) {
            throw new MalformedURLException("Can't parse URI because is relative "
                  + "and no provided container URL " + uri);
         }
         urlContainer = containerUrl;
         // TODO LATER OPT also accept local type-relative uri (type-less iri) ???
      } else if (containerUrl != null && !containerUrl.equals(urlContainer)) {
         // external resource (another Datacore or any Web resource) :
         // NB. can't know if it's another Datacore
         // so Web resource :
         ///isExternalWebUri = true;
         // doesn't know how to parse modelType out of containerAndPathWithoutSlash
         // TODO LATER knownWebContainerUrlSet allowing such parsing & auth schemes
         // therefore return :
         return new DCURI(urlContainer, null, containerAndPathWithoutSlash[1], false, false, true);
      }
      
      String[] iri = parseIri(containerAndPathWithoutSlash[1]);
      return new DCURI(urlContainer, iri[0], iri[1], isRelativeUri, false, false);
   }
   /**
{'fraction_non_alphanumeric': 0.10525712976749513, 'fraction_numerical': 0.006184858549994273, 'mean_word_length': 3.023963133640553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2773918', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2437, 'n_words': 682}