1 22 23 package org.gjt.sp.jedit.proto.jeditresource; 24 25 import java.io.*; 27 import java.net.*; 28 import org.gjt.sp.jedit.*; 29 31 public class PluginResURLConnection extends URLConnection 32 { 33 public PluginResURLConnection(URL url) 34 throws IOException 35 { 36 super(url); 37 38 String file = url.getFile(); 39 40 int index = file.indexOf('!',0); 41 if(index == -1) 42 { 43 plugin = null; 44 resource = file; 45 } 46 else 47 { 48 int start; 49 if(file.charAt(0) == '/') 50 start = 1; 51 else 52 start = 0; 53 54 plugin = file.substring(start,index); 55 resource = file.substring(index + 1); 56 } 57 58 if(plugin != null && resource.startsWith("/")) 59 resource = resource.substring(1); 60 } 61 62 public void connect() throws IOException 63 { 64 if(!connected) 65 { 66 if(plugin == null) 67 { 68 in = jEdit.class.getResourceAsStream(resource); 69 } 70 else 71 { 72 PluginJAR[] plugins = jEdit.getPluginJARs(); 73 for(int i = 0; i < plugins.length; i++) 74 { 75 PluginJAR jar = plugins[i]; 76 String jarName =MiscUtilities.getFileName(jar.getPath()).toLowerCase(); 77 if(plugin.equalsIgnoreCase(jarName)) 78 { 79 in = jar.getClassLoader() 80 .getResourceAsStream(resource); 81 break; 82 } 83 } 84 } 85 86 if(in == null) 87 { 88 throw new IOException("Resource not found: " + plugin + "!" 89 + resource); 90 } 91 92 connected = true; 93 } 94 } 95 96 public InputStream getInputStream() 97 throws IOException 98 { 99 connect(); 100 return in; 101 } 102 103 public String getHeaderField(String name) 104 { 105 if(name.equals("content-type")) 106 { 107 String lcResource = resource.toLowerCase(); 108 if(lcResource.endsWith(".html")) 109 return "text/html"; 110 else if(lcResource.endsWith(".txt")) 111 return "text/plain"; 112 else if(lcResource.endsWith(".rtf")) 113 return "text/rtf"; 114 else if(lcResource.endsWith(".gif")) 115 return "image/gif"; 116 else if(lcResource.endsWith(".jpg") 117 || lcResource.endsWith(".jpeg")) 118 return "image/jpeg"; 119 else 120 return null; 121 } 122 else 123 return null; 124 } 125 126 private InputStream in; 128 private String plugin; 129 private String resource; 130 } 131 | Popular Tags |