PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/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. 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. String jarName =MiscUtilities.getFileName(jar.getPath()).toLowerCase();
  69. if(plugin.equalsIgnoreCase(jarName))
  70. {
  71. in = jar.getClassLoader()
  72. .getResourceAsStream(resource);
  73. break;
  74. }
  75. }
  76. }
  77. if(in == null)
  78. {
  79. throw new IOException("Resource not found: " + plugin + "!"
  80. + resource);
  81. }
  82. connected = true;
  83. }
  84. }
  85. public InputStream getInputStream()
  86. throws IOException
  87. {
  88. connect();
  89. return in;
  90. }
  91. public String getHeaderField(String name)
  92. {
  93. if(name.equals("content-type"))
  94. {
  95. String lcResource = resource.toLowerCase();
  96. if(lcResource.endsWith(".html"))
  97. return "text/html";
  98. else if(lcResource.endsWith(".txt"))
  99. return "text/plain";
  100. else if(lcResource.endsWith(".rtf"))
  101. return "text/rtf";
  102. else if(lcResource.endsWith(".gif"))
  103. return "image/gif";
  104. else if(lcResource.endsWith(".jpg")
  105. || lcResource.endsWith(".jpeg"))
  106. return "image/jpeg";
  107. else
  108. return null;
  109. }
  110. else
  111. return null;
  112. }
  113. // private members
  114. private InputStream in;
  115. private String plugin;
  116. private String resource;
  117. }