CLASS-ID. XmlConfiguration as "CoolThings.Commons.XmlConfiguration".
environment division.
configuration section.
repository.
class SystemException as "System.Exception"
class SystemString as "System.String"
class SystemXmlDocument as "System.Xml.XmlDocument"
class SystemXmlNode as "System.Xml.XmlNode"
class SystemIOFileNotFoundException as "System.IO.FileNotFoundException"
class SystemNullReferenceException as "System.NullReferenceException"
class SystemEnvironment as "System.Environment"
class SystemInt32 as "System.Int32"
*> object properties
property InnerText as "InnerText"
property StringValue as "StringValue"
property StringEmpty as "Empty".
static.
data division.
working-storage section.
01 instance usage object reference XmlConfiguration is private.
procedure division.
method-id. GetInstance AS "GetInstance".
data division.
linkage section.
01 localSingleton usage object reference XmlConfiguration.
procedure division returning localSingleton.
if (instance equal null) then
invoke XmlConfiguration "NEW" returning localSingleton
set instance to localSingleton
else
set localSingleton to instance
end-if
end method GetInstance.
end static.
*> Instance's data and methods
object.
environment division.
data division.
working-storage section.
01 xmlDocument usage object reference SystemXmlDocument is private.
01 xmlNode usage object reference SystemXmlNode is private.
01 envName usage object reference SystemString is private.
01 emptyString usage object reference SystemString is private.
01 nullReferenceException usage object reference SystemNullReferenceException.
01 sysException usage object reference SystemException.
01 fileNotFoundException usage object reference SystemIOFileNotFoundException.
procedure division.
method-id. NEW is private.
data division.
working-storage section.
01 environmentVariable usage object reference SystemEnvironment.
procedure division.
*> Retrieve file name from environment variable "XML_CONFIGFILE"
*> If XML_CONFIGFILE is not set then the default would be "application.config"
invoke SystemEnvironment "GetEnvironmentVariable" using "XML_CONFIGFILE" returning envName
if (SystemString::"Equals"(NULL, envName)= b"1") then
set envName to "app.config"
end-if
*> Load the xml document
invoke SystemXmlDocument "NEW" returning xmlDocument
try
invoke xmlDocument "Load" using envName
catch fileNotFoundException
display "File not found" *> TODO: to replace with a logger
end-try.
end method NEW.
method-id. GetKey as "GetKey".
data division.
working-storage section.
linkage section.
01 aKey usage object reference SystemString.
01 keyValue usage object reference SystemString.
procedure division using aKey returning keyValue.
try
invoke self "ValidateKey" using aKey
set aKey to SystemString::"Concat"(n"/", aKey)
invoke xmlDocument "SelectSingleNode" using aKey returning xmlNode
set keyValue to InnerText OF xmlNode
catch SysException
set keyValue to SystemString::"Concat"(n"@@Invalid key:", aKey)
end-try
end method GetKey.
method-id. ValidateKey as "ValidateKey" is private.
data division.
working-storage section.
01 valueZeros USAGE BINARY-LONG SIGNED value zeros.
01 valueMinusOne USAGE BINARY-LONG SIGNED value -1.
linkage section.
01 aKey usage object reference SystemString.
procedure division using aKey raising SystemException.
set valueMinusOne to aKey::"IndexOf"("//")
set valueZeros to aKey::"IndexOf"("/")
if valueMinusOne not = -1 or valueZeros not equal zeros then
invoke SystemException "NEW" returning sysException
exit method raising SysException
end-if
end method ValidateKey.
end object.
END CLASS XmlConfiguration.