Search in sources :

Example 6 with InstanceReader

use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.

the class StreamGmlWriterTest method loadGML.

/**
 * Load GML from a file.
 *
 * @param sourceData the GML file
 * @param schema the schema location
 * @return the features
 * @throws IOException if loading the file fails
 * @throws IOProviderConfigurationException if the instance reader is not
 *             configured correctly
 */
public static InstanceCollection loadGML(URI sourceData, Schema schema) throws IOException, IOProviderConfigurationException {
    InstanceReader instanceReader = new GmlInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(sourceData));
    instanceReader.setSourceSchema(schema);
    IOReport instanceReport = instanceReader.execute(null);
    assertTrue(instanceReport.isSuccess());
    return instanceReader.getInstances();
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)

Example 7 with InstanceReader

use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.

the class IOReferenceContent method getInputStream.

@Override
public InputStream getInputStream(String pluginID, String href, Locale locale) {
    // it is an I/O provider reference
    if (href.startsWith(IO_PROVIDERS_TOPIC_PATH)) {
        String providerId = href.substring(IO_PROVIDERS_TOPIC_PATH.length());
        // strip everything after a ?
        int ind = providerId.indexOf('?');
        if (ind >= 0) {
            providerId = providerId.substring(0, ind);
        }
        // strip the .*htm? ending
        if (providerId.endsWith("html") || providerId.endsWith("htm")) {
            providerId = providerId.substring(0, providerId.lastIndexOf('.'));
        }
        try {
            return getIOProviderContent(providerId);
        } catch (Exception e) {
            log.error("Error creating instance io info page.", e);
            return null;
        }
    } else // should be an I/O provider overview by type
    if (href.startsWith(OVERVIEW_TOPIC_PATH)) {
        // extract provider type name
        String providerType = href.substring(OVERVIEW_TOPIC_PATH.length());
        // strip everything after a ?
        int ind = providerType.indexOf('?');
        if (ind >= 0) {
            providerType = providerType.substring(0, ind);
        }
        // strip the .*htm? ending
        if (providerType.endsWith("html") || providerType.endsWith("htm")) {
            providerType = providerType.substring(0, providerType.lastIndexOf('.'));
        }
        Class<? extends IOProvider> providerClass = null;
        switch(providerType) {
            case "InstanceReader":
                providerClass = InstanceReader.class;
                break;
            case "InstanceWriter":
                providerClass = InstanceWriter.class;
                break;
            case "InstanceValidator":
                providerClass = InstanceValidator.class;
                break;
        }
        if (providerClass != null) {
            final Class<? extends IOProvider> provider = providerClass;
            try {
                return getContentFromTemplate("overview." + providerType, TEMPLATE_OVERVIEW, new Callable<VelocityContext>() {

                    @Override
                    public VelocityContext call() throws Exception {
                        VelocityContext context = new VelocityContext();
                        // getProviderFactorries returns
                        // Collection<IOProviderDescriptor>
                        Collection<IOProviderDescriptor> writer = HaleIO.getProviderFactories(provider);
                        context.put("providers", writer);
                        context.put("providerType", provider.getSimpleName());
                        return context;
                    }
                });
            } catch (Exception e) {
                log.error("Error creating provider overview", e);
                return null;
            }
        }
        return null;
    }
    return null;
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) VelocityContext(org.apache.velocity.VelocityContext) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) TransformerException(javax.xml.transform.TransformerException) Callable(java.util.concurrent.Callable)

Example 8 with InstanceReader

use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.

the class CSVInstanceReaderTest method readCSVInstances.

private InstanceCollection readCSVInstances(String sourceLocation, String typeName, boolean skipFirst, Schema sourceSchema, String seperator, String quote, String escape, String decimal) throws Exception {
    InstanceReader instanceReader = new CSVInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(getClass().getResource(sourceLocation).toURI()));
    instanceReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
    instanceReader.setParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE, Value.of(skipFirst));
    instanceReader.setParameter(CSVSchemaReader.PARAM_SEPARATOR, Value.of(seperator));
    instanceReader.setParameter(CSVSchemaReader.PARAM_QUOTE, Value.of(quote));
    instanceReader.setParameter(CSVSchemaReader.PARAM_ESCAPE, Value.of(escape));
    instanceReader.setParameter(CSVSchemaReader.PARAM_DECIMAL, Value.of(decimal));
    instanceReader.setSourceSchema(sourceSchema);
    // Test instances
    IOReport report = instanceReader.execute(null);
    assertTrue("Data import was not successfull.", report.isSuccess());
    return instanceReader.getInstances();
}
Also used : CSVInstanceReader(eu.esdihumboldt.hale.io.csv.reader.internal.CSVInstanceReader) InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) CSVInstanceReader(eu.esdihumboldt.hale.io.csv.reader.internal.CSVInstanceReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Example 9 with InstanceReader

use of eu.esdihumboldt.hale.common.instance.io.InstanceReader 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 10 with InstanceReader

use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.

the class AbstractHandlerTest method loadXMLInstances.

/**
 * Load an instance collection from a GML file.
 *
 * @param schemaLocation the GML application schema location
 * @param xmlLocation the GML file location
 * @return the instance collection
 * @throws IOException if reading schema or instances failed
 * @throws IOProviderConfigurationException if the I/O providers were not
 *             configured correctly
 */
public static InstanceCollection loadXMLInstances(URI schemaLocation, URI xmlLocation) throws IOException, IOProviderConfigurationException {
    SchemaReader reader = new XmlSchemaReader();
    reader.setSharedTypes(null);
    reader.setSource(new DefaultInputSupplier(schemaLocation));
    IOReport schemaReport = reader.execute(null);
    assertTrue(schemaReport.isSuccess());
    Schema sourceSchema = reader.getSchema();
    InstanceReader instanceReader = new GmlInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(xmlLocation));
    instanceReader.setSourceSchema(sourceSchema);
    IOReport instanceReport = instanceReader.execute(null);
    assertTrue(instanceReport.isSuccess());
    return instanceReader.getInstances();
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader) SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)

Aggregations

InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)13 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)8 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)7 GmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)5 SchemaReader (eu.esdihumboldt.hale.common.schema.io.SchemaReader)4 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)4 XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)4 IOException (java.io.IOException)3 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)2 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)2 XmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.XmlInstanceReader)2 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 AbstractIOAdvisor (eu.esdihumboldt.hale.common.core.io.impl.AbstractIOAdvisor)1 ProjectInfoAware (eu.esdihumboldt.hale.common.core.io.project.ProjectInfoAware)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 LocatableInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.LocatableInputSupplier)1 ProjectTransformationEnvironment (eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)1 TransformationWorkspace (eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace)1 DefaultTransformedInstanceValidator (eu.esdihumboldt.hale.common.headless.transform.validate.impl.DefaultTransformedInstanceValidator)1 InstanceValidator (eu.esdihumboldt.hale.common.instance.io.InstanceValidator)1