File Source: URLBuilder.java

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








SofCheck Inspector Build Version : 2.17854
URLBuilder.java 2009-Jun-25 01:54:24
URLBuilder.class 2009-Sep-02 17:04:17