<commit_before>/*
 * Copyright 2010-2011, Sikuli.org
 * Released under the MIT License.
 *
 */
package org.sikuli.script;

import java.io.*;
import java.util.Map;
import java.util.HashMap;
import java.net.URL;
import java.net.MalformedURLException;

public class ImageLocator {

   Map<URL,String> _cache = new HashMap<URL, String>();
   String _cache_dir;
   String _bundle_path;

   public ImageLocator(String bundlePath){
      _bundle_path = bundlePath;
      String name = "";
      if(bundlePath != null){
         File f = new File(bundlePath);
         name = f.getName() + "/";
      }
      _cache_dir = System.getProperty("java.io.tmpdir")
                   + "/sikuli_cache/" + name;
      File dir = new File(_cache_dir);
      if(!dir.exists())
         dir.mkdir();
      else{
         //TODO: init _cache from the content of the cache dir
      }
   }

   public ImageLocator(){
      this(Settings.BundlePath);
   }

   protected URL getURL(String s){
      try{
         URL url = new URL(s);
         return url;
      }
      catch(MalformedURLException e){
         return null;
      }
   }

   public String locateURL(URL url) throws IOException{
      Debug.log(3, "locateURL " + url);
      if(_cache.containsKey(url))
         return _cache.get(url);
      try{
         String localFile = Util.downloadURL(url, _cache_dir);
         Debug.log(3, "download " + url + " to local: "  + localFile);
         _cache.put(url, localFile);
         return localFile;
      }
      catch(IOException e){
         //e.printStackTrace();
         throw e;
      }
   }

   public static void addImagePath(String path){
      String imgPath = System.getProperty("SIKULI_IMAGE_PATH");
      if(imgPath != null)
         imgPath += Env.getSeparator() + path;
      else
         imgPath = path;
      System.setProperty("SIKULI_IMAGE_PATH", imgPath);
   }<commit_msg>resolved a blocker issue: Performance - Maps and sets of URLs can be performance hogs.  use URI instead of URL in ImageLocator.<commit_after>/*
 * Copyright 2010-2011, Sikuli.org
 * Released under the MIT License.
 *
 */
package org.sikuli.script;

import java.io.*;
import java.util.Map;
import java.util.HashMap;
import java.net.URI;
import java.net.URL;
import java.net.MalformedURLException;

public class ImageLocator {

   Map<URI,String> _cache = new HashMap<URI, String>();
   String _cache_dir;
   String _bundle_path;

   public ImageLocator(String bundlePath){
      _bundle_path = bundlePath;
      String name = "";
      if(bundlePath != null){
         File f = new File(bundlePath);
         name = f.getName() + "/";
      }
      _cache_dir = System.getProperty("java.io.tmpdir")
                   + "/sikuli_cache/" + name;
      File dir = new File(_cache_dir);
      if(!dir.exists())
         dir.mkdir();
      else{
         //TODO: init _cache from the content of the cache dir
      }
   }

   public ImageLocator(){
      this(Settings.BundlePath);
   }

   protected URL getURL(String s){
      try{
         URL url = new URL(s);
         return url;
      }
      catch(MalformedURLException e){
         return null;
      }
   }

   public String locateURL(URL url) throws IOException{
      Debug.log(3, "locateURL " + url);
      try{
         URI uri = url.toURI();
         if(_cache.containsKey(uri))
            return _cache.get(uri);
         String localFile = Util.downloadURL(url, _cache_dir);
         Debug.log(3, "download " + uri + " to local: "  + localFile);
         _cache.put(uri, localFile);
         return localFile;
      }
      catch(java.net.URISyntaxException e){
         Debug.error("URI syntax error: " + url + ", " + e.getMessage());
         return null;
      }
      catch(IOException e){
         //e.printStackTrace();
         throw e;
      }
   }

   public static void addImagePath(String path){
      String imgPath = System.getProperty("SIKULI_IMAGE_PATH");
      if(imgPath != null)
         imgPath += Env.getSeparator() + path;
      else
         imgPath = path;
      System.setProperty("SIKULI_IMAGE_PATH", imgPath);
   }
{'fraction_non_alphanumeric': 0.11774785801713586, 'fraction_numerical': 0.004895960832313341, 'mean_word_length': 2.242857142857143, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1087349', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1239, 'n_words': 308}