use of eu.esdihumboldt.hale.common.core.io.supplier.Locatable in project hale by halestudio.
the class HaleConnectProjectWriter method createReporter.
/**
* @see IOProvider#createReporter()
*/
@Override
public IOReporter createReporter() {
if (!(haleConnect instanceof BasePathResolver) || !HaleConnectUrnBuilder.isValidProjectUrn(getTarget().getLocation())) {
return super.createReporter();
}
try {
URI targetUri = getTarget().getLocation();
Locatable prettifiedTarget = new LocatableURI(prettifyTarget(targetUri));
return new DefaultIOReporter(prettifiedTarget, MessageFormat.format("{0} export", getTypeName()), getActionId(), true);
} catch (Throwable t) {
return super.createReporter();
}
}
use of eu.esdihumboldt.hale.common.core.io.supplier.Locatable in project hale by halestudio.
the class FileValidateTarget method addValidator.
private void addValidator(IOProviderDescriptor selection) {
InstanceValidator validator;
try {
validator = (InstanceValidator) selection.createExtensionObject();
// set schemas
List<? extends Locatable> schemas = getWizard().getProvider().getValidationSchemas();
validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
getWizard().addValidator(validator);
ValidatorEntry entry = new ValidatorEntry(validator, selection);
validators.add(entry);
updateWizard(getSelectedValidator());
} catch (Exception e) {
this.getPage().setErrorMessage("Could not instantiate validator.");
log.error(MessageFormat.format("Could not instantiate validator {0}", selection.getIdentifier(), e));
}
}
use of eu.esdihumboldt.hale.common.core.io.supplier.Locatable in project hale by halestudio.
the class ValidationJob method run.
/**
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
protected IStatus run(IProgressMonitor monitor) {
boolean successful = true;
for (InstanceValidator validator : this.validators) {
IOReporter defaultReporter = validator.createReporter();
defaultReporter.setSuccess(false);
IOReport report = defaultReporter;
try {
ATransaction trans = log.begin(defaultReporter.getTaskName());
try {
if (writer != null) {
// set validation schemas (may have been determined only
// during writer execution)
// set schemas
List<? extends Locatable> schemas = writer.getValidationSchemas();
validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
}
validator.setServiceProvider(serviceProvider);
IOReport result = validator.execute(new ProgressMonitorIndicator(monitor));
if (result != null) {
report = result;
} else {
defaultReporter.setSuccess(true);
}
} catch (Throwable e) {
defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
} finally {
trans.end();
}
} catch (Throwable e) {
defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
}
if (monitor.isCanceled()) {
reset();
return Status.CANCEL_STATUS;
}
// add report to report service
reportHandler.publishReport(report);
// show message to user
if (report.isSuccess()) {
// info message
log.info(report.getSummary());
} else {
// error message
log.error(report.getSummary());
successful = false;
}
}
reset();
if (successful) {
log.userInfo("All validations completed successfully.");
return Status.OK_STATUS;
} else {
log.userError("There were validation failures. Please check the reports for details.");
return ERROR_STATUS;
}
}
use of eu.esdihumboldt.hale.common.core.io.supplier.Locatable in project hale by halestudio.
the class TestUtil method loadAlignment.
/**
* Loads the specified alignment. Assumes that its base alignments don't
* need a location update.
*
* @param location the URI specifying the location of the alignment
* @param sourceTypes the source type index
* @param targetTypes the target type index
* @return the loaded alignment
* @throws Exception if the alignment or other resources could not be loaded
*/
public static Alignment loadAlignment(final URI location, Schema sourceTypes, Schema targetTypes) throws Exception {
DefaultInputSupplier input = new DefaultInputSupplier(location);
IOReporter report = new DefaultIOReporter(new Locatable() {
@Override
public URI getLocation() {
return location;
}
}, "Load alignment", AlignmentIO.ACTION_LOAD_ALIGNMENT, true);
Alignment alignment;
try {
alignment = CastorAlignmentIO.load(input.getInput(), report, sourceTypes, targetTypes, new PathUpdate(null, null));
} catch (Exception e) {
alignment = JaxbAlignmentIO.load(input.getInput(), report, sourceTypes, targetTypes, new PathUpdate(null, null), null, null);
}
assertTrue("Errors are contained in the report", report.getErrors().isEmpty());
return alignment;
}
use of eu.esdihumboldt.hale.common.core.io.supplier.Locatable in project hale by halestudio.
the class StreamGmlWriter method write.
/**
* Write the given instances to an {@link XMLStreamWriter}.<br>
* <br>
* Use {@link #createWriter(OutputStream, IOReporter)} to create a properly
* configured writer for this method.
*
* @param instances the instance collection
* @param writer the writer to write the instances to
* @param reporter the reporter
* @param progress the progress
* @see #createWriter(OutputStream, IOReporter)
*/
protected void write(InstanceCollection instances, PrefixAwareStreamWriter writer, ProgressIndicator progress, IOReporter reporter) {
this.writer = writer;
try {
final SubtaskProgressIndicator sub = new SubtaskProgressIndicator(progress) {
@Override
protected String getCombinedTaskName(String taskName, String subtaskName) {
return taskName + " (" + subtaskName + ")";
}
};
progress = sub;
progress.begin(getTaskName(), instances.size());
XmlElement container = findDefaultContainter(targetIndex, reporter);
TypeDefinition containerDefinition = (container == null) ? (null) : (container.getType());
QName containerName = (container == null) ? (null) : (container.getName());
if (containerDefinition == null) {
XmlElement containerElement = getConfiguredContainerElement(this, getXMLIndex());
if (containerElement != null) {
containerDefinition = containerElement.getType();
containerName = containerElement.getName();
container = containerElement;
} else {
// this is the last option, so we can throw a specific error
throw new IllegalStateException("Configured container element not found");
}
}
if (containerDefinition == null || containerName == null) {
throw new IllegalStateException("No root element/container found");
}
/*
* Add schema for container to validation schemas, if the namespace
* differs from the main namespace or additional schemas.
*
* Needed for validation based on schemaLocation attribute.
*/
if (container != null && !containerName.getNamespaceURI().equals(targetIndex.getNamespace()) && !additionalSchemas.containsKey(containerName.getNamespaceURI())) {
try {
final URI containerSchemaLoc = stripFragment(container.getLocation());
if (containerSchemaLoc != null) {
addValidationSchema(containerName.getNamespaceURI(), new Locatable() {
@Override
public URI getLocation() {
return containerSchemaLoc;
}
}, null);
}
} catch (Exception e) {
reporter.error("Could not determine location of container definition", e);
}
}
// additional schema namespace prefixes
for (Entry<String, String> schemaNs : additionalSchemaPrefixes.entrySet()) {
GmlWriterUtil.addNamespace(writer, schemaNs.getKey(), schemaNs.getValue());
}
writer.writeStartDocument();
if (documentWrapper != null) {
documentWrapper.startWrap(writer, reporter);
}
GmlWriterUtil.writeStartElement(writer, containerName);
// generate mandatory id attribute (for feature collection)
String containerId = getParameter(PARAM_CONTAINER_ID).as(String.class);
GmlWriterUtil.writeID(writer, containerDefinition, null, false, containerId);
// write schema locations
StringBuffer locations = new StringBuffer();
String noNamespaceLocation = null;
if (targetIndex.getNamespace() != null && !targetIndex.getNamespace().isEmpty()) {
locations.append(targetIndex.getNamespace());
// $NON-NLS-1$
locations.append(" ");
locations.append(targetIndex.getLocation().toString());
} else {
noNamespaceLocation = targetIndex.getLocation().toString();
}
for (Entry<String, Locatable> schema : additionalSchemas.entrySet()) {
if (schema.getKey() != null && !schema.getKey().isEmpty()) {
if (locations.length() > 0) {
// $NON-NLS-1$
locations.append(" ");
}
locations.append(schema.getKey());
// $NON-NLS-1$
locations.append(" ");
locations.append(schema.getValue().getLocation().toString());
} else {
noNamespaceLocation = schema.getValue().getLocation().toString();
}
}
if (locations.length() > 0) {
// $NON-NLS-1$
writer.writeAttribute(SCHEMA_INSTANCE_NS, "schemaLocation", locations.toString());
}
if (noNamespaceLocation != null) {
// $NON-NLS-1$
writer.writeAttribute(// $NON-NLS-1$
SCHEMA_INSTANCE_NS, // $NON-NLS-1$
"noNamespaceSchemaLocation", noNamespaceLocation);
}
writeAdditionalElements(writer, containerDefinition, reporter);
// write the instances
ResourceIterator<Instance> itInstance = instances.iterator();
try {
Map<TypeDefinition, DefinitionPath> paths = new HashMap<TypeDefinition, DefinitionPath>();
long lastUpdate = 0;
int count = 0;
Descent lastDescent = null;
while (itInstance.hasNext() && !progress.isCanceled()) {
Instance instance = itInstance.next();
TypeDefinition type = instance.getDefinition();
/*
* Skip all objects that are no features when writing to a
* GML feature collection.
*/
boolean skip = useFeatureCollection && !GmlWriterUtil.isFeatureType(type);
if (skip) {
progress.advance(1);
continue;
}
// get stored definition path for the type
DefinitionPath defPath;
if (paths.containsKey(type)) {
// get the stored path, may be null
defPath = paths.get(type);
} else {
// determine a valid definition path in the container
defPath = findMemberAttribute(containerDefinition, containerName, type);
// store path (may be null)
paths.put(type, defPath);
}
if (defPath != null) {
// write the feature
lastDescent = Descent.descend(writer, defPath, lastDescent, false);
writeMember(instance, type, reporter);
} else {
reporter.warn(new IOMessageImpl(MessageFormat.format("No compatible member attribute for type {0} found in root element {1}, one instance was skipped", type.getDisplayName(), containerName.getLocalPart()), null));
}
progress.advance(1);
count++;
long now = System.currentTimeMillis();
// only update every 100 milliseconds
if (now - lastUpdate > 100 || !itInstance.hasNext()) {
lastUpdate = now;
sub.subTask(String.valueOf(count) + " instances");
}
}
if (lastDescent != null) {
lastDescent.close();
}
} finally {
itInstance.close();
}
// FeatureCollection
writer.writeEndElement();
if (documentWrapper != null) {
documentWrapper.endWrap(writer, reporter);
}
writer.writeEndDocument();
writer.close();
reporter.setSuccess(reporter.getErrors().isEmpty());
} catch (Exception e) {
reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
reporter.setSuccess(false);
} finally {
progress.end();
}
}
Aggregations