PageRenderTime 95ms CodeModel.GetById 86ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/proto/jeditresource/PluginResURLConnection.java

#
Java | 131 lines | 94 code | 13 blank | 24 comment | 25 complexity | df08a82037646a7fadea43aca89354c5 MD5 | raw file
  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					if(MiscUtilities.getFileName(jar.getPath())
 77						.equalsIgnoreCase(plugin))
 78					{
 79						in = jar.getClassLoader()
 80							.getResourceAsStream(
 81							resource);
 82						break;
 83					}
 84				}
 85			}
 86
 87			if(in == null)
 88			{
 89				throw new IOException("Resource not found: "
 90					+ resource);
 91			}
 92
 93			connected = true;
 94		}
 95	}
 96
 97	public InputStream getInputStream()
 98		throws IOException
 99	{
100		connect();
101		return in;
102	}
103
104	public String getHeaderField(String name)
105	{
106		if(name.equals("content-type"))
107		{
108			String lcResource = resource.toLowerCase();
109			if(lcResource.endsWith(".html"))
110				return "text/html";
111			else if(lcResource.endsWith(".txt"))
112				return "text/plain";
113			else if(lcResource.endsWith(".rtf"))
114				return "text/rtf";
115			else if(lcResource.endsWith(".gif"))
116				return "image/gif";
117			else if(lcResource.endsWith(".jpg")
118				|| lcResource.endsWith(".jpeg"))
119				return "image/jpeg";
120			else
121				return null;
122		}
123		else
124			return null;
125	}
126
127	// private members
128	private InputStream in;
129	private String plugin;
130	private String resource;
131}