Type converter

The type converter service defined by the interface IlpTypeConverter is mainly used to read and write XML files that contain data source information. All the attributes of business objects are converted using this service. Refer to Type conversion in the Business Objects and Data Sources documentation for more information.
The main methods of the IlpTypeConverter interface are the following:
  • createJavaInstance(Class type, String value) . Creates a Java™ instance from a class type. The value is initialized with the supplied string value. IlEnum , dates, colors and fonts are specifically supported. Icons are also specifically supported to add a full path to the icon name. All Java basic types and any Java class which has a string constructor are supported.
  • String createStringValue(Class type, Object value) . Creates a String value from a general Java Object . This method produces string values that can be used in an XML file. Specifically, dates, colors, and fonts produce strings that are correctly formatted for use in data storage XML files. Any types that are not specifically handled will return the result of calling toString() on the value.
By default, the type converter service is implemented by the class IlpDefaultTypeConverter. This class can convert basic classes into Strings or Strings into basic classes. It handles the following basic types by default:
  • java.lang.Byte
  • java.lang.Character
  • java.lang.Double
  • java.lang.Float
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Short
  • java.lang.String
  • java.awt.Color
  • java.awt.Image
  • java.awt.Font
  • javax.swing.SwingConstants
  • javax.swing.KeyStroke
  • ilog.util.IlEnum
  • ilog.util.Date
  • ilog.cpl.graphic.IlpGraphicRenderer
  • ilog.cpl.interactor.IlpInteractor
The default type converter implementation uses the following two mechanisms to convert values from String to instance and instance to String:
  1. A String-based constructor: This mechanism looks in the target class using the reflection API to retrieve a String-based constructor. This constructor is then used to convert String values to instances of a given class. Conversely, when converting instance values to String values, the method toString is used. The easiest way to automatically convert a user class using the default type converter implementation is to provide both a String-based constructor and a toString method in the user class.
  2. Property editors: The default type converter implementation also allows you to register property editors (see java.beans.PropertyEditor ) for a given class. You can register your own property editor using the following method:
    IlpDefaultTypeConverter.registerEditor (Class targetType, Class editorClass)
    
    The property editor for the following classes must not be overridden: Date , Boolean , Color , Font , IlpPoint , IlpRect and IlpShelfItemPosition . The default type converter class implements a specific treatment for these classes which cannot be overridden.
When a default context instance is created, it is initialized with a type converter that is an instance of IlpDefaultTypeConverter. You can change this configuration through the deployment descriptor file or through the API.

How to initialize the type converter service through the deployment descriptor file

You can initialize the type converter service in the deployment descriptor file using the tag <typeConverter> as follows:
[<typeConverter javaClass="class name"/>]
where the javaClass indicates the name of a Java class that implements the IlpTypeConverter interface.
The following example illustrates the customization of the type converter service through the deployment descriptor:
<?xml version="1.0"?>
<deployment xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation = "ilog/cpl/schema/deploy.xsd">
  <typeConverter javaClass = "package.MyTypeConverter" />
</deployment>

How to initialize the type converter service using the API

IlpDefaultContext context = ... 
IlpTypeConverter converter = new IlpDefaultTypeConverter(context);
context.addService(IlpTypeConverter.class, converter);

How to use the type converter service through the API

IlpTypeConverter converter = context.getTypeConverter();
Integer value = (Integer) converter.createJavaInstance(Integer.class, "10");
and
Integer value = ...;
String valueString = converter.createStringValue(null, value);
The default type converter can be extended to support complex XML types (made up of several XML tags) through the interface IlpSAXSerializable. For more information, refer to Complex types.