<commit_before>package com.example.myapp.main.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import javax.ejb.Stateless;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

@Stateless
public class Exporter {

	/**
	 * Simple and buggy export to a standard CSV file ("." as decimal separator,
	 * "," as column delimiter, '"' as string delimiter)
	 * 
	 * @param headers
	 * @param rows
	 * @return
	 * @throws IOException
	 */
	public File exportCSV(String[] headers, List<Object[]> rows) throws IOException {
		File f = File.createTempFile("export", ".csv");
		FileWriter fw = new FileWriter(f);

		String comma = "";
		for (String s : headers) {
			fw.write(comma + "\"" + s + "\"");
			comma = ", ";
		}

		NumberFormat formatter = NumberFormat.getInstance(Locale.US);

		for (Object[] item : rows) {
			comma = "";
			for (Object value : item) {

				if (value instanceof Number) {

					fw.write(comma + formatter.format(value));
				} else {
					fw.write(comma + "\"" + value.toString() + "\"");
				}
				comma = ", ";
			}
			fw.flush();
		}

		fw.close();
		return f;
	}

	/**
	 * Create a (currently very simple) XLSX workbook that contains given data.
	 * 
	 * @param headers
	 * @param rows
	 * @return
	 * @throws IOException
	 * @throws InvalidFormatException
	 */
	public File exportXLSX(String[] headers, List<Object[]> rows) throws IOException {

		// FIXME handle columns format

		File f = File.createTempFile("export", ".xlsx");

		XSSFWorkbook wb = new XSSFWorkbook();
		Sheet sheet = wb.createSheet();
		Row row = sheet.createRow(0);

		CellStyle headerStyle = createCellStyleWhiteBold(wb);

		int colnum = 0;
		for (String s : headers) {
			Cell c = row.createCell(colnum++);
			c.setCellStyle(headerStyle);
			c.setCellValue(s);
		}

		int rownum = 1;
		for (Object[] item : rows) {
			row = sheet.createRow(rownum);
			colnum = 0;
			for (Object value : item) {
				Cell c = row.createCell(colnum);
				// FIXME many more types
				if (value instanceof Number) {
					c.setCellValue(((Number) value).doubleValue());
				} else {
					c.setCellValue(value.toString());
				}
				++colnum;
			}
			++rownum;
		}

		FileOutputStream fileOut = new FileOutputStream(f.getAbsolutePath());
		wb.write(fileOut);
		fileOut.close();

		return f;

	}

	private CellStyle createCellStyleWhiteBold(Workbook wb) {
		CellStyle cs = wb.createCellStyle();
		cs.setFillForegroundColor(HSSFColor.WHITE.index);
		cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		Font fontBlueBold = wb.createFont();
		fontBlueBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		fontBlueBold.setColor(HSSFColor.DARK_BLUE.index);
		cs.setFont(fontBlueBold);
		return cs;
	}
}<commit_msg>improvements<commit_after>package com.example.myapp.main.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.ejb.Stateless;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

@Stateless
public class Exporter {

	/**
	 * Simple export to a standard CSV file ("." as decimal separator, "," as
	 * column delimiter, '"' as string delimiter)
	 * 
	 * @param headers
	 * @param rows
	 * @return
	 * @throws IOException
	 */
	public File exportCSV(String[] headers, List<Object[]> rows) throws IOException {
		File f = File.createTempFile("export", ".csv");
		FileWriter fw = new FileWriter(f);

		final String FIELD_DELIMITER = ", ";
		final String ROW_DELIMITER = "\r\n";
		final String STRING_DELIMITER = "\"";

		String comma = "";
		for (String s : headers) {
			fw.write(comma + "\"" + s + "\"");
			comma = ", ";
		}

		for (Object[] item : rows) {
			comma = "";
			for (Object value : item) {

				fw.write(comma + formatCSVField(value, STRING_DELIMITER));
				comma = FIELD_DELIMITER;
			}
			fw.write(ROW_DELIMITER);
			fw.flush();
		}

		fw.close();
		return f;
	}

	private NumberFormat formatter = NumberFormat.getInstance(Locale.US);
	private DateFormat dateFormatter = DateFormat.getInstance();

	private String formatCSVField(Object value, String stringDelimiter) {

		if (value == null)
			return "";

		if (value instanceof Number) {
			return formatter.format(value);
		} else if (value instanceof Date) {
			return dateFormatter.format(value);
		} else {
			String sanitizedString = value.toString().replaceAll("\r", "").replaceAll("\n", "").replaceAll("\"", "'");
			return stringDelimiter + sanitizedString + stringDelimiter;
		}
	}

	/**
	 * Create a (currently very simple) XLSX workbook that contains given data.
	 * 
	 * @param headers
	 * @param rows
	 * @return
	 * @throws IOException
	 * @throws InvalidFormatException
	 */
	public File exportXLSX(String[] headers, List<Object[]> rows) throws IOException {

		// FIXME handle columns format

		File f = File.createTempFile("export", ".xlsx");

		XSSFWorkbook wb = new XSSFWorkbook();
		Sheet sheet = wb.createSheet();
		Row row = sheet.createRow(0);

		CellStyle headerStyle = createCellStyleWhiteBold(wb);

		int colnum = 0;
		for (String s : headers) {
			Cell c = row.createCell(colnum++);
			c.setCellStyle(headerStyle);
			c.setCellValue(s);
		}

		int rownum = 1;
		for (Object[] item : rows) {
			row = sheet.createRow(rownum);
			colnum = 0;
			for (Object value : item) {
				Cell cell = row.createCell(colnum);
				formatXLSXField(value, cell);
				++colnum;
			}
			++rownum;
		}

		FileOutputStream fileOut = new FileOutputStream(f.getAbsolutePath());
		wb.write(fileOut);
		fileOut.close();

		return f;

	}

	private CellStyle createCellStyleWhiteBold(Workbook wb) {
		CellStyle cs = wb.createCellStyle();
		cs.setFillForegroundColor(HSSFColor.WHITE.index);
		cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		Font fontBlueBold = wb.createFont();
		fontBlueBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		fontBlueBold.setColor(HSSFColor.DARK_BLUE.index);
		cs.setFont(fontBlueBold);
		return cs;
	}

	private void formatXLSXField(Object value, Cell c) {
		if (value == null)
			return;
		if (value instanceof Number) {
			c.setCellValue(((Number) value).doubleValue());
		} else if (value instanceof Date) {
			c.setCellValue((Date) value);
		} else if (value instanceof Calendar) {
			c.setCellValue((Calendar) value);
		} else {
			c.setCellValue(value.toString());
		}
	}
}
{'fraction_non_alphanumeric': 0.13554095723287304, 'fraction_numerical': 0.001340662287169862, 'mean_word_length': 3.9046679815910585, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5827893', 'n_tokens_mistral': 2890, 'n_tokens_neox': 2557, 'n_words': 546}