comparison xul/content/rdfds.js @ 199:49cb8a445126

restarting with version control of xul sidebar/toolbar
author luginbue
date Fri, 27 Feb 2004 11:24:53 +0100
parents
children
comparison
equal deleted inserted replaced
198:c50e0e77d697 199:49cb8a445126
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is rdfds
13 *
14 * The Initial Developer of the Original Code is Neil Deakin
15 * Portions created by Neil Deakin are Copyright (C) 2002 Neil Deakin.
16 * All Rights Reserved.
17 *
18 * Contributor(s):
19 */
20
21 /* This is a library for easier access to RDF datasources and resources.
22 * It contains four objects, RDFDataSource, RDFNode, RDFLiteral. and
23 * RDFEnumerator.
24 *
25 * An RDF DataSource is a graph of nodes and literals. The constructor
26 * for RDFDataSource takes one argument, a URI of an RDF file to use.
27 * If the URI exists, the contents of the RDF file are loaded. If it
28 * does not exist, resources can be added to it and then written using
29 * this save method. If the URL argument is null, a blank datasource
30 * is created.
31 *
32 * This library is designed for convenience not for efficiency.
33 *
34 * The API is documented at:
35 * http://www.xulplanet.com/tutorials/xultu/rdfds/
36 *
37 * Example:
38 *
39 * var ds=new RDFDataSource("file:///main/mozilla/mimtest.rdf");
40 * var node=ds.getNode("urn:xpimaker:packlist");
41 * var child=ds.getNode("urn:xpimaker:packlist:appinfo");
42 * child=node.addChild(child);
43 * child.addTarget("http://www.xulplanet.com/rdf/xpimaker#appname","Find Files");
44 * ds.save();
45 *
46 */
47
48 var RDFService = "@mozilla.org/rdf/rdf-service;1";
49 RDFService = Components.classes[RDFService].getService();
50 RDFService = RDFService.QueryInterface(Components.interfaces.nsIRDFService);
51
52 var RDFContainerUtilsService = "@mozilla.org/rdf/container-utils;1";
53 RDFContainerUtilsService = Components.classes[RDFContainerUtilsService].getService();
54 RDFContainerUtilsService = RDFContainerUtilsService.QueryInterface(Components.interfaces.nsIRDFContainerUtils);
55
56 /* RDFLoadObserver
57 * this object is necessary to listen to RDF files being loaded. The Init
58 * function should be called to initialize the callback when the RDF file is
59 * loaded.
60 */
61 function RDFLoadObserver(){}
62
63 RDFLoadObserver.prototype =
64 {
65 callback: null,
66 callbackDataSource: null,
67
68 Init: function(c,cDS){
69 this.callback=c;
70 this.callbackDataSource=cDS;
71 },
72
73 QueryInterface: function(iid){
74 if (iid.equals(Components.interfaces.nsIRDFXMLSinkObserver)) return this;
75 else throw Components.results.NS_ERROR_NO_INTERFACE;
76 },
77
78 onBeginLoad : function(sink){},
79 onInterrupt : function(sink){},
80 onResume : function(sink){},
81 onError : function(sink,status,msg){},
82
83 onEndLoad : function(sink){
84 if (this.callback!=null) this.callback(this.callbackDataSource);
85 }
86 };
87
88 function RDFDataSource(uri,callbackFn)
89 {
90 if (uri==null) this.datasource=null;
91 else this.load(uri,callbackFn);
92 }
93
94 RDFDataSource.prototype.load=
95 function(uri,callbackFn)
96 {
97 if (uri.indexOf(":") == -1){
98 var docurl=document.location.href;
99 if (document.location.pathname == null) uri=docurl+"/"+uri;
100 else uri=docurl.substring(0,docurl.lastIndexOf("/")+1)+uri;
101 }
102
103 if (callbackFn == null){
104 this.datasource=RDFService.GetDataSourceBlocking(uri);
105 }
106 else {
107 this.datasource=RDFService.GetDataSource(uri);
108 var ds;
109 try {
110 var ds=this.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
111 }
112 catch (ex){
113 callbackFn(this);
114 return;
115 }
116 if (ds.loaded){
117 callbackFn(this);
118 return;
119 }
120
121 var packObserver=new RDFLoadObserver();
122 packObserver.Init(callbackFn,this);
123
124 var rawsource=this.datasource;
125 rawsource=rawsource.QueryInterface(Components.interfaces.nsIRDFXMLSink);
126 rawsource.addXMLSinkObserver(packObserver);
127 }
128 }
129
130 RDFDataSource.prototype.Init=
131 function (dsource)
132 {
133 this.datasource=dsource;
134 }
135
136 RDFDataSource.prototype.parseFromString=
137 function (str,baseUri)
138 {
139 if (this.datasource==null) this.makeemptyds();
140 var ios=Components.classes["@mozilla.org/network/io-service;1"]
141 .getService(Components.interfaces.nsIIOService);
142 baseUri=ios.newURI(baseUri,null,null);
143 var xmlParser=Components.classes["@mozilla.org/rdf/xml-parser;1"]
144 .createInstance(Components.interfaces.nsIRDFXMLParser);
145 xmlParser.parseString(this.datasource,baseUri,str);
146 }
147
148 RDFDataSource.prototype.serializeToString=
149 function ()
150 {
151 var outputStream = {
152 data: "",
153 close : function(){},
154 flush : function(){},
155 write : function (buffer,count){
156 this.data += buffer;
157 return count;
158 },
159 writeFrom : function (stream,count){},
160 isNonBlocking: false
161 }
162 this.serializeToStream(outputStream);
163 return outputStream.data;
164 }
165
166 RDFDataSource.prototype.serializeToStream=
167 function (outputStream)
168 {
169 var ser=Components.classes["@mozilla.org/rdf/xml-serializer;1"]
170 .createInstance(Components.interfaces.nsIRDFXMLSerializer);
171 ser.init(this.datasource);
172 ser.QueryInterface(Components.interfaces.nsIRDFXMLSource).Serialize(outputStream);
173 }
174
175 RDFDataSource.prototype.makeemptyds=
176 function (uri)
177 {
178 this.datasource=Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"]
179 .createInstance(Components.interfaces.nsIRDFDataSource);
180 }
181
182 RDFDataSource.prototype.getAllResources=
183 function ()
184 {
185 if (this.datasource==null) return null;
186 return new RDFEnumerator(this.datasource.GetAllResources(),this.datasource);
187 }
188
189 RDFDataSource.prototype.getRawDataSource=
190 function ()
191 {
192 if (this.datasource==null) this.makeemptyds();
193 return this.datasource;
194 }
195
196 RDFDataSource.prototype.getNode=
197 function (uri)
198 {
199 if (this.datasource==null) this.makeemptyds();
200 var node=new RDFNode(uri,this);
201 return node;
202 }
203
204 RDFDataSource.prototype.getAnonymousNode=
205 function ()
206 {
207 if (this.datasource==null) this.makeemptyds();
208
209 var anon=RDFService.GetAnonymousResource();
210 var node=new RDFNode();
211 node.Init(anon,this.datasource);
212 return node;
213 }
214
215 RDFDataSource.prototype.getLiteral=
216 function (uri)
217 {
218 if (this.datasource==null) this.makeemptyds();
219
220 return new RDFLiteral(uri,this);
221 }
222
223 RDFDataSource.prototype.refresh=
224 function (sync)
225 {
226 try {
227 var ds=this.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
228 ds.Refresh(sync);
229 return true;
230 }
231 catch (ex){
232 return false;
233 }
234 }
235
236 RDFDataSource.prototype.save=
237 function ()
238 {
239 try {
240 var ds=this.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
241 ds.Flush();
242 return true;
243 }
244 catch (ex){
245 return false;
246 }
247 }
248
249 RDFDataSource.prototype.copyAllToDataSource=
250 function (dsource2)
251 {
252 if (this.datasource==null) this.makeemptyds();
253 if (dsource2.datasource==null) dsource2.makeemptyds();
254
255 var dsource1=this.datasource;
256 dsource2=dsource2.datasource;
257
258 var sourcelist=dsource1.GetAllResources();
259 while(sourcelist.hasMoreElements()){
260 var source=sourcelist.getNext();
261 var props=dsource1.ArcLabelsOut(source);
262 while(props.hasMoreElements()){
263 var prop=props.getNext();
264 prop=prop.QueryInterface(Components.interfaces.nsIRDFResource);
265 var target=dsource1.GetTarget(source,prop,true);
266 if (target!=null) dsource2.Assert(source,prop,target,true);
267 }
268 }
269 }
270
271 RDFDataSource.prototype.deleteRecursive=
272 function (val)
273 {
274 var node;
275 var dsource=this.datasource;
276
277 if (dsource==null) return;
278
279 if (typeof val == "string") node=RDFService.GetResource(val);
280 else node=val.source;
281
282 this.deleteRecursiveH(dsource,node); // remove descendants
283
284 // remove the node itself
285 var props=dsource.ArcLabelsIn(node);
286 while(props.hasMoreElements()){
287 var prop=props.getNext();
288 var source=dsource.GetSource(prop,node,true);
289 dsource.Unassert(source,prop,node);
290 }
291 }
292
293 RDFDataSource.prototype.deleteRecursiveH=
294 function (dsource,node)
295 {
296 var props=dsource.ArcLabelsOut(node);
297 while(props.hasMoreElements()){
298 var prop=props.getNext();
299 var target=dsource.GetTarget(node,prop,true);
300 try {
301 target=target.QueryInterface(Components.interfaces.nsIRDFResource);
302 this.deleteRecursiveH(dsource,target);
303 }
304 catch (e){}
305 dsource.Unassert(node,prop,target)
306 }
307 }
308
309 function RDFNode(uri,dsource)
310 {
311 if (uri==null) this.source=null;
312 else this.source=RDFService.GetResource(uri);
313
314 if (dsource==null) this.datasource=null;
315 else this.datasource=dsource.datasource;
316
317 this.container=null;
318 }
319
320 RDFNode.prototype.Init=
321 function (source,dsource)
322 {
323 this.source=source;
324 this.datasource=dsource;
325 this.container=null;
326 }
327
328 RDFNode.prototype.getValue=
329 function ()
330 {
331 return this.source.Value;
332 }
333
334 RDFNode.prototype.rlify=
335 function (val)
336 {
337 var res=null;
338
339 if (val!=null){
340 try {
341 val=val.QueryInterface(Components.interfaces.nsIRDFResource);
342 res=new RDFNode();
343 res.Init(val,this.datasource);
344 }
345 catch (ex){
346 try {
347 val=val.QueryInterface(Components.interfaces.nsIRDFLiteral);
348 res=new RDFLiteral();
349 res.Init(val,this.datasource);
350 }
351 catch (ex2){
352 }
353 }
354 }
355 return res;
356 }
357
358 RDFNode.prototype.makeres=
359 function (val)
360 {
361 if (typeof val == "string") return RDFService.GetResource(val);
362 else return val.source;
363 }
364
365 RDFNode.prototype.makelit=
366 function (val)
367 {
368 if (typeof val == "string") return RDFService.GetLiteral(val);
369 else return val.source;
370 }
371
372 RDFNode.prototype.makecontain=
373 function ()
374 {
375 if (this.container!=null) return true;
376
377 var RDFContainer = '@mozilla.org/rdf/container;1';
378 RDFContainer = Components.classes[RDFContainer].createInstance();
379 RDFContainer = RDFContainer.QueryInterface(Components.interfaces.nsIRDFContainer);
380
381 try {
382 RDFContainer.Init(this.datasource,this.source);
383 this.container=RDFContainer;
384 return true;
385 }
386 catch (ex){
387 return false;
388 }
389 }
390
391 RDFNode.prototype.addTarget=
392 function (prop,target)
393 {
394 prop=this.makeres(prop);
395 target=this.makelit(target);
396 this.datasource.Assert(this.source,prop,target,true);
397 }
398
399 RDFNode.prototype.addTargetOnce=
400 function (prop,target)
401 {
402 prop=this.makeres(prop);
403 target=this.makelit(target);
404
405 var oldtarget=this.datasource.GetTarget(this.source,prop,true);
406 if (oldtarget!=null){
407 this.datasource.Change(this.source,prop,oldtarget,target);
408 }
409 else {
410 this.datasource.Assert(this.source,prop,target,true);
411 }
412 }
413
414 RDFNode.prototype.modifyTarget=
415 function (prop,oldtarget,newtarget)
416 {
417 prop=this.makeres(prop);
418 oldtarget=this.makelit(oldtarget);
419 newtarget=this.makelit(newtarget);
420 this.datasource.Change(this.source,prop,oldtarget,newtarget);
421 }
422
423 RDFNode.prototype.modifySource=
424 function (prop,oldsource,newsource)
425 {
426 prop=this.makeres(prop);
427 oldsource=this.makeres(oldsource);
428 newsource=this.makeres(newsource);
429 this.datasource.Move(oldsource,newsource,prop,this.source);
430 }
431
432 RDFNode.prototype.targetExists=
433 function (prop,target)
434 {
435 prop=this.makeres(prop);
436 target=this.makelit(target);
437 return this.datasource.HasAssertion(this.source,prop,target,true);
438 }
439
440 RDFNode.prototype.removeTarget=
441 function (prop,target)
442 {
443 prop=this.makeres(prop);
444 target=this.makelit(target);
445 this.datasource.Unassert(this.source,prop,target);
446 }
447
448 RDFNode.prototype.getProperties=
449 function ()
450 {
451 return new RDFEnumerator(this.datasource.ArcLabelsOut(this.source),this.datasource);
452 }
453
454 RDFNode.prototype.getInProperties=
455 function ()
456 {
457 return new RDFEnumerator(this.datasource.ArcLabelsIn(this.source),this.datasource);
458 }
459
460 RDFNode.prototype.propertyExists=
461 function (prop)
462 {
463 prop=this.makeres(prop);
464 return this.datasource.hasArcOut(this.source,prop);
465 }
466
467 RDFNode.prototype.inPropertyExists=
468 function (prop)
469 {
470 prop=this.makeres(prop);
471 return this.datasource.hasArcIn(this.source,prop);
472 }
473
474 RDFNode.prototype.getTarget=
475 function (prop)
476 {
477 prop=this.makeres(prop);
478 return this.rlify(this.datasource.GetTarget(this.source,prop,true));
479 }
480
481 RDFNode.prototype.getSource=
482 function (prop)
483 {
484 prop=this.makeres(prop);
485 var src=this.datasource.GetSource(prop,this.source,true);
486 if (src==null) return null;
487 var res=new RDFNode();
488 res.Init(src,this.datasource);
489 return res;
490 }
491
492 RDFNode.prototype.getTargets=
493 function (prop)
494 {
495 prop=this.makeres(prop);
496 return new RDFEnumerator(
497 this.datasource.GetTargets(this.source,prop,true),this.datasource);
498 }
499
500 RDFNode.prototype.getSources=
501 function (prop)
502 {
503 prop=this.makeres(prop);
504 return new RDFEnumerator(
505 this.datasource.GetSources(prop,this.source,true),this.datasource);
506 }
507
508 RDFNode.prototype.makeBag=
509 function ()
510 {
511 this.container=RDFContainerUtilsService.MakeBag(this.datasource,this.source);
512 }
513
514 RDFNode.prototype.makeSeq=
515 function ()
516 {
517 this.container=RDFContainerUtilsService.MakeSeq(this.datasource,this.source);
518 }
519
520 RDFNode.prototype.makeAlt=
521 function ()
522 {
523 this.container=RDFContainerUtilsService.MakeAlt(this.datasource,this.source);
524 }
525
526 RDFNode.prototype.isBag=
527 function ()
528 {
529 return RDFContainerUtilsService.isBag(this.datasource,this.source);
530 }
531
532 RDFNode.prototype.isSeq=
533 function ()
534 {
535 return RDFContainerUtilsService.isSeq(this.datasource,this.source);
536 }
537
538 RDFNode.prototype.isAlt=
539 function ()
540 {
541 return RDFContainerUtilsService.isAlt(dsource,this.source);
542 }
543
544 RDFNode.prototype.isContainer=
545 function ()
546 {
547 return RDFContainerUtilsService.IsContainer(this.datasource,this.source);
548 }
549
550 RDFNode.prototype.getChildCount=
551 function ()
552 {
553 if (this.makecontain()){
554 return this.container.GetCount();
555 }
556 return -1;
557 }
558
559 RDFNode.prototype.getChildren=
560 function ()
561 {
562 if (this.makecontain()){
563 return new RDFEnumerator(this.container.GetElements(),this.datasource);
564 }
565 else return null;
566 }
567
568 RDFNode.prototype.addChild=
569 function (child,exists)
570 {
571 if (this.makecontain()){
572 var childres=null;
573 if (typeof child == "string"){
574 childres=RDFService.GetResource(child);
575 child=new RDFNode();
576 child.Init(childres,this.datasource);
577 }
578 else childres=child.source;
579
580 if (!exists && this.container.IndexOf(childres)>=0) return child;
581
582 this.container.AppendElement(childres);
583 return child;
584 }
585 else return null;
586 }
587
588 RDFNode.prototype.addChildAt=
589 function (child,idx)
590 {
591 if (this.makecontain()){
592 var childres=null;
593 if (typeof child == "string"){
594 childres=RDFService.GetResource(child);
595 child=new RDFNode();
596 child.Init(childres,this.datasource);
597 }
598 else childres=child.source;
599 this.container.InsertElementAt(childres,idx,true);
600 return child;
601 }
602 else return null;
603 }
604
605 RDFNode.prototype.removeChild=
606 function (child)
607 {
608 if (this.makecontain()){
609 var childres=null;
610 if (typeof child == "string"){
611 childres=RDFService.GetResource(child);
612 child=new RDFNode();
613 child.Init(childres,this.datasource);
614 }
615 else childres=child.source;
616 this.container.RemoveElement(childres,true);
617 return child;
618 }
619 else return null;
620 }
621
622 RDFNode.prototype.removeChildAt=
623 function (idx)
624 {
625 if (this.makecontain()){
626 var childres=this.container.RemoveElementAt(idx,true);
627 return this.rlify(childres);
628 }
629 else return null;
630 }
631
632 RDFNode.prototype.getChildIndex=
633 function (child)
634 {
635 if (this.makecontain()){
636 return this.container.IndexOf(child.source);
637 }
638 else return -1;
639 }
640
641 RDFNode.prototype.type="Node";
642
643
644 function RDFLiteral(val,dsource)
645 {
646 if (val==null) this.source=null;
647 else this.source=RDFService.GetLiteral(val);
648
649 if (dsource==null) this.datasource=null;
650 else this.datasource=dsource.datasource;
651 }
652
653 RDFLiteral.prototype.Init=
654 function (source,dsource)
655 {
656 this.source=source;
657 this.datasource=dsource;
658 }
659
660 RDFLiteral.prototype.getValue=
661 function ()
662 {
663 return this.source.Value;
664 }
665
666 RDFLiteral.prototype.makeres=
667 function (val)
668 {
669 if (typeof val == "string") return RDFService.GetResource(val);
670 else return val.source;
671 }
672
673 RDFLiteral.prototype.makelit=
674 function (val)
675 {
676 if (typeof val == "string") return RDFService.GetLiteral(val);
677 else return val.source;
678 }
679
680 RDFLiteral.prototype.modifySource=
681 function (prop,oldsource,newsource)
682 {
683 prop=this.makeres(prop);
684 oldsource=this.makeres(oldsource);
685 newsource=this.makeres(newsource);
686 this.datasource.Move(oldsource,newsource,prop,this.source);
687 }
688
689 RDFLiteral.prototype.getInProperties=
690 function (prop)
691 {
692 return new RDFEnumerator(this.datasource.ArcLabelsIn(this.source),this.datasource);
693 }
694
695 RDFLiteral.prototype.inPropertyExists=
696 function (prop)
697 {
698 prop=this.makeres(prop);
699 return this.datasource.hasArcIn(this.source,prop);
700 }
701
702 RDFLiteral.prototype.getSource=
703 function (prop)
704 {
705 prop=this.makeres(prop);
706 var src=this.datasource.GetSource(prop,this.source,true);
707 if (src==null) return null;
708 var res=new RDFNode();
709 res.Init(src,this.datasource);
710 return res;
711 }
712
713 RDFLiteral.prototype.getSources=
714 function (prop)
715 {
716 prop=this.makeres(prop);
717 return new RDFEnumerator(
718 this.datasource.GetSources(prop,this.source,true),this.datasource);
719 }
720
721 RDFLiteral.prototype.type="Literal";
722
723
724 function RDFEnumerator(enumeration,dsource)
725 {
726 this.enumeration=enumeration;
727 this.datasource=dsource;
728 }
729
730 RDFEnumerator.prototype.hasMoreElements=
731 function ()
732 {
733 return this.enumeration.hasMoreElements();
734 }
735
736 RDFEnumerator.prototype.getNext=
737 function ()
738 {
739 var res=null;
740 var val=this.enumeration.getNext();
741
742 if (val!=null){
743 try {
744 val=val.QueryInterface(Components.interfaces.nsIRDFResource);
745 res=new RDFNode();
746 res.Init(val,this.datasource);
747 }
748 catch (ex){
749 try {
750 val=val.QueryInterface(Components.interfaces.nsIRDFLiteral);
751 res=new RDFLiteral();
752 res.Init(val,this.datasource);
753 }
754 catch (ex2){
755 }
756 }
757 }
758 return res;
759 }
760