Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

URLBuilder.java 7.5KB

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