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 >
Wrap
Text File
|
2001-08-13
|
13KB
|
368 lines
// resource.js
//
// Initial Author: Alex Hui
// Date: May 10, 2000
//
// Purpose:
// --------
// This file contains the functions to access the resources,
// such as the string tables and image tables, which are stored
// in a resource.xml. The document is in XML format, but the
// users of the library of functions provided here should not
// need to know that detail.
//
// To use this "library", if we may call it so, add this line
// to the HTML file:
// <SCRIPT LANGUAGE="JavaScript" SRC="resource.js"></SCRIPT>
// Note:
// -----
// The support for XML under IE4 and IE5 are quite different.
// IE5 provides a rich set of methods such as searching for
// certain nodes given a tag name or certain attribute values.
// On the other hand, basically IE4 only provides methods to
// traverse the XML document tree and retrieve data.
//
// In fact, the XML object in the two versions are distinct.
// In IE4, many things have to be done ourselves. With some
// minor adjustments code written for IE4 will also work in
// IE5. However, for the following 4 reasons there will be
// separate implementations for IE4 and IE5+:
// 1) robustness, 2) efficiency, 3) in case future versions of
// IE will drop support for the old XML object and 4) for the
// sake of not using legacy code if we ever drop support for IE4.
// "Exports":
// ----------
// function LoadResourceDoc()
// function GetString(StringID, Section, Language)
// function GetImage(ImageID, Section)
// (other functions in this file are supposed to be internal)
var DefaultLanguage; // language to use when not specified
var DefaultImageTableSection; // image table section to use when not specified
var IE4ResourceDoc; // the XML object in IE4
var IE5ResourceDoc; // the XML object in IE5 or higher
// IE Version Detection ***********************************
function IEVersion()
{
var ua = window.navigator.userAgent
var msie = ua.indexOf("MSIE ")
// If Internet Explorer, return version number
// If another browser, return 0
if (msie > 0)
return parseInt( ua.substring(msie+5, ua.indexOf(".", msie)) );
else
return 0;
}
// IE4 Functions ******************************************
function GetXMLDocURL()
{
var CurrentURL; // HREF of the viewed document
var XMLDocURL; // built from CurrentURL but with the XML document as the filename
var FilenameIndex; // position of the filename (no path) within CurrentURL
// note: the filename is always preceded by "/" no matter what protocol
CurrentURL = window.location.href;
FilenameIndex = CurrentURL.lastIndexOf("/") + 1;
XMLDocURL = CurrentURL.substring(0, FilenameIndex) + "resource.xml";
return XMLDocURL;
}
function IE4LoadResourceDoc()
{
IE4ResourceDoc = new ActiveXObject("msxml");
// Docs say async should be false so that the whole file
// is loaded before returning, but we get the error
// "no data available in requested resource" whenever we
// use the dll resources.
// Setting it to true seems to work fine. (?!)
IE4ResourceDoc.async = true;
IE4ResourceDoc.url = GetXMLDocURL(); // in IE4 we must specify a protocol
var bLoaded = (IE4ResourceDoc.root != null);
if (bLoaded)
{
DefaultLanguage = IE4ResourceDoc.root.getAttribute("DEFAULT_LANGUAGE");
DefaultImageTableSection = IE4ResourceDoc.root.getAttribute("DEFAULT_IMAGETABLE_SECTION");
}
return bLoaded;
}
// returns a <STRINGTABLE> xml element or null
function IE4FindStringTable(Section, Language)
{
// enumerate immediate children of the Resources root
var Level1Nodes = IE4ResourceDoc.root.children; // root is level 0
var CurrentNode;
var NodeLanguage;
var NodeSection;
var bIsElement
var n;
for (n = 0; n < Level1Nodes.length; n++)
{
CurrentNode = Level1Nodes.item(n);
bIsElement = (CurrentNode.type == 0);
// check if the CurrentNode refers to a STRINGTABLE element
// (note: second expression must not execute if not an element)
if (bIsElement && CurrentNode.tagName == "STRINGTABLE")
{
NodeLanguage = CurrentNode.getAttribute("LANGUAGE");
NodeSection = CurrentNode.getAttribute("SECTION");
if (NodeSection == Section && NodeLanguage == Language)
return CurrentNode; // found!
}
} // for each level 1 nodes
// all level 1 nodes exhausted
return null;
}
// returns a <STRING> xml element or null
function IE4FindString(StringID, StringTable)
{
// only <STRING> elements and comments are inside a string table
var Nodes = StringTable.children;
var CurrentNode;
var NodeID;
var n;
if (Nodes != null)
{
for (n = 0; n < Nodes.length; n++)
{
CurrentNode = Nodes.item(n);
NodeID = CurrentNode.getAttribute("ID");
if (NodeID == StringID)
return CurrentNode; // found!
}
}
// string table child nodes exhausted
return null;
}
// returns the retrieved string or or null
// see IE5GetString() for additional information
function IE4GetString(StringID, Section, Language)
{
var bLanguageNotSpecified = (Language == "");
var StringTableSection;
var StringElement;
if (Section == "") Section = "default"
if (Language == "") Language = DefaultLanguage;
// Get the string table with the matching language and section
StringTableSection = IE4FindStringTable(Section, Language);
if (StringTableSection == null)
return null;
StringElement = IE4FindString(StringID, StringTableSection);
if (StringElement == null && bLanguageNotSpecified)
{
// since no language was specified, we first tried the DefaultLanguage,
// we can't find it so now we try the neutral language
return IE4GetString(StringID, Section, "Neutral");
}
else if (StringElement == null)
return null;
else
return StringElement.text;
}
// Each of the following image functions are similar to a
// corresponding string function, but I have provided two
// sets of functions. This is to anticipate that the two
// types of tables might develop into entirely different
// formats. Also, code is easier to read this way (though
// we may sacrifice some maintainability).
// I state my reasoning here to provide a basis for evaluation
// when changes are being considered.
// returns a <IMAGETABLE> xml element or null
function IE4FindImageTable(Section)
{
// enumerate immediate children of the Resources root
var Level1Nodes = IE4ResourceDoc.root.children; // root is level 0
var CurrentNode;
var NodeSection;
var bIsElement
var n;
for (n = 0; n < Level1Nodes.length; n++)
{
CurrentNode = Level1Nodes.item(n);
bIsElement = (CurrentNode.type == 0);
// check if the CurrentNode refers to a IMAGETABLE element
// (note: second expression must not execute if not an element)
if (bIsElement && CurrentNode.tagName == "IMAGETABLE")
{
NodeSection = CurrentNode.getAttribute("SECTION");
if (NodeSection == Section)
return CurrentNode; // found!
}
} // for each level 1 nodes
// all level 1 nodes exhausted
return null;
}
// returns an <IMAGE> xml element or null
function IE4FindImage(ImageID, ImageTable)
{
// only <IMAGE> elements and comments are inside a image table
var Nodes = ImageTable.children;
var CurrentNode;
var NodeID;
var n;
for (n = 0; n < Nodes.length; n++)
{
CurrentNode = Nodes.item(n);
NodeID = CurrentNode.getAttribute("ID");
if (NodeID == ImageID)
return CurrentNode; // found!
}
// image table child nodes exhausted
return null;
}
// returns the image's filename or null
// See IE5GetImage() for more details
function IE4GetImage(ImageID, Section)
{
var ImageTableSection;
var ImageElement;
if (Section == "")
Section = DefaultImageTableSection;
// Get the image table with the matching section
ImageTableSection = IE4FindImageTable(Section);
if (ImageTableSection == null)
return null;
ImageElement = IE4FindImage(ImageID, ImageTableSection);
if (ImageElement == null)
return null;
else
return ImageElement.text;
}
// IE5+ Functions *****************************************
function IE5LoadResourceDoc()
{
IE5ResourceDoc = new ActiveXObject("Microsoft.XMLDOM");
// Docs say async should be false so that the whole file
// is loaded before returning, but we get the error
// "no data available in requested resource" whenever
// we get the file from the dll resources.
// Setting it to true seems to work fine. (?!)
IE5ResourceDoc.async = true;
var bLoaded;
bLoaded = IE5ResourceDoc.load("resource.xml");
if (bLoaded)
{
DefaultLanguage = IE5ResourceDoc.documentElement.getAttribute("DEFAULT_LANGUAGE");
DefaultImageTableSection = IE5ResourceDoc.documentElement.getAttribute("DEFAULT_IMAGETABLE_SECTION");
}
return bLoaded;
}
// uses global variable IE5ResourceDoc as the loaded resource document
// uses global variable DefaultLanguage when no Language is specified
// returns string, or null if not found
// concept: if you specify it, we assume that's what you want..
// if you don't specify it, we'll search default places for you
function IE5GetString(StringID, Section, Language)
{
var bLanguageNotSpecified = (Language == "");
var StringTableSection;
var StringElement;
if (Section == "") Section = "default";
if (Language == "") Language = DefaultLanguage;
// Get the string table with the matching language and section
StringTableSection = IE5ResourceDoc.documentElement.selectSingleNode(
"./STRINGTABLE"
+ "[@LANGUAGE='" + Language + "']"
+ "[@SECTION='" + Section + "']"
);
if (StringTableSection == null)
return null;
StringElement = StringTableSection.selectSingleNode(
"./STRING"
+ "[@ID='" + StringID + "']"
);
if (StringElement == null && bLanguageNotSpecified)
// since no language was specified, we first tried the DefaultLanguage,
// we can't find it so now try the neutral language
return IE5GetString(StringID, Section, "Neutral");
else if (StringElement == null)
return null;
else
return StringElement.text;
}
// returns the image's filename or null
// Parameters: ImageID is always needed, but if no Section is specified
// then the default section is assumed
function IE5GetImage(ImageID, Section)
{
var ImageTableSection;
var ImageElement;
if (Section == "")
Section = DefaultImageTableSection;
// Get the image table with the matching section
ImageTableSection = IE5ResourceDoc.documentElement.selectSingleNode(
"./IMAGETABLE"
+ "[@SECTION='" + Section + "']"
);
if (ImageTableSection == null)
return null;
ImageElement = ImageTableSection.selectSingleNode(
"./IMAGE"
+ "[@ID='" + ImageID + "']"
);
if (ImageElement == null)
return null;
else
return ImageElement.text;
}
// Generic Functions **************************************
function LoadResourceDoc()
{
if (IEVersion() >= 5)
return IE5LoadResourceDoc();
else
return IE4LoadResourceDoc();
}
function GetString(StringID, Section, Language)
{
if (IEVersion() >= 5)
return IE5GetString(StringID, Section, Language);
else
return IE4GetString(StringID, Section, Language);
}
function GetImage(ImageID, Section)
{
if (IEVersion() >= 5)
return IE5GetImage(ImageID, Section);
else
return IE4GetImage(ImageID, Section);
}