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