use of eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable in project hale by halestudio.
the class XLSSchemaTypePage method update.
// update whole page with current sheet number
private void update(int sheetNum) throws Exception {
// if the sheet is empty an Exception occurs
AnalyseXLSSchemaTable analyser = new AnalyseXLSSchemaTable(getWizard().getProvider().getSource().getLocation(), sheetNum);
setHeader(analyser.getHeader().toArray(new String[0]));
if (analyser.getSecondRow() != null) {
setSecondRow(analyser.getSecondRow().toArray(new String[0]));
} else {
// there is no second row, so the header is the data
setSecondRow(analyser.getHeader().toArray(new String[0]));
}
}
use of eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable in project hale by halestudio.
the class XLSInstanceReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
boolean skipFirst = getParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE).as(Boolean.class);
// first sheet as default
sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
instances = new DefaultInstanceCollection(new ArrayList<Instance>());
try {
// analyze the excel sheet to get all information
analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
} catch (Exception e) {
reporter.error(new IOMessageImpl("Reading the excel sheet has failed", e));
return reporter;
}
// get type definition of the schema
type = getSourceSchema().getType(QName.valueOf(getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class)));
// get property definition
propAr = type.getChildren().toArray(new PropertyDefinition[type.getChildren().size()]);
Collection<List<String>> rows = analyser.getRows();
// skip if first row is a header
if (!skipFirst) {
// otherwise first line is also an instance
createInstanceCollection(analyser.getHeader(), reporter);
line++;
}
// iterate over all rows to create the instances
Iterator<List<String>> allRows = rows.iterator();
while (allRows.hasNext()) {
List<String> row = allRows.next();
createInstanceCollection(row, reporter);
line++;
}
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable in project hale by halestudio.
the class XLSSchemaReader method loadFromSource.
@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
progress.begin("Load XLS/XLSX schema", ProgressIndicator.UNKNOWN);
String namespace = "http://www.esdi-humboldt.eu/hale/xls";
DefaultSchema schema = new DefaultSchema(namespace, getSource().getLocation());
AnalyseXLSSchemaTable analyser;
try {
analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
header = analyser.getHeader();
// create type definition
String typename = getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class);
if (typename == null || typename.isEmpty()) {
reporter.setSuccess(false);
reporter.error(new IOMessageImpl("No Typename was set", null));
return null;
}
DefaultTypeDefinition type = new DefaultTypeDefinition(new QName(typename));
// constraints on main type
type.setConstraint(MappingRelevantFlag.ENABLED);
type.setConstraint(MappableFlag.ENABLED);
type.setConstraint(HasValueFlag.DISABLED);
type.setConstraint(AbstractFlag.DISABLED);
// set metadata for main type
type.setLocation(getSource().getLocation());
StringBuffer defaultPropertyTypeBuffer = new StringBuffer();
String[] comboSelections;
if (getParameter(PARAM_PROPERTYTYPE).isEmpty()) {
for (int i = 0; i < header.size(); i++) {
defaultPropertyTypeBuffer.append("java.lang.String");
defaultPropertyTypeBuffer.append(",");
}
defaultPropertyTypeBuffer.deleteCharAt(defaultPropertyTypeBuffer.lastIndexOf(","));
String combs = defaultPropertyTypeBuffer.toString();
comboSelections = combs.split(",");
} else {
comboSelections = getParameter(PARAM_PROPERTYTYPE).as(String.class).split(",");
}
String[] properties;
if (getParameter(PARAM_PROPERTY).isEmpty()) {
properties = header.toArray(new String[0]);
} else {
properties = getParameter(PARAM_PROPERTY).as(String.class).split(",");
}
// than the entries in the first line
if ((header.size() != properties.length && properties.length != 0) || (header.size() != comboSelections.length && comboSelections.length != 0)) {
fail("Not the same number of entries for property names, property types and words in the first line of the file");
}
for (int i = 0; i < comboSelections.length; i++) {
PropertyType propertyType = PropertyTypeExtension.getInstance().getFactory(comboSelections[i]).createExtensionObject();
DefaultPropertyDefinition property = new DefaultPropertyDefinition(new QName(properties[i]), type, propertyType.getTypeDefinition());
configureProperty(property);
}
boolean skip = Arrays.equals(properties, header.toArray(new String[0]));
type.setConstraint(new CSVConfiguration(CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this), skip));
schema.addType(type);
} catch (Exception e) {
reporter.error(new IOMessageImpl("Cannot load xls/xlsx schema", e));
reporter.setSuccess(false);
return null;
}
reporter.setSuccess(true);
return schema;
}
Aggregations