home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / URLUtils.jsm < prev    next >
Text File  |  2012-06-08  |  6KB  |  212 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set sw=2 :miv */
  3. /*
  4.  *=BEGIN SONGBIRD GPL
  5.  *
  6.  * This file is part of the Songbird web player.
  7.  *
  8.  * Copyright(c) 2005-2010 POTI, Inc.
  9.  * http://www.songbirdnest.com
  10.  *
  11.  * This file may be licensed under the terms of of the
  12.  * GNU General Public License Version 2 (the ``GPL'').
  13.  *
  14.  * Software distributed under the License is distributed
  15.  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
  16.  * express or implied. See the GPL for the specific language
  17.  * governing rights and limitations.
  18.  *
  19.  * You should have received a copy of the GPL along with this
  20.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  21.  * or write to the Free Software Foundation, Inc.,
  22.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23.  *
  24.  *=END SONGBIRD GPL
  25.  */
  26.  
  27. /**
  28.  * \file  URLUtils.jsm
  29.  * \brief Javascript source for the URL utility services.
  30.  */
  31.  
  32. //------------------------------------------------------------------------------
  33. //
  34. // URL utility JSM configuration.
  35. //
  36. //------------------------------------------------------------------------------
  37.  
  38. EXPORTED_SYMBOLS = [ "URLUtils" ];
  39.  
  40.  
  41. //------------------------------------------------------------------------------
  42. //
  43. // URL utility defs.
  44. //
  45. //------------------------------------------------------------------------------
  46.  
  47. const Cc = Components.classes;
  48. const Ci = Components.interfaces;
  49. const Cr = Components.results
  50. const Cu = Components.utils
  51.  
  52.  
  53. //------------------------------------------------------------------------------
  54. //
  55. // URL utility services.
  56. //
  57. //------------------------------------------------------------------------------
  58.  
  59. var URLUtils = {
  60.   //----------------------------------------------------------------------------
  61.   //
  62.   // URL utility services.
  63.   //
  64.   //----------------------------------------------------------------------------
  65.  
  66.   /**
  67.    * Return an nsIURI object for the URI spec and base URI specified by aSpec
  68.    * and aBaseURI using the charset specified by aCharset.
  69.    *
  70.    * \param aSpec               URI spec.
  71.    * \param aCharset            URI character set.
  72.    * \param aBaseURI            Base URI.
  73.    *
  74.    * \return                    nsIURI object.
  75.    */
  76.  
  77.   newURI: function URLUtils_newURI(aSpec, aCharset, aBaseURI) {
  78.     var ioService = Cc["@mozilla.org/network/io-service;1"]
  79.                       .getService(Ci.nsIIOService);
  80.     var uri = null;
  81.     try {
  82.       uri = ioService.newURI(aSpec, aCharset, aBaseURI);
  83.     } catch (ex) {
  84.       Cu.reportError(ex);
  85.     }
  86.     return uri;
  87.   },
  88.  
  89.  
  90.   /**
  91.    * Add the query parameters specified by aQueryParams to the URL specified by
  92.    * aURL and return the resulting URL.
  93.    *
  94.    * \param aURL                URL to which to add query parameters.
  95.    * \param aQueryParams        Query parameters.
  96.    *
  97.    * \return                    URL with query parameters added.
  98.    */
  99.  
  100.   addQuery: function URLUtils_addQuery(aURL, aQueryParams) {
  101.     // Get the URL query parameters.
  102.     var urlQueryParams = {};
  103.     this.extractQuery(aURL, urlQueryParams);
  104.  
  105.     // Add the query parameters.
  106.     for (paramName in aQueryParams) {
  107.       urlQueryParams[paramName] = aQueryParams[paramName];
  108.     }
  109.  
  110.     // Produce the new URL query string.
  111.     var urlQuery = this.produceQuery(urlQueryParams);
  112.  
  113.     // Copy the specified URL and set the new query string.
  114.     var url = Cc["@mozilla.org/network/standard-url;1"]
  115.                 .createInstance(Ci.nsIStandardURL);
  116.     url.init(Ci.nsIStandardURL.URLTYPE_STANDARD, 0, aURL, null, null);
  117.     url.QueryInterface(Ci.nsIURL);
  118.     url.query = urlQuery;
  119.  
  120.     return url.spec;
  121.   },
  122.  
  123.  
  124.   /**
  125.    * Produce a URL query string from the query parameters specified by the
  126.    * aParams object.  Produce a URL query parameter for each field in the
  127.    * aParams object, applying proper URI encoding.
  128.    *
  129.    * \param aParams             URL query params.
  130.    *
  131.    * \return                    A URL query string.
  132.    */
  133.  
  134.   produceQuery: function URLUtils_produceQuery(aParams) {
  135.     if (typeof aParams !== 'object') { return null; }
  136.  
  137.     var stack = [];
  138.     for (var param in aParams) {
  139.       stack.push(encodeURIComponent(param) + "=" +
  140.                  encodeURIComponent(aParams[param]));
  141.     }
  142.  
  143.     return stack.join("&");
  144.   },
  145.  
  146.  
  147.   /**
  148.    * Extract the query parameters from the URL specified by aURLSpec and return
  149.    * them in the object specified by aQueryParams.  If the URL contains a query,
  150.    * return true; otherwise, return false.
  151.    *
  152.    * \param aURLSpec            URL from which to extract query.
  153.    * \param aQueryParams        Returned query parameters.
  154.    *
  155.    * \return                    True if URL contains a query.
  156.    */
  157.  
  158.   extractQuery: function URLUtils_extractQuery(aURLSpec, aQueryParams) {
  159.     // Get a URL object.
  160.     var url = this.newURI(aURLSpec).QueryInterface(Ci.nsIURL);
  161.  
  162.     // Get the URL query string.  Just return false if URL does not contain a
  163.     // query string.
  164.     var queryStr = url.query;
  165.     if (!queryStr)
  166.       return false;
  167.  
  168.     // Extract the query parameters.
  169.     var queryParamList = queryStr.split("&");
  170.     for (var i = 0; i < queryParamList.length; i++) {
  171.       var queryParam = queryParamList[i].split("=");
  172.       if (queryParam.length >= 2) {
  173.         aQueryParams[queryParam[0]] = decodeURIComponent(queryParam[1]);
  174.       }
  175.       else if (queryParam.length == 1) {
  176.         aQueryParams[queryParam[0]] = true;
  177.       }
  178.     }
  179.  
  180.     return true;
  181.   },
  182.  
  183.  
  184.   convertURLToDisplayName: function URLUtils_convertURLToDisplayName(aURL) {
  185.     var urlDisplay = "";
  186.  
  187.     try {
  188.       urlDisplay = decodeURI( aURL );
  189.     } catch(err) {
  190.       dump("convertURLToDisplayName, oops! URI decode weirdness: " + err + "\n");
  191.     }
  192.  
  193.     // Set the title display
  194.     if ( urlDisplay.lastIndexOf('/') != -1 )
  195.     {
  196.       urlDisplay = urlDisplay.substring( urlDisplay.lastIndexOf('/') + 1, urlDisplay.length );
  197.     }
  198.     else if ( aURL.lastIndexOf('\\') != -1 )
  199.     {
  200.       urlDisplay = aURL.substring( aURL.lastIndexOf('\\') + 1, aURL.length );
  201.     }
  202.  
  203.     if ( ! urlDisplay.length )
  204.     {
  205.       urlDisplay = aURL;
  206.     }
  207.  
  208.     return urlDisplay;
  209.   }
  210. };
  211.  
  212.