use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class MathematicalExpression method evaluate.
/**
* @see AbstractSingleTargetPropertyTransformation#evaluate(String,
* TransformationEngine, ListMultimap, String,
* PropertyEntityDefinition, Map, TransformationLog)
*/
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
// get the mathematical expression
String expression = getParameterChecked(PARAMETER_EXPRESSION).as(String.class);
// replace transformation variables in expression
expression = getExecutionContext().getVariables().replaceVariables(expression);
List<PropertyValue> vars = variables.get(ENTITY_VARIABLE);
try {
return evaluateExpression(expression, vars);
} catch (XExpression e) {
throw new TransformationException("Error evaluating the cell expression", e);
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class CityGMLXsltExport method writeContainerIntro.
@Override
protected void writeContainerIntro(XMLStreamWriter writer, XsltGenerationContext context) throws XMLStreamException, IOException {
if (targetCityModel != null && sourceCityModel != null) {
// copy GML boundedBy
// do it in a special template
String template = context.reserveTemplateName("copyBoundedBy");
writer.writeStartElement(NS_URI_XSL, "call-template");
writer.writeAttribute("name", template);
writer.writeEndElement();
// find source property
PropertyDefinition sourceBB = null;
for (ChildDefinition<?> child : sourceCityModel.getType().getChildren()) {
if (child.asProperty() != null && child.getName().getLocalPart().equals("boundedBy") && child.getName().getNamespaceURI().startsWith(GML_NAMESPACE_CORE)) {
sourceBB = child.asProperty();
break;
}
}
// find target property
PropertyDefinition targetBB = null;
for (ChildDefinition<?> child : targetCityModel.getType().getChildren()) {
if (child.asProperty() != null && child.getName().getLocalPart().equals("boundedBy") && child.getName().getNamespaceURI().startsWith(GML_NAMESPACE_CORE)) {
targetBB = child.asProperty();
break;
}
}
if (sourceBB != null && targetBB != null) {
// create templated
OutputStreamWriter out = new OutputStreamWriter(context.addInclude().openBufferedStream(), getCharset());
try {
out.write("<xsl:template name=\"" + template + "\">");
StringBuilder selectSource = new StringBuilder();
selectSource.append('/');
selectSource.append(GroovyXslHelpers.asPrefixedName(sourceCityModel.getName(), context));
selectSource.append('/');
selectSource.append(GroovyXslHelpers.asPrefixedName(sourceBB.getName(), context));
selectSource.append("[1]");
out.write("<xsl:for-each select=\"" + selectSource.toString() + "\">");
String elementName = GroovyXslHelpers.asPrefixedName(targetBB.getName(), context);
out.write("<" + elementName + ">");
// create bogus rename cell
DefaultCell cell = new DefaultCell();
// source
ListMultimap<String, Entity> source = ArrayListMultimap.create();
List<ChildContext> sourcePath = new ArrayList<ChildContext>();
sourcePath.add(new ChildContext(sourceBB));
PropertyEntityDefinition sourceDef = new PropertyEntityDefinition(sourceCityModel.getType(), sourcePath, SchemaSpaceID.SOURCE, null);
source.put(null, new DefaultProperty(sourceDef));
cell.setSource(source);
// target
ListMultimap<String, Entity> target = ArrayListMultimap.create();
List<ChildContext> targetPath = new ArrayList<ChildContext>();
targetPath.add(new ChildContext(targetBB));
PropertyEntityDefinition targetDef = new PropertyEntityDefinition(targetCityModel.getType(), targetPath, SchemaSpaceID.TARGET, null);
target.put(null, new DefaultProperty(targetDef));
cell.setTarget(target);
// parameters
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
parameters.put(RenameFunction.PARAMETER_STRUCTURAL_RENAME, new ParameterValue("true"));
parameters.put(RenameFunction.PARAMETER_IGNORE_NAMESPACES, new ParameterValue("true"));
cell.setTransformationParameters(parameters);
// variables
ListMultimap<String, XslVariable> variables = ArrayListMultimap.create();
variables.put(null, new XslVariableImpl(sourceDef, "."));
try {
out.write(context.getPropertyTransformation(RenameFunction.ID).selectFunction(cell).getSequence(cell, variables, context, null));
} catch (TransformationException e) {
throw new IllegalStateException("Failed to create template for boundedBy copy.", e);
}
out.write("</" + elementName + ">");
out.write("</xsl:for-each>");
out.write("</xsl:template>");
} finally {
out.close();
}
}
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class XsltGenerator method generateTypeTransformation.
/**
* Generate a XSL fragment for transformation based on the given type
* relation.
*
* @param templateName name of the XSL template
* @param typeCell the type relation
* @param targetfile the target file to write the fragment to
* @param targetElement the target element to use to hold a transformed
* instance
* @throws TransformationException if an unrecoverable error occurs during
* the XSLT transformation generation
*/
protected void generateTypeTransformation(String templateName, XmlElement targetElement, Cell typeCell, File targetfile) throws TransformationException {
XslTypeTransformation xslt;
try {
xslt = XslTypeTransformationExtension.getInstance().getTransformation(typeCell.getTransformationIdentifier());
} catch (Exception e) {
throw new TransformationException("Could not retrieve XSLT transformation generator for cell function", e);
}
xslt.setContext(context);
xslt.generateTemplate(templateName, targetElement, typeCell, new FileIOSupplier(targetfile));
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class AbstractVelocityXslTransformation method writeTemplate.
/**
* Merge the velocity template with the context and write it to the given
* output supplier.
*
* @param template the velocity template
* @param context the velocity context
* @param out the output supplier
* @throws TransformationException if merging or writing the template fails
*/
protected void writeTemplate(Template template, VelocityContext context, LocatableOutputSupplier<? extends OutputStream> out) throws TransformationException {
try {
OutputStream outStream = out.getOutput();
Writer writer = new OutputStreamWriter(outStream, "UTF-8");
try {
template.merge(context, writer);
writer.flush();
} finally {
writer.close();
}
} catch (IOException e) {
throw new TransformationException("Merging the transformation template failed", e);
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class XslRetype method createPropertiesFragment.
/**
* Create a XSL fragment for populating the properties of a target instance.
*
* @param typeCell the associated type cell
* @return the XSL fragment
* @throws TransformationException if creating the fragment failed
*/
protected String createPropertiesFragment(final Cell typeCell) throws TransformationException {
final TransformationTree tree = new TransformationTreeImpl(context().getAlignment(), typeCell);
FunctionService functionService = new AlignmentFunctionService(context().getAlignment());
/*
* Create the transformation graph derived from the transformation tree
* and perform context matching.
*/
/*
* XXX proxying multi-result nodes omitted for now, see
* proxyMultiResultNodes imlementation
*/
// final TGraph graph = new TGraphImpl(tree).proxyMultiResultNodes().performContextMatching();
final TGraph graph = new TGraphImpl(tree, functionService).performContextMatching();
// TODO tree as GraphML as informative annotation into XSLT?
try {
StringWriter propsOut = new StringWriter();
try {
AbstractTransformationTraverser trav = new RetypeTraverser(context(), propsOut, typeCell);
trav.traverse(graph);
} finally {
propsOut.close();
}
return propsOut.toString();
} catch (Exception e) {
throw new TransformationException("Failed to create property transformations", e);
}
}
Aggregations