Register  Login
OO Cobol Examples » XML » How to: XML configuration file
 

Sponsors


Using an XML as configuration file

XML files are widely used by Java / .Net applications. OO Cobol too. The following class provides a method called GetKey to retrieve key values in an XML file. The default XML file name is app.config, but you can override this by defining an environment variable XML_CONFIGFILE.

 1:  CLASS-ID. XmlConfiguration as "CoolThings.Commons.XmlConfiguration".
 2:      
 3:      environment division.
 4:          configuration section.
 5:              repository.
 6:                  class  SystemException               as "System.Exception"
 7:                  class  SystemString                  as "System.String"
 8:                  class  SystemXmlDocument             as "System.Xml.XmlDocument"
 9:                  class  SystemXmlNode                 as "System.Xml.XmlNode"
 10:                  class  SystemIOFileNotFoundException as "System.IO.FileNotFoundException"
 11:                  class  SystemNullReferenceException  as "System.NullReferenceException"
 12:                  class  SystemEnvironment             as "System.Environment"
 13:                  class  SystemInt32                   as "System.Int32"
 14:                  
 15:                  *> object properties
 16:                  property  InnerText                 as "InnerText"
 17:                  property  StringValue               as "StringValue"
 18:                  property  StringEmpty               as "Empty".
 19:          
 20:      static.
 21:          data division.
 22:             working-storage section.
 23:             01 instance    usage object reference XmlConfiguration      is private.
 24:          
 25:          procedure division.
 26:              method-id. GetInstance AS "GetInstance".
 27:                  data division.
 28:                      linkage section.
 29:                      01 localSingleton usage object reference XmlConfiguration.
 30:                      
 31:                  procedure division returning localSingleton.
 32:                      if (instance equal null) then
 33:                            invoke XmlConfiguration "NEW" returning localSingleton
 34:                            set    instance  to localSingleton
 35:                      else
 36:                            set    localSingleton    to instance
 37:                      end-if
 38:                      
 39:              end method GetInstance.
 40:      end static.
 41:      
 42:      *> Instance's data and methods       
 43:      object.
 44:          environment division.
 45:              data division.
 46:                  working-storage section.
 47:                  
 48:                  01 xmlDocument usage object reference SystemXmlDocument is private.
 49:                  01 xmlNode     usage object reference SystemXmlNode     is private.
 50:                  01 envName     usage object reference SystemString      is private.
 51:                  01 emptyString usage object reference SystemString      is private.
 52:                  
 53:                  01 nullReferenceException usage object reference SystemNullReferenceException.
 54:                  01 sysException           usage object reference SystemException.
 55:                  01 fileNotFoundException  usage object reference SystemIOFileNotFoundException.
 56:          
 57:          procedure division.
 58:            
 59:              method-id. NEW is private.
 60:                  data division.
 61:                      working-storage section.
 62:                          01 environmentVariable      usage object reference SystemEnvironment.
 63:                  procedure division.
 64:                      *> Retrieve file name from environment variable "XML_CONFIGFILE"
 65:                      *> If XML_CONFIGFILE is not set then the default would be "application.config"
 66:                      
 67:                      invoke SystemEnvironment "GetEnvironmentVariable" using "XML_CONFIGFILE" returning envName
 68:                      
 69:                      if  (SystemString::"Equals"(NULL, envName)= b"1") then
 70:                          set  envName           to  "app.config"    
 71:                      end-if
 72:                      
 73:                      *> Load the xml document
 74:                      invoke SystemXmlDocument "NEW" returning xmlDocument
 75:                      try
 76:                           invoke xmlDocument "Load" using envName
 77:                      
 78:                      catch fileNotFoundException
 79:                           display "File not found" *> TODO: to replace with a logger
 80:                          
 81:                      end-try.
 82:                          
 83:              end method NEW.
 84:              
 85:              method-id. GetKey as "GetKey".
 86:                  data division.
 87:                      working-storage section.
 88:                      linkage section.
 89:                          01 aKey     usage object reference SystemString.
 90:                          01 keyValue usage object reference SystemString.
 91:                          
 92:                  procedure division using aKey returning keyValue.
 93:                      
 94:                      try
 95:                          invoke self "ValidateKey" using aKey
 96:                          
 97:                          set  aKey      to  SystemString::"Concat"(n"/", aKey)
 98:                          
 99:                          invoke xmlDocument "SelectSingleNode" using aKey returning xmlNode
 100:                          
 101:                          set  keyValue        to InnerText OF xmlNode
 102:                          
 103:                      catch SysException
 104:                          set  keyValue        to  SystemString::"Concat"(n"@@Invalid key:", aKey)
 105:                          
 106:                      end-try
 107:                  
 108:              end method GetKey.
 109:              
 110:              method-id. ValidateKey as "ValidateKey" is private.
 111:                  data division.
 112:                      working-storage section.
 113:                          01 valueZeros     USAGE BINARY-LONG SIGNED value zeros.
 114:                          01 valueMinusOne  USAGE BINARY-LONG SIGNED value -1.
 115:                          
 116:                      linkage section.
 117:                          01 aKey     usage object reference SystemString.
 118:                  
 119:                  procedure division using aKey raising SystemException.
 120:                      set valueMinusOne     to  aKey::"IndexOf"("//")
 121:                      set valueZeros        to  aKey::"IndexOf"("/")
 122:                    
 123:                      if valueMinusOne not = -1  or valueZeros not equal zeros then
 124:                         invoke SystemException "NEW" returning sysException
 125:                         exit method raising SysException
 126:                      end-if
 127:              end method ValidateKey.
 128:              
 129:      end object.        
 130:  END CLASS XmlConfiguration.
 131:        
 132:        

 The XML file must be a valid XML file, for example:

 1:  ?xml version="1.0" encoding="utf-8" ?
 2:  configuration
 3:      checkout
 4:          currentSupplier/configuration/checkout/suppliers/fujitsu/currentSupplier
 5:          suppliers
 6:              fujitsu
 7:                  currentSet/configuration/checkout/suppliers/fujitsu/sets/TeamPos500/currentSet
 8:                  sets
 9:                      TeamPos500
 10:                          cashDrawerTeamPos1054258002/cashDrawer
 11:                          receiptPrinterTeamPoSFD21/receiptPrinter
 12:                      /TeamPos500
 13:                      TeamPos1000
 14:                          cashDrawerTeamPos10PB60014/cashDrawer
 15:                          receiptPrinterTeamPoSFD22/receiptPrinter
 16:                      /TeamPos1000
 17:                  /sets
 18:              /fujitsu
 19:              hp
 20:                  currentSet/configuration/checkout/suppliers/hp/sets/HPPOS4SmallBusiness/currentSet
 21:                  sets
 22:                      HPPOS4SmallBusiness
 23:                          cashDrawerHPFK182AT/cashDrawer
 24:                          receiptPrinterHPFK224AT/receiptPrinter
 25:                      /HPPOS4SmallBusiness
 26:                  /sets
 27:              /hp
 28:          /suppliers
 29:      /checkout
 30:      db
 31:          connection
 32:              oracle
 33:                  DataSourceData Source=TORCL/DataSource
 34:                  UserIdadmin/UserId
 35:                  PasswordmyPassword/Password
 36:              /oracle
 37:          /connection
 38:      /db
 39:  /configuration

The syntax is pretty simple: xmlConfig::"GetKey"("/path/keyName"). The following class shows how to use our little utility class:

 1:  CLASS-ID. Utils as "AFP.Utils".
 2:      environment division.
 3:          configuration section.
 4:              repository.
 5:                  class     ClassXmlConfiguration          as "CoolThings.Commons.XmlConfiguration"
 6:                  class     SystemString                   as "System.String"
 7:                  class     CollectionHashTable            as "System.Collections.Hashtable".
 8:      
 9:      static.
 10:          data division.
 11:              working-storage section.
 12:                  01 xmlConfig           usage object reference ClassXmlConfiguration.
 13:                  01 driverName          usage object reference SystemString.
 14:                  01 keyValue            pic n(200).
 15:          procedure division.
 16:              method-id. GetCheckoutDrivers as "GetCheckoutDrivers" is public.
 17:                  data division.
 18:                      linkage section.
 19:                      01 hashTable       usage object reference CollectionHashTable.
 20:                      
 21:                  procedure division returning hashTable.
 22:                      invoke ClassXmlConfiguration "GetInstance" returning xmlConfig
 23:                      
 24:                      invoke CollectionHashTable "NEW" returning hashTable
 25:                      
 26:                      set    keyValue    to  xmlConfig::"GetKey"(n"/configuration/checkout/currentSupplier")
 27:                      set    keyValue    to  SystemString::"Concat"(keyValue, n"/currentSet")
 28:                      set    keyValue    to  xmlConfig::"GetKey"(keyValue)
 29:                      set    driverName  to  SystemString::"Concat"(keyValue, n"/cashDrawer")
 30:                      set    driverName  to  xmlConfig::"GetKey"(driverName)
 31:                      
 32:                      invoke hashTable "Add" using "CashDrawer", driverName
 33:                      set    driverName  to  SystemString::"Concat"(keyValue, n"/receiptPrinter")
 34:                      set    driverName  to  xmlConfig::"GetKey"(driverName)
 35:                      
 36:                      invoke hashTable "Add" using "ReceiptPrinter", driverName.
 37:                      
 38:              end method GetCheckoutDrivers.
 39:      end static.
 40:  END CLASS Utils.            
 
 
 
 
Creative Commons License The text of this site is licensed under a Creative Commons License.

 

 

Comments

 Name  
 Email
 Comment  
CAPTCHA image
Enter the code shown above

Terms Of Use | Privacy Statement | Copyright 2009-2010 by RedRailsDynnamite DotNetNuke Skins & Modules