use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.
the class XsltGenerator method write.
/**
* Generate the XSLT transformation and write it to the given target.
*
* @param target the target output supplier
* @return the report
* @throws Exception if a unrecoverable error occurs during the process
*/
public IOReport write(LocatableOutputSupplier<? extends OutputStream> target) throws Exception {
Template root = ve.getTemplate(Templates.ROOT, "UTF-8");
VelocityContext context = XslTransformationUtil.createStrictVelocityContext();
// project info
context.put("info", ProjectXslInfo.getInfo(projectInfo));
// collects IDs of type cells
Set<String> typeIds = new HashSet<String>();
// type cells
for (Cell typeCell : alignment.getTypeCells()) {
if (typeCell.getTransformationMode() != TransformationMode.disabled) {
// ignore disabled cells
Entity targetEntity = CellUtil.getFirstEntity(typeCell.getTarget());
if (targetEntity != null) {
// assign identifiers for type transformations
String targetName = targetEntity.getDefinition().getDefinition().getName().getLocalPart();
String id = cellIdentifiers.getId(typeCell, targetName);
typeIds.add(id);
} else {
reporter.warn(new IOMessageImpl("Ignoring type relation without target type", null));
}
}
}
// collects IDs of type cells mapped to target element names
Map<String, QName> targetElements = new HashMap<String, QName>();
// container
File container = new File(workDir, "container.xsl");
progress.setCurrentTask("Generating container");
generateContainer(typeIds, container, targetElements);
Set<String> passiveCellIds = new HashSet<String>(typeIds);
progress.setCurrentTask("Generate type transformations");
// all active cells templates
for (Entry<String, QName> entry : targetElements.entrySet()) {
// generate XSL fragments for type transformations
String id = entry.getKey();
QName elementName = entry.getValue();
Cell typeCell = cellIdentifiers.getObject(id);
// this is not a passive cell
passiveCellIds.remove(id);
XmlElement targetElement = targetSchema.getElements().get(elementName);
String filename = "_" + id + ".xsl";
File file = new File(workDir, filename);
includes.add(filename);
generateTypeTransformation(id, targetElement, typeCell, file);
}
// all passive cell templates
for (String passiveId : passiveCellIds) {
Cell typeCell = cellIdentifiers.getObject(passiveId);
String filename = "_" + passiveId + ".xsl";
File file = new File(workDir, filename);
includes.add(filename);
// XXX dummy target element
XmlElement targetElement = new XmlElement(new QName(NS_XSL_DEFINITIONS, "dummy"), null, null);
generateTypeTransformation(passiveId, targetElement, typeCell, file);
// for passive cells no variables should be created
typeIds.remove(passiveId);
}
// namespaces that occur additionally to the fixed namespaces
Map<String, String> additionalNamespaces = new HashMap<String, String>(prefixes.asMap());
for (String fixedPrefix : FIXED_PREFIXES.keySet()) {
additionalNamespaces.remove(fixedPrefix);
}
context.put("additionalNamespaces", additionalNamespaces);
// types cells
/*
* The type identifiers are used as variable name to store the result of
* the equally named template.
*/
context.put("targets", typeIds);
// includes
// TODO check if files to include are actually there?
context.put("includes", includes);
OutputStream out = target.getOutput();
XMLPrettyPrinter printer = new XMLPrettyPrinter(out);
Future<?> ready = printer.start();
Writer writer = new OutputStreamWriter(printer, "UTF-8");
try {
root.merge(context, writer);
writer.flush();
} finally {
writer.close();
ready.get();
out.close();
}
reporter.setSuccess(reporter.getErrors().isEmpty());
return reporter;
}
use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.
the class MathematicalExpressionExplanation method getExplanation.
@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
Entity target = CellUtil.getFirstEntity(cell.getTarget());
String expression = CellUtil.getFirstParameter(cell, MathematicalExpression.PARAMETER_EXPRESSION).as(String.class);
List<? extends Entity> sources = cell.getSource().get(MathematicalExpression.ENTITY_VARIABLE);
if (target != null && expression != null) {
if (html)
expression = "<pre>" + expression + "</pre>";
String explanation = MessageFormat.format(getMessage("main", locale), formatEntity(target, html, true, locale), expression);
if (html)
explanation = explanation.replaceAll("\n", "<br />");
if (html) {
Map<String, String> varToProperty = sources.stream().collect(Collectors.toMap(entity -> {
return getEntityNameWithoutCondition(entity);
}, entity -> {
return formatEntity(entity, true, false, locale);
}));
explanation += buildReplacementTable(varToProperty, locale);
}
return explanation;
}
return null;
}
use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.
the class DefaultAlignmentMigrator method updateAligmment.
@Override
public MutableAlignment updateAligmment(Alignment originalAlignment, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
MutableAlignment result = new DefaultAlignment(originalAlignment);
// XXX TODO adapt custom functions?!
// result.getCustomPropertyFunctions();
Collection<? extends Cell> cellList = new ArrayList<>(result.getCells());
for (Cell cell : cellList) {
// XXX
if (cell instanceof MutableCell) {
CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
MutableCell newCell = cm.updateCell(cell, migration, options, log);
MigrationUtil.removeIdPrefix(newCell, options.transferBase(), options.transferBase());
result.removeCell(cell);
if (newCell != null) {
result.addCell(newCell);
}
} else {
// XXX can we deal with other cases? (Base alignment cells)
if (options.transferBase()) {
// include base alignment cell as mutable mapping cell
CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
MutableCell newCell = cm.updateCell(cell, migration, options, log);
MigrationUtil.removeIdPrefix(newCell, true, true);
result.removeCell(cell);
if (newCell != null) {
result.addCell(newCell);
}
}
}
}
if (options.transferBase()) {
MigrationUtil.removeBaseCells(result);
} else {
// does something need to be done to correctly retain base
// alignments?
}
return result;
}
use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.
the class EffectiveMapping method expand.
/**
* Convert an alignment into an expanded version where all effective
* mappings are defined explicitly, i.e. mappings effective due to
* inheritance are converted to use the respective sub-types. Also, base
* alignment cells are converted into normal cells.
*
* @param alignment the alignment to convert
* @return the expanded copy of the alignment
*/
public static MutableAlignment expand(Alignment alignment) {
MutableAlignment result = new DefaultAlignment(alignment);
// remove base alignment cells keeping custom functions
MigrationUtil.removeBaseCells(result);
// remove other cells
result.clearCells();
// transfer cells based on effective mapping
// set of all cells used as they are in the resulting alignment
Set<Cell> usedAsIs = new HashSet<>();
for (Cell typeCell : alignment.getTypeCells()) {
// transfer type cell unchanged
MutableCell typeCellNew = new DefaultCell(typeCell);
MigrationUtil.removeIdPrefix(typeCellNew, true, true);
result.addCell(typeCellNew);
usedAsIs.add(typeCell);
Collection<? extends Cell> propertyCells = alignment.getPropertyCells(typeCell, true, false);
for (Cell propertyCell : propertyCells) {
// FIXME what does this do in case of a join where there are
// potentially multiple cells to be handled?
Cell reparented = AlignmentUtil.reparentCell(propertyCell, typeCell, true);
if (reparented == propertyCell) {
// use as is
if (!usedAsIs.contains(propertyCell)) {
// only add if not done yet
// transfer unchanged
MutableCell newCell = new DefaultCell(propertyCell);
MigrationUtil.removeIdPrefix(newCell, true, true);
result.addCell(newCell);
usedAsIs.add(propertyCell);
}
} else {
// inherited cell
// add the reparented cell
// TODO check if similar cell has been added already?
MutableCell rCell = (MutableCell) reparented;
MigrationUtil.removeIdPrefix(rCell, true, true);
// avoid ID collision
// no updates needed in other places because it's a property
// cell
rCell.setId(rCell.getId() + '_' + typeCellNew.getId());
result.addCell(rCell);
}
}
}
return result;
}
use of eu.esdihumboldt.hale.common.align.model.Cell in project hale by halestudio.
the class FormattedStringExplanation method getExplanation.
@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
Entity target = CellUtil.getFirstEntity(cell.getTarget());
String pattern = CellUtil.getFirstParameter(cell, PARAMETER_PATTERN).as(String.class);
List<? extends Entity> sources = null;
if (cell.getSource() != null) {
sources = cell.getSource().get(ENTITY_VARIABLE);
}
if (target != null && pattern != null) {
if (html)
pattern = "<pre>" + pattern + "</pre>";
String explanation = MessageFormat.format(getMessage("main", locale), formatEntity(target, html, true, locale), pattern);
if (html)
explanation = explanation.replaceAll("\n", "<br />");
if (sources != null && html) {
Map<String, String> varToProperty = sources.stream().collect(Collectors.toMap(entity -> {
return '{' + getEntityNameWithoutCondition(entity) + '}';
}, entity -> {
return formatEntity(entity, true, false, locale);
}));
explanation += buildReplacementTable(varToProperty, locale);
}
return explanation;
}
return null;
}
Aggregations