URLUtils.jsm
Go to the documentation of this file.
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 
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 
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 
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 
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 
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 
const Cu
nsString encodeURIComponent(const nsString &c)
EXPORTED_SYMBOLS
Definition: URLUtils.jsm:38
var ioService
const Cr
Definition: URLUtils.jsm:49
return null
Definition: FeedWriter.js:1143
function newURI(aURLString)
function url(spec)
var uri
Definition: FeedWriter.js:1135
const Ci
Definition: URLUtils.jsm:48
const Cc
Definition: URLUtils.jsm:47
_getSelectedPageStyle s i