home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 July / VPR0207A.ISO / ANTI_VIR / NAV2002 / nav2002.exe / NAV / SWPlugin.dll / HTML / RESOURCE.JS < prev    next >
Text File  |  2001-08-13  |  13KB  |  368 lines

  1. // resource.js
  2. //
  3. // Initial Author:  Alex Hui
  4. // Date:  May 10, 2000
  5. //
  6. // Purpose:
  7. // --------
  8. // This file contains the functions to access the resources,
  9. // such as the string tables and image tables, which are stored
  10. // in a resource.xml.  The document is in XML format, but the
  11. // users of the library of functions provided here should not
  12. // need to know that detail.
  13. //
  14. // To use this "library", if we may call it so, add this line
  15. // to the HTML file:
  16. //   <SCRIPT LANGUAGE="JavaScript" SRC="resource.js"></SCRIPT>
  17.  
  18. // Note:
  19. // -----
  20. // The support for XML under IE4 and IE5 are quite different.
  21. // IE5 provides a rich set of methods such as searching for
  22. // certain nodes given a tag name or certain attribute values.
  23. // On the other hand, basically IE4 only provides methods to
  24. // traverse the XML document tree and retrieve data.
  25. //
  26. // In fact, the XML object in the two versions are distinct.
  27. // In IE4, many things have to be done ourselves.  With some
  28. // minor adjustments code written for IE4 will also work in
  29. // IE5.  However, for the following 4 reasons there will be
  30. // separate implementations for IE4 and IE5+:
  31. // 1) robustness, 2) efficiency, 3) in case future versions of
  32. // IE will drop support for the old XML object and 4) for the
  33. // sake of not using legacy code if we ever drop support for IE4.
  34.  
  35. // "Exports":
  36. // ----------
  37. // function LoadResourceDoc()
  38. // function GetString(StringID, Section, Language)
  39. // function GetImage(ImageID, Section)
  40. // (other functions in this file are supposed to be internal)
  41.  
  42. var DefaultLanguage;            // language to use when not specified
  43. var DefaultImageTableSection;   // image table section to use when not specified
  44. var IE4ResourceDoc;             // the XML object in IE4
  45. var IE5ResourceDoc;             // the XML object in IE5 or higher
  46.  
  47. // IE Version Detection ***********************************
  48. function IEVersion()
  49. {
  50.     var ua = window.navigator.userAgent
  51.     var msie = ua.indexOf("MSIE ")
  52.  
  53.     // If Internet Explorer, return version number
  54.     // If another browser, return 0
  55.     if (msie > 0)
  56.         return parseInt( ua.substring(msie+5, ua.indexOf(".", msie)) );
  57.     else
  58.         return 0;
  59. }
  60.  
  61. // IE4 Functions ******************************************
  62. function GetXMLDocURL()
  63. {
  64.     var CurrentURL;         // HREF of the viewed document
  65.     var XMLDocURL;          // built from CurrentURL but with the XML document as the filename
  66.     var FilenameIndex;      // position of the filename (no path) within CurrentURL
  67.     
  68.     // note: the filename is always preceded by "/" no matter what protocol
  69.     CurrentURL = window.location.href;
  70.     FilenameIndex = CurrentURL.lastIndexOf("/") + 1;
  71.     XMLDocURL = CurrentURL.substring(0, FilenameIndex) + "resource.xml";
  72.     return XMLDocURL;
  73. }
  74.  
  75. function IE4LoadResourceDoc()
  76. {
  77.     IE4ResourceDoc = new ActiveXObject("msxml");
  78.     // Docs say async should be false so that the whole file
  79.     // is loaded before returning, but we get the error
  80.     // "no data available in requested resource" whenever we
  81.     // use the dll resources.
  82.     // Setting it to true seems to work fine. (?!)
  83.     IE4ResourceDoc.async = true;
  84.     IE4ResourceDoc.url = GetXMLDocURL();            // in IE4 we must specify a protocol
  85.     var bLoaded = (IE4ResourceDoc.root != null);
  86.     if (bLoaded)
  87.     {
  88.         DefaultLanguage = IE4ResourceDoc.root.getAttribute("DEFAULT_LANGUAGE");
  89.         DefaultImageTableSection = IE4ResourceDoc.root.getAttribute("DEFAULT_IMAGETABLE_SECTION");
  90.     }
  91.  
  92.     return bLoaded;
  93. }
  94.  
  95. // returns a <STRINGTABLE> xml element or null
  96. function IE4FindStringTable(Section, Language)
  97. {
  98.     // enumerate immediate children of the Resources root
  99.     var Level1Nodes = IE4ResourceDoc.root.children;     // root is level 0
  100.     var CurrentNode;
  101.     var NodeLanguage;
  102.     var NodeSection;
  103.     var bIsElement
  104.     var n;
  105.     for (n = 0; n < Level1Nodes.length; n++)
  106.     {
  107.         CurrentNode = Level1Nodes.item(n);
  108.         bIsElement = (CurrentNode.type == 0);
  109.             
  110.         // check if the CurrentNode refers to a STRINGTABLE element
  111.         // (note: second expression must not execute if not an element)
  112.         if (bIsElement && CurrentNode.tagName == "STRINGTABLE")
  113.         {
  114.             NodeLanguage = CurrentNode.getAttribute("LANGUAGE");
  115.             NodeSection = CurrentNode.getAttribute("SECTION");
  116.             if (NodeSection == Section && NodeLanguage == Language)
  117.                 return CurrentNode;     // found!
  118.         }
  119.     }   // for each level 1 nodes
  120.     
  121.     // all level 1 nodes exhausted
  122.     return null;
  123. }
  124.  
  125. // returns a <STRING> xml element or null
  126. function IE4FindString(StringID, StringTable)
  127. {
  128.     // only <STRING> elements and comments are inside a string table
  129.     var Nodes = StringTable.children;
  130.     var CurrentNode;
  131.     var NodeID;
  132.     var n;
  133.     
  134.     if (Nodes != null)
  135.     {
  136.         for (n = 0; n < Nodes.length; n++)
  137.         {
  138.             CurrentNode = Nodes.item(n);
  139.             NodeID = CurrentNode.getAttribute("ID");
  140.             if (NodeID == StringID)
  141.                 return CurrentNode;         // found!
  142.         }
  143.     }
  144.     
  145.     // string table child nodes exhausted
  146.     return null;
  147. }
  148.     
  149. // returns the retrieved string or or null
  150. // see IE5GetString() for additional information
  151. function IE4GetString(StringID, Section, Language)
  152. {
  153.     var bLanguageNotSpecified = (Language == "");
  154.     var StringTableSection;
  155.     var StringElement;
  156.         
  157.     if (Section == "") Section = "default"
  158.     if (Language == "") Language = DefaultLanguage;
  159.             
  160.     // Get the string table with the matching language and section
  161.     StringTableSection = IE4FindStringTable(Section, Language);
  162.     if (StringTableSection == null)
  163.         return null;
  164.         
  165.     StringElement = IE4FindString(StringID, StringTableSection);
  166.     if (StringElement == null && bLanguageNotSpecified)
  167.     {
  168.         // since no language was specified, we first tried the DefaultLanguage,
  169.         // we can't find it so now we try the neutral language
  170.         return IE4GetString(StringID, Section, "Neutral");
  171.     }
  172.     else if (StringElement == null)
  173.         return null;
  174.     else
  175.         return StringElement.text;
  176. }
  177.  
  178. // Each of the following image functions are similar to a
  179. // corresponding string function, but I have provided two
  180. // sets of functions.  This is to anticipate that the two
  181. // types of tables might develop into entirely different
  182. // formats.  Also, code is easier to read this way (though
  183. // we may sacrifice some maintainability).
  184. // I state my reasoning here to provide a basis for evaluation
  185. // when changes are being considered.
  186.  
  187. // returns a <IMAGETABLE> xml element or null
  188. function IE4FindImageTable(Section)
  189. {
  190.     // enumerate immediate children of the Resources root
  191.     var Level1Nodes = IE4ResourceDoc.root.children;     // root is level 0
  192.     var CurrentNode;
  193.     var NodeSection;
  194.     var bIsElement
  195.     var n;
  196.     for (n = 0; n < Level1Nodes.length; n++)
  197.     {
  198.         CurrentNode = Level1Nodes.item(n);
  199.         bIsElement = (CurrentNode.type == 0);
  200.             
  201.         // check if the CurrentNode refers to a IMAGETABLE element
  202.         // (note: second expression must not execute if not an element)
  203.         if (bIsElement && CurrentNode.tagName == "IMAGETABLE")
  204.         {
  205.             NodeSection = CurrentNode.getAttribute("SECTION");
  206.             if (NodeSection == Section)
  207.                 return CurrentNode;     // found!
  208.         }
  209.     }   // for each level 1 nodes
  210.     
  211.     // all level 1 nodes exhausted
  212.     return null;
  213. }
  214.  
  215. // returns an <IMAGE> xml element or null
  216. function IE4FindImage(ImageID, ImageTable)
  217. {
  218.     // only <IMAGE> elements and comments are inside a image table
  219.     var Nodes = ImageTable.children;
  220.     var CurrentNode;
  221.     var NodeID;
  222.     var n;
  223.     for (n = 0; n < Nodes.length; n++)
  224.     {
  225.         CurrentNode = Nodes.item(n);
  226.         NodeID = CurrentNode.getAttribute("ID");
  227.         if (NodeID == ImageID)
  228.             return CurrentNode;         // found!
  229.     }
  230.     
  231.     // image table child nodes exhausted
  232.     return null;
  233. }
  234.     
  235. // returns the image's filename or null
  236. // See IE5GetImage() for more details
  237. function IE4GetImage(ImageID, Section)
  238. {
  239.     var ImageTableSection;
  240.     var ImageElement;
  241.         
  242.     if (Section == "")
  243.         Section = DefaultImageTableSection;
  244.             
  245.     // Get the image table with the matching section
  246.     ImageTableSection = IE4FindImageTable(Section);
  247.     if (ImageTableSection == null)
  248.         return null;
  249.         
  250.     ImageElement = IE4FindImage(ImageID, ImageTableSection);
  251.     if (ImageElement == null)
  252.         return null;
  253.     else
  254.         return ImageElement.text;
  255. }
  256.  
  257. // IE5+ Functions *****************************************
  258. function IE5LoadResourceDoc()
  259. {
  260.     IE5ResourceDoc = new ActiveXObject("Microsoft.XMLDOM");
  261.     // Docs say async should be false so that the whole file
  262.     // is loaded before returning, but we get the error
  263.     // "no data available in requested resource" whenever
  264.     // we get the file from the dll resources.
  265.     // Setting it to true seems to work fine. (?!)
  266.     IE5ResourceDoc.async = true;
  267.     var bLoaded;
  268.     bLoaded = IE5ResourceDoc.load("resource.xml");
  269.     if (bLoaded)
  270.     {
  271.         DefaultLanguage = IE5ResourceDoc.documentElement.getAttribute("DEFAULT_LANGUAGE");
  272.         DefaultImageTableSection = IE5ResourceDoc.documentElement.getAttribute("DEFAULT_IMAGETABLE_SECTION");
  273.     }
  274.  
  275.     return bLoaded;
  276. }
  277.  
  278. // uses global variable IE5ResourceDoc as the loaded resource document
  279. // uses global variable DefaultLanguage when no Language is specified
  280. // returns string, or null if not found
  281. // concept: if you specify it, we assume that's what you want.. 
  282. //          if you don't specify it, we'll search default places for you
  283. function IE5GetString(StringID, Section, Language)
  284. {
  285.     var bLanguageNotSpecified = (Language == "");
  286.     var StringTableSection;
  287.     var StringElement;
  288.         
  289.     if (Section == "") Section = "default";
  290.     if (Language == "") Language = DefaultLanguage;
  291.             
  292.     // Get the string table with the matching language and section
  293.     StringTableSection = IE5ResourceDoc.documentElement.selectSingleNode(
  294.         "./STRINGTABLE"
  295.         + "[@LANGUAGE='" + Language + "']"
  296.         + "[@SECTION='" + Section + "']"
  297.     );
  298.     if (StringTableSection == null)
  299.         return null;
  300.         
  301.     StringElement = StringTableSection.selectSingleNode(
  302.         "./STRING"
  303.         + "[@ID='" + StringID + "']"
  304.     );
  305.     if (StringElement == null && bLanguageNotSpecified)
  306.         // since no language was specified, we first tried the DefaultLanguage,
  307.         // we can't find it so now try the neutral language
  308.         return IE5GetString(StringID, Section, "Neutral");
  309.     else if (StringElement == null)
  310.         return null;
  311.     else
  312.         return StringElement.text;
  313. }
  314.  
  315. // returns the image's filename or null
  316. // Parameters: ImageID is always needed, but if no Section is specified
  317. //             then the default section is assumed
  318. function IE5GetImage(ImageID, Section)
  319. {
  320.     var ImageTableSection;
  321.     var ImageElement;
  322.  
  323.     if (Section == "")
  324.         Section = DefaultImageTableSection;
  325.  
  326.     // Get the image table with the matching section
  327.     ImageTableSection = IE5ResourceDoc.documentElement.selectSingleNode(
  328.         "./IMAGETABLE"
  329.         + "[@SECTION='" + Section + "']"
  330.     );
  331.     if (ImageTableSection == null)
  332.         return null;
  333.         
  334.     ImageElement = ImageTableSection.selectSingleNode(
  335.         "./IMAGE"
  336.         + "[@ID='" + ImageID + "']"
  337.     );
  338.     if (ImageElement == null)
  339.         return null;
  340.     else
  341.         return ImageElement.text;
  342. }
  343.  
  344. // Generic Functions **************************************
  345. function LoadResourceDoc()
  346. {
  347.     if (IEVersion() >= 5)
  348.         return IE5LoadResourceDoc();
  349.     else
  350.         return IE4LoadResourceDoc();    
  351. }
  352.  
  353. function GetString(StringID, Section, Language)
  354. {
  355.     if (IEVersion() >= 5)
  356.         return IE5GetString(StringID, Section, Language);
  357.     else
  358.         return IE4GetString(StringID, Section, Language);
  359. }
  360.  
  361. function GetImage(ImageID, Section)
  362. {
  363.     if (IEVersion() >= 5)
  364.         return IE5GetImage(ImageID, Section);
  365.     else
  366.         return IE4GetImage(ImageID, Section);
  367. }
  368.