[Jslib] Re: RDF classes

Martin Kutschker dreckskerl@glump.at
Mon, 11 Feb 2002 21:04:58 +0100


Martin Kutschker wrote:

> 
> The container classes lack the insertAt and removeFrom calls the XPCOM 
> interface provides.


RDFContainer.prototype.addSeqAt = function(aSeq, aIndex) {
   return this.addContainer(aSeq, "seq", aIndex);
};

RDFContainer.prototype.addBagAt = function(aBag, aIndex) {
   return this.addContainer(aBag, "bag", aIndex);
};

RDFContainer.prototype.addAlt = function(aAlt, aIndex) {
   return this.addContainer(aAlt, "alt", aIndex);
};

// XXX add third optional parameter
RDFContainer.prototype.addContainer = function(aContainer, aType, aIndex)
{
   if(this.isValid()) {
     if(!aContainer || !aType)
       jslibError(null, "Must supply two arguments", null, 
JS_RDFCONTAINER_FILE+":addContainer");

// XXX add check
     if(aIndex && aIndex < 1)
       jslibError(null, "Third argument must be integer greater then or 
equal 1", null, JS_RDFCONTAINER_FILE+":addContainer");

     var res = this.RDF.GetResource(this.subject+":"+aContainer);

     if( this.resource ) {
       this.RDFC.Init(this.dsource, this.resource );

       if(aType == "bag") {
         this.RDFCUtils.MakeBag(this.dsource, res);
       } else if(aType == "alt") {
         this.RDFCUtils.MakeAlt(this.dsource, res);
       } else {
         this.RDFCUtils.MakeSeq(this.dsource, res);
       }
// XXX insert or append
       if(aIndex)
         this.RDFC.InsertElementAt(res, aIndex, true);
       else
         this.RDFC.AppendElement(res);
     }
     return new RDFContainer(aType, this.subject+":"+aContainer, 
this.parent, this.dsource);
   } else {
     jslibError(null, "RDFContainer is no longer valid!\n", 
"NS_ERROR_UNEXPECTED",
           JS_RDFCONTAINER_FILE+":addContainer");
     return null;
   }
};

RDFContainer.prototype.addNodeAt = function(aNode, aIndex) {
   return this.addNode(aNode, aIndex);
};

// XXX add "hidden" parameter (or revamp the function as private)
RDFContainer.prototype.addNode = function(aNode, aIndex) {
   if(this.isValid()) {
// XXX add check
     if(!aNode)
       jslibError(null, "Must supply one argument", null, 
JS_RDFCONTAINER_FILE+":addNode");

     if(aIndex && aIndex < 1)
       jslibError(null, "Second argument must be integer greater then or 
equal 1", null, JS_RDFCONTAINER_FILE+":addNode");

     var res = this.RDF.GetResource(this.subject+":"+aNode);
     this.RDFC.Init(this.dsource, this.resource);

// XXX insert or append
     if (aIndex)
       this.RDFC.InsertElementAt(res, aIndex, true);
     else
       this.RDFC.AppendElement(res);

     return new RDFResource("node", this.subject+":"+aNode, 
this.subject, this.dsource);
   } else {
       jslibError(null, "RDFContainer is no longer valid!\n", 
"NS_ERROR_UNEXPECTED",
             JS_RDFCONTAINER_FILE+":addNode");
		return null;
	}
};

Masi