All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.uniroma2.art.sheet2rdf.sheet.ODSSheetManager Maven / Gradle / Ivy

There is a newer version: 6.0.6
Show newest version
package it.uniroma2.art.sheet2rdf.sheet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;

public class ODSSheetManager implements SheetManager {
	
	private SpreadSheet spreadSheet;
	private Sheet dataSheet, prefixMappingSheet;
	
	private static final String PREFIX_MAPPING_SHEET_NAME = "prefix_mapping";

	public ODSSheetManager(File file){
		try {
			spreadSheet = SpreadSheet.createFromFile(file);
			dataSheet = spreadSheet.getSheet(0);
			prefixMappingSheet = spreadSheet.getSheet(PREFIX_MAPPING_SHEET_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List getHeaders(boolean includeDuplicate) {
		ArrayList headers = new ArrayList();
		int nCol = getDataSheetColumnCount();
		for (int i = 0; i < nCol; i++){
			MutableCell headerCell = dataSheet.getCellAt(i, 0);
			String header = headerCell.getTextValue().trim();
			if (!headers.contains(header))//if the headers is not yet in the list, add it
				headers.add(header);
			else { //else add it only if includeDuplicate = true
				if (includeDuplicate)
					headers.add(header);
			}
		}
		return headers;
	}

	@Override
	public ArrayList> getDataTable() {
		int rows = getDataSheetRowCount();
		int columns = getDataSheetColumnCount();
		ArrayList> table = new ArrayList>();
		for (int r = 1; r < rows; r++){
			ArrayList arrayListRow = new ArrayList();
			for (int c = 0; c < columns; c++){
				String data = getCellValueInDataSheet(r, c); 
				arrayListRow.add(data);
			}
			table.add(arrayListRow);
		}
		return table;
	}

	@Override
	public String getCellValueInDataSheet(int row, int column) {
		String value = "";
		MutableCell cell = dataSheet.getCellAt(column, row);
		if (cell != null){
			value = cell.getTextValue();
		}
		return value;
	}

	@Override
	public Map getPrefixNamespaceMapping() {
		Map map = new HashMap();
		if (prefixMappingSheet != null){
			int nRow = getPrefixSheetRowCount();
			for (int i = 0; i < nRow; i++){				
				MutableCell prefCell = prefixMappingSheet.getCellAt(0, i);
				MutableCell nsCell = prefixMappingSheet.getCellAt(1, i);
				if (prefCell != null && nsCell != null){
					String pref = prefCell.getTextValue();
					String ns = nsCell.getTextValue();
					map.put(pref, ns);
				}
			}
		}
		return map;
	}

	@Override
	public boolean isMultipleHeader(String headerValue) {
		int nCol = getDataSheetColumnCount();
		int idxHeaderValue = 0;
		//find the column index of the headerValue
		for (int i = 0; i < nCol; i++){
			MutableCell headerCell = dataSheet.getCellAt(i, 0);
			String h = headerCell.getTextValue().trim();
			if (h.equals(headerValue)){
				idxHeaderValue = i;
				break;
			}
		}
		//check if following column has the same headerValue
		MutableCell followingHeader = dataSheet.getCellAt(idxHeaderValue+1, 0);
		if (followingHeader != null) {//check necessary cause the header could be the last one
			return followingHeader.getTextValue().trim().equals(headerValue);
		}
		else
			return false;
	}

	@Override
	public boolean existsPrefixMappingSheet() {
		return (spreadSheet.getSheet(PREFIX_MAPPING_SHEET_NAME) != null);
	}

	@Override
	public int getDataSheetColumnCount() {
		for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy