home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 June / comonline0602.iso / software / cogitum / cociter.exe / CogitumH.___ / HTML / XMLBASEIO.JS < prev   
Encoding:
Text File  |  2001-08-08  |  13.2 KB  |  501 lines

  1. @if ( @support != @support)
  2. // 1 means sense support on
  3. // 2 means tracker support on
  4. @set @support            = 1;
  5. @end
  6. @set @SenseSupport        = ( (@support) == 1);
  7. @set @TrackerSupport    = ( (@support) == 2);
  8. @set @BannerPileSupport    = ( (@support) == 4);
  9. @set @CitatesSupport    = ( (@support) == 8);
  10.  
  11.  
  12. var cxbiMajorDatabaseVersion = 1;
  13. var cxbiMinorDatabaseVersion = 0;
  14. var cxbiDatabaseVersion =    cxbiMajorDatabaseVersion.toString() + "." + 
  15.                             cxbiMinorDatabaseVersion.toString();
  16.  
  17. var cxbiRoot            = "root";
  18.  
  19. var cxbiAttTimeStamp        = "TimeStamp";
  20. var cxbiNoTimeStamp        = "-1";
  21. var cxbiAttNextUIN        = "NextUIN";
  22. var cxbiAttUIN            = "UIN";
  23.  
  24. var cxbiAttVersion        = "dbversion";
  25.  
  26. var cxbiDatabase        = "CogTracker:ctDATABASE";
  27. var cxbiAttCurrentNode        = "CurrentNode";
  28. var cxbiAttIsDatabase        = "IsDatabase";
  29.  
  30. var cxbiNODE            = "CogTracker:ctNODE";
  31.  
  32. var cxbiFRAGMENT        = "CogTracker:ctFRAGMENT";
  33.  
  34. //var cxbiAttComment        = "Comment";
  35. var cxbiAttText            = "Text";
  36. var cxbiAttComment        = "Comment";
  37. var cxbiAttURL            = "BaseURL";
  38. var cxbiAttDate            = "CreationDate";
  39.  
  40. //BannerPile database support
  41. var cxbiCTLIST                = "CTLIST";
  42. var cxbiAttCurrentCategory    = "CurrentCategory";
  43.  
  44. var cxbiCATEGORY            = "CATEGORY";
  45. var cxbiAttCID                = "CID";
  46. var cxbiAttName                = "Name";
  47. cxbiAttUnsorted                = "IsUnsortedNode";
  48.  
  49. var cxbiBANNERBASE            = "BANNERBASE";
  50. var cxbiAttOpenMode            = "OpenMode";
  51. var    cxbiAttSortOrder        = "SortOrder";
  52. var cxbiAttShowSlots        = "ShowSlots";
  53.  
  54. var cxbiBANNER                = "BANNER";
  55. L_COMMON_CAT_NAME            = "Common";
  56. L_UNNAMED_NODE_DEF            = "My Citations";
  57. L_UNSORTED_NODE                = "Default Collection";
  58.  
  59. // Gets database version of XML database.
  60. function fnGetVersion(someBase) {
  61.     var oDBVersion = "";
  62.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  63.     if (ctRoot != null) {
  64.         oDBVersion = ctRoot.getAttribute(cxbiAttVersion);
  65.     };
  66.     if (oDBVersion == null) {
  67.         oDBVersion = "";
  68.     };
  69.     return oDBVersion;
  70. };
  71.  
  72. // Checks if database version is current.
  73. function fnCheckVersion(someBase) {
  74.     return (fnGetVersion(someBase) == cxbiDatabaseVersion);
  75. };
  76.  
  77. // Sets database version of XML database to current.
  78. function fnSetVersion(someBase) {
  79.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  80.     if (ctRoot != null) {
  81.         ctRoot.setAttribute(cxbiAttVersion, cxbiDatabaseVersion);
  82.     };
  83. };
  84.  
  85. // Prompts user to confirm DB conversion.
  86. // Returns true if user presses OK / false on Cancel
  87. function fnConfirmConversion(someBase) {
  88.     var sVersion = fnGetVersion(someBase);
  89.     
  90.      
  91.     var bConfirmed = confirm ("XML Database version is not current.\n\n" +
  92.                 "Expected version: '" + cxbiDatabaseVersion + "'\n" + 
  93.                 "Found version: " + 
  94.                 ( (sVersion == "") ? ("No version mark found") : 
  95.                                                  ("'" + sVersion + "'") ) 
  96.                 +
  97.                 "\n\nDo you want to convert it?");
  98.     return bConfirmed;
  99. };
  100.  
  101. //Performs DB conversions
  102. function fnPerformConversion(someBase) {
  103.     var sVersion = fnGetVersion(someBase);
  104.  
  105.     if (sVersion == cxbiDatabaseVersion) {
  106.         // No conversion necessary;
  107.         return true;
  108.     };
  109.     
  110.     var nMajorVersion = 0;
  111.     var nMinorVersion = 0;
  112.     
  113.     if (sVersion != "") {
  114.         var nVerDelimiterPos = sVersion.indexOf(".");
  115.         if (nVerDelimiterPos != -1) {
  116.             nMajorVersion = parseInt(sVersion.slice(0, nVerDelimiterPos));
  117.             if (isNaN(nMajorVersion)) {
  118.                 nMajorVersion = 0;
  119.             }
  120.             nMinorVersion = parseInt(sVersion.slice(nVerDelimiterPos+1));
  121.             if (isNaN(nMinorVersion)) {
  122.                 nMinorVersion = 0;
  123.             }
  124.         }
  125.     }
  126.     
  127.     var bConvert = false;
  128.     if (nMajorVersion != 0 || nMinorVersion != 0) {
  129.         if    ( (nMajorVersion > cxbiMajorDatabaseVersion) || 
  130.               (nMajorVersion == cxbiMajorDatabaseVersion &&
  131.                nMinorVersion >  cxbiMinorDatabaseVersion)
  132.             ) {
  133.             
  134.             if (confirm("Database version is higher than supported.\n\n" +
  135.                         "Are you sure that you want to continue?") == true) {
  136.                         
  137.                 bConvert = true;    
  138.                 
  139.             }
  140.             else {
  141.                 bConvert = false;
  142.             }
  143.         }
  144.         else 
  145.             bConvert = true;
  146.              
  147.     }
  148.     else {
  149.         bConvert = true;
  150.     };
  151.     
  152.     if (bConvert) {
  153.         // Here switch'es should be placed to convert from older/newer versions
  154.         // of the DB...
  155.         fnSetVersion(someBase);
  156.         return true;
  157.     }
  158.     else {
  159.         return false;
  160.     };
  161. };
  162.  
  163. // Gets time stamp of XML database.
  164. // When the DB has no timestamp, returns -1.
  165. function fnGetTimeStamp(someBase) {
  166.     var oStamp = cxbiNoTimeStamp;
  167.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  168.     if (ctRoot != null) {
  169.         oStamp = ctRoot.getAttribute(cxbiAttTimeStamp);
  170.         if (oStamp == "" || oStamp == null) {
  171.             oStamp = cxbiNoTimeStamp;
  172.         }
  173.         if (isNaN(parseInt(oStamp, 10))) {
  174.             oStamp = "1";
  175.         };
  176.     };
  177.     return oStamp;
  178. };
  179.  
  180. // Checks to see if memDatabase has the same TimeStamp as diskDatabase
  181. // Note: if diskDatabase's TimeStamp is not present, the function 
  182. //     always returns true;
  183. function fnCheckTimeStampsEqual(memDatabase, diskDatabase) {
  184.     var oMemStamp    = fnGetTimeStamp(memDatabase);
  185.     var oDiskStamp    = fnGetTimeStamp(diskDatabase);
  186.     
  187.     if (oDiskStamp != cxbiNoTimeStamp) {
  188.         if (oMemStamp != oDiskStamp) {
  189.             return false;
  190.         };
  191.     };
  192.  
  193.     return true;
  194. };
  195.  
  196. function fnIncrementTimeStamp(someBase) {
  197.     var ctRoot = someBase.selectSingleNode(cxbiRoot);
  198.     var oStamp = fnGetTimeStamp(someBase);
  199.     oStamp = ( (parseInt(oStamp, 10) + 1) % 1000000  ).toString();
  200.     ctRoot.setAttribute(cxbiAttTimeStamp, oStamp);
  201.  
  202.     return true;
  203. };
  204.  
  205. function fnCreateNewCID(someBase) {
  206.  
  207. @if (@BannerPileSupport)
  208.     var UIN = 0;
  209.     var ctCategoriesList = someBase.selectNodes(
  210.                         cxbiRoot + "/" + cxbiCTLIST + "/" + cxbiCATEGORY);
  211.     
  212.     for (i = 0; i < ctCategoriesList.length; i++) {
  213.         var currCID = parseInt(ctCategoriesList[i].getAttribute(cxbiAttCID), 10);
  214.         
  215.         if ( ( ! isNaN(currCID)) && (currCID > UIN) ) {
  216.             UIN = currCID;
  217.         }
  218.     };
  219.     UIN ++;
  220.     var nLength = UIN.toString().length;
  221.     UIN = "0000000000000000" + UIN;
  222.     UIN = UIN.slice(nLength);
  223.     
  224.     return UIN;
  225. @else 
  226.     alert("This function should not be used in current configuration !");
  227.     window.close();
  228.     return false;
  229. @end
  230. };
  231.  
  232. // Returns new UIN for the element;
  233. // increments NextUIN of the root element
  234. function fnCreateNewUIN(someBase) {
  235.  
  236. @if (@BannerPileSupport)
  237.     var UIN = 0;
  238.     var ctBannersList = someBase.selectNodes(
  239.                         cxbiRoot + "/" + cxbiBANNERBASE + "/" + cxbiBANNER);
  240.     
  241.     for (i = 0; i < ctBannersList.length; i++) {
  242.         var currUIN = parseInt(ctBannersList[i].getAttribute(cxbiAttUIN), 10);
  243.         
  244.         if ( ( ! isNaN(currUIN)) && (currUIN > UIN) ) {
  245.             UIN = currUIN;
  246.         }
  247.     };
  248.     UIN ++;
  249.     var nLength = UIN.toString().length;
  250.     UIN = "0000000000000000" + UIN;
  251.     UIN = UIN.slice(nLength);
  252.     
  253.     return UIN;
  254. @else
  255.     var UIN = "";
  256.     var ctRoot = someBase.selectSingleNode(cxbiRoot);
  257.     var NextUIN = ctRoot.getAttribute(cxbiAttNextUIN);
  258.     if (NextUIN == null || NextUIN == "" || isNaN(parseInt(NextUIN)) ) {
  259.         NextUIN = "0";
  260.         ctRoot.setAttribute(cxbiAttNextUIN, NextUIN);
  261.     };
  262.     UIN = NextUIN;
  263.     NextUIN = (parseInt(NextUIN) + 1).toString();
  264.     ctRoot.setAttribute(cxbiAttNextUIN, NextUIN);
  265.     return UIN;
  266. @end
  267. };
  268.  
  269. // Returns element's UIN.
  270. // If the element has no UIN, UIN is created and assigned.
  271. function fnGetElementUIN(someBase, someElement) {
  272.  
  273. @if (@BannerPileSupport)
  274.     var sAttUIN = cxbiAttUIN;
  275.     if (someElement.nodeName == cxbiCATEGORY) {
  276.         sAttUIN = cxbiAttCID;
  277.     }
  278.     
  279.     var UIN = someElement.getAttribute(sAttUIN);
  280.     if (UIN == null || UIN == "" || isNaN(parseInt(UIN)) ) {
  281.     
  282.         if (sAttUIN == cxbiAttCID)
  283.             UIN = fnCreateNewCID(someBase);
  284.         else
  285.             UIN = fnCreateNewUIN(someBase);
  286.         someElement.setAttribute(sAttUIN, UIN);
  287.     };
  288.     return UIN;
  289.     
  290. @else
  291.     var UIN = someElement.getAttribute(cxbiAttUIN);
  292.     if (UIN == null || UIN == "" || isNaN(parseInt(UIN)) ) {
  293.     
  294.         UIN = fnCreateNewUIN(someBase);
  295.         someElement.setAttribute(cxbiAttUIN, UIN);
  296.     };
  297.     return UIN;
  298. @end
  299. };
  300.  
  301.                                 
  302. // Looks for element with specified UIN
  303. function fnFindElementByUIN(someBase, UIN) {
  304.     return someBase.selectSingleNode('//*[@UIN="' + UIN + '"]');
  305. };
  306.  
  307.  
  308. // Loads the database and makes it valid
  309. function fnCheckedInitializeFile(someBase, sPath) {
  310.  
  311. var bChanged = false;
  312.     someBase.load(sPath);
  313.     
  314. @if ( @SenseSupport )
  315.  
  316.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  317.     
  318.     if (ctRoot == null)    {
  319.         ctRoot = someBase.createElement(cxbiRoot);
  320.         
  321.         //ctRoot.setAttribute("xmlns:CogTracker", "http://www.rword.ru/tracker/");
  322.         ctRoot.setAttribute("xmlns:rw", "http://www.cogitum.com/sense/rw");
  323.         ctRoot.text = "\n\t";
  324.  
  325.         ctRoot.setAttribute(cxbiAttTimeStamp, cxbiNoTimeStamp);
  326.         ctRoot.setAttribute(cxbiAttNextUIN,"0");
  327.         ctRoot.setAttribute(cxbiAttVersion, cxbiDatabaseVersion);
  328.         someBase.appendChild(ctRoot);
  329.         
  330.         //var xmlInstr = someBase.createProcessingInstruction("xml", 'version="1.0" encoding="Windows-1251"');
  331.         //someBase.insertBefore(xmlInstr, ctRoot);
  332.  
  333.         bChanged = true;
  334.     }
  335.  
  336. @elif ( @TrackerSupport )    
  337.  
  338.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  339.     
  340.     if (ctRoot == null)    {
  341.         ctRoot = someBase.createElement(cxbiRoot);
  342.         
  343.         ctRoot.setAttribute("xmlns:CogTracker", "http://www.rword.ru/tracker/");
  344.         //ctRoot.setAttribute("xmlns:rw", "http://www.rword.ru/smysl/");
  345.         ctRoot.text = "\n\t";
  346.  
  347.         ctRoot.setAttribute(cxbiAttTimeStamp, cxbiNoTimeStamp);
  348.         ctRoot.setAttribute(cxbiAttNextUIN,"0");
  349.         ctRoot.setAttribute(cxbiAttVersion, cxbiDatabaseVersion);
  350.         someBase.appendChild(ctRoot);
  351.         
  352.         //var xmlInstr = someBase.createProcessingInstruction("xml", 'version="1.0" encoding="Windows-1251"');
  353.         //someBase.insertBefore(xmlInstr, ctRoot);
  354.  
  355.         bChanged = true;
  356.     }
  357.  
  358.     //CogTracker temp. turned off...
  359.     var    ctDATABASE = someBase.selectSingleNode(cxbiRoot + "/" + cxbiDatabase);
  360.     if (ctDATABASE == null)    {
  361.         ctDATABASE = someBase.createElement(cxbiDatabase);
  362.  
  363.         ctDATABASE.text = "\n\t\t";
  364.  
  365.         ctRoot.appendChild(ctDATABASE);
  366.  
  367.         bChanged = true;
  368.     }
  369. @elif (@BannerPileSupport)
  370.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  371.     
  372.     if (ctRoot == null)    {
  373.         ctRoot = someBase.createElement(cxbiRoot);
  374.         
  375.         ctRoot.text = "\n\t";
  376.  
  377.         ctRoot.setAttribute(cxbiAttTimeStamp, cxbiNoTimeStamp);
  378.         ctRoot.setAttribute(cxbiAttNextUIN,"0");
  379.         ctRoot.setAttribute(cxbiAttVersion, cxbiDatabaseVersion);
  380.         someBase.appendChild(ctRoot);
  381.         
  382.         var xmlInstr = someBase.createProcessingInstruction("xml", 'version="1.0" encoding="Windows-1251"');
  383.         someBase.insertBefore(xmlInstr, ctRoot);
  384.  
  385.         bChanged = true;
  386.     }
  387.  
  388.     
  389.     var    CtList = someBase.selectSingleNode(cxbiRoot + "/" + cxbiCTLIST);
  390.     if (CtList == null)    {
  391.         CtList = someBase.createElement(cxbiCTLIST);
  392.         CtList.setAttribute(cxbiAttCurrentCategory, "0000000000000000");
  393.                 
  394.         var CtCommon = someBase.createElement(cxbiCATEGORY);
  395.         CtCommon.setAttribute(cxbiAttCID, "0000000000000001");
  396.         CtCommon.setAttribute(cxbiAttName, L_COMMON_CAT_NAME);
  397.         CtCommon.text = "\n";
  398.                 
  399.         ctRoot.appendChild(CtList);
  400.         CtList.appendChild(CtCommon);
  401.  
  402.         bChanged = true;
  403.     }
  404.     
  405.     var    ctDATABASE = someBase.selectSingleNode(cxbiRoot + "/" + cxbiBANNERBASE);
  406.     if (ctDATABASE == null)    {
  407.         ctDATABASE = someBase.createElement(cxbiBANNERBASE);
  408.  
  409.         ctDATABASE.setAttribute(cxbiAttOpenMode, "_blank");
  410.         ctDATABASE.setAttribute(cxbiAttSortOrder, "-@CreationDate");
  411.         ctDATABASE.setAttribute(cxbiAttShowSlots, "63");
  412.         ctDATABASE.text = "\n\t\t";
  413.  
  414.         ctRoot.appendChild(ctDATABASE);
  415.  
  416.         bChanged = true;
  417.     }
  418. @elif (@CitatesSupport)
  419.  
  420.     var    ctRoot = someBase.selectSingleNode(cxbiRoot);
  421.     
  422.     if (ctRoot == null)    {
  423.         ctRoot = someBase.createElement(cxbiRoot);
  424.         
  425.         ctRoot.setAttribute("xmlns:CogTracker", "http://www.rword.ru/tracker/");
  426.         //ctRoot.setAttribute("xmlns:rw", "http://www.rword.ru/smysl/");
  427.         ctRoot.text = "\n\t";
  428.  
  429.         ctRoot.setAttribute(cxbiAttTimeStamp, cxbiNoTimeStamp);
  430.         ctRoot.setAttribute(cxbiAttNextUIN,"0");
  431.         ctRoot.setAttribute(cxbiAttVersion, cxbiDatabaseVersion);
  432.         someBase.appendChild(ctRoot);
  433.         
  434.         var xmlInstr = someBase.createProcessingInstruction("xml", 'version="1.0" encoding="UTF-8"');
  435.         someBase.insertBefore(xmlInstr, ctRoot);
  436.  
  437.         bChanged = true;
  438.     }
  439.     else {
  440.         if (ctRoot.getAttribute("xmlns:CogTracker") != "http://www.rword.ru/tracker/") {
  441.             ctRoot.setAttribute("xmlns:CogTracker", "http://www.rword.ru/tracker/");
  442.  
  443.             bChanged = true;
  444.         };
  445.     };
  446.  
  447.     var    ctDATABASE = someBase.selectSingleNode(cxbiRoot + "/" + cxbiDatabase);
  448.     if (ctDATABASE == null)    {
  449.         ctDATABASE = someBase.createElement(cxbiDatabase);
  450.  
  451.         ctDATABASE.setAttribute(cxbiAttName, L_UNNAMED_NODE_DEF);
  452.                 ctDATABASE.setAttribute(cxbiAttIsDatabase, "-1");
  453.  
  454.         ctDATABASE.text = "\n\t\t";
  455.  
  456.         ctRoot.appendChild(ctDATABASE);
  457.  
  458.         bChanged = true;
  459.     }
  460.     
  461.     var    ctUNSORTED = someBase.selectSingleNode(cxbiRoot + "/" + cxbiDatabase + "/" + cxbiNODE);
  462.     if (ctUNSORTED == null)    {
  463.         ctUNSORTED = someBase.createElement(cxbiNODE);
  464.  
  465.         ctUNSORTED.setAttribute(cxbiAttName, L_UNSORTED_NODE);
  466.         ctUNSORTED.setAttribute(cxbiAttUnsorted, "1");
  467.         
  468.         ctUNSORTED.text = "\n\t\t";
  469.  
  470.         ctDATABASE.appendChild(ctUNSORTED);
  471.  
  472.         bChanged = true;
  473.     }
  474.     
  475.     
  476. @else 
  477.  
  478.     alert("none of support options turned on!");
  479.     window.close();
  480.     return false;
  481.  
  482. @end
  483.  
  484.     if (bChanged) {
  485. // If file does not exist, this code generates an error on IE 5.5
  486. /*
  487.         var oTmp = fnCreateXMLctl();
  488.         someBase.save(oTmp);
  489.         oTmp.save(someBase);
  490. */
  491.         var sXML = new String(someBase.xml);
  492.         someBase.loadXML("");
  493.         someBase.loadXML(sXML);
  494.         
  495.         
  496.         //someBase.save(someBase);
  497.     };
  498.     //someBase.load(someBase);
  499.     return true;
  500. };
  501.