home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / phoenx05.zip / phoenix / components / nsDictionary.js < prev    next >
Text File  |  2002-12-10  |  4KB  |  130 lines

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public License Version
  3.  * 1.1 (the "License"); you may not use this file except in compliance with the
  4.  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  5.  * 
  6.  * Software distributed under the License is distributed on an "AS IS" basis,
  7.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  8.  * the specific language governing rights and limitations under the License.
  9.  * 
  10.  * The Original Code is Mozilla XPCOM Dictionary.
  11.  * 
  12.  * The Initial Developer of the Original Code is Digital Creations 2, Inc.
  13.  * Portions created by Digital Creations 2, Inc. are Copyright (C) 2000 Digital
  14.  * Creations 2, Inc.  All Rights Reserved.
  15.  * 
  16.  * Contributor(s): Martijn Pieters <mj@digicool.com> (original author)
  17.  */
  18.  
  19. /*
  20.  *  nsDictionary XPCOM component
  21.  *  Version: $Revision: 1.6 $
  22.  *
  23.  *  $Id: nsDictionary.js,v 1.6 2002/01/29 21:21:33 dougt%netscape.com Exp $
  24.  */
  25.  
  26. /*
  27.  * Constants
  28.  */
  29. const DICTIONARY_CONTRACTID = '@mozilla.org/dictionary;1';
  30. const DICTIONARY_CID = Components.ID('{1dd0cb45-aea3-4a52-8b29-01429a542863}');
  31. const DICTIONARY_IID = Components.interfaces.nsIDictionary;
  32.  
  33. /*
  34.  * Class definitions
  35.  */
  36.  
  37. /* The nsDictionary class constructor. */
  38. function nsDictionary() {
  39.     this.hash = {};
  40. }
  41.  
  42. /* the nsDictionary class def */
  43. nsDictionary.prototype= {
  44.     hasKey: function(key) { return this.hash.hasOwnProperty(key) },
  45.  
  46.     getKeys: function(count) {
  47.         var asKeys = new Array();
  48.         for (var sKey in this.hash) asKeys.push(sKey);
  49.         count.value = asKeys.length;
  50.         return asKeys;
  51.     },
  52.  
  53.     getValue: function(key) { 
  54.         if (!this.hasKey(key))
  55.             throw Components.Exception("Key doesn't exist");
  56.         return this.hash[key]; 
  57.     },
  58.  
  59.     setValue: function(key, value) { this.hash[key] = value; },
  60.     
  61.     deleteValue: function(key) {
  62.         if (!this.hasKey(key))
  63.             throw Components.Exception("Key doesn't exist");
  64.         var oOld = this.getValue(key);
  65.         delete this.hash[key];
  66.         return oOld;
  67.     },
  68.  
  69.     clear: function() { this.hash = {}; },
  70.  
  71.     QueryInterface: function(iid) {
  72.         if (!iid.equals(Components.interfaces.nsISupports) &&
  73.             !iid.equals(DICTIONARY_IID))
  74.             throw Components.results.NS_ERROR_NO_INTERFACE;
  75.         return this;
  76.     }
  77. };
  78.  
  79. /*
  80.  * Objects
  81.  */
  82.  
  83. /* nsDictionary Module (for XPCOM registration) */
  84. var nsDictionaryModule = {
  85.     registerSelf: function(compMgr, fileSpec, location, type) {
  86.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  87.         compMgr.registerFactoryLocation(DICTIONARY_CID, 
  88.                                         "nsDictionary JS component", 
  89.                                         DICTIONARY_CONTRACTID, 
  90.                                         fileSpec, 
  91.                                         location,
  92.                                         type);
  93.     },
  94.  
  95.     getClassObject: function(compMgr, cid, iid) {
  96.         if (!cid.equals(DICTIONARY_CID))
  97.             throw Components.results.NS_ERROR_NO_INTERFACE;
  98.  
  99.         if (!iid.equals(Components.interfaces.nsIFactory))
  100.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  101.  
  102.         return nsDictionaryFactory;
  103.     },
  104.  
  105.     canUnload: function(compMgr) { return true; }
  106. };
  107.  
  108. /* nsDictionary Class Factory */
  109. var nsDictionaryFactory = {
  110.     createInstance: function(outer, iid) {
  111.         if (outer != null)
  112.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  113.     
  114.         if (!iid.equals(DICTIONARY_IID) &&
  115.             !iid.equals(Components.interfaces.nsISupports))
  116.             throw Components.results.NS_ERROR_INVALID_ARG;
  117.  
  118.         return new nsDictionary();
  119.     }
  120. }
  121.  
  122. /*
  123.  * Functions
  124.  */
  125.  
  126. /* module initialisation */
  127. function NSGetModule(comMgr, fileSpec) { return nsDictionaryModule; }
  128.  
  129. // vim:sw=4:sr:sta:et:sts:
  130.