home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / components / nsDictionary.js < prev    next >
Text File  |  2001-02-14  |  4KB  |  126 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.4 $
  22.  *
  23.  *  $Id: nsDictionary.js,v 1.4 2000/09/13 23:51:27 rayw%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.registerComponentWithType(DICTIONARY_CID, 
  87.             "nsDictionary JS component", DICTIONARY_CONTRACTID, fileSpec, location,
  88.             true, true, type);
  89.     },
  90.  
  91.     getClassObject: function(compMgr, cid, iid) {
  92.         if (!cid.equals(DICTIONARY_CID))
  93.             throw Components.results.NS_ERROR_NO_INTERFACE;
  94.  
  95.         if (!iid.equals(Components.interfaces.nsIFactory))
  96.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  97.  
  98.         return nsDictionaryFactory;
  99.     },
  100.  
  101.     canUnload: function(compMgr) { return true; }
  102. };
  103.  
  104. /* nsDictionary Class Factory */
  105. var nsDictionaryFactory = {
  106.     createInstance: function(outer, iid) {
  107.         if (outer != null)
  108.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  109.     
  110.         if (!iid.equals(DICTIONARY_IID) &&
  111.             !iid.equals(Components.interfaces.nsISupports))
  112.             throw Components.results.NS_ERROR_INVALID_ARG;
  113.  
  114.         return new nsDictionary();
  115.     }
  116. }
  117.  
  118. /*
  119.  * Functions
  120.  */
  121.  
  122. /* module initialisation */
  123. function NSGetModule(comMgr, fileSpec) { return nsDictionaryModule; }
  124.  
  125. // vim:sw=4:sr:sta:et:sts:
  126.