use of javax.xml.datatype.DatatypeFactory in project OpenClinica by OpenClinica.
the class StudySubjectEndpoint method getXMLGregorianCalendarTime.
/**
* Helper method that translates a date object (specifically time) to an XMLGregorianCalendar which is the data type used in jaxb generated classes.
*
* @param date
* @return XMLGregorianCalendar
* @throws Exception
*/
private XMLGregorianCalendar getXMLGregorianCalendarTime(Date date) throws Exception {
GregorianCalendar gc = new GregorianCalendar(locale);
gc.setTime(date);
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar gcTime = df.newXMLGregorianCalendarTime(gc.get(Calendar.HOUR_OF_DAY), gc.get(Calendar.MINUTE), gc.get(Calendar.SECOND), null, DatatypeConstants.FIELD_UNDEFINED);
return gcTime;
}
use of javax.xml.datatype.DatatypeFactory in project OpenClinica by OpenClinica.
the class StudySubjectEndpoint method getXMLGregorianCalendarDate.
/**
* Helper method that translates a date object to an XMLGregorianCalendar which is the data type used in jaxb generated classes.
*
* @param date
* @return XMLGregorianCalendar
* @throws Exception
*/
private XMLGregorianCalendar getXMLGregorianCalendarDate(Date date) throws Exception {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar gcDate = df.newXMLGregorianCalendarDate(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH) + 1, gc.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
return gcDate;
}
use of javax.xml.datatype.DatatypeFactory in project camel by apache.
the class NotificationXmlFormatter method format.
public String format(Notification aNotification) throws NotificationFormatException {
ObjectHelper.notNull(jaxbContext, "jaxbContext");
NotificationEventType jaxb;
boolean wrap = false;
if (aNotification instanceof AttributeChangeNotification) {
AttributeChangeNotification ac = (AttributeChangeNotification) aNotification;
jaxb = mObjectFactory.createAttributeChangeNotification().withAttributeName(ac.getAttributeName()).withAttributeType(ac.getAttributeType()).withNewValue(ac.getNewValue() == null ? null : String.valueOf(ac.getNewValue())).withOldValue(ac.getOldValue() == null ? null : String.valueOf(ac.getOldValue()));
} else if (aNotification instanceof JMXConnectionNotification) {
jaxb = mObjectFactory.createJMXConnectionNotification().withConnectionId(((JMXConnectionNotification) aNotification).getConnectionId());
} else if (aNotification instanceof MBeanServerNotification) {
jaxb = mObjectFactory.createMBeanServerNotification().withMBeanName(String.valueOf(((MBeanServerNotification) aNotification).getMBeanName()));
} else if (aNotification instanceof MonitorNotification) {
MonitorNotification mn = (MonitorNotification) aNotification;
jaxb = mObjectFactory.createMonitorNotification().withDerivedGauge(String.valueOf(mn.getDerivedGauge())).withObservedAttribute(mn.getObservedAttribute()).withObservedObject(String.valueOf(mn.getObservedObject())).withTrigger(String.valueOf(mn.getTrigger()));
} else if (aNotification instanceof RelationNotification) {
RelationNotification rn = (RelationNotification) aNotification;
jaxb = mObjectFactory.createRelationNotification().withObjectName(String.valueOf(rn.getObjectName())).withRelationId(rn.getRelationId()).withRelationTypeName(rn.getRelationTypeName()).withRoleName(rn.getRoleName());
if (rn.getNewRoleValue() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getNewRoleValue());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withNewRoleValue(ont);
}
if (rn.getOldRoleValue() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getOldRoleValue());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withOldRoleValue(ont);
}
if (rn.getMBeansToUnregister() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getMBeansToUnregister());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withMBeansToUnregister(ont);
}
} else if (aNotification instanceof TimerNotification) {
jaxb = mObjectFactory.createTimerNotification().withNotificationId(((TimerNotification) aNotification).getNotificationID());
} else {
jaxb = mObjectFactory.createNotificationEventType();
wrap = true;
}
// add all of the common properties
jaxb.withMessage(aNotification.getMessage()).withSequence(aNotification.getSequenceNumber()).withSource(String.valueOf(aNotification.getSource())).withTimestamp(aNotification.getTimeStamp()).withType(aNotification.getType());
if (aNotification.getUserData() != null) {
jaxb.withUserData(String.valueOf(aNotification.getUserData()));
}
try {
DatatypeFactory df = getDatatypeFactory();
Date date = new Date(aNotification.getTimeStamp());
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
jaxb.withDateTime(df.newXMLGregorianCalendar(gc));
Object bean = wrap ? mObjectFactory.createNotificationEvent(jaxb) : jaxb;
StringWriter sw = new StringWriter();
// must create a new marshaller as its not thread safe
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(bean, sw);
return sw.toString();
} catch (JAXBException e) {
throw new NotificationFormatException(e);
} catch (DatatypeConfigurationException e) {
throw new NotificationFormatException(e);
}
}
use of javax.xml.datatype.DatatypeFactory in project cloudstack by apache.
the class PerfManagerMO method calendarToXMLGregorianCalendar.
/**
* Converts Calendar object into XMLGregorianCalendar
*
* @param calendar Object to be converted
* @return XMLGregorianCalendar
*/
private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar calendar) throws DatatypeConfigurationException {
DatatypeFactory dtf = DatatypeFactory.newInstance();
XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar();
xgc.setYear(calendar.get(Calendar.YEAR));
xgc.setMonth(calendar.get(Calendar.MONTH) + 1);
xgc.setDay(calendar.get(Calendar.DAY_OF_MONTH));
xgc.setHour(calendar.get(Calendar.HOUR_OF_DAY));
xgc.setMinute(calendar.get(Calendar.MINUTE));
xgc.setSecond(calendar.get(Calendar.SECOND));
xgc.setMillisecond(calendar.get(Calendar.MILLISECOND));
// Calendar ZONE_OFFSET and DST_OFFSET fields are in milliseconds.
int offsetInMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000);
xgc.setTimezone(offsetInMinutes);
return xgc;
}
use of javax.xml.datatype.DatatypeFactory in project jena by apache.
the class FunctionsSQL method toSQLdatetimeString.
/** Turn the lexical form of an XSD date into what SQL believes in */
public static String toSQLdatetimeString(String lex) {
try {
DatatypeFactory f = DatatypeFactory.newInstance();
XMLGregorianCalendar cal = f.newXMLGregorianCalendar(lex);
long millis = cal.toGregorianCalendar().getTimeInMillis();
Timestamp timestamp = new Timestamp(millis);
return timestamp.toString();
} catch (DatatypeConfigurationException e) {
LoggerFactory.getLogger(SQLUtils.class).warn("Failed to convert " + lex, e);
return "0000-00-00 00:00:00";
}
}
Aggregations