Search in sources :

Example 1 with XslVariableImpl

use of eu.esdihumboldt.hale.io.xslt.functions.impl.XslVariableImpl 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)

Aggregations

ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)1 Entity (eu.esdihumboldt.hale.common.align.model.Entity)1 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)1 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)1 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 XslVariable (eu.esdihumboldt.hale.io.xslt.functions.XslVariable)1 XslVariableImpl (eu.esdihumboldt.hale.io.xslt.functions.impl.XslVariableImpl)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1