Using a Java properties file
Gosu provides a utilities class, gw.util.PropertiesFileAccess, that supports parsing a Java properties file. By using this class, you can access the values of the properties in a straightforward way.
You use the getProperties method of the PropertiesFileAccess class to load
the properties from the file as a Java Properties object. This method takes the full path and
name of the properties file as a String parameter. For example, if your properties file is
GlobalProperties.properties in com.mycompany.properties, you pass
com/mycompany/properties/GlobalProperties.properties to getProperties.
You then use the Java getProperty method on the Properties object to get each
Java Property object. You can convert the Java Property object into a static
Gosu property.
Example: Accessing Java properties in Gosu
Consider the following Java properties file, MyProps.properties, which is located in the gsrc.doc.examples folder in Guidewire Studio.
# doc.examples.MyProps
# The hash character as the first char means that the line is a comment
! The exclamation mark character as the first char means that the line is a comment
website = http://guidewire.com/
transport = bicycle
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
Gosu!
# Unicode support
tab : \u0009
# multiple levels of hierarchy for the key
gosu.example.properties.Key1 = Value1
The following Gosu class converts some of the Java properties into static Gosu properties.
package doc.examples
uses gw.util.PropertiesFileAccess
class MyPropsAccess {
static property get MyProperties() : Properties {
// Using getProperties reads the file only once. The Properties are cached as a static property.
// You must use the fully qualified path to the properties file even though MyPropsAccess and
// MyProps.properties are in the same folder.
return PropertiesFileAccess.getProperties("doc/examples/MyProps.properties")
}
static property get Website() : String {
return MyProperties.getProperty("website")
}
static property get Transport() : String {
return MyProperties.getProperty("transport")
}
static property get Message() : String {
return MyProperties.getProperty("message")
}
}
The following code reads the static Gosu properties.
// Get static properties
print(" website: ${MyPropsAccess.Website }")
print("transport: ${MyPropsAccess.Transport }")
print(" message: ${MyPropsAccess.Message }")
// Use methods on the MyProperties static property to access other property values
print(" tab: before${MyPropsAccess.MyProperties.getProperty("tab")}after")
// Access a multi-level property
print("multi-level: ${MyPropsAccess.MyProperties.getProperty("gosu.example.properties.Key1")}")
Running this code produces the following output.
website: http://guidewire.com/
transport: bicycle
message: Welcome to Gosu!
tab: before after
multi-level: Value1
