~ubuntu-branches/ubuntu/utopic/sikuli/utopic

« back to all changes in this revision

Viewing changes to sikuli-script/src/main/java/org/sikuli/script/ImageLocator.java

  • Committer: Bazaar Package Importer
  • Author(s): Gilles Filippini
  • Date: 2011-10-04 23:32:13 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20111004233213-36fm78hx0z53tkuw
Tags: 1.0~x~rc3-dfsg1-2
* New patch fix-cmake-sikuli-ide.patch:
  + Fix random FTBFS due to missing inter target dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010-2011, Sikuli.org
 
3
 * Released under the MIT License.
 
4
 *
 
5
 */
1
6
package org.sikuli.script;
2
7
 
3
8
import java.io.*;
4
9
import java.util.Map;
5
10
import java.util.HashMap;
 
11
import java.net.URI;
6
12
import java.net.URL;
7
13
import java.net.MalformedURLException;
8
14
 
9
15
public class ImageLocator {
10
16
 
11
 
   Map<URL,String> _cache = new HashMap<URL, String>();
 
17
   Map<URI,String> _cache = new HashMap<URI, String>();
12
18
   String _cache_dir;
13
19
   String _bundle_path;
14
20
 
45
51
 
46
52
   public String locateURL(URL url) throws IOException{
47
53
      Debug.log(3, "locateURL " + url);
48
 
      if(_cache.containsKey(url))
49
 
         return _cache.get(url);
50
54
      try{
 
55
         URI uri = url.toURI();
 
56
         if(_cache.containsKey(uri))
 
57
            return _cache.get(uri);
51
58
         String localFile = Util.downloadURL(url, _cache_dir);
52
 
         Debug.log(3, "download " + url + " to local: "  + localFile);
53
 
         _cache.put(url, localFile);
 
59
         Debug.log(3, "download " + uri + " to local: "  + localFile);
 
60
         _cache.put(uri, localFile);
54
61
         return localFile;
55
62
      }
 
63
      catch(java.net.URISyntaxException e){
 
64
         Debug.error("URI syntax error: " + url + ", " + e.getMessage());
 
65
         return null;
 
66
      }
56
67
      catch(IOException e){
57
68
         //e.printStackTrace();
58
69
         throw e;
73
84
      path = path.replaceAll("[Hh][Tt][Tt][Pp][Ss]://","__https__//");
74
85
      String[] ret = path.split(Env.getSeparator());
75
86
      for(int i=0;i<ret.length;i++){
76
 
         if(ret[i].indexOf("__http__")>=0)
 
87
         boolean isURL = false;
 
88
         if(ret[i].indexOf("__http__")>=0){
77
89
            ret[i] = ret[i].replaceAll("__http__//", "http://");
78
 
         else if(ret[i].indexOf("__https__")>=0)
 
90
            isURL = true;
 
91
         }
 
92
         else if(ret[i].indexOf("__https__")>=0){
79
93
            ret[i] = ret[i].replaceAll("__https__//", "https://");
80
 
         if(!ret[i].endsWith(File.separator))
81
 
            ret[i] += File.separator;
 
94
            isURL = true;
 
95
         }
 
96
         if(isURL){
 
97
            if(!ret[i].endsWith("/")) ret[i] += "/";
 
98
         }
 
99
         else{
 
100
            if(!ret[i].endsWith(File.separator))
 
101
               ret[i] += File.separator;
 
102
         }
82
103
      }
83
104
      return ret;
84
105
   }
119
140
   }
120
141
 
121
142
   protected String searchFile(String filename) throws IOException {
122
 
      Debug.log(4,"ImageLocator.searchFile: " + filename + " bundle path: " + _bundle_path);
 
143
      Debug.log(4, "ImageLocator.searchFile: " + filename + " bundle path: " + _bundle_path);
123
144
      File f = new File(_bundle_path, filename);
124
145
      if( f.exists() ) return f.getAbsolutePath();
125
146
      String[] sikuli_img_path = getImagePath();
164
185
         if(ret != null)
165
186
            return ret;
166
187
      }
167
 
      throw new FileNotFoundException("File " + ret + " not exists");
 
188
      throw new FileNotFoundException("File " + filename + " not exists");
168
189
   }
169
190
}