<commit_before>package openaf.plugins;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.mozilla.javascript.ConsString;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;

import openaf.AFCmdBase;

/**
 * XLS plugin
 * 
 * @author <NAME>
 * 
 */
public class XLS extends ScriptableObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9058684071530344845L;
	protected static String dataformat = "yyyy-d-m h:mm:ss";
	protected Workbook wbook;
	protected FormulaEvaluator evaluator;
	
	protected enum TableType {
		HORIZONTAL, VERTICAL, CUSTOM;
	}
	
	@Override
	public String getClassName() {
		return "XLS";
	}

	/**
	 * <odoc>
	 * <key>XLS.toNumber(aName) : Number</key>
	 * Converts a column letter combination into the corresponding column number. Example:\
	 * \
	 * var xls = new XLS();\
	 * xls.toNumber("AF"); // 32\
	 * \
	 * </odoc>
	 */
	@JSFunction
	public static int toNumber(String name) {
		int number = 0;
		for(int i = 0; i < name.length(); i++) {
			number = number * 26 + (name.charAt(i) - ('A' - 1));
		}
		return number;
	}
	
	/**
	 * <odoc>
	 * <key>XLS.toName(aNumber) : String</key>
	 * Converts a column number into the corresponding column letter combination. Example:\
	 * \
	 * var xls = new XLS();\
	 * xls.toName(32); // AF\
	 * \
	 * </odoc>
	 */
	@JSFunction
    public static String toName(int number) {
        StringBuilder sb = new StringBuilder();
        while (number-- > 0) {
            sb.append((char)('A' + (number % 26)));
            number /= 26;
        }
        return sb.reverse().toString();
    }
	
	/**
	 * 
	 * @param sy
	 * @return
	 */
	protected static int translateObject(Object sy) {
		int y;
		
		if (sy instanceof Integer) {
			return ((Integer) sy).intValue();
		} 
		
		if (sy instanceof Double) {
			return ((Double) sy).intValue();
		}
		
		y = toNumber((String) sy);
		
		return y;
	}
	
	/**
	 * 
	 * @param type
	 * @return
	 */
	protected static String translateType(Cell cell) {
		@SuppressWarnings("deprecation")
		CellType c = cell.getCellTypeEnum();
		if (c == CellType.BLANK) return "BLANK";
		if (c == CellType.BOOLEAN) return "BOOLEAN";
		if (c == CellType.ERROR) return "ERROR";
		if (c == CellType.FORMULA) return "FORMULA";
		if (c == CellType.NUMERIC) if (HSSFDateUtil.isCellDateFormatted(cell)) return "DATE"; else return "NUMERIC";
		if (c == CellType.STRING) return "STRING";
		
		return null;
	}
	
	/**
	 * <odoc>
	 * <key>XLS.XLS(aObject) : XLS</key>
	 * Creates a new instance. You can optionally provide a filename to load or use as a template and/or an
	 * array of bytes (aObject). Example:\
	 * \
	 * var xls = new XLS("c:/test.xlsx");\
	 * \
	 * </odoc>
	 * @throws InvalidFormatException 
	 * @throws EncryptedDocumentException 
	 */
	@JSConstructor
	public void newXLS(Object arg) throws IOException, EncryptedDocumentException, InvalidFormatException {
		wbook = null;
		
		if (arg instanceof String) {
			// It's a filename	
			try {
				POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream((String) arg));
				wbook = WorkbookFactory.create(poifs);
			} catch(Exception e) {
				wbook = new XSSFWorkbook((String) arg);
			}
		}
		
		if (arg instanceof byte[]) {<commit_msg>issue-149 (read-only mode for XLS)<commit_after>package openaf.plugins;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.mozilla.javascript.ConsString;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;

import openaf.AFCmdBase;

/**
 * XLS plugin
 * 
 * @author <NAME>
 * 
 */
public class XLS extends ScriptableObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9058684071530344845L;
	protected static String dataformat = "yyyy-d-m h:mm:ss";
	protected Workbook wbook;
	protected FormulaEvaluator evaluator;
	
	protected enum TableType {
		HORIZONTAL, VERTICAL, CUSTOM;
	}
	
	@Override
	public String getClassName() {
		return "XLS";
	}

	/**
	 * <odoc>
	 * <key>XLS.toNumber(aName) : Number</key>
	 * Converts a column letter combination into the corresponding column number. Example:\
	 * \
	 * var xls = new XLS();\
	 * xls.toNumber("AF"); // 32\
	 * \
	 * </odoc>
	 */
	@JSFunction
	public static int toNumber(String name) {
		int number = 0;
		for(int i = 0; i < name.length(); i++) {
			number = number * 26 + (name.charAt(i) - ('A' - 1));
		}
		return number;
	}
	
	/**
	 * <odoc>
	 * <key>XLS.toName(aNumber) : String</key>
	 * Converts a column number into the corresponding column letter combination. Example:\
	 * \
	 * var xls = new XLS();\
	 * xls.toName(32); // AF\
	 * \
	 * </odoc>
	 */
	@JSFunction
    public static String toName(int number) {
        StringBuilder sb = new StringBuilder();
        while (number-- > 0) {
            sb.append((char)('A' + (number % 26)));
            number /= 26;
        }
        return sb.reverse().toString();
    }
	
	/**
	 * 
	 * @param sy
	 * @return
	 */
	protected static int translateObject(Object sy) {
		int y;
		
		if (sy instanceof Integer) {
			return ((Integer) sy).intValue();
		} 
		
		if (sy instanceof Double) {
			return ((Double) sy).intValue();
		}
		
		y = toNumber((String) sy);
		
		return y;
	}
	
	/**
	 * 
	 * @param type
	 * @return
	 */
	protected static String translateType(Cell cell) {
		@SuppressWarnings("deprecation")
		CellType c = cell.getCellTypeEnum();
		if (c == CellType.BLANK) return "BLANK";
		if (c == CellType.BOOLEAN) return "BOOLEAN";
		if (c == CellType.ERROR) return "ERROR";
		if (c == CellType.FORMULA) return "FORMULA";
		if (c == CellType.NUMERIC) if (HSSFDateUtil.isCellDateFormatted(cell)) return "DATE"; else return "NUMERIC";
		if (c == CellType.STRING) return "STRING";
		
		return null;
	}
	
	/**
	 * <odoc>
	 * <key>XLS.XLS(aObject, aPassword, readOnly) : XLS</key>
	 * Creates a new instance. You can optionally provide a filename to load (with aPassword if defined) or use as a template (readOnly = true) and/or an
	 * array of bytes (aObject). Example:\
	 * \
	 * var xls = new XLS("c:/test.xlsx");\
	 * \
	 * </odoc>
	 * @throws InvalidFormatException 
	 * @throws EncryptedDocumentException 
	 */
	@JSConstructor
	public void newXLS(Object arg, String password, boolean readOnly) throws IOException, EncryptedDocumentException, InvalidFormatException {
		wbook = null;
		
		if (arg instanceof String) {
			// It's a filename	
			try {
				//POIFSFileSystem poifs = new POIFSFileSystem(new File((String) arg), readOnly);
				if (readOnly) {
					wbook = WorkbookFactory.create(new FileInputStream(new File((String) arg)), password);
				} else {
					wbook = WorkbookFactory.create(new File((String) arg), password);
				}
			} catch(Exception e) {
				e.printStackTrace(); // Temporary since it shouldn't happen any more
				wbook = new XSSFWorkbook((String) arg);
			}
		}
		
		if (arg instanceof byte[]) {
{'fraction_non_alphanumeric': 0.1315105520324358, 'fraction_numerical': 0.007381224659528018, 'mean_word_length': 3.694973157637872, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '93636', 'n_tokens_mistral': 3541, 'n_tokens_neox': 3169, 'n_words': 741}