001: package gnu.text;
002:
003: import java.io.*;
004: import java.net.*;
005: import gnu.mapping.WrappedException; // FIXME - move to gnu.kawa.util
006:
007: /** A Path that wraps a URL. */
008:
009: public class URLPath extends URIPath {
010: URL url;
011:
012: URLPath(URL url) {
013: /* #ifdef use:java.net.URI */
014: super (toURI(url));
015: ;
016: /* #else */
017: // super(url.toString());
018: /* #endif */
019: this .url = url;
020: }
021:
022: public static URLPath valueOf(URL url) {
023: return new URLPath(url);
024: }
025:
026: public boolean isAbsolute() {
027: return true;
028: }
029:
030: public long getLastModified() {
031: return getLastModified(url);
032: }
033:
034: public static long getLastModified(URL url) {
035: try {
036: return url.openConnection().getLastModified();
037: } catch (Throwable ex) {
038: return 0;
039: }
040: }
041:
042: public long getContentLength() {
043: return getLastModified(url);
044: }
045:
046: public static int getContentLength(URL url) {
047: try {
048: return url.openConnection().getContentLength();
049: } catch (Throwable ex) {
050: return -1;
051: }
052: }
053:
054: public URL toURL() {
055: return url;
056: }
057:
058: /* #ifdef use:java.net.URI */
059: public static URI toURI(URL url) {
060: try {
061: /* #ifdef JAVA5 */
062: // return url.toURI();
063: /* #else */
064: return new URI(url.toString());
065: /* #endif */
066: } catch (Throwable ex) {
067: throw WrappedException.wrapIfNeeded(ex);
068: }
069: }
070:
071: public URI toURI() {
072: return toURI(url);
073: }
074:
075: public String toURIString() {
076: return url.toString();
077: }
078:
079: /* #else */
080: // public String toURI () { return url.toString(); }
081: // public String toURIString () { return uri.toString(); }
082: /* #endif */
083:
084: public Path resolve(String relative) {
085: try {
086: return valueOf(new URL(url, relative));
087: } catch (Throwable ex) {
088: throw WrappedException.wrapIfNeeded(ex);
089: }
090: }
091:
092: public static InputStream openInputStream(URL url)
093: throws IOException {
094: return url.openConnection().getInputStream();
095: }
096:
097: public InputStream openInputStream() throws IOException {
098: return openInputStream(url);
099: }
100:
101: public static OutputStream openOutputStream(URL url)
102: throws IOException {
103: String str = url.toString();
104: // Note JDK (upto 1.5.0, at least) throws an UnknownServiceException
105: // "protocol doesn't support output" if you do getOutputStream on
106: // a "file:" URL. That seems lame, but let's avoid that!
107: if (str.startsWith("file:")) {
108: /* #ifdef use:java.net.URI */
109: try {
110: return new FileOutputStream(new File(new URI(str)));
111: } catch (Throwable ex) {
112: }
113: /* #else */
114: // return new FileOutputStream(new File(str.substring(5)));
115: /* #endif */
116: }
117: URLConnection conn = url.openConnection();
118: conn.setDoInput(false);
119: conn.setDoOutput(true);
120: return conn.getOutputStream();
121: }
122:
123: public OutputStream openOutputStream() throws IOException {
124: return openOutputStream(url);
125: }
126:
127: public static URLPath classResourcePath(Class clas) {
128: try {
129: return valueOf(ResourceStreamHandler.makeURL(clas));
130: } catch (Throwable ex) {
131: throw WrappedException.wrapIfNeeded(ex);
132: }
133: }
134: }
|