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();
}
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;
}
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();
}
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);
}
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();
}
Aggregations