001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jdt;
017:
018: import com.google.gwt.core.ext.UnableToCompleteException;
019: import com.google.gwt.core.ext.typeinfo.CompilationUnitProvider;
020: import com.google.gwt.dev.util.Util;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.net.URL;
025: import java.net.URLConnection;
026:
027: /**
028: * Implements {@link CompilationUnitProvider} in terms of a URL.
029: */
030: public class URLCompilationUnitProvider implements
031: CompilationUnitProvider {
032:
033: private static File trySimplify(URL url) {
034: String s = url.toExternalForm();
035: File f = null;
036: if (s.startsWith("file:")) {
037: // Strip the file: off, and use the result. If the result
038: // does not start with file, we cannot simplify. Using URI
039: // to do the simplification fails for paths with spaces.
040: // Any number of slashes at the beginning cause no problem for Java, so
041: // if c:/windows exists so will ///////c:/windows.
042: f = new File(s.substring(5));
043: if (!f.exists()) {
044: f = null;
045: }
046: }
047: return f;
048: }
049:
050: private final File file;
051:
052: private final String location;
053:
054: private final String packageName;
055:
056: private char[] source;
057:
058: private long sourceCurrentTime = Long.MIN_VALUE;
059:
060: private final URL url;
061:
062: private final String mainTypeName;
063:
064: public URLCompilationUnitProvider(URL url, String packageName) {
065: assert (url != null);
066: assert (packageName != null);
067: this .url = url;
068:
069: // Files are faster to work with, so use file if available.
070: this .file = trySimplify(url);
071: String simpleTypeName;
072: if (file == null) {
073: this .location = url.toExternalForm();
074: simpleTypeName = new File(url.getPath()).getName();
075: } else {
076: this .location = this .file.getAbsolutePath();
077: simpleTypeName = this .file.getName();
078: }
079:
080: int i = simpleTypeName.lastIndexOf(".java");
081: if (i != -1) {
082: simpleTypeName = simpleTypeName.substring(0, i);
083: }
084: mainTypeName = simpleTypeName;
085:
086: this .packageName = packageName;
087: }
088:
089: public long getLastModified() throws UnableToCompleteException {
090: try {
091: if (file != null) {
092: return file.lastModified();
093: } else {
094: String converted = Util.findFileName(location);
095: if (converted != location) {
096: return new File(converted).lastModified();
097: }
098: URLConnection conn = url.openConnection();
099: return conn.getLastModified();
100: }
101: } catch (IOException e) {
102: throw new UnableToCompleteException();
103: }
104: }
105:
106: public String getLocation() {
107: return location;
108: }
109:
110: public String getMainTypeName() {
111: return mainTypeName;
112: }
113:
114: public String getPackageName() {
115: return packageName;
116: }
117:
118: public char[] getSource() throws UnableToCompleteException {
119: long lastModified = getLastModified();
120: if (sourceCurrentTime >= lastModified && source != null) {
121: return source;
122: } else {
123: sourceCurrentTime = lastModified;
124: }
125: if (file == null) {
126: // Pre-read source.
127: source = Util.readURLAsChars(url);
128: } else {
129: source = Util.readFileAsChars(file);
130: }
131: if (source == null) {
132: throw new UnableToCompleteException();
133: }
134: return source;
135: }
136:
137: public boolean isTransient() {
138: return false;
139: }
140:
141: @Override
142: public String toString() {
143: return location;
144: }
145: }
|