FeatureCsvWriter implements CsvWriter { @Override public void writeCsv(Object... input) throws IOException { validate(input); extract(input); final StringBuilder csv = new StringBuilder(); final List<AttributeDescriptor> attributeDescriptors = featureType.getAttributeDescriptors(); String[] attributes = new String[attributeDescriptors.size()]; Class[] types = new Class[attributeDescriptors.size()]; for (int i = 0; i < attributeDescriptors.size(); i++) { AttributeDescriptor attributeDescriptor = attributeDescriptors.get(i); attributes[i] = attributeDescriptor.getName().toString(); types[i] = attributeDescriptor.getType().getBinding(); } final String header = targetFormat.formatHeader(attributes, types); csv.append(header); SimpleFeature[] simpleFeatureArray = toSimpleFeatureArray(featureCollection); for (int recordNo = 0; recordNo < simpleFeatureArray.length; recordNo++) { SimpleFeature feature = simpleFeatureArray[recordNo]; String[] values = new String[feature.getAttributeCount()]; for (int i = 0; i < values.length; i++) { values[i] = feature.getAttribute(i).toString(); } csv.append("\n"); csv.append(targetFormat.formatRecord(recordNo + "", values)); } csv.append("\n"); writer.writeCsv(csv.toString()); } FeatureCsvWriter(WriteStrategy writer, OutputFormatStrategy targetFormat); @Override void writeCsv(Object... input); @Override boolean isValidInput(Object... input);  }
@Test public void testWriteCsv() throws Exception { final StringBuilder s = new StringBuilder(); final CsvWriter csvWriter = new FeatureCsvWriter(new WriteStrategy() { @Override public void writeCsv(String fullOutput) throws IOException { s.append(fullOutput); } }, new CsvWriterBuilder.BeamOutputFormat()); SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder(); featureTypeBuilder.setName(new NameImpl("")); featureTypeBuilder.add("attr_1", String.class); featureTypeBuilder.add("attr_2", Float.class); final SimpleFeatureType featureType = featureTypeBuilder.buildFeatureType(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); featureBuilder.set("attr_1", "val_1"); featureBuilder.set("attr_2", 10.0f); final SimpleFeature firstFeature = featureBuilder.buildFeature("0"); featureBuilder.reset(); featureBuilder.set("attr_1", "val_2"); featureBuilder.set("attr_2", 20.0f); final SimpleFeature secondFeature = featureBuilder.buildFeature("1"); final DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("id", featureType); featureCollection.add(firstFeature); featureCollection.add(secondFeature); csvWriter.writeCsv(featureType, featureCollection); final String csv = s.toString(); final StringBuilder expected = new StringBuilder(); expected.append("featureId\tattr_1:string\tattr_2:float"); expected.append("\n"); expected.append("0\tval_1\t10.0"); expected.append("\n"); expected.append("1\tval_2\t20.0"); expected.append("\n"); assertEquals(expected.toString(), csv); }