1 16 package com.google.gwt.dev.jdt; 17 18 import com.google.gwt.core.ext.UnableToCompleteException; 19 import com.google.gwt.core.ext.typeinfo.CompilationUnitProvider; 20 import com.google.gwt.dev.util.Util; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 27 30 public class URLCompilationUnitProvider implements CompilationUnitProvider { 31 32 private static File trySimplify(URL url) { 33 String s = url.toExternalForm(); 34 File f = null; 35 if (s.startsWith("file:")) { 36 f = new File (s.substring(5)); 42 if (!f.exists()) { 43 f = null; 44 } 45 } else { 46 f = null; 47 } 48 return f; 49 } 50 51 private final File file; 52 53 private final String location; 54 55 private final String packageName; 56 57 private char[] source; 58 59 private long sourceCurrentTime = Long.MIN_VALUE; 60 61 private final URL url; 62 63 public URLCompilationUnitProvider(URL url, String packageName) { 64 assert (url != null); 65 assert (packageName != null); 66 this.url = url; 67 68 this.file = trySimplify(url); 70 if (file == null) { 71 this.location = url.toExternalForm(); 72 } else { 73 this.location = this.file.getAbsolutePath(); 74 } 75 this.packageName = packageName; 76 } 77 78 public long getLastModified() throws UnableToCompleteException { 79 try { 80 if (file != null) { 81 return file.lastModified(); 82 } else { 83 String converted = Util.findFileName(location); 84 if (converted != location) { 85 return new File (converted).lastModified(); 86 } 87 URLConnection conn = url.openConnection(); 88 return conn.getLastModified(); 89 } 90 } catch (IOException e) { 91 throw new UnableToCompleteException(); 92 } 93 } 94 95 public String getLocation() { 96 return location; 97 } 98 99 public String getPackageName() { 100 return packageName; 101 } 102 103 public char[] getSource() throws UnableToCompleteException { 104 long lastModified = getLastModified(); 105 if (sourceCurrentTime >= lastModified && source != null) { 106 return source; 107 } else { 108 sourceCurrentTime = lastModified; 109 } 110 if (file == null) { 111 source = Util.readURLAsChars(url); 113 } else { 114 source = Util.readFileAsChars(file); 115 } 116 if (source == null) { 117 throw new UnableToCompleteException(); 118 } 119 return source; 120 } 121 122 public boolean isTransient() { 123 return false; 124 } 125 126 public String toString() { 127 return location; 128 } 129 } 130 | Popular Tags |