Search in sources :

Example 11 with TransformationException

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);
    }
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) XExpression(com.iabcinc.jmep.XExpression)

Example 12 with TransformationException

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();
            }
        }
    }
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) XslVariableImpl(eu.esdihumboldt.hale.io.xslt.functions.impl.XslVariableImpl) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) XslVariable(eu.esdihumboldt.hale.io.xslt.functions.XslVariable) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) OutputStreamWriter(java.io.OutputStreamWriter)

Example 13 with TransformationException

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));
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) XslTypeTransformation(eu.esdihumboldt.hale.io.xslt.XslTypeTransformation) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) XMLStreamException(javax.xml.stream.XMLStreamException) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 14 with TransformationException

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);
    }
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 15 with TransformationException

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);
    }
}
Also used : TransformationTreeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TransformationTreeImpl) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) StringWriter(java.io.StringWriter) AbstractTransformationTraverser(eu.esdihumboldt.hale.io.xslt.transformations.base.AbstractTransformationTraverser) TransformationTree(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree) FunctionService(eu.esdihumboldt.hale.common.align.service.FunctionService) AlignmentFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService) TGraphImpl(eu.esdihumboldt.hale.common.align.tgraph.impl.TGraphImpl) AlignmentFunctionService(eu.esdihumboldt.hale.common.align.service.impl.AlignmentFunctionService) TGraph(eu.esdihumboldt.hale.common.align.tgraph.TGraph) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)

Aggregations

TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)30 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)11 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)9 Geometry (com.vividsolutions.jts.geom.Geometry)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)8 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)7 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)7 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)7 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)5 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)4 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)4 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)4 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)4 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)4 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)4 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)4 Binding (groovy.lang.Binding)4 Script (groovy.lang.Script)4