/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/proto/jeditresource/PluginResURLConnection.java
Java | 130 lines | 93 code | 13 blank | 24 comment | 25 complexity | 59d62b0b1062a4553bc29fce879a90bb MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
1/*
2 * PluginResURLConnection.java - jEdit plugin resource URL connection
3 * :tabSize=8:indentSize=8:noTabs=false:
4 * :folding=explicit:collapseFolds=1:
5 *
6 * Copyright (C) 1999, 2000, 2001 Slava Pestov
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23package org.gjt.sp.jedit.proto.jeditresource;
24
25//{{{ Imports
26import java.io.*;
27import java.net.*;
28import org.gjt.sp.jedit.*;
29//}}}
30
31public 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 members
127 private InputStream in;
128 private String plugin;
129 private String resource;
130}