KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jdt > URLCompilationUnitProvider


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

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 JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26
27 /**
28  * Implements {@link CompilationUnitProvider} in terms of a URL.
29  */

30 public class URLCompilationUnitProvider implements CompilationUnitProvider {
31
32   private static File JavaDoc trySimplify(URL JavaDoc url) {
33     String JavaDoc s = url.toExternalForm();
34     File JavaDoc f = null;
35     if (s.startsWith("file:")) {
36       // Strip the file: off, and use the result. If the result
37
// does not start with file, we cannot simplify. Using URI
38
// to do the simplification fails for paths with spaces.
39
// Any number of slashes at the beginning cause no problem for Java, so
40
// if c:/windows exists so will ///////c:/windows.
41
f = new File JavaDoc(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 JavaDoc file;
52
53   private final String JavaDoc location;
54
55   private final String JavaDoc packageName;
56
57   private char[] source;
58
59   private long sourceCurrentTime = Long.MIN_VALUE;
60
61   private final URL JavaDoc url;
62
63   public URLCompilationUnitProvider(URL JavaDoc url, String JavaDoc packageName) {
64     assert (url != null);
65     assert (packageName != null);
66     this.url = url;
67
68     // Files are faster to work with, so use file if available.
69
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 JavaDoc converted = Util.findFileName(location);
84         if (converted != location) {
85           return new File JavaDoc(converted).lastModified();
86         }
87         URLConnection JavaDoc conn = url.openConnection();
88         return conn.getLastModified();
89       }
90     } catch (IOException JavaDoc e) {
91       throw new UnableToCompleteException();
92     }
93   }
94
95   public String JavaDoc getLocation() {
96     return location;
97   }
98
99   public String JavaDoc 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       // Pre-read source.
112
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 JavaDoc toString() {
127     return location;
128   }
129 }
130
Popular Tags