Example usage for javax.xml.datatype DatatypeFactory newInstance

List of usage examples for javax.xml.datatype DatatypeFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.datatype DatatypeFactory newInstance.

Prototype

public static DatatypeFactory newInstance() throws DatatypeConfigurationException 

Source Link

Document

Obtain a new instance of a DatatypeFactory .

Usage

From source file:be.fedict.eid.tsl.TrustService.java

public TrustService(String serviceName, String serviceTypeIdentifier, String serviceStatus,
        DateTime statusStartingDate, X509Certificate... certificates) {
    this.serviceName = serviceName;
    this.statusStartingDate = statusStartingDate;
    this.objectFactory = new ObjectFactory();
    try {//from  w w w .j a  v  a 2 s  .  com
        this.datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException("datatype config error: " + e.getMessage(), e);
    }
    this.eccObjectFactory = new be.fedict.eid.tsl.jaxb.ecc.ObjectFactory();
    this.xadesObjectFactory = new be.fedict.eid.tsl.jaxb.xades.ObjectFactory();

    this.tspService = this.objectFactory.createTSPServiceType();
    TSPServiceInformationType tspServiceInformation = this.objectFactory.createTSPServiceInformationType();
    this.tspService.setServiceInformation(tspServiceInformation);
    tspServiceInformation.setServiceTypeIdentifier(serviceTypeIdentifier);
    InternationalNamesType i18nServiceName = this.objectFactory.createInternationalNamesType();
    List<MultiLangNormStringType> serviceNames = i18nServiceName.getName();
    MultiLangNormStringType serviceNameJaxb = this.objectFactory.createMultiLangNormStringType();
    serviceNames.add(serviceNameJaxb);
    serviceNameJaxb.setLang(Locale.ENGLISH.getLanguage());
    X509Certificate certificate = certificates[0];
    if (null == this.serviceName) {
        serviceNameJaxb.setValue(certificate.getSubjectX500Principal().toString());
    } else {
        serviceNameJaxb.setValue(this.serviceName);
    }
    tspServiceInformation.setServiceName(i18nServiceName);

    DigitalIdentityListType digitalIdentityList = createDigitalIdentityList(certificates);
    tspServiceInformation.setServiceDigitalIdentity(digitalIdentityList);

    tspServiceInformation.setServiceStatus(serviceStatus);

    GregorianCalendar statusStartingCalendar;
    if (null == this.statusStartingDate) {
        statusStartingCalendar = new DateTime(certificate.getNotBefore()).toGregorianCalendar();
    } else {
        statusStartingCalendar = this.statusStartingDate.toGregorianCalendar();
    }
    statusStartingCalendar.setTimeZone(TimeZone.getTimeZone("Z"));
    XMLGregorianCalendar statusStartingTime = this.datatypeFactory
            .newXMLGregorianCalendar(statusStartingCalendar);
    tspServiceInformation.setStatusStartingTime(statusStartingTime);
    /*
    if (null != serviceHistoryStatus){
       this.tspService.setServiceHistory(serviceHistoryStatus);
    }
    */
}

From source file:com.microsoft.exchange.ExchangeEventConverterImplTest.java

@Test
public void convertedCalendarMatchesStartTime() throws DatatypeConfigurationException {
    CalendarItemType calendarItem = new CalendarItemType();
    Date dateStartIn = new Date();
    XMLGregorianCalendar xmlStartIn = DateHelp.convertDateToXMLGregorianCalendar(dateStartIn);
    calendarItem.setStart(xmlStartIn);// ww  w. j av a2s.c o m

    Duration duration = DatatypeFactory.newInstance().newDuration(1000 * 60 * 60);
    XMLGregorianCalendar end = calendarItem.getStart();
    end.add(duration);
    calendarItem.setEnd(end);

    log.info("created calendar item with start=" + calendarItem.getStart());

    Calendar calendar = eventConverter.convertToCalendar(Collections.singleton(calendarItem), null);

    //calendar should not be null
    assertNotNull(calendar);
    ComponentList components = calendar.getComponents();
    //calendar should have components
    assertNotNull(components);

    //calendar should have exactly one component
    assertEquals(1, components.size());

    //components should be events
    assertEquals(components, calendar.getComponents(VEvent.VEVENT));

    Object object = components.get(0);
    assertNotNull(object);
    assertTrue(object instanceof VEvent);

    VEvent event = (VEvent) object;
    assertNotNull(event);

    DtStart dtStart = event.getStartDate();
    assertNotNull(dtStart);
    net.fortuna.ical4j.model.Date dateStartOut = dtStart.getDate();
    assertNotNull(dateStartOut);
    XMLGregorianCalendar xmlStartOut = DateHelp.convertDateToXMLGregorianCalendar(dateStartOut);

    log.info("dateStartIn=" + dateStartIn);
    log.info("xmlStartIn=" + xmlStartIn);
    log.info("dateStartOut=" + dateStartOut);
    log.info("xmlStartOut+=" + xmlStartOut);

    assertEquals(dateStartIn, new Date(dateStartIn.getTime()));
    assertEquals(xmlStartIn, xmlStartOut);
}

From source file:com.orange.cepheus.broker.Subscriptions.java

/**
 * @return the duration in String format
 * @throws SubscriptionException/*  w ww.  j  ava  2 s.c  om*/
 */
private Duration convertDuration(String duration) throws SubscriptionException {
    // Use java.xml.datatype functions as java.time do not handle durations with months and years...
    try {
        long longDuration = DatatypeFactory.newInstance().newDuration(duration).getTimeInMillis(new Date());
        return Duration.ofMillis(longDuration);
    } catch (Exception e) {
        throw new SubscriptionException("bad duration: " + duration, e);
    }
}

From source file:org.energyos.espi.datacustodian.web.api.ExportServiceTests.java

private String getXMLTime(int millis) throws DatatypeConfigurationException {
    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    GregorianCalendar cal = getGregorianCalendar(millis);
    XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar(cal);
    xmlGregorianCalendar.setFractionalSecond(null);
    return xmlGregorianCalendar.toXMLFormat();
}

From source file:net.di2e.ecdr.describe.generator.DescribeGeneratorImpl.java

public DescribeGeneratorImpl(CatalogFramework fw, FilterBuilder builder, GeneratorConfiguration config) {
    this.framework = fw;
    this.filterBuilder = builder;
    this.generatorConfig = config;

    try {/*w w w.j  ava  2  s. com*/
        dtf = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException e) {
        LOGGER.error(e.getMessage(), e);
    }

    this.requestProperties = new HashMap<>();
    this.requestProperties.put("ddf.security.subject", this.getSystemSubject());
    namespaceMap.put("ddms", DDMS_NAMESPACE);
    namespaceMap.put("ICISM", ICISM_NAMESPACE);
}

From source file:com.fusesource.example.camel.ingest.SimpleFileIngestorRouteBuilderTest.java

@Test
@DirtiesContext//from   ww  w  . ja v  a  2  s  .co m
public void testTerminalJdbcFailure() throws Exception {

    configureJdbcFailure(3);

    context.start();

    DatatypeFactory dtf = DatatypeFactory.newInstance();

    Set<String> expectedIds = new HashSet<String>();

    output.setExpectedMessageCount(9);
    output.setResultWaitTime(12000l);

    for (int i = 0; i < 10; i++) {
        RecordType recordType = new RecordType();
        recordType.setId(String.valueOf(i));
        recordType.setDate(dtf.newXMLGregorianCalendar(new GregorianCalendar()));
        recordType.setDescription("Record number: " + i);

        if (i != 1) {
            expectedIds.add(String.valueOf(i));
        }

        try {
            trigger.sendBody(SimpleFileIngestorRouteBuilder.HANDLE_RECORD_ROUTE_ENDPOINT_URI,
                    marshallToXml(recordType));
        } catch (CamelExecutionException e) {
            assertEquals("1", recordType.getId());
        }
    }

    output.assertIsSatisfied();

    for (Exchange exchange : output.getReceivedExchanges()) {
        assertTrue(expectedIds.remove(exchange.getIn().getBody(Record.class).getId()));
    }

    assertTrue(expectedIds.isEmpty());
}

From source file:fr.xebia.springframework.jms.support.converter.JaxbMessageConverterSpringTest.java

@Test
public void testFromMessage() throws Exception {

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<Employee>" + "<id>1</id>"
            + "<lastName>Le Clerc</lastName>" + "<firstName>Cyrille</firstName>" + "<gender>MALE</gender>"
            + "<birthdate>1976-01-05</birthdate>" + "</Employee>";
    Message message = session.createTextMessage(xml);

    Employee expected = new Employee(1, "Cyrille", "Le Clerc", Gender.MALE, DatatypeFactory.newInstance()
            .newXMLGregorianCalendarDate(1976, 01, 05, DatatypeConstants.FIELD_UNDEFINED));

    Employee actual = (Employee) jaxbMessageConverter.fromMessage(message);

    Assert.assertEquals(expected, actual);
}

From source file:com.evolveum.midpoint.task.quartzimpl.cluster.NodeRegistrar.java

private XMLGregorianCalendar getCurrentTime() {

    try {/*from   w ww  . j a va  2  s .  c  o  m*/
        // AFAIK the DatatypeFactory is not thread safe, so we have to create an instance every time
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
    } catch (DatatypeConfigurationException e) {
        // this should not happen
        throw new SystemException("Cannot create DatatypeFactory (to create XMLGregorianCalendar instance).",
                e);
    }
}

From source file:fr.xebia.springframework.jms.support.converter.JaxbMessageConverterTest.java

@Test
public void testFromMessage() throws Exception {

    JaxbMessageConverter jaxbMessageConverter = createJaxbMessageConverter(Employee.class, Gender.class);

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<Employee>" + "<id>1</id>"
            + "<lastName>Le Clerc</lastName>" + "<firstName>Cyrille</firstName>" + "<gender>MALE</gender>"
            + "<birthdate>1976-01-05</birthdate>" + "</Employee>";
    Message message = session.createTextMessage(xml);

    Employee expected = new Employee(1, "Cyrille", "Le Clerc", Gender.MALE, DatatypeFactory.newInstance()
            .newXMLGregorianCalendarDate(1976, 01, 05, DatatypeConstants.FIELD_UNDEFINED));

    Employee actual = (Employee) jaxbMessageConverter.fromMessage(message);

    Assert.assertEquals(expected, actual);
}

From source file:gov.nih.nci.cabig.caaers.api.InvestigatorMigratorServiceTest.java

private void modifyDates(Staff staff) throws Exception {

    DatatypeFactory df = DatatypeFactory.newInstance();
    Calendar gcNow = GregorianCalendar.getInstance();
    int year = gcNow.get(Calendar.YEAR);
    int month = gcNow.get(Calendar.MONTH) + 1;
    int day = gcNow.get(Calendar.DAY_OF_MONTH);
    int tz = DatatypeConstants.FIELD_UNDEFINED;

    XMLGregorianCalendar currXmlCal = df.newXMLGregorianCalendarDate(year, month, day, tz);
    XMLGregorianCalendar furXmlCal = df.newXMLGregorianCalendarDate(year + 1, month, day, tz);

    if (staff != null) {
        List<InvestigatorType> investigatorTypeList = staff.getInvestigator();
        for (InvestigatorType investigatorType : investigatorTypeList) {
            for (SiteInvestigatorType siType : investigatorType.getSiteInvestigator()) {
                siType.setStartDate(currXmlCal);
                siType.setEndDate(furXmlCal);
            }// www.  ja va 2  s  .c  o m
        }
    }

}