Search in sources :

Example 1 with XmlAnySimpleType

use of org.apache.xmlbeans.XmlAnySimpleType in project hale by halestudio.

the class SimpleTypeUtil method convertToXml.

/**
 * Convert a simple type value to a string
 *
 * @param <T> the type of the value
 * @param value the value
 * @param type the type definition of the simple type
 * @return the string representation of the value or <code>null</code> if
 *         the value is <code>null</code>
 */
public static <T> String convertToXml(T value, TypeDefinition type) {
    if (value == null) {
        return null;
    }
    ConversionService conversionService = HalePlatform.getService(ConversionService.class);
    Class<? extends XmlAnySimpleType> simpleType = getSimpleType(type);
    if (simpleType != null) {
        try {
            XmlAnySimpleType simpleTypeValue = conversionService.convert(value, simpleType);
            if (simpleTypeValue instanceof XmlDateTime) {
                XmlDateTime xmlDateTime = (XmlDateTime) simpleTypeValue;
                // use Zulu time to have a reproducable result
                // (as the old Java Date types always assume the current
                // time zone!)
                // 
                // XXX this should be removed when time/date types are used
                // that are timezone aware
                Calendar calendar = xmlDateTime.getCalendarValue();
                GDateBuilder builder = new GDateBuilder(calendar);
                builder.normalizeToTimeZone(0);
                GDate gdate = builder.toGDate();
                xmlDateTime.setGDateValue(gdate);
            }
            if (simpleTypeValue != null) {
                return simpleTypeValue.getStringValue();
            }
        } catch (ConversionException e) {
        // ignore
        }
    }
    // try to convert to string
    try {
        String stringValue = conversionService.convert(value, String.class);
        if (stringValue != null) {
            return stringValue;
        }
    } catch (ConversionException e) {
    // ignore
    }
    // fall-back
    return value.toString();
}
Also used : XmlAnySimpleType(org.apache.xmlbeans.XmlAnySimpleType) ConversionException(org.springframework.core.convert.ConversionException) XmlDateTime(org.apache.xmlbeans.XmlDateTime) ConversionService(org.springframework.core.convert.ConversionService) Calendar(java.util.Calendar) GDateBuilder(org.apache.xmlbeans.GDateBuilder) GDate(org.apache.xmlbeans.GDate)

Aggregations

Calendar (java.util.Calendar)1 GDate (org.apache.xmlbeans.GDate)1 GDateBuilder (org.apache.xmlbeans.GDateBuilder)1 XmlAnySimpleType (org.apache.xmlbeans.XmlAnySimpleType)1 XmlDateTime (org.apache.xmlbeans.XmlDateTime)1 ConversionException (org.springframework.core.convert.ConversionException)1 ConversionService (org.springframework.core.convert.ConversionService)1