StringUtils.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 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2009 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27 */
28 
34 //------------------------------------------------------------------------------
35 //
36 // String utility JSM configuration.
37 //
38 //------------------------------------------------------------------------------
39 
40 EXPORTED_SYMBOLS = [ "SBString",
41  "SBBrandedString",
42  "SBFormattedString",
43  "SBBrandedFormattedString",
44  "SBFormattedCountString",
45  "SBStringBrandShortName",
46  "SBStringGetDefaultBundle",
47  "SBStringGetBrandBundle",
48  "SBStringBundle",
49  "StringSet" ];
50 
51 
52 //------------------------------------------------------------------------------
53 //
54 // String utility defs.
55 //
56 //------------------------------------------------------------------------------
57 
58 const Cc = Components.classes;
59 const Ci = Components.interfaces;
60 const Cr = Components.results
61 
62 
63 //------------------------------------------------------------------------------
64 //
65 // String utility globals.
66 //
67 //------------------------------------------------------------------------------
68 
69 var gSBStringDefaultBundle = null;
71 
72 
73 //------------------------------------------------------------------------------
74 //
75 // String utility localization services.
76 //
77 //------------------------------------------------------------------------------
78 
93 function SBString(aKey, aDefault, aStringBundle) {
94  // Use default Songbird string bundle utility object if no bundle specified.
95  if (!aStringBundle)
96  return SBStringGetDefaultBundle().get(aKey, aDefault);
97 
98  // Set the default value.
99  var value = aKey;
100  if ((typeof(aDefault) != "undefined") && (aDefault !== null))
101  value = aDefault;
102 
103  // Try getting the string from the bundle.
104  try {
105  value = aStringBundle.GetStringFromName(aKey);
106  } catch(ex) {}
107 
108  return value;
109 }
110 
111 
128 function SBBrandedString(aKey, aDefault, aStringBundle) {
129  return SBFormattedString(aKey,
131  aDefault,
132  aStringBundle);
133 }
134 
135 
152 function SBFormattedString(aKey, aParams, aDefault, aStringBundle) {
153  // Use default Songbird string bundle utility object if no bundle specified.
154  if (!aStringBundle)
155  return SBStringGetDefaultBundle().format(aKey, aParams, aDefault);
156 
157  // Set the default value.
158  var value = aKey;
159  if ((typeof(aDefault) != "undefined") && (aDefault !== null))
160  value = aDefault;
161 
162  // Try formatting string from bundle.
163  try {
164  value = aStringBundle.formatStringFromName(aKey, aParams, aParams.length);
165  } catch(ex) {}
166 
167  return value;
168 }
169 
170 
190 function SBBrandedFormattedString(aKey, aParams, aDefault, aStringBundle) {
191  return SBFormattedString(aKey,
192  aParams.concat(SBStringBrandShortName()),
193  aDefault,
194  aStringBundle);
195 }
196 
197 
221 function SBFormattedCountString(aKeyBase,
222  aCount,
223  aParams,
224  aDefault,
225  aStringBundle) {
226  // Use default Songbird string bundle utility object if no bundle specified.
227  if (!aStringBundle) {
228  return SBStringGetDefaultBundle().formatCountString(aKeyBase,
229  aCount,
230  aParams,
231  aDefault);
232  }
233 
234  // Get the format parameters.
235  var params;
236  if (aParams)
237  params = aParams;
238  else
239  params = [ aCount ];
240 
241  // Produce the string key.
242  var key = aKeyBase;
243  if (aCount == 1)
244  key += "_1";
245  else
246  key += "_n";
247 
248  // Set the default value.
249  var value = aKeyBase;
250  if ((typeof(aDefault) != "undefined") && (aDefault !== null))
251  value = aDefault;
252 
253  // Try formatting the string from the bundle.
254  try {
255  value = aStringBundle.formatStringFromName(key, params, params.length);
256  } catch(ex) {}
257 
258  return value;
259 }
260 
261 
269  return SBString("brandShortName", "Songbird", SBStringGetBrandBundle());
270 }
271 
272 
273 //------------------------------------------------------------------------------
274 //
275 // Internal string utility services.
276 //
277 //------------------------------------------------------------------------------
278 
286  if (!gSBStringDefaultBundle)
287  gSBStringDefaultBundle = new SBStringBundle();
288 
289  return gSBStringDefaultBundle;
290 }
291 
292 
300  if (!gSBStringBrandBundle) {
302  Cc["@mozilla.org/intl/stringbundle;1"]
303  .getService(Ci.nsIStringBundleService)
304  .createBundle("chrome://branding/locale/brand.properties");
305  }
306 
307  return gSBStringBrandBundle;
308 }
309 
310 
311 //------------------------------------------------------------------------------
312 //
313 // Songbird string bundle utility object.
314 //
315 // The Songbird string bundle utility object provides an expanded set of
316 // string bundle services. In particular, this object allows string bundles to
317 // include other string bundles and to include bundle strings in other bundle
318 // strings.
319 // To include one or more string bundles in a top-level string bundle, define
320 // the string "include_bundle_list" in the top-level bundle. This string should
321 // consist of a comma separated list of included string bundle URI's. When
322 // the Songbird string bundle utility object looks up a string, it will look in
323 // the top-level string bundle and all included string bundles. The included
324 // string bundles can include additional string bundles too.
325 // To include a bundle string in another bundle string, encapsulate the
326 // included bundle string in "&" and ";", much like XML entities. Use "&"
327 // for a literal "&".
328 //
329 // Example:
330 //
331 // include_bundle_list=chrome://bundles1.properties,chrome://bundle2.properties
332 //
333 // string1=World
334 // string2=Hello &string1; & Everyone Else
335 //
336 // string2 evaluates to "Hello World & Everyone Else".
337 //
338 //------------------------------------------------------------------------------
339 
349 function SBStringBundle(aBundle)
350 {
351  // Get the string bundle service. */
352  this._stringBundleService = Cc["@mozilla.org/intl/stringbundle;1"]
353  .getService(Ci.nsIStringBundleService);
354 
355  // Use the default Songbird string bundle if none specified.
356  if (!aBundle)
357  aBundle = "chrome://songbird/locale/songbird.properties";
358 
359  // Initialize the bundle list.
360  this._bundleList = [];
361 
362  // Load the string bundle.
363  this._loadBundle(aBundle);
364 }
365 
366 // Define the class.
367 SBStringBundle.prototype = {
368  // Set the constructor.
370 
371  //
372  // Songbird string bundle utility object fields.
373  //
374  // _stringBundleService String bundle service object.
375  // _bundleList List of string bundles.
376  //
377 
378  _stringBundleService: null,
379  _bundleList: null,
380 
381 
393  get: function SBStringBundle_get(aKey, aDefault) {
394  // Set the default string value.
395  var value = aKey;
396  if ((typeof(aDefault) != "undefined") && (aDefault !== null))
397  value = aDefault;
398 
399  // Get the string from the bundle list.
400  for (var i = 0; i < this._bundleList.length; i++) {
401  var bundle = this._bundleList[i];
402  try {
403  value = bundle.GetStringFromName(aKey);
404  break;
405  } catch (ex) {}
406  }
407 
408  // Apply string substitutions.
409  value = this._applySubstitutions(value);
410 
411  return value;
412  },
413 
414 
428  format: function SBStringBundle_format(aKey, aParams, aDefault) {
429  // Set the default string value.
430  var value = aKey;
431  if ((typeof(aDefault) != "undefined") && (aDefault !== null))
432  value = aDefault;
433 
434  // Get the string from the bundle list.
435  for (var i = 0; i < this._bundleList.length; i++) {
436  var bundle = this._bundleList[i];
437  try {
438  value = bundle.formatStringFromName(aKey, aParams, aParams.length);
439  break;
440  } catch (ex) {}
441  }
442 
443  // Apply string substitutions.
444  value = this._applySubstitutions(value);
445 
446  return value;
447  },
448 
449 
470  formatCountString: function SBStringBundle_formatCountString(aKeyBase,
471  aCount,
472  aParams,
473  aDefault) {
474  // Get the format parameters.
475  var params;
476  if (aParams)
477  params = aParams;
478  else
479  params = [ aCount ];
480 
481  // Produce the string key.
482  var key = aKeyBase;
483  if (aCount == 1)
484  key += "_1";
485  else
486  key += "_n";
487 
488  // Set the default string value.
489  var value = aKeyBase;
490  if ((typeof(aDefault) != "undefined") && (aDefault !== null))
491  value = aDefault;
492 
493  // Get the string from the bundle list.
494  for (var i = 0; i < this._bundleList.length; i++) {
495  var bundle = this._bundleList[i];
496  try {
497  value = bundle.formatStringFromName(key, params, params.length);
498  break;
499  } catch (ex) {}
500  }
501 
502  // Apply string substitutions.
503  value = this._applySubstitutions(value);
504 
505  return value;
506  },
507 
508 
509  //----------------------------------------------------------------------------
510  //
511  // Internal Songbird string bundle utility object services.
512  //
513  //----------------------------------------------------------------------------
514 
523  _loadBundle: function SBStringBundle__loadBundle(aBundle) {
524  var bundle = aBundle;
525 
526  // If the bundle is specified as a URI spec string, create a bundle from it.
527  if (typeof(bundle) == "string")
528  bundle = this._stringBundleService.createBundle(bundle);
529 
530  // Add the string bundle to the list of string bundles.
531  this._bundleList.push(bundle);
532 
533  // Get the list of included string bundles.
534  var includeBundleList;
535  try {
536  includeBundleList =
537  bundle.GetStringFromName("include_bundle_list").split(",");
538  } catch (ex) {
539  includeBundleList = [];
540  }
541 
542  // Load each of the included string bundles.
543  for (var i = 0; i < includeBundleList.length; i++) {
544  this._loadBundle(includeBundleList[i]);
545  }
546  },
547 
548 
549  /*
550  * Apply any string bundle substitutions to the string specified by aString.
551  *
552  * \param aString String to which to apply substitutions.
553  */
554 
555  _applySubstitutions: function SBStringBundle__applySubstitutions(aString) {
556  // Set up a replacement function.
557  var _this = this;
558  var replaceFunc = function(aMatch, aKey) {
559  if (aKey == "amp")
560  return "&";
561  return _this.get(aKey, "");
562  }
563 
564  // Apply all string substitutions and return result.
565  return aString.replace(/&([^&;]*);/g, replaceFunc);
566  }
567 };
568 
569 
570 //------------------------------------------------------------------------------
571 //
572 // Songbird string set utility object.
573 //
574 // A string set is a string containing a set of strings, separated by a
575 // delimiter (e.g., " "). A string set does not contain duplicates. A string
576 // set could be used, for example, with the class attribute of a DOM element.
577 //
578 //------------------------------------------------------------------------------
579 
580 var StringSet = {
593  add: function StringSet_add(aStringSet, aString, aDelimiter) {
594  // Get the delimiter.
595  var delimiter = aDelimiter;
596  if (!delimiter)
597  delimiter = " ";
598 
599  // Split the string set into an array and add string if it's not already
600  // present.
601  var stringSet = aStringSet.split(delimiter);
602  if (stringSet.indexOf(aString) < 0)
603  stringSet.push(aString);
604 
605  // Return new string set.
606  return stringSet.join(delimiter);
607  },
608 
609 
622  remove: function StringSet_remove(aStringSet, aString, aDelimiter) {
623  // Get the delimiter.
624  var delimiter = aDelimiter;
625  if (!delimiter)
626  delimiter = " ";
627 
628  // Split the string set into an array and remove all instances of the
629  // string.
630  var stringSet = aStringSet.split(delimiter);
631  var stringCount = stringSet.length;
632  for (var i = stringCount - 1; i >= 0; i--) {
633  if (stringSet[i] == aString)
634  stringSet.splice(i, 1);
635  }
636 
637  // Return new string set.
638  return stringSet.join(delimiter);
639  },
640 
641 
654  contains: function StringSet_contains(aStringSet, aString, aDelimiter) {
655  // Get the delimiter.
656  var delimiter = aDelimiter;
657  if (!delimiter)
658  delimiter = " ";
659 
660  // Split the string set into an array and return whether the string set
661  // contains the string.
662  var stringSet = aStringSet.split(delimiter);
663  return (stringSet.indexOf(aString) >= 0);
664  }
665 };
666 
function SBBrandedString(aKey, aDefault, aStringBundle)
EXPORTED_SYMBOLS
Definition: StringUtils.jsm:40
var StringSet
const Cr
Definition: StringUtils.jsm:60
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
function SBStringGetDefaultBundle()
function SBBrandedFormattedString(aKey, aParams, aDefault, aStringBundle)
ui plugin add("draggable","cursor",{start:function(e, ui){var t=$('body');if(t.css("cursor")) ui.options._cursor=t.css("cursor");t.css("cursor", ui.options.cursor);}, stop:function(e, ui){if(ui.options._cursor)$('body').css("cursor", ui.options._cursor);}})
function SBStringGetBrandBundle()
function SBStringBrandShortName()
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
var gSBStringBrandBundle
Definition: StringUtils.jsm:70
const Cc
Definition: StringUtils.jsm:58
var bundle
function SBFormattedCountString(aKeyBase, aCount, aParams, aDefault, aStringBundle)
DataRemote prototype constructor
const Ci
Definition: StringUtils.jsm:59
var _this
function SBStringBundle(aBundle)
return null
Definition: FeedWriter.js:1143
countRef value
Definition: FeedWriter.js:1423
restoreHistoryPrecursor aCount
_getSelectedPageStyle s i