01: package com.jclark.xml.parse;
02:
03: import java.net.URL;
04: import java.io.File;
05: import java.io.FileDescriptor;
06: import java.io.FileInputStream;
07: import java.io.IOException;
08: import java.io.InputStream;
09:
10: /**
11: * A default implementation of <code>EntityManager</code>.
12: * @see EntityManager
13: * @version $Revision: 1.4 $ $Date: 1998/02/17 04:20:35 $
14: */
15: public class EntityManagerImpl implements EntityManager {
16: public OpenEntity open(String systemId, URL baseURL, String publicId)
17: throws IOException {
18: URL useURL = new URL(baseURL, systemId);
19: return new OpenEntity(useURL.openStream(), useURL.toString(),
20: useURL, null);
21: }
22:
23: /**
24: * Creates an OpenEntity from a file name.
25: */
26: static public OpenEntity openFile(String name) throws IOException {
27: File file = new File(name);
28: return new OpenEntity(new FileInputStream(file), name,
29: fileToURL(file), null);
30: }
31:
32: /**
33: * Creates an OpenEntity for the standard input.
34: */
35: static public OpenEntity openStandardInput() throws IOException {
36: return new OpenEntity(new FileInputStream(FileDescriptor.in),
37: "<standard input>", userDirURL(), null);
38: }
39:
40: /**
41: * Generates a <code>URL</code> from a <code>File</code>.
42: */
43: static public URL fileToURL(File file) {
44: String path = file.getAbsolutePath();
45: String fSep = System.getProperty("file.separator");
46: if (fSep != null && fSep.length() == 1)
47: path = path.replace(fSep.charAt(0), '/');
48: if (path.length() > 0 && path.charAt(0) != '/')
49: path = '/' + path;
50: try {
51: return new URL("file", null, path);
52: } catch (java.net.MalformedURLException e) {
53: /* According to the spec this could only happen if the file
54: protocol were not recognized. */
55: throw new Error("unexpected MalformedURLException");
56: }
57: }
58:
59: /**
60: * Generates a URL for the current working directory.
61: */
62: static public URL userDirURL() {
63: return fileToURL(new File(System.getProperty("user.dir")
64: + System.getProperty("file.separator")));
65: }
66:
67: }
|