| Context and Deployment Descriptor > The Context Services > Type Converter |
Type Converter |
INDEX
PREVIOUS
NEXT
|
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:
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.
java.beans.PropertyEditor) for a given class. You can register your own property editor using the following method:
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.
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>
IlpDefaultContext context = ... IlpTypeConverter converter = new IlpDefaultTypeConverter(context); context.addService(IlpTypeConverter.class, converter);
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 in the Business Objects and Data Sources documentation.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. All rights reserved. Legal terms. | PREVIOUS NEXT |