You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URLBuilder.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import com.dmdirc.events.UserErrorEvent;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.plugins.PluginManager;
  26. import com.dmdirc.ui.themes.ThemeManager;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. import javax.inject.Inject;
  30. import javax.inject.Provider;
  31. import javax.inject.Singleton;
  32. import net.engio.mbassy.bus.MBassador;
  33. /**
  34. * Provides methods for building URLs to reference DMDirc resources.
  35. */
  36. @Singleton
  37. public class URLBuilder {
  38. /** Provider to retrieve a plugin manager instance when needed. */
  39. private final Provider<PluginManager> pluginManagerProvider;
  40. /** Provider to retrieve a theme manager instance when needed. */
  41. private final Provider<ThemeManager> themeManagerProvider;
  42. /** The event bus to post errors on. */
  43. private final MBassador eventBus;
  44. /**
  45. * Creates a new instance of URLBuilder.
  46. *
  47. * @param pluginManagerProvider Provider to retrieve a plugin manager instance when needed.
  48. * @param themeManagerProvider Provider to retrieve a theme manager instance when needed.
  49. * @param eventBus The event bus to post errors on.
  50. */
  51. @Inject
  52. public URLBuilder(
  53. final Provider<PluginManager> pluginManagerProvider,
  54. final Provider<ThemeManager> themeManagerProvider,
  55. final MBassador eventBus) {
  56. this.pluginManagerProvider = pluginManagerProvider;
  57. this.themeManagerProvider = themeManagerProvider;
  58. this.eventBus = eventBus;
  59. }
  60. /**
  61. * Constructs an URL pointing to the specified resource on the file system.
  62. *
  63. * @param path The path that the URL is for
  64. *
  65. * @return An URL corresponding to the specified path, or null on failure
  66. */
  67. public URL getUrlForFile(final String path) {
  68. final String prefix = path.startsWith("file://") ? "" : "file://";
  69. try {
  70. return new URL(prefix + path);
  71. } catch (MalformedURLException ex) {
  72. eventBus.publish(new UserErrorEvent(ErrorLevel.HIGH, ex, "Unable to build file URL", ""));
  73. return null;
  74. }
  75. }
  76. /**
  77. * Constructs an URL pointing to the specified resource within a jar file.
  78. *
  79. * @param jarFile Path to the jar file (including scheme)
  80. * @param path Path to the resource within the jar file
  81. *
  82. * @return An URL corresponding to the specified resource, or null on failure
  83. */
  84. public URL getUrlForJarFile(final String jarFile, final String path) {
  85. try {
  86. String url = "jar:" + getUrl(jarFile) + "!/" + path;
  87. if (url.startsWith("jar:file://")) {
  88. url = "jar:file:/" + url.substring(11);
  89. }
  90. return new URL(url);
  91. } catch (MalformedURLException ex) {
  92. eventBus.publish(new UserErrorEvent(ErrorLevel.HIGH, ex, "Unable to build jar URL", ""));
  93. return null;
  94. }
  95. }
  96. /**
  97. * Constructs an URL pointing to the specified resource within the DMDirc project.
  98. *
  99. * @param resource The path to the resource
  100. *
  101. * @return An URL corresponding to the specified resource
  102. */
  103. public URL getUrlForDMDircResource(final String resource) {
  104. return Thread.currentThread().getContextClassLoader().getResource(resource);
  105. }
  106. /**
  107. * Builds an URL pointing to a resource within a DMDirc theme.
  108. *
  109. * @param theme The theme which the resource is located in
  110. * @param path The path within the theme of the resource
  111. *
  112. * @return An URL corresponding to the specified resource, or null on failure
  113. */
  114. public URL getUrlForThemeResource(final String theme, final String path) {
  115. return getUrlForJarFile(themeManagerProvider.get().getDirectory()
  116. + theme + ".zip", path);
  117. }
  118. /**
  119. * Builds an URL pointing to a resource within a DMDirc plugin.
  120. *
  121. * @param plugin The plugin which the resource is located in
  122. * @param path The path within the theme of the resource
  123. *
  124. * @return An URL corresponding to the specified resource, or null on failure
  125. */
  126. public URL getUrlForPluginResource(final String plugin, final String path) {
  127. return getUrlForJarFile(
  128. pluginManagerProvider.get().getPluginInfoByName(plugin)
  129. .getMetaData().getPluginUrl().getPath(), path);
  130. }
  131. /**
  132. * Constructs an URL corresponding to the described resource.
  133. *
  134. * @param spec The resource location. May take the form of: <ul>
  135. * <li>dmdirc://com/dmdirc/etc/
  136. * <li>jar://path/to/jarfile:path/inside/jarfile
  137. * <li>zip://path/to/zipfile:path/inside/zipfile
  138. * <li>theme://theme_name:file/inside/theme
  139. * <li>plugin://plugin_name:file/inside/plugin
  140. * <li>http://server/path
  141. * <li>https://server/path
  142. * <li>[file://]/path/on/filesystem</ul>
  143. *
  144. * @return An URL corresponding to the specified resource, or null on failure
  145. */
  146. public URL getUrl(final String spec) {
  147. if (spec.startsWith("dmdirc://")) {
  148. return getUrlForDMDircResource(spec.substring(9));
  149. } else if (spec.startsWith("jar://") || spec.startsWith("zip://")) {
  150. final int offset = spec.indexOf(':', 6);
  151. if (offset < 0) {
  152. eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, null,
  153. "Invalid URL, must contain ':': " + spec, ""));
  154. return null;
  155. } else {
  156. return getUrlForJarFile(spec.substring(6, offset), spec.substring(offset + 1));
  157. }
  158. } else if (spec.startsWith("plugin://")) {
  159. final int offset = spec.indexOf(':', 8);
  160. if (offset < 0) {
  161. eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, null,
  162. "Invalid URL, must contain ':': " + spec, ""));
  163. return null;
  164. } else {
  165. return getUrlForPluginResource(spec.substring(9, offset), spec.substring(offset + 1));
  166. }
  167. } else if (spec.startsWith("theme://")) {
  168. final int offset = spec.indexOf(':', 8);
  169. if (offset < 0) {
  170. eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, null,
  171. "Invalid URL, must contain ':': " + spec, ""));
  172. return null;
  173. } else {
  174. return getUrlForThemeResource(spec.substring(8, offset), spec.substring(offset + 1));
  175. }
  176. } else if (spec.startsWith("http://") || spec.startsWith("https://")) {
  177. try {
  178. return new URL(spec);
  179. } catch (MalformedURLException ex) {
  180. eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, ex,
  181. "Unable to load resource", ""));
  182. return null;
  183. }
  184. } else {
  185. return getUrlForFile(spec);
  186. }
  187. }
  188. }