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:com.marklogic.client.test.EvalTest.java

private void runAndTestJavascript(ServerEvaluationCall call)
        throws DatatypeConfigurationException, JsonProcessingException, IOException {
    call = call//from w w w .j a  v  a  2 s.  com
            // String is directly supported in any EvalBuilder
            .addVariable("myString", "Mars")
            // ArrayNode extends JSONNode which is mapped to implicitly use JacksonHandle
            .addVariableAs("myArray", new ObjectMapper().createArrayNode().add("item1").add("item2"))
            // ObjectNode extends JSONNode which is mapped to implicitly use JacksonHandle
            .addVariableAs("myObject", new ObjectMapper().createObjectNode().put("item1", "value1"))
            // the rest are auto-boxed by EvalBuilder.addVariable(String, Number)
            .addVariable("myBool", true).addVariable("myInteger", 123).addVariable("myDouble", 1.1)
            .addVariable("myDate", DatatypeFactory.newInstance().newXMLGregorianCalendar(septFirst).toString())
            // we can pass a null node
            .addVariable("myNull", (String) null);
    EvalResultIterator results = call.eval();
    try {
        assertEquals("myString looks wrong", "Mars", results.next().getAs(String.class));
        assertEquals("myArray looks wrong", new ObjectMapper().readTree("[\"item1\",\"item2\"]"),
                results.next().getAs(JsonNode.class));
        assertEquals("myObject looks wrong", new ObjectMapper().readTree("{\"item1\":\"value1\"}"),
                results.next().getAs(JsonNode.class));
        assertEquals("myBool looks wrong", true, results.next().getBoolean());
        assertEquals("myInteger looks wrong", 123, results.next().getNumber().intValue());
        assertEquals("myDouble looks wrong", 1.1, results.next().getNumber().doubleValue(), .001);
        // the same format we sent in (from javax.xml.datatype.XMLGregorianCalendar.toString())
        assertEquals("myDate looks wrong", "2014-09-01T00:00:00.000+02:00", results.next().getString());
        assertEquals("myNull looks wrong", null, results.next().getString());
    } finally {
        results.close();
    }

}

From source file:it.cnr.icar.eric.server.persistence.rdb.SubscriptionDAO.java

@SuppressWarnings("unchecked")
protected void loadObject(Object obj, ResultSet rs) throws RegistryException {
    try {//w  ww  .  j a  v a2 s  .  c  o m
        if (!(obj instanceof SubscriptionType)) {
            throw new RegistryException(ServerResourceBundle.getInstance()
                    .getString("message.SubscriptionTypeExpected", new Object[] { obj }));
        }

        SubscriptionType ebSubscriptionType = (SubscriptionType) obj;
        super.loadObject(obj, rs);

        String selector = rs.getString("selector");
        ebSubscriptionType.setSelector(selector);

        // Need to work around a bug in PostgreSQL and loading of
        // ClassificationScheme data from NIST tests
        try {
            Timestamp endTimestamp = rs.getTimestamp("endTime");

            if (endTimestamp != null) {
                // Calendar calendar = Calendar.getInstance();
                // calendar.setTimeInMillis(endTime.getTime());

                GregorianCalendar calendar = new GregorianCalendar();
                calendar.setTimeInMillis(endTimestamp.getTime());
                XMLGregorianCalendar endTime = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);

                ebSubscriptionType.setEndTime(endTime);
            }
        } catch (StringIndexOutOfBoundsException e) {
            String id = rs.getString("id");
            log.error(ServerResourceBundle.getInstance().getString("message.SubscriptionDAOId",
                    new Object[] { id }), e);
        }

        String notificationIntervalString = rs.getString("notificationInterval");

        if (notificationIntervalString != null) {
            Duration notificationInterval = DatatypeFactory.newInstance()
                    .newDuration(notificationIntervalString);
            ebSubscriptionType.setNotificationInterval(notificationInterval);
        }

        // Need to work around a bug in PostgreSQL and loading of
        // ClassificationScheme data from NIST tests
        try {
            Timestamp startTimestamp = rs.getTimestamp("startTime");

            if (startTimestamp != null) {
                // Calendar calendar = Calendar.getInstance();
                // calendar.setTimeInMillis(startTime.getTime());

                GregorianCalendar calendar = new GregorianCalendar();
                calendar.setTimeInMillis(startTimestamp.getTime());

                XMLGregorianCalendar startTime = DatatypeFactory.newInstance()
                        .newXMLGregorianCalendar(new GregorianCalendar());

                ebSubscriptionType.setStartTime(startTime);
            }
        } catch (StringIndexOutOfBoundsException e) {
            String id = rs.getString("id");
            log.error(ServerResourceBundle.getInstance().getString("message.SubscriptionDAOId",
                    new Object[] { id }), e);
        }

        NotifyActionDAO notifyActionDAO = new NotifyActionDAO(context);
        notifyActionDAO.setParent(ebSubscriptionType);
        @SuppressWarnings("rawtypes")
        List notifyActions = notifyActionDAO.getByParent();
        if (notifyActions != null) {
            bu.getActionTypeListFromElements(ebSubscriptionType.getAction()).addAll(notifyActions);
        }

    } catch (SQLException e) {
        log.error(ServerResourceBundle.getInstance().getString("message.CaughtException1"), e);
        throw new RegistryException(e);
    } catch (DatatypeConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:jenkins.plugins.coverity.DefectFilters.java

public XMLGregorianCalendar getXMLCutOffDate() {
    GregorianCalendar calender = new GregorianCalendar();
    calender.setTime(cutOffDate);//from  w ww.j a  va  2 s  . co m
    try {
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(calender);
    } catch (Exception e) {

    }
    return null;
}

From source file:mx.bigdata.sat.cfdi.TFDv11.java

private TimbreFiscalDigital createStamp(UUID uuid, Date date, String PAC, String leyenda)
        throws DatatypeConfigurationException {
    ObjectFactory of = new ObjectFactory();
    TimbreFiscalDigital tfds = of.createTimbreFiscalDigital();
    tfds.setVersion("1.1");
    tfds.setUUID(uuid.toString());/*from w  w  w . j  av a2s  .  c  om*/
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    tfds.setFechaTimbrado(DatatypeFactory.newInstance().newXMLGregorianCalendar(c.get(Calendar.YEAR),
            c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY),
            c.get(Calendar.MINUTE), c.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED,
            DatatypeConstants.FIELD_UNDEFINED));
    tfds.setRfcProvCertif(PAC);
    tfds.setLeyenda(leyenda);
    tfds.setSelloCFD(document.getSello());
    BigInteger bi = cert.getSerialNumber();
    tfds.setNoCertificadoSAT(new String(bi.toByteArray()));
    return tfds;
}

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

@Test
public void testToMessage() throws Exception {

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

    TextMessage actualMessage = (TextMessage) jaxbMessageConverter.toMessage(employee, session);

    String actual = actualMessage.getText();
    String expected = "<?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>";

    Assert.assertEquals(expected, actual);
}

From source file:eu.europa.ec.markt.dss.signature.xades.XAdESProfileBES.java

/**
 * @return the dataFactory/*from   w w w. jav a  2  s  .c o  m*/
 */
public DatatypeFactory getDataFactory() {
    if (dataFactory == null) {
        try {
            dataFactory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException ex) {
            throw new RuntimeException(ex);
        }
    }
    return dataFactory;
}

From source file:fi.vrk.xroad.catalog.lister.JaxbConverter.java

protected XMLGregorianCalendar toXmlGregorianCalendar(LocalDateTime localDateTime) {
    if (localDateTime == null) {
        return null;
    } else {//from w  w  w  . j a  v a  2 s .c  o  m
        GregorianCalendar cal = GregorianCalendar.from(localDateTime.atZone(ZoneId.systemDefault()));
        XMLGregorianCalendar xc = null;
        try {
            xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        } catch (DatatypeConfigurationException e) {
            throw new CatalogListerRuntimeException("Cannot instantiate DatatypeFactory", e);
        }
        return xc;
    }
}

From source file:com.opencsv.bean.BeanFieldDate.java

/**
 * Converts the input to/from a calendar object.
 * <p>This method should work for any type that implements
 * {@link java.util.Calendar} or is derived from
 * {@link javax.xml.datatype.XMLGregorianCalendar}. The following types are
 * explicitly supported://  w  ww .j  a va  2  s  . co  m
 * <ul><li>Calendar (always a GregorianCalendar)</li>
 * <li>GregorianCalendar</li>
 * <li>XMLGregorianCalendar</li></ul>
 * It is also known to work with
 * org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl.</p>
 *
 * @param <U> The type to be converted to
 * @param value The string to be converted into a date/time type or vice
 *   versa
 * @param fieldType The class of the destination field
 * @return The object resulting from the conversion
 * @throws CsvDataTypeMismatchException If the conversion fails
 */
private <U> U convertCalendar(Object value, Class<U> fieldType) throws CsvDataTypeMismatchException {
    U o;

    if (value instanceof String) {
        // Parse input
        Date d;
        try {
            d = getFormat().parse((String) value);
        } catch (ParseException e) {
            CsvDataTypeMismatchException csve = new CsvDataTypeMismatchException(value, fieldType);
            csve.initCause(e);
            throw csve;
        }

        // Make a GregorianCalendar out of it, because this works for all
        // supported types, at least as an intermediate step.
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(d);

        // XMLGregorianCalendar requires special processing.
        if (fieldType == XMLGregorianCalendar.class) {
            try {
                o = fieldType.cast(DatatypeFactory.newInstance().newXMLGregorianCalendar(gc));
            } catch (DatatypeConfigurationException e) {
                // I've never known how to handle this exception elegantly,
                // especially since I can't conceive of the circumstances
                // under which it is thrown.
                CsvDataTypeMismatchException ex = new CsvDataTypeMismatchException(
                        "It was not possible to initialize an XMLGregorianCalendar.");
                ex.initCause(e);
                throw ex;
            }
        } else {
            o = fieldType.cast(gc);
        }
    } else {
        Calendar c;
        if (value instanceof XMLGregorianCalendar) {
            c = ((XMLGregorianCalendar) value).toGregorianCalendar();
        } else if (value instanceof Calendar) {
            c = (Calendar) value;
        } else {
            throw new CsvDataTypeMismatchException(value, fieldType, NOT_DATE);
        }
        o = fieldType.cast(getFormat().format(c.getTime()));
    }

    return o;
}

From source file:com.fusesource.example.camel.process.camel.ProcessorRouteBuilderTest.java

@Test
@DirtiesContext//from  w w  w  .j  a  va 2s  .c om
public void testNonTerminalJdbcFailure() throws Exception {

    configureJdbcFailure(1);

    context.start();

    DatatypeFactory dtf = DatatypeFactory.newInstance();

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

    output.setExpectedMessageCount(10);
    output.setResultWaitTime(15000l);

    dlq.setExpectedMessageCount(0);

    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);
        expectedIds.add(String.valueOf(i));

        trigger.sendBody(recordType);
    }

    output.assertIsSatisfied();
    dlq.assertIsSatisfied(10000);

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

    assertTrue(expectedIds.isEmpty());
}

From source file:mx.bigdata.sat.cfdi.TFDv11c33.java

private TimbreFiscalDigital createStamp(UUID uuid, Date date, String PAC, String leyenda)
        throws DatatypeConfigurationException {
    Calendar c = Calendar.getInstance();
    c.setTime(date);//from   w  ww . j  av a 2s .co m
    ObjectFactory of = new ObjectFactory();
    TimbreFiscalDigital tfds = of.createTimbreFiscalDigital();
    tfds.setVersion("1.1");
    tfds.setUUID(uuid.toString());
    tfds.setFechaTimbrado(DatatypeFactory.newInstance().newXMLGregorianCalendar(c.get(Calendar.YEAR),
            c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR), c.get(Calendar.MINUTE),
            c.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED));
    tfds.setRfcProvCertif(PAC);
    tfds.setLeyenda(leyenda);
    tfds.setSelloCFD(document.getSello());
    BigInteger bi = cert.getSerialNumber();
    tfds.setNoCertificadoSAT(new String(bi.toByteArray()));
    return tfds;
}