<commit_before>package com.equalize.xpi.af.modules.excel;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

public class XML2ExcelTransformer extends ExcelTransformer {

	// Module parameters
	private String sheetName;
	private String excelFormat;
	private String addHeaderLine;
	private String fieldNames;

	private ArrayList<ArrayList<String>> contents;
	private boolean addHeaderFromXML;
	private String[] columnNames;

	public XML2ExcelTransformer(ModuleContext mc, MessageKey key, AuditAccess audit) {
		super(mc, key, audit);
	}

	@Override
	public void retrieveModuleParameters() throws Exception {
		// Output format
		this.excelFormat = getParaWithDefault("excelFormat", "xlsx");
		if(!this.excelFormat.equalsIgnoreCase("xls") && !this.excelFormat.equalsIgnoreCase("xlsx")) {
			throw new Exception("Value " + this.excelFormat + " not valid for parameter excelFormat");
		}
		// Sheet name
		this.sheetName = getParaWithDefault("sheetName", "Sheet1");
		// Header line
		this.addHeaderLine = getParaWithDefault("addHeaderLine", "none");
		if(this.addHeaderLine.equalsIgnoreCase("none")) {
			this.addHeaderFromXML = false;
		} else if(this.addHeaderLine.equalsIgnoreCase("fromXML")) {
			this.addHeaderFromXML = true;			
		} else if(this.addHeaderLine.equalsIgnoreCase("fromConfiguration")) {
			this.addHeaderFromXML = false;
			this.fieldNames = this.moduleParam.getContextData("fieldNames");
			if(this.fieldNames == null || this.fieldNames.replaceAll("\\s+", "").equals("")) {
				throw new Exception("Parameter fieldNames is required when addHeaderLine = fromConfiguration");
			} else {
				this.columnNames = this.fieldNames.split(",");
			}
		} else {
			throw new Exception("Value " + this.addHeaderLine + " not valid for parameter addHeaderLine");
		}
	}

	@Override
	public void parseInput(InputStream inStream) throws Exception {
		// Parse input XML with SAX custom handler
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		this.contents = new ArrayList<ArrayList<String>>();
		SAXSimpleParser handler = new SAXSimpleParser(this.contents, this.addHeaderFromXML);
		addLog(AuditLogStatus.SUCCESS, "Parsing XML contents");
		parser.parse(inStream, handler);		

		// Add header line from configuration
		if(this.addHeaderLine.equalsIgnoreCase("fromConfiguration")) {
			ArrayList<String> header = new ArrayList<String>();
			for(int i = 0; i < this.columnNames.length; i++) {
				header.add(this.columnNames[i]);
			}
			this.contents.add(0, header);
		}		
	}

	@Override
	public ByteArrayOutputStream generateOutput() throws Exception {
		// Create workbook
		Workbook wb = null;
		if(this.excelFormat.equalsIgnoreCase("xls")) {
			wb = new HSSFWorkbook();
		} else if(this.excelFormat.equalsIgnoreCase("xlsx")) {
			wb = new XSSFWorkbook();
		}

		addLog(AuditLogStatus.SUCCESS, "Constructing Excel output");
		addLog(AuditLogStatus.SUCCESS, "Creating sheet " + this.sheetName);
		// Create sheet
		Sheet sheet = wb.createSheet(this.sheetName);
		
		// Loop through the 2D array of saved contents 
		for(int i = 0; i < this.contents.size(); i++) {
			Row row = sheet.createRow(i);
			ArrayList<String> contentRow = this.contents.get(i);
			for(int j = 0; j < contentRow.size(); j++) {
				Cell cell = row.createCell(j);
				cell.setCellType(Cell.CELL_TYPE_STRING);
				cell.setCellValue(contentRow.get(j));
			}				
		}
		// Write output stream
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		wb.write(baos);
		baos.close();

		addLog(AuditLogStatus.SUCCESS, "Conversion complete");
		return baos;
	}
}<commit_msg>Update XML2ExcelTransformer.java<commit_after>package com.equalize.xpi.af.modules.excel;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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;

import com.equalize.xpi.af.modules.util.AbstractModuleConverter;
import com.equalize.xpi.af.modules.util.AuditLogHelper;
import com.equalize.xpi.af.modules.util.DynamicConfigurationHelper;
import com.equalize.xpi.af.modules.util.ParameterHelper;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

public class XML2ExcelTransformer extends AbstractModuleConverter {

	// Module parameters
	private String sheetName;
	private String excelFormat;
	private String addHeaderLine;
	private String fieldNames;

	private ArrayList<ArrayList<String>> contents;
	private boolean addHeaderFromXML;
	private String[] columnNames;

	public XML2ExcelTransformer(Message msg, ParameterHelper param, AuditLogHelper audit, DynamicConfigurationHelper dyncfg) {
		super(msg, param, audit, dyncfg);
	}

	@Override
	public void retrieveModuleParameters() throws ModuleException {
		// Output format
		this.excelFormat = this.param.getParameter("excelFormat", "xlsx", true);
		this.param.checkParamValidValues("excelFormat", "xlsx,xls");

		// Sheet name
		this.sheetName = this.param.getParameter("sheetName", "Sheet1", true);
		// Header line
		this.addHeaderLine = this.param.getParameter("addHeaderLine", "none", false);
		this.param.checkParamValidValues("addHeaderLine", "none,fromXML,fromConfiguration");
		if(this.addHeaderLine.equalsIgnoreCase("none")) {
			this.addHeaderFromXML = false;
		} else if(this.addHeaderLine.equalsIgnoreCase("fromXML")) {
			this.addHeaderFromXML = true;			
		} else if(this.addHeaderLine.equalsIgnoreCase("fromConfiguration")) {
			this.addHeaderFromXML = false;
			this.fieldNames = this.param.getParameter("fieldNames");
			if(this.fieldNames == null || this.fieldNames.replaceAll("\\s+", "").isEmpty()) {
				throw new ModuleException("Parameter 'fieldNames' required when 'addHeaderLine' = fromConfiguration");
			} else {
				this.columnNames = this.fieldNames.split(",");
			}
		}
	}

	@Override
	public void parseInput() throws ModuleException {
		try {
			// Parse input XML with SAX custom handler
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			this.contents = new ArrayList<ArrayList<String>>();
			SAXSimpleParser handler = new SAXSimpleParser(this.contents, this.addHeaderFromXML);
			this.audit.addLog(AuditLogStatus.SUCCESS, "Parsing input XML");
			parser.parse(this.payload.getInputStream(), handler);		
		} catch (Exception e) {
			throw new ModuleException(e.getMessage(), e);
		}
		// Add header line from configuration
		if(this.addHeaderLine.equalsIgnoreCase("fromConfiguration")) {
			ArrayList<String> header = new ArrayList<String>();
			for(int i = 0; i < this.columnNames.length; i++) {
				header.add(this.columnNames[i]);
			}
			this.contents.add(0, header);
		}		
	}

	@Override
	public byte[] generateOutput() throws ModuleException {
		// Create workbook
		Workbook wb = null;
		if(this.excelFormat.equalsIgnoreCase("xls")) {
			wb = new HSSFWorkbook();
		} else if(this.excelFormat.equalsIgnoreCase("xlsx")) {
			wb = new XSSFWorkbook();
		}

		this.audit.addLog(AuditLogStatus.SUCCESS, "Constructing output Excel");
		this.audit.addLog(AuditLogStatus.SUCCESS, "Creating sheet " + this.sheetName);
		// Create sheet
		Sheet sheet = wb.createSheet(this.sheetName);

		// Loop through the 2D array of saved contents 
		for(int i = 0; i < this.contents.size(); i++) {
			Row row = sheet.createRow(i);
			ArrayList<String> contentRow = this.contents.get(i);
			for(int j = 0; j < contentRow.size(); j++) {
				Cell cell = row.createCell(j);
				cell.setCellType(Cell.CELL_TYPE_STRING);
				cell.setCellValue(contentRow.get(j));
			}				
		}
		// Write output stream
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			wb.write(baos);
			baos.close();
		} catch (IOException e) {
			throw new ModuleException(e.getMessage(), e);
		}

		this.audit.addLog(AuditLogStatus.SUCCESS, "Conversion complete");
		return baos.toByteArray();
	}
}
{'fraction_non_alphanumeric': 0.12540808285489136, 'fraction_numerical': 0.0019137678712146796, 'mean_word_length': 4.8408941485864565, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3494077', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2808, 'n_words': 554}