use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class CSVSchemaReaderTest method failTest.
/**
* Test for no given property names and only 2 (of 4) given property types
* (if there are not given 0 or maximum, in this case 4, property types we
* expect an error)
*
* @throws Exception the Exception thrown if the test fails
*/
public void failTest() throws Exception {
CSVSchemaReader schemaReader = new CSVSchemaReader();
schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/data/test1.csv").toURI()));
schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("TestTyp"));
schemaReader.setParameter(CSVSchemaReader.PARAM_PROPERTY, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_PROPERTYTYPE, Value.of("java.lang.String,java.lang.String"));
schemaReader.setParameter(CSVSchemaReader.PARAM_SEPARATOR, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_QUOTE, null);
schemaReader.setParameter(CSVSchemaReader.PARAM_ESCAPE, null);
IOReport report = schemaReader.execute(new LogProgressIndicator());
assertFalse(report.getErrors().isEmpty());
assertFalse(report.isSuccess());
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class CSVSchemaReaderTest method failTest2.
/**
* Test for no given type name. So we expect the reporter not to be
* successful.
*
* @throws Exception the Exception thrown if the test fails
*/
@Test
public void failTest2() throws Exception {
CSVSchemaReader schemaReader2 = new CSVSchemaReader();
schemaReader2.setSource(new DefaultInputSupplier(getClass().getResource("/data/test1.csv").toURI()));
schemaReader2.setParameter(CommonSchemaConstants.PARAM_TYPENAME, null);
IOReport report = schemaReader2.execute(new LogProgressIndicator());
assertFalse(report.isSuccess());
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier 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();
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class ArchiveProjectWriter method updateResources.
/**
* Update the resources and copy them into the target directory
*
* @param targetDirectory target directory
* @param includeWebResources whether to include web resources in the copy
* @param progress the progress indicator
* @param reporter the reporter to use for the execution report
* @throws IOException if an I/O operation fails
*/
protected void updateResources(File targetDirectory, boolean includeWebResources, ProgressIndicator progress, IOReporter reporter) throws IOException {
progress.begin("Copy resources", ProgressIndicator.UNKNOWN);
try {
List<IOConfiguration> resources = getProject().getResources();
// every resource needs his own directory
int count = 1;
// true if excluded files should be skipped; false is default
boolean excludeDataFiles = getParameter(EXLUDE_DATA_FILES).as(Boolean.class, false);
// resource locations mapped to new resource path
Map<URI, String> handledResources = new HashMap<>();
Iterator<IOConfiguration> iter = resources.iterator();
while (iter.hasNext()) {
IOConfiguration resource = iter.next();
// import not possible due to cycle errors
if (excludeDataFiles && resource.getActionId().equals("eu.esdihumboldt.hale.io.instance.read.source")) {
// delete reference in project file
iter.remove();
continue;
}
// get resource path
Map<String, Value> providerConfig = resource.getProviderConfiguration();
String path = providerConfig.get(ImportProvider.PARAM_SOURCE).toString();
URI pathUri;
try {
pathUri = new URI(path);
} catch (URISyntaxException e1) {
reporter.error(new IOMessageImpl("Skipped resource because of invalid URI: " + path, e1));
continue;
}
if (!pathUri.isAbsolute()) {
if (getPreviousTarget() != null) {
pathUri = getPreviousTarget().resolve(pathUri);
} else {
log.warn("Could not resolve relative path " + pathUri.toString());
}
}
// check if path was already handled
if (handledResources.containsKey(pathUri)) {
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(handledResources.get(pathUri)));
// skip copying the resource
continue;
}
String scheme = pathUri.getScheme();
LocatableInputSupplier<? extends InputStream> input = null;
if (scheme != null) {
if (scheme.equals("http") || scheme.equals("https")) {
// web resource
if (includeWebResources) {
input = new DefaultInputSupplier(pathUri);
} else {
// web resource that should not be included this
// time
// but the resolved URI should be stored
// nevertheless
// otherwise the URI may be invalid if it was
// relative
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(pathUri.toASCIIString()));
continue;
}
} else if (scheme.equals("file") || scheme.equals("platform") || scheme.equals("bundle") || scheme.equals("jar")) {
// files need always to be included
// platform resources (or other internal resources)
// should be included as well
input = new DefaultInputSupplier(pathUri);
} else {
// other type of URI, e.g. JDBC
// not to be included
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(pathUri.toASCIIString()));
continue;
}
} else {
// now can't open that, can we?
reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + pathUri.toString(), null));
continue;
}
progress.setCurrentTask("Copying resource at " + path);
// every resource file is copied into an own resource
// directory in the target directory
String resourceFolder = "resource" + count;
File newDirectory = new File(targetDirectory, resourceFolder);
try {
newDirectory.mkdir();
} catch (SecurityException e) {
throw new IOException("Can not create directory " + newDirectory.toString(), e);
}
// Extract the file name from pathUri.getPath().
// This will produce a non-URL-encoded file name to be used in
// the File(File parent, String child) constructor below
String fileName = FilenameUtils.getName(pathUri.getPath().toString());
if (path.isEmpty()) {
fileName = "file";
}
File newFile = new File(newDirectory, fileName);
Path target = newFile.toPath();
// retrieve the resource advisor
Value ct = providerConfig.get(ImportProvider.PARAM_CONTENT_TYPE);
IContentType contentType = null;
if (ct != null) {
contentType = HalePlatform.getContentTypeManager().getContentType(ct.as(String.class));
}
ResourceAdvisor ra = ResourceAdvisorExtension.getInstance().getAdvisor(contentType);
// copy the resource
progress.setCurrentTask("Copying resource at " + path);
// Extract the URL-encoded file name of the copied resource and
// build the new relative resource path
String resourceName = FilenameUtils.getName(target.toUri().toString());
String newPath = resourceFolder + "/" + resourceName;
boolean skipCopy = getParameter(EXCLUDE_CACHED_RESOURCES).as(Boolean.class, false) && !resource.getCache().isEmpty();
if (!skipCopy) {
ra.copyResource(input, target, contentType, includeWebResources, reporter);
// store new path for resource
handledResources.put(pathUri, newPath);
}
// update the provider configuration
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(newPath));
count++;
}
} finally {
progress.end();
}
}
use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.
the class DefaultProjectReader method execute.
/**
* @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Load project", ProgressIndicator.UNKNOWN);
InputStream in = getSource().getInput();
if (archive) {
// read from archive
ZipInputStream zip = new ZipInputStream(new BufferedInputStream(in));
try {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
progress.setCurrentTask(MessageFormat.format("Load {0}", name));
if (name.equals(ProjectIO.PROJECT_FILE)) {
try {
setProjectChecked(Project.load(new EntryInputStream(zip)), reporter);
} catch (Exception e) {
// fail if main project file cannot be loaded
throw new IOProviderConfigurationException("Source is no valid project archive", e);
}
} else {
ProjectFile file = getProjectFiles().get(name);
if (file != null) {
try {
file.load(new EntryInputStream(zip));
} catch (Exception e) {
reporter.error(new IOMessageImpl("Error while loading project file {0}, file will be reset.", e, -1, -1, name));
// reset file
file.reset();
}
}
}
}
} finally {
zip.close();
}
} else {
// read from XML
try {
setProjectChecked(Project.load(in), reporter);
} catch (Exception e) {
// fail if main project file cannot be loaded
throw new IOProviderConfigurationException("Source is no valid project file", e);
} finally {
in.close();
}
}
URI oldProjectLocation;
if (getProject().getSaveConfiguration() == null) {
oldProjectLocation = getSource().getLocation();
} else {
oldProjectLocation = URI.create(getProject().getSaveConfiguration().getProviderConfiguration().get(ExportProvider.PARAM_TARGET).as(String.class));
}
PathUpdate update = new PathUpdate(oldProjectLocation, getSource().getLocation());
// check if there are any external project files listed
if (getProjectFiles() != null) {
// only if project files set at all
for (ProjectFileInfo fileInfo : getProject().getProjectFiles()) {
ProjectFile projectFile = getProjectFiles().get(fileInfo.getName());
if (projectFile != null) {
URI location = fileInfo.getLocation();
location = update.findLocation(location, false, DefaultInputSupplier.SCHEME_LOCAL.equals(getSource().getLocation().getScheme()), false);
if (location == null && getSource().getLocation() != null) {
// 1st try: appending file name to project location
try {
URI candidate = new URI(getSource().getLocation().toString() + "." + fileInfo.getName());
if (HaleIO.testStream(candidate, true)) {
location = candidate;
}
} catch (URISyntaxException e) {
// ignore
}
// 2nd try: file name next to project
if (location != null) {
try {
String projectLoc = getSource().getLocation().toString();
int index = projectLoc.lastIndexOf('/');
if (index > 0) {
URI candidate = new URI(projectLoc.substring(0, index + 1) + fileInfo.getName());
if (HaleIO.testStream(candidate, true)) {
location = candidate;
}
}
} catch (URISyntaxException e) {
// ignore
}
}
}
boolean fileSuccess = false;
if (location != null) {
try {
DefaultInputSupplier dis = new DefaultInputSupplier(location);
try (InputStream input = dis.getInput()) {
projectFile.load(input);
fileSuccess = true;
} catch (Exception e) {
// hand down
throw e;
}
} catch (Exception e) {
reporter.error(new IOMessageImpl("Loading project file failed", e));
}
}
if (!fileSuccess) {
reporter.error(new IOMessageImpl("Error while loading project file {0}, file will be reset.", null, -1, -1, fileInfo.getName()));
projectFile.reset();
}
} else {
reporter.info(new IOMessageImpl("No handler for external project file {0} found.", null, -1, -1, fileInfo.getName()));
}
}
}
// clear project infos
/*
* XXX was there any particular reason why this was done? I suspect it
* was done so when saving the project this information is not saved
* again as-is, but on the basis of actual files written. However, this
* case is handled in the project writer already.
*
* As this information is in fact necessary when trying to identify
* certain files like the alignment, clearing the list of project files
* was commented out.
*/
// getProject().getProjectFiles().clear();
progress.end();
reporter.setSuccess(true);
return reporter;
}
Aggregations