Search in sources :

Example 1 with DataCellToJavaConverter

use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.

the class DataCellToJavaConversionTest method testXML.

/**
 * Test XMLValue -> Document conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testXML() throws Exception {
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Document>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(XMLCell.TYPE, Document.class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<DataCell, Document> converter = (DataCellToJavaConverter<DataCell, Document>) factory.get().create();
    assertNotNull(converter);
    final NodeList children = converter.convert(XMLCellFactory.create("<tag/>")).getChildNodes();
    assertEquals(children.getLength(), 1);
    assertEquals(children.item(0).getNodeName(), "tag");
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) NodeList(org.w3c.dom.NodeList) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 2 with DataCellToJavaConverter

use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.

the class DataCellToJavaConversionTest method testCollectionTypes.

/**
 * Test ListCell(IntCell) -> Integer[] conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testCollectionTypes() throws Exception {
    ArrayList<DataCell> coll = new ArrayList<>();
    for (int i = 0; i < 5; ++i) {
        coll.add(new IntCell(i * i));
    }
    // collection cells can always contain missing cells.
    coll.add(new MissingCell("42"));
    final ListCell listCell = CollectionCellFactory.createListCell(coll);
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[].class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<DataCell, Integer[]> converter = (DataCellToJavaConverter<DataCell, Integer[]>) factory.get().create();
    assertNotNull(converter);
    final Integer[] array = converter.convert(listCell);
    for (int i = 0; i < 5; ++i) {
        assertEquals(new Integer(i * i), array[i]);
    }
    assertNull(array[5]);
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) MissingCell(org.knime.core.data.MissingCell) ListCell(org.knime.core.data.collection.ListCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 3 with DataCellToJavaConverter

use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.

the class DataCellToJavaConversionTest method testBinaryObject.

/**
 * Test BinaryObjectDataCell -> InputStream conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testBinaryObject() throws Exception {
    /* retrieve converter from DataCellToJavaConverterRegistry */
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, InputStream>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(BinaryObjectDataCell.TYPE, InputStream.class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<DataCell, InputStream> converter = (DataCellToJavaConverter<DataCell, InputStream>) factory.get().create();
    assertNotNull(converter);
    final BinaryObjectCellFactory cellFactory = new BinaryObjectCellFactory();
    assertTrue(converter.convert(cellFactory.create(new byte[] { 4, 2 })) instanceof InputStream);
    /* convert a BinaryObjectDataCell */
    InputStream stream = converter.convert(cellFactory.create(new byte[] { 4, 2 }));
    assertEquals(stream.read(), 4);
    assertEquals(stream.read(), 2);
    stream.close();
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) InputStream(java.io.InputStream) BinaryObjectCellFactory(org.knime.core.data.blob.BinaryObjectCellFactory) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) Test(org.junit.Test)

Example 4 with DataCellToJavaConverter

use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.

the class DataCellToJavaConversionTest method testNestedCollectionTypes.

/**
 * Test ListCell(ListCell(IntCell)) -> Integer[][] conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testNestedCollectionTypes() throws Exception {
    ArrayList<DataCell> coll = new ArrayList<>();
    for (int i = 0; i < 5; ++i) {
        coll.add(new IntCell(i * i));
    }
    // collection cells can always contain missing cells.
    coll.add(new MissingCell("42"));
    final ListCell listCell = CollectionCellFactory.createListCell(Arrays.asList(CollectionCellFactory.createListCell(coll)));
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[][]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[][].class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<DataCell, Integer[][]> converter = (DataCellToJavaConverter<DataCell, Integer[][]>) factory.get().create();
    assertNotNull(converter);
    final Integer[][] array = converter.convert(listCell);
    for (int i = 0; i < 5; ++i) {
        assertEquals(new Integer(i * i), array[0][i]);
    }
    assertNull(array[0][5]);
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) MissingCell(org.knime.core.data.MissingCell) ListCell(org.knime.core.data.collection.ListCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 5 with DataCellToJavaConverter

use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.

the class ExtensionPointTest method testDataCellToJava.

/**
 * Test whether {@link StringCellToIntTestConverterFactory} was correctly registered at the
 * "org.knime.core.DataCellToJavaConverter" extension point and can be used for conversion.
 *
 * @throws Exception
 */
@Test
public void testDataCellToJava() throws Exception {
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, Integer.class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<StringValue, Integer> converter = (DataCellToJavaConverter<StringValue, Integer>) factory.get().create();
    assertNotNull(converter);
    final Integer convert = converter.convert(new StringCell("Answer to Life, the Universe, and Everything"));
    assertEquals(convert, new Integer(42));
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) StringCell(org.knime.core.data.def.StringCell) StringValue(org.knime.core.data.StringValue) Test(org.junit.Test)

Aggregations

DataCellToJavaConverter (org.knime.core.data.convert.java.DataCellToJavaConverter)6 Test (org.junit.Test)5 DataCell (org.knime.core.data.DataCell)5 BinaryObjectDataCell (org.knime.core.data.blob.BinaryObjectDataCell)4 ArrayList (java.util.ArrayList)2 MissingCell (org.knime.core.data.MissingCell)2 ListCell (org.knime.core.data.collection.ListCell)2 IntCell (org.knime.core.data.def.IntCell)2 InputStream (java.io.InputStream)1 StringValue (org.knime.core.data.StringValue)1 BinaryObjectCellFactory (org.knime.core.data.blob.BinaryObjectCellFactory)1 StringCell (org.knime.core.data.def.StringCell)1 Pair (org.knime.core.util.Pair)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1