Search in sources :

Example 1 with GDateBuilder

use of org.apache.xmlbeans.GDateBuilder 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)

Example 2 with GDateBuilder

use of org.apache.xmlbeans.GDateBuilder in project arctic-sea by 52North.

the class SweCommonEncoderv101 method createDateTime.

private XmlDateTime createDateTime(DateTime sosDateTime) {
    XmlDateTime xbDateTime = XmlDateTime.Factory.newInstance(getXmlOptions());
    // encode the DateTime in UTC
    GDateBuilder gdb = new GDateBuilder(sosDateTime.toDate());
    gdb.normalize();
    xbDateTime.setGDateValue(gdb.toGDate());
    return xbDateTime;
}
Also used : XmlDateTime(org.apache.xmlbeans.XmlDateTime) GDateBuilder(org.apache.xmlbeans.GDateBuilder)

Aggregations

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