Search in sources :

Example 1 with CSVConfiguration

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;
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) AnalyseXLSSchemaTable(eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyType(eu.esdihumboldt.hale.io.csv.PropertyType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) CSVConfiguration(eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)

Example 2 with CSVConfiguration

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);
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) InputStreamReader(java.io.InputStreamReader) CSVConfiguration(eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration) QName(javax.xml.namespace.QName) BufferedReader(java.io.BufferedReader) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 3 with CSVConfiguration

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);
}
Also used : CSVConfiguration(eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration) QName(javax.xml.namespace.QName) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 4 with CSVConfiguration

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);
}
Also used : GridLayout(org.eclipse.swt.layout.GridLayout) CSVConfiguration(eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration) SchemaService(eu.esdihumboldt.hale.ui.service.schema.SchemaService) Button(org.eclipse.swt.widgets.Button) TypeDefinitionSelector(eu.esdihumboldt.hale.ui.common.definition.selector.TypeDefinitionSelector) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) GridData(org.eclipse.swt.layout.GridData) Label(org.eclipse.swt.widgets.Label) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

CSVConfiguration (eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration)4 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)3 QName (javax.xml.namespace.QName)3 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)2 IOException (java.io.IOException)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)1 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)1 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)1 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)1 PropertyType (eu.esdihumboldt.hale.io.csv.PropertyType)1 AnalyseXLSSchemaTable (eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable)1 TypeDefinitionSelector (eu.esdihumboldt.hale.ui.common.definition.selector.TypeDefinitionSelector)1 SchemaService (eu.esdihumboldt.hale.ui.service.schema.SchemaService)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 GridData (org.eclipse.swt.layout.GridData)1