use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType in project hale by halestudio.
the class TemplateProject method onSuccess.
@Override
protected void onSuccess(Void context, String projectId, File projectFile, Project project, ReportFile reportFile) {
super.onSuccess(context, projectId, projectFile, project, reportFile);
// update locations in project file
LocationUpdater updater = new LocationUpdater(project, projectFile.toURI());
updater.updateProject(false);
resources.clear();
List<Path> invalidSources = new ArrayList<>();
Path projectFolder = getProjectFolder().toPath();
// validate resources
for (IOConfiguration config : project.getResources()) {
Resource resource = new IOConfigurationResource(config, getProjectFile().toURI());
// check if file URIs are valid and inside project folder
URI source = resource.getSource();
if (source != null) {
Path path = null;
if (source.getScheme() == null) {
// is a relative URI
path = projectFile.toPath().resolve(source.toString()).normalize();
} else if ("file".equals(source.getScheme())) {
// is a file URI
path = Paths.get(source).normalize();
}
if (path != null) {
if (!path.startsWith(projectFolder) || !Files.exists(path)) {
// invalid source
invalidSources.add(path);
}
}
}
resources.put(resource.getActionId(), resource);
}
valid = invalidSources.isEmpty();
if (!valid) {
StringBuilder builder = new StringBuilder("Files referenced by the project could not be found: ");
for (int i = 0; i < invalidSources.size(); i++) {
if (i > 0)
builder.append(", ");
Path path = invalidSources.get(i);
builder.append(path.getFileName().toString());
}
notValidMessage = builder.toString();
} else {
notValidMessage = "";
}
// additionally, try to find out cell count
definedRelations = 0;
// check if default alignment file exists
try {
File defAlignmentFile = new File(URI.create(projectFile.toURI().toASCIIString() + "." + AlignmentIO.PROJECT_FILE_ALIGNMENT));
if (defAlignmentFile.exists()) {
try (InputStream in = new BufferedInputStream(new FileInputStream(defAlignmentFile))) {
/*
* Try loading the file with JAXB - only supports 2.6+
* projects.
*/
AlignmentType alignment = JaxbToAlignment.load(in, null);
// XXX ignoring base alignments
int count = 0;
for (Object element : alignment.getCellOrModifier()) {
if (element instanceof CellType) {
count++;
}
}
definedRelations = count;
} catch (Exception e) {
// ignore
}
}
} catch (Exception e) {
// ignore
}
}
use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType in project hale by halestudio.
the class JaxbAlignmentIO method printCell.
/**
* Print a cell to an output stream (intended for tests/debugging).
*
* @param cell the cell to print
* @param out the output stream
* @throws Exception if an error occurs trying to print the cell
*/
public static void printCell(MutableCell cell, OutputStream out) throws Exception {
DefaultAlignment alignment = new DefaultAlignment();
alignment.addCell(cell);
IOReporter reporter = new DefaultIOReporter(new Locatable() {
@Override
public URI getLocation() {
return null;
}
}, "Print cell", null, false);
PathUpdate pathUpdate = new PathUpdate(null, null);
AlignmentType at = convert(alignment, reporter, pathUpdate);
CellType ct = (CellType) at.getCellOrModifier().get(0);
JAXBContext jc = JAXBContext.newInstance(ALIGNMENT_CONTEXT, ObjectFactory.class.getClassLoader());
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
ObjectFactory of = new ObjectFactory();
try {
m.marshal(of.createCell(ct), out);
} finally {
out.flush();
out.close();
}
}
use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType in project hale by halestudio.
the class JaxbToAlignment method createUnmigratedCell.
private static UnmigratedCell createUnmigratedCell(CellType cell, LoadAlignmentContext context, IOReporter reporter, EntityResolver resolver, ServiceProvider serviceProvider) {
// The sourceCell represents the cell as it was imported from the
// XML alignment. The conversion to the resolved cell must be performed
// later by migrating the UnmigratedCell returned from this function.
final DefaultCell sourceCell = new DefaultCell();
sourceCell.setTransformationIdentifier(cell.getRelation());
final FunctionDefinition<?> cellFunction = FunctionUtil.getFunction(sourceCell.getTransformationIdentifier(), serviceProvider);
final CellMigrator migrator;
if (cellFunction != null) {
migrator = cellFunction.getCustomMigrator().orElse(new DefaultCellMigrator());
} else {
migrator = new DefaultCellMigrator();
}
Map<EntityDefinition, EntityDefinition> mappings = new HashMap<>();
try {
// The returned Entity pair consists of
// (1st) a dummy entity representing the entity read from JAXB
// (2nd) the resolved entity
ListMultimap<String, Pair<Entity, Entity>> convertedSourceEntities = convertEntities(cell.getSource(), context.getSourceTypes(), SchemaSpaceID.SOURCE, resolver);
if (convertedSourceEntities == null) {
sourceCell.setSource(null);
} else {
sourceCell.setSource(Multimaps.transformValues(convertedSourceEntities, pair -> pair.getFirst()));
for (Pair<Entity, Entity> pair : convertedSourceEntities.values()) {
mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
}
}
ListMultimap<String, Pair<Entity, Entity>> convertedTargetEntities = convertEntities(cell.getTarget(), context.getTargetTypes(), SchemaSpaceID.TARGET, resolver);
if (convertedTargetEntities == null) {
sourceCell.setTarget(null);
} else {
sourceCell.setTarget(Multimaps.transformValues(convertedTargetEntities, pair -> pair.getFirst()));
for (Pair<Entity, Entity> pair : convertedTargetEntities.values()) {
mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
}
}
if (sourceCell.getTarget() == null || sourceCell.getTarget().isEmpty()) {
// target is mandatory for cells!
throw new IllegalStateException("Cannot create cell without target");
}
} catch (Exception e) {
if (reporter != null) {
reporter.error(new IOMessageImpl("Could not create cell", e));
}
return null;
}
if (!cell.getAbstractParameter().isEmpty()) {
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
for (JAXBElement<? extends AbstractParameterType> param : cell.getAbstractParameter()) {
AbstractParameterType apt = param.getValue();
if (apt instanceof ParameterType) {
// treat string parameters or null parameters
ParameterType pt = (ParameterType) apt;
ParameterValue pv = new ParameterValue(pt.getType(), Value.of(pt.getValue()));
parameters.put(pt.getName(), pv);
} else if (apt instanceof ComplexParameterType) {
// complex parameters
ComplexParameterType cpt = (ComplexParameterType) apt;
parameters.put(cpt.getName(), new ParameterValue(new ElementValue(cpt.getAny(), context)));
} else
throw new IllegalStateException("Illegal parameter type");
}
sourceCell.setTransformationParameters(parameters);
}
// annotations & documentation
for (Object element : cell.getDocumentationOrAnnotation()) {
if (element instanceof AnnotationType) {
// add annotation to the cell
AnnotationType annot = (AnnotationType) element;
// but first load it from the DOM
AnnotationDescriptor<?> desc = AnnotationExtension.getInstance().get(annot.getType());
if (desc != null) {
try {
Object value = desc.fromDOM(annot.getAny(), null);
sourceCell.addAnnotation(annot.getType(), value);
} catch (Exception e) {
if (reporter != null) {
reporter.error(new IOMessageImpl("Error loading cell annotation", e));
} else
throw new IllegalStateException("Error loading cell annotation", e);
}
} else
reporter.error(new IOMessageImpl("Cell annotation of type {0} unknown, cannot load the annotation object", null, -1, -1, annot.getType()));
} else if (element instanceof DocumentationType) {
// add documentation to the cell
DocumentationType doc = (DocumentationType) element;
sourceCell.getDocumentation().put(doc.getType(), doc.getValue());
}
}
sourceCell.setId(cell.getId());
// a default value is assured for priority
String priorityStr = cell.getPriority().value();
Priority priority = Priority.fromValue(priorityStr);
if (priority != null) {
sourceCell.setPriority(priority);
} else {
// used.
throw new IllegalArgumentException();
}
return new UnmigratedCell(sourceCell, migrator, mappings);
}
Aggregations