home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-mac-0.9.sea.hqx / mozilla-mac-0.9 / Chrome / chatzilla.jar / content / chatzilla / rdf.js < prev    next >
Text File  |  2001-05-05  |  6KB  |  213 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Chatzilla
  14.  *
  15.  * The Initial Developer of the Original Code is New Dimensions Consulting,
  16.  * Inc. Portions created by New Dimensions Consulting, Inc. are
  17.  * Copyright (C) 1999 New Dimenstions Consulting, Inc. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Robert Ginda, rginda@ndcico.com, original author
  22.  */
  23.  
  24. const RES_PFX = "http://home.netscape.com/NC-irc#";
  25.  
  26. const nsIRDFResource = Components.interfaces.nsIRDFResource;
  27. const nsIRDFNode = Components.interfaces.nsIRDFNode;
  28.  
  29. function RDFHelper()
  30. {
  31.     const RDF_MEMORYDS_CONTRACTID =
  32.         "@mozilla.org/rdf/datasource;1?name=in-memory-datasource";
  33.     const RDF_DS_IID = Components.interfaces.nsIRDFDataSource;
  34.  
  35.     const RDF_DS_CONTRACTID = "@mozilla.org/rdf/rdf-service;1";
  36.     const RDF_SVC_IID = Components.interfaces.nsIRDFService;
  37.  
  38.     this.ds =
  39.         Components.classes[RDF_MEMORYDS_CONTRACTID].createInstance(RDF_DS_IID);
  40.     this.svc = 
  41.         Components.classes[RDF_DS_CONTRACTID].getService(RDF_SVC_IID);    
  42.  
  43.     /* predefined nodes */
  44.     this.resRoot     = this.svc.GetResource ("NC:chatzilla-data");
  45.     this.resNullUser = this.svc.GetResource (RES_PFX + "NUSER");
  46.     this.resNullChan = this.svc.GetResource (RES_PFX + "NCHAN");
  47.  
  48.     /* predefined arcs */
  49.     this.resNetwork  = this.svc.GetResource (RES_PFX + "network");
  50.     this.resServer   = this.svc.GetResource (RES_PFX + "server");
  51.     this.resChannel  = this.svc.GetResource (RES_PFX + "channel");
  52.     this.resChanUser = this.svc.GetResource (RES_PFX + "chanuser");
  53.     this.resOp       = this.svc.GetResource (RES_PFX + "op");
  54.     this.resVoice    = this.svc.GetResource (RES_PFX + "voice");
  55.     this.resNick     = this.svc.GetResource (RES_PFX + "nick");
  56.     this.resUser     = this.svc.GetResource (RES_PFX + "user");
  57.     this.resHost     = this.svc.GetResource (RES_PFX + "host");
  58.  
  59.     /* predefined literals */
  60.     this.litTrue     = this.svc.GetLiteral ("true");
  61.     this.litFalse    = this.svc.GetLiteral ("false");
  62.     this.litUnk      = this.svc.GetLiteral ("----"); 
  63.  
  64.     this.ds.Assert (this.resNullUser, this.resOp, this.litFalse, true);
  65.     this.ds.Assert (this.resNullUser, this.resVoice, this.litFalse, true);
  66.     this.ds.Assert (this.resNullUser, this.resNick, this.litUnk, true);
  67.     this.ds.Assert (this.resNullUser, this.resUser, this.litUnk, true);
  68.     this.ds.Assert (this.resNullUser, this.resHost, this.litUnk, true);
  69.     /*
  70.     this.ds.Assert (this.resNullChan, this.resChanUser, this.resNullUser, true);
  71.     */
  72.     this.ds.Assert (this.resRoot, this.resChannel, this.resNullChan, true);
  73.     
  74. }
  75.  
  76. RDFHelper.prototype.GetResource =
  77. function rdf_getr (s)
  78. {
  79.     return this.svc.GetResource(s);
  80. }
  81.  
  82. RDFHelper.prototype.GetLiteral =
  83. function rdf_getl (s)
  84. {
  85.     return this.svc.GetLiteral(s);
  86. }
  87.  
  88. RDFHelper.prototype.Assert =
  89. function rdf_assert (n1, a, n2, f)
  90. {
  91.  
  92.     if (typeof f == "undefined")
  93.         f = true;
  94.  
  95.         //return this.dAssert (n1, a, n2, f);
  96.     return this.ds.Assert (n1, a, n2, f);
  97. }
  98.  
  99. RDFHelper.prototype.Unassert =
  100. function rdf_uassert (n1, a, n2)
  101. {
  102.     /*return this.dUnassert (n1, a, n2);*/
  103.     return this.ds.Unassert (n1, a, n2);
  104. }
  105.  
  106. RDFHelper.prototype.dAssert =
  107. function rdf_dassert (n1, a, n2, f)
  108. {
  109.     var n1v = n1 ? n1.Value : "!!!";
  110.     var av = a ? a.Value : "!!!";
  111.     var n2v = n2 ? n2.Value : "!!!";
  112.     
  113.     if (!n1 || !a || !n2)
  114.         dd(getStackTrace());
  115.     
  116.     this.ds.Assert (n1, a, n2, f)
  117. }
  118.  
  119. RDFHelper.prototype.dUnassert =
  120. function rdf_duassert (n1, a, n2)
  121. {
  122.  
  123.     var n1v = n1 ? n1.Value : "!!!";
  124.     var av = a ? a.Value : "!!!";
  125.     var n2v = n2 ? n2.Value : "!!!";
  126.     
  127.     if (!n1 || !a || !n2)
  128.         dd(getStackTrace());
  129.     
  130.     this.ds.Unassert (n1, a, n2)
  131.  
  132. }
  133.  
  134. RDFHelper.prototype.Change =
  135. function rdf_duassert (n1, a, n2)
  136. {
  137.  
  138.     var oldN2 = this.ds.GetTarget (n1, a, true);
  139.     if (!oldN2)
  140.     {
  141.         dd ("** Unable to change " + n1.Value + " -[" + a.Value + "]->, " +
  142.             "because old value was not found.");
  143.         return null;
  144.     }
  145.     
  146.     return this.ds.Change (n1, a, oldN2, n2);
  147.     
  148. }
  149.  
  150. RDFHelper.prototype.clearTargets =
  151. function rdf_inittree (n1, a, recurse)
  152. {
  153.     if (typeof recurse == "undefined")
  154.         recurse = false;
  155.  
  156.     var targets = this.ds.GetTargets(n1, a, true);
  157.  
  158.     while (targets.hasMoreElements())
  159.     {
  160.         var n2 = targets.getNext().QueryInterface(nsIRDFNode);
  161.  
  162.         if (recurse)
  163.         {
  164.             try
  165.             {
  166.                 var resN2 = n2.QueryInterface(nsIRDFResource);
  167.                 var arcs = this.ds.ArcLabelsOut(resN2);
  168.  
  169.                 while (arcs.hasMoreElements())
  170.                 {
  171.                     arc = arcs.getNext().QueryInterface(nsIRDFNode);
  172.                     this.clearTargets (resN2, arc, true);
  173.                 }
  174.             }
  175.             catch (e)
  176.             {
  177.                 /*
  178.                 dd ("** Caught " + e + " while recursivley clearing " +
  179.                     n2.Value + " **");
  180.                 */
  181.             }
  182.         }
  183.         
  184.         this.Unassert (n1, a, n2);
  185.     }
  186. }        
  187.     
  188.  
  189. RDFHelper.prototype.initTree =
  190. function rdf_inittree (id)
  191. {
  192.     var tree = document.getElementById (id);
  193.     tree.database.AddDataSource (this.ds);
  194. }
  195.  
  196. RDFHelper.prototype.setTreeRoot =
  197. function rdf_settroot (id, root)
  198. {
  199.     var tree = document.getElementById (id);
  200.  
  201.     if (typeof root == "object")
  202.         root = root.Value;
  203.     tree.setAttribute ("ref", root);
  204. }
  205.  
  206. RDFHelper.prototype.getTreeRoot =
  207. function rdf_gettroot (id, root)
  208. {
  209.     var tree = document.getElementById (id);
  210.  
  211.     return tree.getAttribute ("ref");
  212. }
  213.