1 package com.jclark.xml.parse; 2 3 import java.net.URL; 4 import java.io.File; 5 import java.io.FileDescriptor; 6 import java.io.FileInputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 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 { open(String systemId, URL baseURL, String publicId)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(), 20 useURL.toString(), 21 useURL, 22 null); 23 } 24 25 /** 26 * Creates an OpenEntity from a file name. 27 */ openFile(String name)28 static public OpenEntity openFile(String name) throws IOException { 29 File file = new File(name); 30 return new OpenEntity(new FileInputStream(file), 31 name, 32 fileToURL(file), 33 null); 34 } 35 36 /** 37 * Creates an OpenEntity for the standard input. 38 */ openStandardInput()39 static public OpenEntity openStandardInput() throws IOException { 40 return new OpenEntity(new FileInputStream(FileDescriptor.in), 41 "<standard input>", 42 userDirURL(), 43 null); 44 } 45 46 /** 47 * Generates a <code>URL</code> from a <code>File</code>. 48 */ fileToURL(File file)49 static public URL fileToURL(File file) { 50 String path = file.getAbsolutePath(); 51 String fSep = System.getProperty("file.separator"); 52 if (fSep != null && fSep.length() == 1) 53 path = path.replace(fSep.charAt(0), '/'); 54 if (path.length() > 0 && path.charAt(0) != '/') 55 path = '/' + path; 56 try { 57 return new URL("file", null, path); 58 } 59 catch (java.net.MalformedURLException e) { 60 /* According to the spec this could only happen if the file 61 protocol were not recognized. */ 62 throw new Error("unexpected MalformedURLException"); 63 } 64 } 65 66 /** 67 * Generates a URL for the current working directory. 68 */ userDirURL()69 static public URL userDirURL() { 70 return fileToURL(new File(System.getProperty("user.dir") 71 + System.getProperty("file.separator"))); 72 } 73 74 } 75 76