use of eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration 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;
}
use of eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration in project hale by halestudio.
the class ReadConfigurationPage method onShowPage.
/**
* @see HaleWizardPage#onShowPage(boolean)
*/
@Override
protected void onShowPage(boolean firstShow) {
super.onShowPage(firstShow);
IOProvider p = getWizard().getProvider();
String[] separatorSelection = new String[] { "TAB", ",", "|", ".", ";" };
try {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(getWizard().getProvider().getSource().getInput(), p.getCharset()));
String line = streamReader.readLine();
int tab = 0, comma = 0, pipe = 0, semicolon = 0;
if (line != null) {
tab = countChar(line, '\t');
comma = countChar(line, ',');
pipe = countChar(line, '|');
semicolon = countChar(line, ';');
}
if (Math.max(tab, comma) == tab && Math.max(tab, pipe) == tab && Math.max(tab, semicolon) == tab) {
p.setParameter(CSVConstants.PARAM_SEPARATOR, Value.of("TAB"));
} else if (Math.max(comma, tab) == comma && Math.max(comma, pipe) == comma && Math.max(comma, semicolon) == comma) {
p.setParameter(CSVConstants.PARAM_SEPARATOR, Value.of(","));
} else if (Math.max(semicolon, tab) == semicolon && Math.max(semicolon, comma) == semicolon && Math.max(semicolon, pipe) == semicolon) {
p.setParameter(CSVConstants.PARAM_SEPARATOR, Value.of(";"));
} else {
p.setParameter(CSVConstants.PARAM_SEPARATOR, Value.of("|"));
}
} catch (IOException e) {
e.printStackTrace();
}
String selection = getWizard().getProvider().getParameter(CSVConstants.PARAM_SEPARATOR).as(String.class);
for (int i = 0; i < separatorSelection.length; i++) {
if (separatorSelection[i].equals(selection)) {
getSeparator().select(i);
break;
} else {
getSeparator().select(0);
}
}
if (p instanceof InstanceReader) {
QName name = QName.valueOf(p.getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class));
if (getLast_name() == null || !(getLast_name().equals(name))) {
TypeDefinition type = ((InstanceReader) p).getSourceSchema().getType(name);
CSVConfiguration config = type.getConstraint(CSVConfiguration.class);
String sep = String.valueOf(config.getSeparator());
if (getBmap().inverse().get(sep) != null) {
getSeparator().setText(getBmap().inverse().get(sep));
} else {
getSeparator().setText(sep);
}
String qu = String.valueOf(config.getQuote());
if (getBmap().inverse().get(qu) != null) {
getQuote().setText(getBmap().inverse().get(qu));
} else {
getQuote().setText(qu);
}
String esc = String.valueOf(config.getEscape());
if (getBmap().inverse().get(esc) != null) {
getSeparator().setText(getBmap().inverse().get(esc));
} else {
getEscape().setText(esc);
}
setLast_name(name);
}
}
setPageComplete(true);
}
use of eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration in project hale by halestudio.
the class ReadInstanceConfigurationPage method onShowPage.
/**
* @see HaleWizardPage#onShowPage(boolean)
*/
@Override
protected void onShowPage(boolean firstShow) {
super.onShowPage(firstShow);
IOProvider p = getWizard().getProvider();
QName name = QName.valueOf(p.getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class));
if (getLast_name() == null || !(getLast_name().equals(name))) {
TypeDefinition type = ((InstanceReader) p).getSourceSchema().getType(name);
CSVConfiguration config = type.getConstraint(CSVConfiguration.class);
String sep = String.valueOf(config.getSeparator());
if (getBmap().inverse().get(sep) != null) {
getSeparator().setText(getBmap().inverse().get(sep));
} else {
getSeparator().setText(sep);
}
String qu = String.valueOf(config.getQuote());
if (getBmap().inverse().get(qu) != null) {
getQuote().setText(getBmap().inverse().get(qu));
} else {
getQuote().setText(qu);
}
String esc = String.valueOf(config.getEscape());
if (getBmap().inverse().get(esc) != null) {
getSeparator().setText(getBmap().inverse().get(esc));
} else {
getEscape().setText(esc);
}
setLast_name(name);
}
setPageComplete(true);
}
use of eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration in project hale by halestudio.
the class TypeSelectionPage method createContent.
/**
* @see eu.esdihumboldt.hale.ui.HaleWizardPage#createContent(org.eclipse.swt.widgets.Composite)
*/
@Override
protected void createContent(Composite page) {
page.setLayout(new GridLayout(2, false));
// XXX needed?
GridData layoutData = new GridData();
layoutData.widthHint = 200;
label = new Label(page, SWT.NONE);
label.setText("Choose your Type:");
SchemaService ss = PlatformUI.getWorkbench().getService(SchemaService.class);
sel = new TypeDefinitionSelector(page, "Select the corresponding schema type", ss.getSchemas(SchemaSpaceID.SOURCE), null);
sel.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
sel.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
setPageComplete(!(event.getSelection().isEmpty()));
if (sel.getSelectedObject() != null) {
TypeDefinition type = sel.getSelectedObject();
CSVConfiguration conf = type.getConstraint(CSVConfiguration.class);
Boolean skip = conf.skipFirst();
button.setSelection(skip);
label.getParent().layout();
}
}
});
button = new Button(page, SWT.CHECK);
button.setText("Skip first line");
button.setSelection(true);
page.pack();
setPageComplete(false);
}
Aggregations