Formatter instance is created from an xml definition. Generally speaking, a formatter definition defines the rule to execute formatting and unformatting. It has the same structure with the corresponding structured data, and the formatter instance created from the formatter definition is capable of formatting and unformatting the corresponding structured data. Each formatter definition has a key to differentiate itself from others.
Get a formatter instance from a key. In this way, the instance will be cached for reuse, which will help reduce the frequency of reading the definition files. In fact, each formatter instance is identified by the key, and there is only one formatter instance for one key. The instance might be required to format many instances of structured data at the same time, so the status of formatting or unformatting process should not be kept in the instance of formatter.
Use the formatter to translate (format) the structured data into a host message. The Formatter instance will process the structured data according to the predefined rule, and put the data content into a flat String or byte[], so that it can be recognized by the host application.
Given a host message (a flat String or byte[]) and an instance of structured data (which is not populated with data content), the Formatter instance will parse the message according to the predefined rule, and populate the data content contained in the host message into the instance of structured data.
Given the structured data with data content populated, the formatter instance will processes it according to the predefined rule and translate it into an XML String.
Given an XML String and an empty instance of structured data, the formatter instance will parse the XML String according to the predefined rule and populate the instance of structured data with data content in the XML String.
Given the structured data with data content populated, the formatter instance will process it according to the predefined rule and translate it into a JSON String.
Given a JSON String and an empty instance of structured data, the formatter instance will parse the JSON String according to the predefined rule and populate the instance of structured data with the data content in the JSON String.
BTT provides a set of FormatElements, which are the elements of formatters. You can implement your own FormatterElement. Besides extending to FormatElement, you can also extend to Formatter so that it can support other kinds of structured data.