home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 February / WN_129_CD.iso / Windows / Extensions Firefox / SwitchProxy Tool / switchproxy_1.3.1.xpi / chrome / switchproxy.jar / content / datasource.js < prev    next >
Encoding:
JavaScript  |  2004-08-05  |  10.1 KB  |  375 lines

  1. //DataSource
  2. var gSProxyDs            = null;
  3. var gSProxyRdf            = null;
  4. var gSProxyRdfC            = null;
  5. var gSProxyRDFUtil        = null
  6.  
  7. //Initializes the RDF Datasource components
  8. function switchProxy_ds_initDataSource(){
  9.     try{
  10.         if(gSProxyRdf == null)
  11.             gSProxyRdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  12.         if(gSProxyDs == null)
  13.             gSProxyDs    = gSProxyRdf.GetDataSourceBlocking(gSProxyRdfDataSouce);
  14.         if(gSProxyRDFUtil == null)
  15.             gSProxyRDFUtil = Components.classes["@mozilla.org/rdf/container-utils;1"].getService(Components.interfaces.nsIRDFContainerUtils);
  16.         if(gSProxyRdfC == null){
  17.             gSProxyRdfC = Components.classes["@mozilla.org/rdf/container;1"].createInstance(Components.interfaces.nsIRDFContainer);
  18.             
  19.             //Get or Add Sequence
  20.             try{
  21.                 gSProxyRdfC.Init(gSProxyDs, gSProxyRdf.GetResource(gSProxyRdfRoot)); //Get
  22.             }catch(err){
  23.                 gSProxyRdfC = gSProxyRDFUtil.MakeSeq(gSProxyDs, gSProxyRdf.GetResource(gSProxyRdfRoot)); //Create
  24.             }
  25.         }
  26.                 
  27.     }catch(err){throw "(switchProxy_ds_initDataSource)\n" + err}
  28. }
  29.  
  30. //Add RDF Observer
  31. function switchProxy_ds_addObserver(oObserver){
  32.     switchProxy_ds_initDataSource();
  33.     
  34.     try{
  35.         gSProxyDs.AddObserver(oObserver);
  36.     }catch(err){throw "(switchProxy_ds_addObserver)\n" + err}
  37. }
  38.  
  39. //Returns resource for the given uri
  40. function switchProxy_ds_getElement(sUri){
  41.     switchProxy_ds_initDataSource();
  42.     
  43.     try{
  44.         return gSProxyRdf.GetResource(sUri);
  45.     }catch(err){ throw "(switchProxy_ds_getElement)\n" + err; }
  46. }
  47.  
  48. //Duplicate of switchProxy_ds_getElement
  49. function switchProxy_ds_getResource(sAbout){
  50.     return switchProxy_ds_getElement(sAbout)
  51. }
  52.  
  53. //Returns array of all SwitchProxy Element URIs
  54. //    array[index] = uri
  55. function switchProxy_ds_getAllElements(){
  56.     switchProxy_ds_initDataSource();
  57.     
  58.     var aOut        = new Array();
  59.     var aElements    = gSProxyRdfC.GetElements();
  60.     while(aElements.hasMoreElements()){
  61.         var oRes = aElements.getNext();
  62.             oRes = oRes.QueryInterface(Components.interfaces.nsIRDFResource);
  63.         
  64.         aOut[aOut.length] = oRes.Value;
  65.     }
  66.     
  67.     return aOut;
  68. }
  69.  
  70. //Returns an associative array of properties (attributes) contained in sUri element
  71. //    array[propName] = oRes
  72. function switchProxy_ds_getPropertiesFor(sUri){
  73.     switchProxy_ds_initDataSource();
  74.     
  75.     var aOut = new Array();
  76.     
  77.     //Get an array of elements for sAbout
  78.     try{
  79.         var oRes    = switchProxy_ds_getElement(sUri);
  80.         var oTrgts    = gSProxyDs.ArcLabelsOut(oRes);
  81.         while(oTrgts.hasMoreElements()){
  82.             var oTrgt = oTrgts.getNext();
  83.             
  84.             if (oTrgt instanceof Components.interfaces.nsIRDFResource){
  85.                 var sTrgName = oTrgt.Value.substring(gSProxyRdfNodeUriRoot.length + 1); //return node name without URI
  86.                 aOut[sTrgName] = oTrgt;
  87.             }
  88.         }
  89.     }catch(err){throw "(switchProxy_ds_getPropertiesFor)\n" + err}
  90.     
  91.     return aOut;
  92. }
  93.  
  94. //Similar to 'switchProxy_ds_getPropertiesFor' however returns uri=>literal_object_value
  95. //    array[uri] = oLiteral
  96. function switchProxy_ds_getPropertyValuesFor(sUri){
  97.     switchProxy_ds_initDataSource();
  98.     
  99.     var aOut = new Array();
  100.     
  101.     //Get an array of elements for sAbout
  102.     try{
  103.         var oRes    = switchProxy_ds_getElement(sUri);
  104.         var oTrgts    = gSProxyDs.ArcLabelsOut(oRes, true);
  105.         while(oTrgts.hasMoreElements()){
  106.             var oTrgt = oTrgts.getNext();
  107.             
  108.             if (oTrgt instanceof Components.interfaces.nsIRDFResource){
  109.                 aOut[oTrgt.Value] = switchProxy_ds_getValueFor(oRes, oTrgt);
  110.             }
  111.         }
  112.     }catch(err){throw "(switchProxy_ds_getPropertyValuesFor)\n" + err}
  113.     
  114.     return aOut;
  115. }
  116.  
  117. //Returns element that has this property/value
  118. function switchProxy_ds_getElementForValue(sPropertyUri, sValue){
  119.     switchProxy_ds_initDataSource();
  120.     
  121.     try{
  122.         var oValue         = gSProxyRdf.GetLiteral(sValue);
  123.         var oProp         = gSProxyRdf.GetResource(sPropertyUri);
  124.         var oSubject    = gSProxyDs.GetSource(oProp, oValue, true);
  125.         
  126.         return oSubject;
  127.         
  128.     }catch(err){throw "(switchProxy_ds_getElementForValue)\n" + err}
  129.     
  130.     return null;
  131. }
  132.  
  133. // Returns all elements that has this property/value
  134. function switchProxy_ds_getElementsForValue(sPropertyUri, sValue){
  135.     switchProxy_ds_initDataSource();
  136.     
  137.     var aOut = new Array();
  138.     
  139.     try{
  140.         var oValue         = gSProxyRdf.GetLiteral(sValue);
  141.         var oProp         = gSProxyRdf.GetResource(sPropertyUri);
  142.         var aSubject    = gSProxyDs.GetSources(oProp, oValue, true);
  143.         var oSubject    = null
  144.         
  145.         while(aSubject.hasMoreElements()){
  146.             oSubject = aSubject.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  147.             aOut[aOut.length] = oSubject;
  148.         }
  149.         
  150.     }catch(err){throw "(switchProxy_ds_getElementsForValue)\n" + err}
  151.     
  152.     return aOut;
  153. }
  154.  
  155. //Change element's URI
  156. function switchProxy_ds_changeElementUri(oRes, sNewUri){
  157.     switchProxy_ds_initDataSource();
  158.         
  159.     try{
  160.         
  161.         //Get All Properties for element
  162.         var aProps = switchProxy_ds_getPropertyValuesFor(oRes.Value);
  163.         
  164.         //Remove Element
  165.         switchProxy_ds_removeElement(oRes.Value);
  166.         
  167.         //Create element again with new URI
  168.         var newElem = gSProxyRdf.GetResource(sNewUri);
  169.         gSProxyRdfC.AppendElement(newElem);
  170.         for(sProp in aProps){
  171.             gSProxyDs.Assert(newElem, gSProxyRdf.GetResource(sProp), gSProxyRdf.GetLiteral(aProps[sProp]), true);
  172.         }
  173.         
  174.     }catch(err){throw "(switchProxy_ds_changeElementUri)\n" + err}
  175. }
  176.  
  177. //Add element with given sUri, returns added resource
  178. function switchProxy_ds_addElement(sUri){
  179.     switchProxy_ds_initDataSource();
  180.         
  181.     try{
  182.         
  183.         return oRes = gSProxyRdfC.AppendElement(gSProxyRdf.GetResource(sUri));
  184.         
  185.     }catch(err){throw "(switchProxy_ds_addElement)\n" + err}
  186. }
  187.  
  188. //Remove Element for sUri
  189. function switchProxy_ds_removeElement(sUri){
  190.     switchProxy_ds_initDataSource();
  191.     
  192.     try{
  193.         var oRes = gSProxyRdf.GetResource(sUri);
  194.     
  195.         //Remove All Archs
  196.         // Loop for duplicates
  197.         var aArchs        = null;
  198.         var hasArchs    = true;
  199.         while(hasArchs){
  200.             aArchs         = gSProxyDs.ArcLabelsOut(oRes);
  201.             hasArchs     = aArchs.hasMoreElements();
  202.             while(aArchs.hasMoreElements()){
  203.                 oArch = aArchs.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  204.                 
  205.                 //Remove
  206.                 gSProxyDs.Unassert(oRes, oArch, gSProxyDs.GetTarget(oRes, oArch, true));
  207.             }
  208.         }
  209.         
  210.         //Remove Element
  211.         gSProxyRdfC.RemoveElement(oRes, true);
  212.     }catch(err){throw "(switchProxy_ds_removeElement)\n" + err}
  213. }
  214.  
  215. //Remove property (sUri) from oRes
  216. function switchProxy_ds_removeProperty(sUri, oRes, sValue){
  217.     switchProxy_ds_initDataSource();
  218.     
  219.     try{
  220.         var oPred     = gSProxyRdf.GetResource(sUri);
  221.         var aValues    = switchProxy_ds_getValuesFor(oRes, oPred);
  222.              
  223.     if(typeof(sValue) == 'undefined'){
  224.         // Make sure to delete all properites of this sUri
  225.         for(var i = 0; i < aValues.length; i++){ 
  226.             gSProxyDs.Unassert(oRes, oPred, gSProxyRdf.GetLiteral(aValues[i]) );
  227.         }
  228.     }
  229.     else{
  230.         // Only delete the property with this value
  231.         sValue = gSProxyRdf.GetLiteral(sValue);
  232.         if(gSProxyDs.HasAssertion(oRes, oPred, sValue, true)){
  233.             gSProxyDs.Unassert(oRes, oPred,  sValue);
  234.         }
  235.     }
  236.         
  237.     }catch(err){throw "(switchProxy_ds_removeProperty)\n" + err}
  238. }
  239.  
  240. //Does URI Exist
  241. function switchProxy_ds_doesProxyElementExist(sProxyUri){
  242.     switchProxy_ds_initDataSource();
  243.     
  244.     try{
  245.         var aElems = switchProxy_ds_getAllElements();
  246.         for(var e = 0; e < aElems.length; e++){
  247.             if(aElems[e] == sProxyUri)
  248.                 return true;
  249.         }        
  250.     }catch(err){throw "(switchProxy_ds_doesElementExist)\n" + err}
  251. }
  252.  
  253. //Get Index of URI
  254. function switchProxy_ds_indexOf(sProxyUri){
  255.     switchProxy_ds_initDataSource();
  256.     
  257.     try{
  258.         return gSProxyRdfC.IndexOf(switchProxy_ds_getResource(sProxyUri));
  259.     }catch(err){throw "(switchProxy_ds_indexOf)\n" + err}
  260. }
  261.  
  262. //Rename oProp's URI to sNewUri
  263. function switchProxy_ds_renamePropertyUri(oRes, oProp, sNewUri){
  264.     switchProxy_ds_initDataSource();
  265.     
  266.     try{
  267.         var sValue = gSProxyDs.GetTarget(oRes, oProp, true).QueryInterface(Components.interfaces.nsIRDFLiteral);
  268.         
  269.         gSProxyDs.Unassert(oRes, oProp, gSProxyDs.GetTarget(oRes, oProp, true));
  270.         gSProxyDs.Assert(oRes, gSProxyRdf.GetResource(sNewUri), sValue, true);
  271.         
  272.     }catch(err){throw "(switchProxy_ds_renamePropertyUri)\n" + err}
  273. }
  274.  
  275. //Add Property oProp to oRes
  276. function switchProxy_ds_addProperty(oRes, oProp, sValue, overwriteExisting){
  277.     switchProxy_ds_initDataSource();
  278.         
  279.     try{
  280.         //Don't overwrite it this property exists
  281.         if(!overwriteExisting && gSProxyDs.hasArcOut(oRes, oProp))
  282.             return;
  283.             
  284.         //Add
  285.         gSProxyDs.Assert(oRes, oProp, gSProxyRdf.GetLiteral(sValue), true);
  286.     }catch(err){throw "(switchProxy_ds_addProperty)\n" + err}
  287. }
  288.  
  289. //Change Property value for sPropUri in oRes
  290. function switchProxy_ds_changePropertyValue(oRes, sPropUri, sValue){
  291.     switchProxy_ds_initDataSource();
  292.         
  293.     try{
  294.         var oProp = gSProxyRdf.GetResource(sPropUri);
  295.         
  296.         //Get old value
  297.         var sOld = switchProxy_ds_getValueFor(oRes, oProp);
  298.         
  299.         //Change
  300.         gSProxyDs.Change(oRes, oProp, gSProxyRdf.GetLiteral(sOld), gSProxyRdf.GetLiteral(sValue));
  301.         
  302.     }catch(err){throw "(switchProxy_ds_changePropertyValue)\n" + err}
  303. }
  304.  
  305. //Get Property Value for Property oProp
  306. function switchProxy_ds_getValueFor(oRes, oProp){
  307.     switchProxy_ds_initDataSource();
  308.     
  309.     try{
  310.         oTrgt = gSProxyDs.GetTarget(oRes, oProp, true);
  311.         
  312.         if(oTrgt instanceof Components.interfaces.nsIRDFLiteral){
  313.             return oTrgt.Value;
  314.         }
  315.         
  316.     }catch(err){throw "(switchProxy_ds_getValueFor)\n" + err}
  317.     
  318.     return null;
  319. }
  320.  
  321. //Get All Property Values for Property oProp
  322. //    This is similiar to switchProxy_ds_getValueFor
  323. //    except it returns ALL values for oProp in an array
  324. //        array[index] = sValue
  325. function switchProxy_ds_getValuesFor(oRes, oProp){
  326.     switchProxy_ds_initDataSource();
  327.  
  328.     var aOut = new Array();
  329.     
  330.     try{
  331.         var aTrgts     = gSProxyDs.GetTargets(oRes, oProp, true)
  332.         var oTrgt    = null;
  333.         
  334.         while(aTrgts.hasMoreElements()){
  335.             oTrgt = aTrgts.getNext()
  336.             
  337.             if(oTrgt instanceof Components.interfaces.nsIRDFLiteral){
  338.                 aOut[aOut.length] = oTrgt.Value;
  339.             }
  340.         }
  341.     }catch(err){throw "(switchProxy_ds_getValueFor)\n" + err}
  342.     
  343.     return aOut;
  344. }
  345.  
  346. //Does Property/Value exist in oRes
  347. function switchproxy_ds_doesPropValueExist(oRes, sPropUri, sValue){
  348.     switchProxy_ds_initDataSource();
  349.         
  350.     try{
  351.         var oProp    = gSProxyRdf.GetResource(sPropUri);
  352.         var aValues    = switchProxy_ds_getValuesFor(oRes, oProp);
  353.         
  354.         //Find in array
  355.         for(var i = 0; i < aValues.length; i++){
  356.             if(aValues[i] == sValue)
  357.                 return true;
  358.         }
  359.         
  360.     }catch(err){throw "(switchproxy_ds_doesPropValueExist)\n" + err}
  361.     
  362.     return false;
  363. }
  364.  
  365. /*
  366. * GET PROPERTIES
  367. */
  368.     
  369.     //Get RDF Container
  370.     function switchProxy_ds_getRDFContainer(){
  371.         switchProxy_ds_initDataSource();
  372.         return gSProxyRdf;
  373.     }
  374.     
  375.