use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class AbstractBaseAlignmentLoader method generatePrefixMapping.
/**
* Function to fill the prefixMapping and alignmentToInfo maps.
*
* @param addBase the URI of the new base alignment to add
* @param projectLocation the project location or <code>null</code>
* @param existingBases the map of existing bases
* @param prefixMapping gets filled with a mapping from local to global
* prefixes
* @param alignmentToInfo gets filled with a mapping from base alignment
* representations to prefixes and URIs
* @param reporter the reporter
* @return whether newBase actually is a new base and the add process should
* continue
*/
private boolean generatePrefixMapping(URI addBase, URI projectLocation, Map<String, URI> existingBases, Map<A, Map<String, String>> prefixMapping, Map<A, AlignmentInfo> alignmentToInfo, IOReporter reporter) {
// Project location may be null if the project wasn't saved yet
// Then, it still is okay, if all bases are absolute.
URI currentAbsolute = projectLocation;
URI usedAddBaseURI = addBase;
URI absoluteAddBaseURI = resolve(currentAbsolute, usedAddBaseURI);
// set of already seen URIs
Set<URI> knownURIs = new HashSet<URI>();
// reverse map of base
Map<URI, String> uriToPrefix = new HashMap<URI, String>();
// create URI to prefix map and known URIs
for (Entry<String, URI> baseEntry : existingBases.entrySet()) {
// make sure to use absolute URIs here for comparison
URI absoluteBase = resolve(currentAbsolute, baseEntry.getValue());
knownURIs.add(absoluteBase);
uriToPrefix.put(absoluteBase, baseEntry.getKey());
}
if (uriToPrefix.containsKey(absoluteAddBaseURI)) {
reporter.info(new IOMessageImpl("The base alignment (" + addBase + ") is already included.", null));
return false;
}
Set<String> existingPrefixes = new HashSet<String>(existingBases.keySet());
String newPrefix = generatePrefix(existingPrefixes);
existingPrefixes.add(newPrefix);
uriToPrefix.put(absoluteAddBaseURI, newPrefix);
/*
* XXX Adding a base alignment could only use a PathUpdate for the
* movement of the base alignment which is not known in the current
* project. Maybe could try the one of the current project either way?
*/
// find all alignments to load (also missing ones) and load the beans
LinkedList<URIPair> queue = new LinkedList<URIPair>();
queue.add(new URIPair(absoluteAddBaseURI, usedAddBaseURI));
while (!queue.isEmpty()) {
URIPair baseURI = queue.pollFirst();
A baseA;
try {
baseA = loadAlignment(new DefaultInputSupplier(baseURI.absoluteURI).getInput(), reporter);
} catch (IOException e) {
reporter.error(new IOMessageImpl("Couldn't load an included base alignment (" + baseURI.absoluteURI + ").", e));
reporter.setSuccess(false);
return false;
}
// add to alignment info map
alignmentToInfo.put(baseA, new AlignmentInfo(uriToPrefix.get(baseURI.absoluteURI), baseURI));
prefixMapping.put(baseA, new HashMap<String, String>());
// load "missing" base alignments, too, add prefix mapping
for (Entry<String, URI> baseEntry : getBases(baseA).entrySet()) {
// rawURI may be relative
URI rawURI = baseEntry.getValue();
URI absoluteURI = baseURI.absoluteURI.resolve(rawURI);
URI usedURI = absoluteURI;
// and rawURI are relative, continue using a relative URI.
if (!usedAddBaseURI.isAbsolute() && !baseURI.usedURI.isAbsolute() && !rawURI.isAbsolute()) {
usedURI = IOUtils.getRelativePath(absoluteURI, currentAbsolute);
}
// check whether this base alignment is missing
if (!knownURIs.contains(absoluteURI)) {
reporter.info(new IOMessageImpl("A base alignment referenced another base alignment (" + absoluteURI + ") that was not yet known. It is now included, too.", null));
queue.add(new URIPair(absoluteURI, usedURI));
knownURIs.add(absoluteURI);
String prefix = generatePrefix(existingPrefixes);
existingPrefixes.add(prefix);
uriToPrefix.put(absoluteURI, prefix);
}
// add prefix mapping
prefixMapping.get(baseA).put(baseEntry.getKey(), uriToPrefix.get(absoluteURI));
}
}
return true;
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class OMLReaderTest method loadAlignment.
private static Alignment loadAlignment(URI sourceSchemaLocation, URI targetSchemaLocation, final URI alignmentLocation) throws IOProviderConfigurationException, IOException {
// load source schema
Schema source = readXMLSchema(new DefaultInputSupplier(sourceSchemaLocation));
// load target schema
Schema target = readXMLSchema(new DefaultInputSupplier(targetSchemaLocation));
OmlReader reader = new OmlReader();
reader.setSourceSchema(source);
reader.setTargetSchema(target);
reader.setSource(new DefaultInputSupplier(alignmentLocation));
reader.validate();
IOReport report = reader.execute(null);
assertTrue(report.isSuccess());
return reader.getAlignment();
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class URILink method createDefaultSelectionAdapter.
// create the the SelectionAdapter for the UriLink
private SelectionAdapter createDefaultSelectionAdapter(final URI uri) {
return new SelectionAdapter() {
private URI removeFragment(URI uri) throws URISyntaxException {
String uristring = uri.toString();
uristring = uristring.substring(0, uristring.indexOf("#"));
return new URI(uristring);
}
// the URI has to be an existing file on the local drive or on a
// server
@Override
public void widgetSelected(SelectionEvent e) {
URI newuri = uri;
try {
// online resource
if (uri.getScheme().equals("http") || uri.getScheme().equals("https")) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e1) {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Opening Error", "No default application set!");
}
return;
}
if (uri.toString().contains("#")) {
newuri = removeFragment(uri);
}
// local resource or bundle resource
if (DefaultInputSupplier.SCHEME_LOCAL.equals(newuri.getScheme()) || "bundleentry".equals(newuri.getScheme())) {
// cannot be opened by system
// so copy resource to temporary file
String name = newuri.getPath();
int index = name.lastIndexOf('/');
if (index >= 0) {
name = name.substring(index + 1);
}
if (!name.isEmpty()) {
File tmpFile = Files.createTempFile("resource", name).toFile();
try (OutputStream out = new FileIOSupplier(tmpFile).getOutput();
InputStream in = new DefaultInputSupplier(newuri).getInput()) {
ByteStreams.copy(in, out);
}
tmpFile.deleteOnExit();
newuri = tmpFile.toURI();
}
}
// try creating a file
File file = new File(newuri);
if (file.exists()) {
try {
Desktop.getDesktop().open(file);
} catch (IOException e2) {
try {
Desktop.getDesktop().browse(newuri);
} catch (IOException e1) {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Opening Error", "No default application set!");
}
}
} else {
try {
Desktop.getDesktop().browse(newuri);
} catch (IOException e1) {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Opening Error", "No default application set!");
}
}
} catch (Exception e1) {
// ignore
}
}
};
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class ProjectParser method load.
private void load(URI source, String actionId, String msgCT, String msgIO, @SuppressWarnings("rawtypes") Class clazz) {
// create IOConfiguration for source data
IOConfiguration conf = new IOConfiguration();
// populate IOConfiguration
// set action ID
conf.setActionId(actionId);
// find provider
File file;
try {
file = new File(source);
} catch (IllegalArgumentException e) {
file = null;
}
@SuppressWarnings("unchecked") IContentType ct = HaleIO.findContentType(clazz, new DefaultInputSupplier(source), (file == null) ? (null) : (file.getAbsolutePath()));
if (ct == null) {
report.error(new IOMessageImpl(msgCT, null, -1, -1, source));
return;
}
@SuppressWarnings("unchecked") IOProviderDescriptor srf = HaleIO.findIOProviderFactory(clazz, ct, null);
if (srf == null) {
report.error(new IOMessageImpl(msgIO, null, -1, -1, source));
return;
}
conf.setProviderId(srf.getIdentifier());
// provider configuration
// source
conf.getProviderConfiguration().put(AbstractImportProvider.PARAM_SOURCE, Value.of(source.toString()));
// content type
conf.getProviderConfiguration().put(AbstractImportProvider.PARAM_CONTENT_TYPE, Value.of(ct.getId()));
// no dependencies needed
// add configuration to project
project.getResources().add(conf);
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class ValidatorConfigurationReaderImpl method execute.
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
* eu.esdihumboldt.hale.common.core.io.report.IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Loading validator configuration.", ProgressIndicator.UNKNOWN);
final URI sourceLocation = getSource().getLocation();
if (sourceLocation == null) {
throw new IOProviderConfigurationException("No source location provided when trying to read validator configuration.");
}
final DefaultInputSupplier validationRuleInputSupplier = new DefaultInputSupplier(sourceLocation);
final InputStream validationRuleInput = validationRuleInputSupplier.getInput();
if (validationRuleInput == null) {
throw new IOProviderConfigurationException("Cannot read validator configuration.");
}
try {
// XXX UTF 8 encoding is assumed here. The actual encoding should be
// detected or be configurable
configuration = new ValidatorConfiguration(IOUtils.toString(validationRuleInput, StandardCharsets.UTF_8), sourceLocation, getContentType());
reporter.setSuccess(true);
} catch (Exception e) {
throw new IOProviderConfigurationException(MessageFormat.format("Could not read validation rule from '{0}': {1}", sourceLocation.toString(), e.getMessage()), e);
} finally {
IOUtils.closeQuietly(validationRuleInput);
}
progress.setCurrentTask("Validation rule loaded.");
return reporter;
}
Aggregations