001: /*
002: * PluginResURLConnection.java - jEdit plugin resource URL connection
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2000, 2001 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.proto.jeditresource;
024:
025: //{{{ Imports
026: import java.io.*;
027: import java.net.*;
028: import org.gjt.sp.jedit.*;
029:
030: //}}}
031:
032: public class PluginResURLConnection extends URLConnection {
033: public PluginResURLConnection(URL url) throws IOException {
034: super (url);
035:
036: String file = url.getFile();
037:
038: int index = file.indexOf('!', 0);
039: if (index == -1) {
040: plugin = null;
041: resource = file;
042: } else {
043: int start;
044: if (file.charAt(0) == '/')
045: start = 1;
046: else
047: start = 0;
048:
049: plugin = file.substring(start, index);
050: resource = file.substring(index + 1);
051: }
052:
053: if (plugin != null && resource.startsWith("/"))
054: resource = resource.substring(1);
055: }
056:
057: public void connect() throws IOException {
058: if (!connected) {
059: if (plugin == null) {
060: in = jEdit.class.getResourceAsStream(resource);
061: } else {
062: PluginJAR[] plugins = jEdit.getPluginJARs();
063: for (int i = 0; i < plugins.length; i++) {
064: PluginJAR jar = plugins[i];
065: String jarName = MiscUtilities.getFileName(
066: jar.getPath()).toLowerCase();
067: if (plugin.equalsIgnoreCase(jarName)) {
068: in = jar.getClassLoader().getResourceAsStream(
069: resource);
070: break;
071: }
072: }
073: }
074:
075: if (in == null) {
076: throw new IOException("Resource not found: " + plugin
077: + "!" + resource);
078: }
079:
080: connected = true;
081: }
082: }
083:
084: public InputStream getInputStream() throws IOException {
085: connect();
086: return in;
087: }
088:
089: public String getHeaderField(String name) {
090: if (name.equals("content-type")) {
091: String lcResource = resource.toLowerCase();
092: if (lcResource.endsWith(".html"))
093: return "text/html";
094: else if (lcResource.endsWith(".txt"))
095: return "text/plain";
096: else if (lcResource.endsWith(".rtf"))
097: return "text/rtf";
098: else if (lcResource.endsWith(".gif"))
099: return "image/gif";
100: else if (lcResource.endsWith(".jpg")
101: || lcResource.endsWith(".jpeg"))
102: return "image/jpeg";
103: else
104: return null;
105: } else
106: return null;
107: }
108:
109: // private members
110: private InputStream in;
111: private String plugin;
112: private String resource;
113: }
|