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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2006-2011 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.logger.ErrorLevel;
  24. import com.dmdirc.logger.Logger;
  25. import com.dmdirc.plugins.PluginManager;
  26. import com.dmdirc.ui.themes.ThemeManager;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. /**
  30. * Provides methods for building URLs to reference DMDirc resources.
  31. *
  32. * @author chris
  33. */
  34. public final class URLBuilder {
  35. /**
  36. * Creates a new instance of URLBuilder.
  37. */
  38. private URLBuilder() {
  39. // Shouldn't be constructed
  40. }
  41. /**
  42. * Constructs an URL pointing to the specified resource on the file system.
  43. *
  44. * @param path The path that the URL is for
  45. * @return An URL corresponding to the specified path, or null on failure
  46. */
  47. public static URL buildFileURL(final String path) {
  48. final String prefix = path.startsWith("file://") ? "" : "file://";
  49. try {
  50. return new URL(prefix + path);
  51. } catch (MalformedURLException ex) {
  52. Logger.appError(ErrorLevel.HIGH, "Unable to build file URL", ex);
  53. return null;
  54. }
  55. }
  56. /**
  57. * Constructs an URL pointing to the specified resource within a jar file.
  58. *
  59. * @param jarFile Path to the jar file (including scheme)
  60. * @param path Path to the resource within the jar file
  61. * @return An URL corresponding to the specified resource, or null on failure
  62. */
  63. public static URL buildJarURL(final String jarFile, final String path) {
  64. try {
  65. String url = "jar:" + buildURL(jarFile) + "!/" + path;
  66. if (url.startsWith("jar:file://")) {
  67. url = "jar:file:/" + url.substring(11);
  68. }
  69. return new URL(url);
  70. } catch (MalformedURLException ex) {
  71. Logger.appError(ErrorLevel.HIGH, "Unable to build jar URL", ex);
  72. return null;
  73. }
  74. }
  75. /**
  76. * Constructs an URL pointing to the specified resource within the DMDirc
  77. * project.
  78. *
  79. * @param resource The path to the resource
  80. * @return An URL corresponding to the specified resource
  81. */
  82. public static URL buildDMDircURL(final String resource) {
  83. return Thread.currentThread().getContextClassLoader().getResource(resource);
  84. }
  85. /**
  86. * Builds an URL pointing to a resource within a DMDirc theme.
  87. *
  88. * @param theme The theme which the resource is located in
  89. * @param path The path within the theme of the resource
  90. * @return An URL corresponding to the specified resource, or null on failure
  91. */
  92. public static URL buildThemeURL(final String theme, final String path) {
  93. return buildJarURL(ThemeManager.getThemeDirectory() + theme + ".zip", path);
  94. }
  95. /**
  96. * Builds an URL pointing to a resource within a DMDirc plugin.
  97. *
  98. * @param plugin The plugin which the resource is located in
  99. * @param path The path within the theme of the resource
  100. * @return An URL corresponding to the specified resource, or null on failure
  101. */
  102. public static URL buildPluginURL(final String plugin, final String path) {
  103. return buildJarURL(
  104. PluginManager.getPluginManager().getPluginInfoByName(plugin).getFullFilename(),
  105. path);
  106. }
  107. /**
  108. * Constructs an URL corresponding to the described resource.
  109. *
  110. * @param spec The resource location. May take the form of: <ul>
  111. * <li>dmdirc://com/dmdirc/etc/
  112. * <li>jar://path/to/jarfile:path/inside/jarfile
  113. * <li>zip://path/to/zipfile:path/inside/zipfile
  114. * <li>theme://theme_name:file/inside/theme
  115. * <li>plugin://plugin_name:file/inside/plugin
  116. * <li>http://server/path
  117. * <li>https://server/path
  118. * <li>[file://]/path/on/filesystem</ul>
  119. *
  120. * @return An URL corresponding to the specified resource, or null on failure
  121. */
  122. public static URL buildURL(final String spec) {
  123. if (spec.startsWith("dmdirc://")) {
  124. return buildDMDircURL(spec.substring(9));
  125. } else if (spec.startsWith("jar://") || spec.startsWith("zip://")) {
  126. final int offset = spec.indexOf(':', 6);
  127. if (offset < 0) {
  128. Logger.userError(ErrorLevel.LOW, "Invalid URL, must contain ':': " + spec);
  129. return null;
  130. } else {
  131. return buildJarURL(spec.substring(6, offset), spec.substring(offset + 1));
  132. }
  133. } else if (spec.startsWith("plugin://")) {
  134. final int offset = spec.indexOf(':', 8);
  135. if (offset < 0) {
  136. Logger.userError(ErrorLevel.LOW, "Invalid URL, must contain ':': " + spec);
  137. return null;
  138. } else {
  139. return buildPluginURL(spec.substring(9, offset), spec.substring(offset + 1));
  140. }
  141. } else if (spec.startsWith("theme://")) {
  142. final int offset = spec.indexOf(':', 8);
  143. if (offset < 0) {
  144. Logger.userError(ErrorLevel.LOW, "Invalid URL, must contain ':': " + spec);
  145. return null;
  146. } else {
  147. return buildThemeURL(spec.substring(8, offset), spec.substring(offset + 1));
  148. }
  149. } else if (spec.startsWith("http://") || spec.startsWith("https://")) {
  150. try {
  151. return new URL(spec);
  152. } catch (MalformedURLException ex) {
  153. Logger.userError(ErrorLevel.MEDIUM, "Unable to load resource", ex);
  154. return null;
  155. }
  156. } else {
  157. return buildFileURL(spec);
  158. }
  159. }
  160. }