datasource/Cached_Content_Loader: added caching content loader
[metux-java.git] / util / Filename.java
blob2a6a8161a2a2b6e991577e4cfe054988f653f199
1 package org.de.metux.util;
3 import java.io.File;
4 import java.io.Reader;
5 import java.io.InputStreamReader;
6 import java.io.FileReader;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.net.MalformedURLException;
13 public class Filename
15 abstract class Instance
17 public abstract String toString();
18 abstract URL toURL() throws MalformedURLException;
19 abstract Reader getReader() throws FileNotFoundException, IOException;
21 class Instance_Name extends Instance
23 String name;
24 Instance_Name(String n)
26 name = n;
28 public String toString()
30 return name;
32 URL toURL() throws MalformedURLException
34 return new URL(name);
36 Reader getReader() throws FileNotFoundException, IOException
38 return new FileReader(new File(name));
41 class Instance_File extends Instance
43 File file;
44 Instance_File(File f)
46 file = f;
48 public String toString()
50 return file.toString();
52 URL toURL() throws MalformedURLException
54 return file.toURL();
56 Reader getReader() throws FileNotFoundException, IOException
58 return new FileReader(file);
61 class Instance_URL extends Instance
63 URL url;
64 Instance_URL(URL u)
66 url = u;
68 public String toString()
70 return url.toString();
72 URL toURL() throws MalformedURLException
74 return url;
76 Reader getReader() throws FileNotFoundException, IOException
78 URLConnection conn = url.openConnection();
79 conn.setDoOutput(true);
80 return new InputStreamReader(conn.getInputStream());
84 private Instance instance;
86 public Filename(String s)
88 /* try whether it's a valid URL */
89 try
91 instance = new Instance_URL(new URL(s));
93 catch (MalformedURLException e)
95 instance = new Instance_Name(s);
99 public Filename(File f)
101 instance = new Instance_File(f);
104 public Filename(URL u)
106 instance = new Instance_URL(u);
109 public URL toURL()
110 throws MalformedURLException
112 return instance.toURL();
115 public String toString()
117 return instance.toString();
120 public Reader getReader()
121 throws FileNotFoundException, IOException
123 return instance.getReader();