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

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