<commit_before>/*
 * Copyright (C) 2014 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.b3p.viewer.features;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import nl.b3p.viewer.config.app.ConfiguredAttribute;
import nl.b3p.viewer.config.services.AttributeDescriptor;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;

/**
 * Download features as csv-file
 * @author <NAME>
 */
public class CSVDownloader extends FeatureDownloader{

    
    private char separator = ';';
    private PrintWriter pw;
    private File f;
    public CSVDownloader(List<ConfiguredAttribute> attributes, SimpleFeatureSource fs, Map<String, AttributeDescriptor> featureTypeAttributes, Map<String, String> attributeAliases) {
        super(attributes, fs, featureTypeAttributes,attributeAliases);
    }

    @Override
    public void init() throws IOException {  
        f = File.createTempFile("csvFeatures", ".csv");
        pw = new PrintWriter(f);
        List<String> header = new ArrayList<String>();
        for (ConfiguredAttribute configuredAttribute : attributes) {
            if(configuredAttribute.isVisible()){
                String alias = attributeAliases.get(configuredAttribute.getAttributeName());
                header.add(alias);
            }
        }
        writeRow(header);
    }

    @Override
    public void processFeature(SimpleFeature oldFeature) {
        List<String> row = new ArrayList<String>();
        for (ConfiguredAttribute configuredAttribute : attributes) {
            if(configuredAttribute.isVisible()){
                row.add(oldFeature.getAttribute(configuredAttribute.getAttributeName()).toString());
            }
        }
        writeRow(row);
    }

    @Override
    public File write() throws IOException {
        pw.close();
        return f;
    }
    
    private void writeRow(List<String> row){
        String completeString = "";
        for (String col : row) {<commit_msg>Prevented nullpointer when retrieving an null attribute and trying to get a .toString() of it. Fixes #294<commit_after>/*
 * Copyright (C) 2014 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.b3p.viewer.features;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import nl.b3p.viewer.config.app.ConfiguredAttribute;
import nl.b3p.viewer.config.services.AttributeDescriptor;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;

/**
 * Download features as csv-file
 * @author <NAME>
 */
public class CSVDownloader extends FeatureDownloader{


    private char separator = ';';
    private PrintWriter pw;
    private File f;
    public CSVDownloader(List<ConfiguredAttribute> attributes, SimpleFeatureSource fs, Map<String, AttributeDescriptor> featureTypeAttributes, Map<String, String> attributeAliases) {
        super(attributes, fs, featureTypeAttributes,attributeAliases);
    }

    @Override
    public void init() throws IOException {
        f = File.createTempFile("csvFeatures", ".csv");
        pw = new PrintWriter(f);
        List<String> header = new ArrayList<String>();
        for (ConfiguredAttribute configuredAttribute : attributes) {
            if(configuredAttribute.isVisible()){
                String alias = attributeAliases.get(configuredAttribute.getAttributeName());
                header.add(alias);
            }
        }
        writeRow(header);
    }

    @Override
    public void processFeature(SimpleFeature oldFeature) {
        List<String> row = new ArrayList<String>();
        for (ConfiguredAttribute configuredAttribute : attributes) {
            if(configuredAttribute.isVisible()){
                Object attribute = oldFeature.getAttribute(configuredAttribute.getAttributeName());
                String value = null;
                if(attribute != null){
                    value = attribute.toString();
                }
                row.add(value);
            }
        }
        writeRow(row);
    }

    @Override
    public File write() throws IOException {
        pw.close();
        return f;
    }

    private void writeRow(List<String> row){
        String completeString = "";
        for (String col : row) {
{'fraction_non_alphanumeric': 0.0915182507494269, 'fraction_numerical': 0.003703050608358314, 'mean_word_length': 3.0514285714285716, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5830713', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1458, 'n_words': 470}