Custom Parsing

Custom parsing is implemented when users must enter values in a form that existing parse operations do not recognize or when some other processing must be performed on values before they are submitted to the application server. Custom parsing may be as simple as a routine that first removes a currency symbol from a numeric value before parsing it, where the currency symbol may have been entered by a user or added by a custom format operation. It could also be something more unusual: a translation of a date to another calendar system, a routine that pads string values, or an arbitrary calculation on numeric values.

  1. Identify an existing converter plug-in class that you want to customize. It will most likely be the converter that is already configured for the domain in question or inherited by it from an ancestor domain.
  2. Create a new sub-class of the relevant converter plug-in and override the parse method.
  3. In the implementation of the method, you can perform some processing before or after invoking the super-class's method of the same name, or implement the parsing code from scratch.
  4. Configure your new plug-in for the relevant domains.

The currency symbol scenario is continued in this example to complement the example shown for a custom format operation above. The example below shows the same class developed to format money values with a currency symbol; the class is now extended with a corresponding parse operation. In a case like this, you do not write separate converter plug-ins for formatting and parsing; you must implement both operations in the same converter plug-in and then associate the plug-in with the appropriate domain.

Figure 1. Custom Parsing for Currency Values
/**
 * Converter that supports the use of a dollar symbol for
 * money values.
 */
public class USDollarConverter
       extends SvrMoneyConverter {
  public String format(Object data)
         throws ConversionException {
    return "$" + super.format(data);
  }

  public Object parse(String data)
         throws ConversionException {
    if (data.startsWith("$")) {
      return super.parse(data.substring(1));
    }
    return super.parse(data);
  }
}

The value passed to the parse method is the same value that was entered by the user; it is possible that it contains no currency symbol or it might contain space characters between the currency symbol and the value. You can use the UML domain definition options to ensure that the pre-parse operation will have removed any whitespace before the currency symbol, or simply report an error if the currency symbol or a digit is not the first character. The parse method above assumes that the currency symbol is the optional first character and then leaves all other decisions up to the parse method of the super-class. This is probably the best approach, as it limits the number of formatting rules that a user needs to be aware of and keeps the code as simple as possible.

The configuration for this plug-in is unchanged from that shown for the custom format operation.