]> git.donarmstrong.com Git - x_full.git/blob - .mozilla/firefox/default/extensions/{b9db16a4-6edc-47ec-a1f4-b86292ed211d}/components/dhUtilService.js
update inkscape preferences
[x_full.git] / .mozilla / firefox / default / extensions / {b9db16a4-6edc-47ec-a1f4-b86292ed211d} / components / dhUtilService.js
1 /******************************************************************************
2  *
3  * This code belongs to its author. All rights reserved.
4  *
5  * Author    : michel.gutierrez@gmail.com
6  *
7  ******************************************************************************/
8
9 /**
10 * @fileoverview Accessing global utilities
11 * @author mig
12 * @version 1.0
13 */
14
15 /**
16  * Constants.
17  */
18 const NS_UTIL_SERVICE_CID = Components.ID("{dbd8dc72-2cdf-44ad-bf9a-5dc7a3fc3036}");
19 const NS_UTIL_SERVICE_PROG_ID = "@downloadhelper.net/util-service;1";
20
21 Node=null;
22 XPathResult=null;
23
24 /**
25 * Service constructor
26 * @class Service accessing global utilities
27 */
28 function UtilService() {
29         this.stringBundle=Components.classes["@mozilla.org/intl/stringbundle;1"].getService().
30                 QueryInterface(Components.interfaces.nsIStringBundleService).
31                         createBundle("chrome://dwhelper/locale/strings.properties");
32         this.RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService().
33                 QueryInterface(Components.interfaces.nsIRDFService);
34         this.RDFCUtils = Components.classes["@mozilla.org/rdf/container-utils;1"].getService().
35                 QueryInterface(Components.interfaces.nsIRDFContainerUtils);
36 }
37
38 UtilService.prototype = {
39 }
40
41 UtilService.prototype.getRDF = function() {
42         return this.RDF;
43 }
44
45 UtilService.prototype.getRDFCUtils = function() {
46         return this.RDFCUtils;
47 }
48
49 UtilService.prototype.getText = function(name) {
50         try {
51                 return this.stringBundle.GetStringFromName(name);
52         } catch(e) {
53                 return name;
54         }
55 }
56
57 UtilService.prototype.getFText=function(name,params,length) {
58         if(params==null)
59                 params=[];
60         try {
61                 return this.stringBundle.formatStringFromName(name,params,params.length);
62         } catch(e) {
63                 return name;
64         }
65 }
66
67 UtilService.prototype.getVersion=function(uuid) {
68         var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService().
69                 QueryInterface(Components.interfaces.nsIRDFService);
70         var RDFCUtils = Components.classes["@mozilla.org/rdf/container-utils;1"].getService().
71                 QueryInterface(Components.interfaces.nsIRDFContainerUtils);
72         var extMgr=Components.classes["@mozilla.org/extensions/manager;1"].
73                 getService(Components.interfaces.nsIExtensionManager);          
74         var target=extMgr.datasource.GetTarget(
75                 RDF.GetResource("urn:mozilla:item:"+uuid),
76                 RDF.GetResource("http://www.mozilla.org/2004/em-rdf#version"),
77                 true);
78         if(target==null)
79                 return null;
80         return target.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
81 }
82
83 UtilService.prototype.getPropertyValue = function(ds,res,prop) {
84         var target=ds.GetTarget(res,prop,true);
85         if(target==null)
86                 return null;
87         return target.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
88 }
89
90 UtilService.prototype.getPropertyValueRS = function(ds,res,prop) {
91         return this.getPropertyValue(ds,res,this.RDF.GetResource(prop));
92 }
93
94 UtilService.prototype.getPropertyValueSR = function(ds,res,prop) {
95         return this.getPropertyValue(ds,this.RDF.GetResource(res),prop);
96 }
97
98 UtilService.prototype.getPropertyValueSS = function(ds,res,prop) {
99         return this.getPropertyValue(ds,this.RDF.GetResource(res),this.RDF.GetResource(prop));
100 }
101
102 UtilService.prototype.setPropertyValue = function(ds,res,prop,value) {
103         value=this.RDF.GetLiteral(value);
104         this.removeProperties(ds,res,prop);
105         if(value!=null) {
106                 ds.Assert(res,prop,value,true);
107         }
108 }
109
110 UtilService.prototype.setPropertyValueSR = function(ds,res,prop,value) {
111         this.setPropertyValue(ds,this.RDF.GetResource(res),prop,value);
112 }
113
114 UtilService.prototype.setPropertyValueRS = function(ds,res,prop,value) {
115         this.setPropertyValue(ds,res,this.RDF.GetResource(prop),value);
116 }
117
118 UtilService.prototype.setPropertyValueSS = function(ds,res,prop,value) {
119         this.setPropertyValue(ds,this.RDF.GetResource(res),this.RDF.GetResource(prop),value);
120 }
121
122 UtilService.prototype.removeProperties = function(ds,res,prop) {
123         var i=ds.GetTargets(res,prop,true);
124         var targets=[];
125         while(i.hasMoreElements()) {
126                 targets.push(i.getNext());
127         }
128         for(var i=0;i<targets.length;i++) {
129                 ds.Unassert(res,prop,targets[i]);
130         }
131 }
132
133 UtilService.prototype.removePropertiesSR = function(ds,res,prop) {
134         this.removeProperties(ds,this.RDF.GetResource(res),prop);
135 }
136
137 UtilService.prototype.removePropertiesRS = function(ds,res,prop) {
138         this.removeProperties(ds,res,this.RDF.GetResource(prop));
139 }
140
141 UtilService.prototype.removePropertiesSS = function(ds,res,prop) {
142         this.removeProperties(ds,this.RDF.GetResource(res),this.RDF.GetResource(prop));
143 }
144
145 UtilService.prototype.getChildResources = function(ds,res,count) {
146         var children=[];
147         var seq=this.RDFCUtils.MakeSeq(ds,res);
148         var j=seq.GetElements();
149         while(j.hasMoreElements()) {
150                 var li=j.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
151                 children.push(li);
152         }
153         count.value=children.length;
154         return children;
155 }
156
157 UtilService.prototype.getChildResourcesS = function(ds,res,count) {
158         return this.getChildResources(ds,this.RDF.GetResource(res),count);
159 }
160
161 UtilService.prototype.getPropertyResource = function(ds,res,prop) {
162         var target=ds.GetTarget(res,prop,true);
163         if(target==null)
164                 return null;
165         return target.QueryInterface(Components.interfaces.nsIRDFResource);
166 }
167
168 UtilService.prototype.getPropertyResourceRS = function(ds,res,prop) {
169         return this.getPropertyResource(ds,res,this.RDF.GetResource(prop));
170 }
171
172 UtilService.prototype.getPropertyResourceSR = function(ds,res,prop) {
173         return this.getPropertyResource(ds,this.RDF.GetResource(res),prop);
174 }
175
176 UtilService.prototype.getPropertyResourceSS = function(ds,res,prop) {
177         return this.getPropertyResource(ds,this.RDF.GetResource(res),this.RDF.GetResource(prop));
178 }
179
180 UtilService.prototype.setPropertyResource = function(ds,res,prop,value) {
181         this.removeProperties(ds,res,prop);
182         if(value!=null) {
183                 ds.Assert(res,prop,value,true);
184         }
185 }
186
187 UtilService.prototype.setPropertyResourceRRS = function(ds,res,prop,value) {
188         this.setPropertyResource(ds,res,prop,this.RDF.GetResource(value));
189 }
190
191 UtilService.prototype.setPropertyResourceRSR = function(ds,res,prop,value) {
192         this.setPropertyResource(ds,res,this.RDF.GetResource(prop),value);
193 }
194
195 UtilService.prototype.setPropertyResourceRSS = function(ds,res,prop,value) {
196         this.setPropertyResource(ds,res,this.RDF.GetResource(prop),this.RDF.GetResource(value));
197 }
198
199 UtilService.prototype.setPropertyResourceSRR = function(ds,res,prop,value) {
200         this.setPropertyResource(ds,this.RDF.GetResource(res),prop,value);
201 }
202
203 UtilService.prototype.setPropertyResourceSRS = function(ds,res,prop,value) {
204         this.setPropertyResource(ds,this.RDF.GetResource(res),prop,this.RDF.GetResource(value));
205 }
206
207 UtilService.prototype.setPropertyResourceSSR = function(ds,res,prop,value) {
208         this.setPropertyResource(ds,this.RDF.GetResource(res),this.RDF.GetResource(prop),value);
209 }
210
211 UtilService.prototype.setPropertyResourceSSS = function(ds,res,prop,value) {
212         this.setPropertyResource(ds,this.RDF.GetResource(res),this.RDF.GetResource(prop),this.RDF.GetResource(value));
213 }
214
215 UtilService.prototype.createNode=function(ds,parentNode,res) {
216         var node=res;
217         if(node==null) {
218                 node=this.RDF.GetAnonymousResource();
219         } 
220         this.setPropertyResourceRSS(ds,node,
221                 "http://www.w3.org/1999/02/22-rdf-syntax-ns#instanceOf",
222                 "http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq");
223         this.setPropertyValueRS(ds,node,
224                 "http://www.w3.org/1999/02/22-rdf-syntax-ns#nextVal","1");
225         
226         if(parentNode!=null) {
227                 parentNode=this.RDFCUtils.MakeSeq(ds,parentNode);
228                 parentNode.AppendElement(node);
229         } 
230         return node;
231 }
232
233 UtilService.prototype.createNodeRS=function(ds,parentNode,resValue) {
234         if(resValue!=null)
235                 resValue=this.RDF.GetResource(resValue);
236         return this.createNode(ds,parentNode,resValue);
237 }
238
239 UtilService.prototype.createNodeSR=function(ds,parentNode,resValue) {
240         if(parentNode!=null)
241                 parentNode=this.RDF.GetResource(parentNode);
242         return this.createNode(ds,parentNode,resValue);
243 }
244
245 UtilService.prototype.createNodeSS=function(ds,parentNode,resValue) {
246         if(parentNode!=null)
247                 parentNode=this.RDF.GetResource(parentNode);
248         if(resValue!=null)
249                 resValue=this.RDF.GetResource(resValue);
250         return this.createNode(ds,parentNode,resValue);
251 }
252
253 UtilService.prototype.createAnonymousNode=function(ds,parentNode) {
254         return this.createNode(ds,parentNode,null);
255 }
256
257 UtilService.prototype.createAnonymousNodeS=function(ds,parentNode) {
258         return this.createAnonymousNode(ds,this.RDF.GetResource(parentNode));
259 }
260
261 UtilService.prototype.createRootNode=function(ds,res) {
262         return this.createNode(ds,null,res);
263 }
264
265 UtilService.prototype.createRootNodeS=function(ds,resValue) {
266         return this.createRootNode(ds,this.RDF.GetResource(resValue));
267 }
268
269 UtilService.prototype.createAnonymousRootNode=function(ds) {
270         return this.createNode(ds,null,null);
271 }
272
273 UtilService.prototype.getDatasourceFromRDFFile=function(file) {
274         var stream = Components.classes['@mozilla.org/network/file-input-stream;1'].
275             createInstance(Components.interfaces.nsIFileInputStream);
276         stream.init(file,1,0,false);
277         var scriptableStream = Components.classes['@mozilla.org/scriptableinputstream;1'].createInstance(Components.interfaces.nsIScriptableInputStream);
278         scriptableStream.init(stream);
279         
280         var fileSize = scriptableStream.available();
281         var fileContents = scriptableStream.read(fileSize);
282
283         scriptableStream.close();
284         stream.close();
285         
286         var parser=Components.classes
287                 ['@mozilla.org/rdf/xml-parser;1'].
288                         createInstance(Components.interfaces.nsIRDFXMLParser);
289         var uri = Components.classes["@mozilla.org/network/standard-url;1"].
290                     createInstance(Components.interfaces.nsIURI);
291         uri.spec = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
292         var memDS=Components.classes
293                 ['@mozilla.org/rdf/datasource;1?name=in-memory-datasource'].
294                         createInstance(Components.interfaces.nsIRDFDataSource);
295         parser.parseString(memDS,uri,fileContents);
296         return memDS;
297 }
298
299 UtilService.prototype.dumpDatasource=function(ds) {
300         if(ds==null)
301                 return;
302         var i = ds.GetAllResources();
303         while(i.hasMoreElements()) {
304                 var source = i.getNext();
305                 var j = ds.ArcLabelsOut(source);
306                 while(j.hasMoreElements()) {
307                         var predicate = j.getNext();
308                         var k = ds.GetTargets(source,predicate,true);
309                         while(k.hasMoreElements()) {
310                                 var target = k.getNext();
311                                 source=source.QueryInterface(Components.interfaces.nsIRDFResource);
312                                 predicate=predicate.QueryInterface(Components.interfaces.nsIRDFResource);
313                                 try {
314                                         target=target.QueryInterface(Components.interfaces.nsIRDFResource);
315                                 } catch(e) {
316                                         target=target.QueryInterface(Components.interfaces.nsIRDFLiteral);
317                                 }
318                                 dump(source.Value+" - "+predicate.Value+" - "+target.Value+"\n");
319                         }
320                 }
321         }
322 }
323
324 UtilService.prototype.getDatasource=function(tree) {
325         if(tree.database==null)
326                 return null;
327         var i=tree.database.GetDataSources();
328         if(i.hasMoreElements()) {
329                 return i.getNext().QueryInterface(Components.interfaces.nsIRDFDataSource);
330         } else {
331                 return null;
332         }
333 }
334
335 UtilService.prototype.clearDatasource=function(tree) {
336         if(tree.database==null)
337                 return;
338         var dss=[];
339         var i=tree.database.GetDataSources();
340         while(i.hasMoreElements()) {
341                 dss.push(i.getNext());
342         }
343         for(var i=0;i<dss.length;i++) {
344                 var ds = dss[i].QueryInterface(Components.interfaces.nsIRDFDataSource);
345                 var i = ds.GetAllResources();
346                 ds.beginUpdateBatch();
347                 while(i.hasMoreElements()) {
348                         var source = i.getNext();
349                         var j = ds.ArcLabelsOut(source);
350                         while(j.hasMoreElements()) {
351                                 var predicate = j.getNext();
352                                 var k = ds.GetTargets(source,predicate,true);
353                                 while(k.hasMoreElements()) {
354                                         var target = k.getNext();
355                                         ds.Unassert(source,predicate,target);
356                                 }
357                         }
358                 }
359                 ds.endUpdateBatch();
360         }
361         for(var i=0;i<dss.length;i++) {
362                 var ds = dss[i].QueryInterface(Components.interfaces.nsIRDFDataSource);
363                 tree.database.RemoveDataSource(ds);
364         }
365 }
366
367 UtilService.prototype.removeDatasources=function(tree) {
368         if(tree.database==null)
369                 return;
370         var dss=[];
371         var i=tree.builder.database.GetDataSources();
372         while(i.hasMoreElements()) {
373                 dss.push(i.getNext());
374         }
375         for(var i=0;i<dss.length;i++) {
376                 var ds = dss[i].QueryInterface(Components.interfaces.nsIRDFDataSource);
377                 tree.builder.database.RemoveDataSource(ds);
378         }
379 }
380
381 UtilService.prototype.setDatasource=function(tree,ds) {
382         this.removeDatasources(tree);
383         if(ds!=null)
384                 tree.builder.database.AddDataSource(ds);
385         tree.builder.rebuild();
386 }
387
388 UtilService.prototype.addDatasource=function(tree,ds) {
389         if(ds!=null)
390                 tree.builder.database.AddDataSource(ds);
391         tree.builder.rebuild();
392 }
393
394 UtilService.prototype.copyDatasource=function(ds) {
395         var memDS=Components.classes
396                 ['@mozilla.org/rdf/datasource;1?name=in-memory-datasource'].
397                     createInstance(Components.interfaces.nsIRDFDataSource);
398         var i = ds.GetAllResources();
399         memDS.beginUpdateBatch();
400         while(i.hasMoreElements()) {
401                 var source = i.getNext();
402                 var j = ds.ArcLabelsOut(source);
403                 while(j.hasMoreElements()) {
404                         var predicate = j.getNext();
405                         var k = ds.GetTargets(source,predicate,true);
406                         while(k.hasMoreElements()) {
407                                 var target = k.getNext();
408                                 memDS.Assert(source,predicate,target,true);
409                         }
410                 }
411         }
412         memDS.endUpdateBatch();
413         
414         return memDS;
415 }
416
417 UtilService.prototype.getParentNode=function(ds,node) {
418         var iter=ds.ArcLabelsIn(node);
419         while(iter.hasMoreElements()) {
420             var arc=iter.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
421             if(ChartletUtil.startsWith(arc.Value,"http://www.w3.org/1999/02/22-rdf-syntax-ns#_")) {
422                 node = ds.GetSource(arc,node,true);
423                 return node;
424             }
425         }
426         return null;
427 }
428
429 UtilService.prototype.getParentNodeS=function(ds,node) {
430         return this.getParentNode(ds,this.RDF.GetResource(node));
431 }
432
433 UtilService.prototype.appendNode=function(ds,parentNode,node) {
434         parentNode=this.RDFCUtils.MakeSeq(ds,parentNode);
435         parentNode.AppendElement(node);
436 }
437
438 UtilService.prototype.appendNodeRS=function(ds,parentNode,node) {
439         this.appendNode(ds,parentNode,this.RDF.GetResource(node));
440 }
441
442 UtilService.prototype.appendNodeSR=function(ds,parentNode,node) {
443         this.appendNode(ds,this.RDF.GetResource(parentNode),node);
444 }
445
446 UtilService.prototype.appendNodeSS=function(ds,parentNode,node) {
447         this.appendNode(ds,this.RDF.GetResource(parentNode),this.RDF.GetResource(node));
448 }
449
450 UtilService.prototype.removeReference = function(ds,res) {
451         var seqElems=[];
452         
453         var deltriplet=[];
454         var i=ds.ArcLabelsIn(res);
455         while(i.hasMoreElements()) {
456             var arc=i.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
457             var j=ds.GetSources(arc,res,true);
458             while(j.hasMoreElements()) {
459                 var source=j.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
460                 var m=/^http\:\/\/www\.w3\.org\/1999\/02\/22\-rdf\-syntax\-ns#_([1-9][0-9]*)$/.exec(arc.Value);
461                 if(m.length==2) {
462                         var seq=ChartletUtil.RDFCUtils.MakeSeq(ds,source);
463                                 seqElems.push({seq: seq, elem: res});                   
464                 } else {
465                         deltriplet.push({
466                                 source: source,
467                                 property: arc,
468                                 target: res
469                         });
470                 }
471             }
472         }
473         
474         var i=ds.ArcLabelsOut(res);
475         while(i.hasMoreElements()) {
476             var arc=i.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
477             var j=ds.GetTargets(res,arc,true);
478             while(j.hasMoreElements()) {
479                 var target=j.getNext().QueryInterface(Components.interfaces.nsIRDFNode);
480                 deltriplet.push({
481                         source: res,
482                         property: arc,
483                         target: target
484                         });
485             }
486         }
487         
488         ds.beginUpdateBatch();
489         for(var i=0;i<deltriplet.length;i++) {
490                 var triplet=deltriplet[i];
491                 ds.Unassert(triplet.source,triplet.property,triplet.target);
492         }
493         for(var i=0;i<seqElems.length;i++) {
494                 seqElems[i].seq.RemoveElement(seqElems[i].elem,true);
495         }
496         ds.endUpdateBatch();
497 }
498
499 UtilService.prototype.removeReferenceS = function(ds,res) {
500         this.removeReference(ds,this.RDF.GetResource(res));
501 }
502
503 UtilService.prototype.xpGetSingleNode = function(node,xpath) {
504         var anode=node.ownerDocument.evaluate(xpath,node,null,
505                 XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;
506         return anode;
507 }
508
509 UtilService.prototype.xpGetNodes = function(node,xpath,count) {
510         var nodes=[];
511     var xpr=node.ownerDocument.evaluate(xpath,
512         node,null,
513         XPathResult.ORDERED_NODE_ITERATOR_TYPE,
514         null);
515     var node0=xpr.iterateNext();
516     while(node0!=null) {
517         nodes.push(node0);
518         node0=xpr.iterateNext();
519     }
520     count.value=nodes.length;
521         return nodes;
522 }
523
524 UtilService.prototype.xpGetStrings = function(node,xpath,count) {
525         var strings=[];
526     var xpr=node.ownerDocument.evaluate(xpath,
527         node,null,
528         XPathResult.ORDERED_NODE_ITERATOR_TYPE,
529         null);
530     var node0=xpr.iterateNext();
531     while(node0!=null) {
532         if(node0.nodeType==Node.TEXT_NODE)
533                 strings.push(node0.nodeValue);
534         else if(node0.firstChild!=null && node0.firstChild.nodeType==Node.TEXT_NODE)
535                 strings.push(node0.firstChild.nodeValue);
536         node0=xpr.iterateNext();
537     }
538     count.value=strings.length;
539         return strings;
540 }
541
542 UtilService.prototype.xpGetString = function(node,xpath) {
543         var text=node.ownerDocument.evaluate(xpath,node,null,
544                 XPathResult.STRING_TYPE,null).stringValue;
545         return text;
546 }
547
548 UtilService.prototype.getProfileDir=function() {
549         var file = Components.classes["@mozilla.org/file/directory_service;1"]
550         .getService(Components.interfaces.nsIProperties)
551         .get("ProfD", Components.interfaces.nsIFile);
552     return file;
553 }
554
555 UtilService.prototype.getProfilePath=function() {
556         return this.getProfileDir().path;
557 }
558
559 UtilService.prototype.getExtensionDir=function(uuid) {
560         var file = this.getProfileDir();
561         file.append("extensions");
562         file.append(uuid);
563         return file;
564 }
565
566 UtilService.prototype.getExtensionPath=function(uuid) {
567         return this.getExtensionDir(uuid).path;
568 }
569
570 UtilService.prototype.removeReferenceS=function(ds,res) {
571         this.removeReference(ds,this.RDF.GetResource(res));
572 }
573
574 UtilService.prototype.removeReference=function(ds,res) {
575
576         var seqElems=[];
577         
578         var deltriplet=[];
579         var i=ds.ArcLabelsIn(res);
580         while(i.hasMoreElements()) {
581             var arc=i.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
582             var j=ds.GetSources(arc,res,true);
583             while(j.hasMoreElements()) {
584                 var source=j.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
585                 var m=/^http\:\/\/www\.w3\.org\/1999\/02\/22\-rdf\-syntax\-ns#_([1-9][0-9]*)$/.exec(arc.Value);
586                 if(m.length==2) {
587                         var seq=this.RDFCUtils.MakeSeq(ds,source);
588                                 seqElems.push({seq: seq, elem: res});                   
589                 } else {
590                         deltriplet.push({
591                                 source: source,
592                                 property: arc,
593                                 target: res
594                         });
595                 }
596             }
597         }
598         
599         var i=ds.ArcLabelsOut(res);
600         while(i.hasMoreElements()) {
601             var arc=i.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
602             var j=ds.GetTargets(res,arc,true);
603             while(j.hasMoreElements()) {
604                 var target=j.getNext().QueryInterface(Components.interfaces.nsIRDFNode);
605                 deltriplet.push({
606                         source: res,
607                         property: arc,
608                         target: target
609                         });
610             }
611         }
612         
613         ds.beginUpdateBatch();
614         for(var i=0;i<deltriplet.length;i++) {
615                 var triplet=deltriplet[i];
616                 ds.Unassert(triplet.source,triplet.property,triplet.target);
617         }
618         for(var i=0;i<seqElems.length;i++) {
619                 seqElems[i].seq.RemoveElement(seqElems[i].elem,true);
620         }
621         ds.endUpdateBatch();
622 }
623
624 UtilService.prototype.generateUuid=function() {
625         var uuid="";
626         var groups=[8,4,4,4,12];
627         for(var i=0;i<groups.length;i++) {
628                 if(i>0)
629                         uuid+="-";
630                 for(var j=0;j<groups[i];j++) {
631                         var d=Math.floor(Math.random()*16);
632                         if(d<10) {
633                                 uuid+=String.fromCharCode("0".charCodeAt(0)+d);
634                         } else {
635                                 uuid+=String.fromCharCode("a".charCodeAt(0)-10+d);
636                         }
637                 }
638         }
639         return uuid;
640 }
641
642 UtilService.prototype.generateXPath = function(node) {
643         var node0=node;
644         var str="";
645         while(node.parentNode!=null) {
646                 var str0="/";
647         if(node.nodeType==Node.TEXT_NODE) {
648             str0+="text()";
649         } else {
650             str0+=node.nodeName.toLowerCase();
651                 }
652                 var index=this.getNodeChildIndex(node);
653         str0+="["+(index+1)+"]";
654                 str=str0+str;
655                 node=node.parentNode;
656         }
657         str=node.nodeName+str;
658         if(str.substr(0,9)=="#document")
659                 str=str.substring(9);
660         return str;
661 }
662
663 UtilService.prototype.getNodeChildIndex = function(child) {
664     var i=0;
665     var parent=child.parentNode;
666     var node=parent.firstChild;
667     while(node!=null) {
668         if(node==child)
669             return i;
670         if(node.nodeName==child.nodeName && 
671                 (node.nodeType==Node.ELEMENT_NODE || node.nodeType==Node.TEXT_NODE))
672             i++;
673         node=node.nextSibling;
674     }
675     return -1;
676 }
677
678 UtilService.prototype.decodeURL = function(text) {
679         var decoder=Components.classes["@mozilla.org/intl/utf8converterservice;1"]
680                 .getService(Components.interfaces.nsIUTF8ConverterService);
681         text=decoder.convertURISpecToUTF8(text,null);
682         var hc="0123456789ABCDEFabcdef"; 
683         var pt="";
684         var i=0;
685         while (i < text.length) {
686         var ch=text.charAt(i);
687         if (ch=="+") {
688                 pt+=" ";
689                         i++;
690                 } else if (ch=="%") {
691                         if (i < (text.length-2) 
692                                         && hc.indexOf(text.charAt(i+1)) != -1 
693                                         && hc.indexOf(text.charAt(i+2)) != -1 ) {
694                                 pt+=unescape( text.substr(i,3) );
695                                 i+=3;
696                         } else {
697                                 pt+="?";
698                                 i++;
699                         }
700                 } else {
701                    pt+=ch;
702                    i++;
703                 }
704         } 
705    return pt;
706 }
707
708 UtilService.prototype.encodeURL = function(text) {
709         var sc="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
710         var h="0123456789ABCDEF";
711         var encoded="";
712         for (var i=0; i < text.length; i++ ) {
713                 var ch=text.charAt(i);
714             if (ch==" ") {
715                     encoded+="+";
716                 } else if (sc.indexOf(ch) != -1) {
717                     encoded+=ch;
718                 } else {
719                     var charCode=ch.charCodeAt(0);
720                         if (charCode > 255) {
721                                 encoded+="+";
722                         } else {
723                                 encoded+="%"+h.charAt((charCode >> 4) & 0xF)+h.charAt(charCode & 0xF);
724                         }
725                 }
726         }
727         return encoded;
728 };
729
730 UtilService.prototype.contentPost = function(url,body,target,targetName,features) {
731         if(target==null) {
732                 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService().
733                         QueryInterface(Components.interfaces.nsIWindowWatcher);
734                 target=wwatch.openWindow(null, "about:blank",
735                          targetName, features, null);
736         }
737         var webNav = target.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
738                 getInterface(Components.interfaces.nsIWebNavigation);
739         var sis = null;
740         if(body!=null) {
741                 sis=Components.classes["@mozilla.org/io/string-input-stream;1"].
742                 createInstance(Components.interfaces.nsIStringInputStream);
743         sis.setData("Content-length: "+body.length+"\r\n\r\n"+body,-1);
744     }
745         webNav.loadURI(url,webNav.LOAD_FLAGS_NONE,null,sis,null);
746 }
747
748 UtilService.prototype.xmlEscape = function(str) {
749         str=str.replace(/&/g,"&amp;");
750         str=str.replace(/</g,"&lt;");
751         str=str.replace(/>/g,"&gt;");
752         return str;
753         str=str.replace(/'/g,"&apos;");
754         str=str.replace(/"/g,"&quot;");
755         var str0="";
756         for(var i=0;i<str.length;i++) {
757                 var cc=str.charCodeAt(i);
758                 if(cc>=0x80)
759                         str0+="&#"+cc+";";
760                 else
761                         str0+=str[i];
762         }
763         return str0;
764 }
765
766 UtilService.prototype.getUTCTime = function() {
767         var date=new Date();
768         var t=date.getTime();
769         t+=date.getTimezoneOffset()*60*1000;
770         return t;
771 }
772
773 UtilService.prototype.formatNumberMinDigits = function(value,digits) {
774         var str=""+value;
775         while(str.length<digits)
776                 str="0"+str;
777         return str;
778 }
779
780 UtilService.prototype.getExceptionStack = function(e) {
781         try {
782                 var msg="";
783                 var location=e.location;
784                 while(location!=null) {
785                         msg+=location.filename+":"+location.lineNumber+"\n";
786                         location=location.caller;
787                 }
788                 return msg;
789         } catch(e0) {
790                 return "[UtilService] error while getting exception frame: "+e0;
791         }
792 }
793
794 UtilService.prototype.removeChild = function(ds,parent,child) {
795         var seq=this.RDFCUtils.MakeSeq(ds,parent);
796         seq.RemoveElement(child,true);
797 }
798
799 UtilService.prototype.removeChildRS = function(ds,parent,child) {
800         var seq=this.RDFCUtils.MakeSeq(ds,parent);
801         seq.RemoveElement(this.RDF.GetResource(child),true);
802 }
803
804 UtilService.prototype.removeChildSR = function(ds,parent,child) {
805         var seq=this.RDFCUtils.MakeSeq(ds,this.RDF.GetResource(parent));
806         seq.RemoveElement(child,true);
807 }
808
809 UtilService.prototype.removeChildSS = function(ds,parent,child) {
810         var seq=this.RDFCUtils.MakeSeq(ds,this.RDF.GetResource(parent));
811         seq.RemoveElement(this.RDF.GetResource(child),true);
812 }
813
814 UtilService.prototype.getRDFNodeValue = function(node) {
815         if(node==null)
816                 return null;
817         try {
818                 var res=node.QueryInterface(Components.interfaces.nsIRDFResource);
819                 return res.Value;
820         } catch(e) {}
821         try {
822                 var lit=node.QueryInterface(Components.interfaces.nsIRDFLiteral);
823                 return lit.Value;
824         } catch(e) {}
825         return null;
826 }
827
828 UtilService.prototype.QueryInterface = function(iid) {
829     if (!iid.equals(Components.interfaces.nsISupports) && 
830         !iid.equals(Components.interfaces.dhIUtilService)) {
831                         //dump("UtilService: requested invalid interface "+iid+"\n");
832             throw Components.results.NS_ERROR_NO_INTERFACE;
833         }
834     return this;
835 }
836
837 var vUtilServiceModule = {
838     firstTime: true,
839     
840     /*
841      * RegisterSelf is called at registration time (component installation
842      * or the only-until-release startup autoregistration) and is responsible
843      * for notifying the component manager of all components implemented in
844      * this module.  The fileSpec, location and type parameters are mostly
845      * opaque, and should be passed on to the registerComponent call
846      * unmolested.
847      */
848     registerSelf: function (compMgr, fileSpec, location, type) {
849
850         if (this.firstTime) {
851             this.firstTime = false;
852             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
853         }
854         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
855         compMgr.registerFactoryLocation(NS_UTIL_SERVICE_CID,
856                                         "UtilService",
857                                         NS_UTIL_SERVICE_PROG_ID, 
858                                         fileSpec,
859                                         location,
860                                         type);
861     },
862
863         unregisterSelf: function(compMgr, fileSpec, location) {
864         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
865         compMgr.unregisterFactoryLocation(NS_UTIL_SERVICE_CID, fileSpec);
866         },
867
868     /*
869      * The GetClassObject method is responsible for producing Factory and
870      * SingletonFactory objects (the latter are specialized for services).
871      */
872     getClassObject: function (compMgr, cid, iid) {
873         if (!cid.equals(NS_UTIL_SERVICE_CID)) {
874                 throw Components.results.NS_ERROR_NO_INTERFACE;
875                 }
876
877         if (!iid.equals(Components.interfaces.nsIFactory)) {
878                 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
879                 }
880
881         return this.vUtilServiceFactory;
882     },
883
884     /* factory object */
885     vUtilServiceFactory: {
886         /*
887          * Construct an instance of the interface specified by iid, possibly
888          * aggregating it with the provided outer.  (If you don't know what
889          * aggregation is all about, you don't need to.  It reduces even the
890          * mightiest of XPCOM warriors to snivelling cowards.)
891          */
892         createInstance: function (outer, iid) {
893             if (outer != null) {
894                                 throw Components.results.NS_ERROR_NO_AGGREGATION;
895                 }
896
897                         if(Node==null)
898                                 Node=Components.interfaces.nsIDOMNode;
899
900                         if(XPathResult==null)
901                                 XPathResult=Components.interfaces.nsIDOMXPathResult;
902         
903                         return (new UtilService()).QueryInterface(iid);
904         }
905     },
906
907     /*
908      * The canUnload method signals that the component is about to be unloaded.
909      * C++ components can return false to indicate that they don't wish to be
910      * unloaded, but the return value from JS components' canUnload is ignored:
911      * mark-and-sweep will keep everything around until it's no longer in use,
912      * making unconditional ``unload'' safe.
913      *
914      * You still need to provide a (likely useless) canUnload method, though:
915      * it's part of the nsIModule interface contract, and the JS loader _will_
916      * call it.
917      */
918     canUnload: function(compMgr) {
919                 return true;
920     }
921 };
922
923 function NSGetModule(compMgr, fileSpec) {
924     return vUtilServiceModule;
925 }