# ConversionService

ConversionService converts an object to the specified type. One common application is to convert to and from a string for typed value properties in the model. Another common application is to convert an input (such as a file) to a resource when instantiating the model.

A ConversionService implementation can delegate to other conversion services to create a conversion chain. In fact, a common conversion of an IFile to an XmlResource is a chain of two ConversionService implementations.

Sapphire includes a number of ConversionService implementations.

ID | Description
--- | ---
Sapphire.ConversionService.DateToString | ConversionService implementation for Date to String conversions.
Sapphire.ConversionService.DateToString.Property | ConversionService implementation for Date to String conversions specified at property level using @Serialization annotation.
Sapphire.ConversionService.EnumToString | ConversionService implementation for Enum to String conversions.
Sapphire.ConversionService.IFileToWorkspaceFileResourceStore | ConversionService implementation for IFile to WorkspaceFileResourceStore conversions.
Sapphire.ConversionService.ModelElementToDomDocument | ConversionService implementation for ModelElement to DOM Document conversions.
Sapphire.ConversionService.ModelElementToDomElement | ConversionService implementation for ModelElement to DOM Element conversions.
Sapphire.ConversionService.ModelElementToXmlElement | ConversionService implementation for ModelElement to XmlElement conversions.
Sapphire.ConversionService.ObjectToRootXmlResource | ConversionService implementation for Object to RootXmlResource conversions. Conversion is only performed if the object can be converted to ByteArrayResourceStore and if the resource store corresponds to a file with "xml" extension or if the context element type has XML binding annotations.
Sapphire.ConversionService.ObjectToString | ConversionService implementation for Object to String conversions that uses object's toString() method.
Sapphire.ConversionService.ResourceStoreToFile | ConversionService implementation for WorkspaceFileResourceStore to File conversions via IFile.
Sapphire.ConversionService.ResourceStoreToFile | ConversionService implementation for ResourceStore to File conversions via FileStoreEditorInput. Used in the case where an editor is open to a file outside of the workspace.
Sapphire.ConversionService.ResourceStoreToIContainer | ConversionService implementation for ResourceStore to IContainer conversions via IFile.
Sapphire.ConversionService.ResourceStoreToIFile | ConversionService implementation for ResourceStore to IFile conversions via FileEditorInput.
Sapphire.ConversionService.ResourceStoreToIJavaProject | ConversionService implementation for ResourceStore to IJavaProject conversions.
Sapphire.ConversionService.ResourceStoreToIProject | ConversionService implementation for ResourceStore to IProject conversions via IFile.
Sapphire.ConversionService.StringToBigDecimal | ConversionService implementation for String to BigDecimal conversions.
Sapphire.ConversionService.StringToBigInteger | ConversionService implementation for String to BigInteger conversions.
Sapphire.ConversionService.StringToBoolean | ConversionService implementation for String to Boolean conversions.
Sapphire.ConversionService.StringToByte | ConversionService implementation for String to Byte conversions.
Sapphire.ConversionService.StringToColor | ConversionService implementation for String to Color conversions.
Sapphire.ConversionService.StringToDate | ConversionService implementation for String to Date conversions.
Sapphire.ConversionService.StringToDate.Property | ConversionService implementation for String to Date conversions specified at property level using @Serialization annotation.
Sapphire.ConversionService.StringToDouble | ConversionService implementation for String to Double conversions.
Sapphire.ConversionService.StringToFileName | ConversionService implementation for String to FileName conversions.
Sapphire.ConversionService.StringToFloat | ConversionService implementation for String to Float conversions.
Sapphire.ConversionService.StringToFunction | ConversionService implementation for String to Function conversions.
Sapphire.ConversionService.StringToImageData | ConversionService implementation for String to ImageData conversions.
Sapphire.ConversionService.StringToInteger | ConversionService implementation for String to Integer conversions.
Sapphire.ConversionService.StringToJavaIdentifier | ConversionService implementation for String to JavaIdentifier conversions.
Sapphire.ConversionService.StringToJavaPackageName | ConversionService implementation for String to JavaPackageName conversions.
Sapphire.ConversionService.StringToJavaTypeName | ConversionService implementation for String to JavaTypeName conversions.
Sapphire.ConversionService.StringToLong | ConversionService implementation for String to Long conversions.
Sapphire.ConversionService.StringToPath | ConversionService implementation for String to Path conversions.
Sapphire.ConversionService.StringToShort | ConversionService implementation for String to Short conversions.
Sapphire.ConversionService.StringToUri | ConversionService implementation for String to URI conversions.
Sapphire.ConversionService.StringToUrl | ConversionService implementation for String to URL conversions.
Sapphire.ConversionService.StringToVersion | ConversionService implementation for String to Version conversions.
Sapphire.ConversionService.StringToVersionConstraint | ConversionService implementation for String to VersionConstraint conversions.
Sapphire.ConversionService.WorkspaceFileResourceStoreToIFile | ConversionService implementation for WorkspaceFileResourceStore to IFile conversions.
Sapphire.ConversionService.XmlEditorResourceStoreToIEditorInputConversionService | ConversionService implementation for XmlEditorResourceStore to IEditorInput conversions.
Sapphire.ConversionService.XmlResourceToDomDocument | ConversionService implementation for XmlResource to DOM Document conversions.
Sapphire.ConversionService.XmlResourceToDomElement | ConversionService implementation for XmlResource to DOM Element conversions.
Sapphire.ConversionService.XmlResourceToXmlElement | ConversionService implementation for XmlResource to XmlElement conversions.
Sapphire.Java.JDT.ConversionService.ElementToContext | ConversionService implementation for Element to Context conversions when the resource is inside a Java project.

Example

In the purchase order sample, a custom ConversionService implementation is used because the default file extension for purchase order files is "po" rather than "xml" and PurchaseOrder element does not have XML binding annotations. The combination of these two factors prevent the framework-provided ConversionService implementations from engaging.

    @Service( impl = PurchaseOrderResourceConversionService.class )

    public interface PurchaseOrder extends Element
    {
        ...
    }

    public class PurchaseOrderResourceConversionService extends ConversionService<Object,RootXmlResource>
    {
        public PurchaseOrderResourceConversionService()
        {
            super( Object.class, RootXmlResource.class );
        }

        @Override
        public RootXmlResource convert( Object object )
        {
            final ByteArrayResourceStore store = service( MasterConversionService.class ).convert( object, ByteArrayResourceStore.class );

            if( store != null )
            {
                return new RootXmlResource( new XmlResourceStore( store ) );
            }

            return null;
        }
    }

Note the use of chaining as part of the presented ConversionService implementation. The input could be any number of things, but as long as another ConversionService implementation knows how to convert it to a ByteArrayResourceStore, this implementation will take the conversion the rest of the way to a Resource.

Copyright (c) 2016 Oracle
Content made available under the terms of Eclipse Public License.