Search in sources :

Example 21 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class GroovyUtil method getScriptString.

/**
 * Get the script string.
 *
 * @param function the transformation function the script is associated to
 * @return the script string
 * @throws TransformationException if getting the script parameter from the
 *             function fails
 */
public static String getScriptString(AbstractTransformationFunction<?> function) throws TransformationException {
    ParameterValue scriptValue = function.getParameterChecked(PARAMETER_SCRIPT);
    String script;
    // try retrieving as text
    Text text = scriptValue.as(Text.class);
    if (text != null) {
        script = text.getText();
    } else {
        // fall back to string value
        script = scriptValue.as(String.class);
    }
    return script;
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text)

Example 22 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class GeographicalName 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, NoResultException {
    // list of all source properties
    List<PropertyValue> inputs = variables.get(null);
    if (inputs.isEmpty()) {
        // no input, so don't create any structure
        throw new NoResultException();
    }
    // get all parameters defined by the wizard page
    String ipa = getParameterChecked(PROPERTY_PRONUNCIATIONIPA).as(String.class);
    // we need a default value and a try/catch-block because in older
    // version we couldn't edit the pronunciationSoundLink text field
    String sound = "";
    try {
        sound = getParameterChecked(PROPERTY_PRONUNCIATIONSOUNDLINK).as(String.class);
    } catch (Exception e) {
    // do nothing
    }
    String language = getParameterChecked(PROPERTY_LANGUAGE).as(String.class);
    String sourceOfName = getParameterChecked(PROPERTY_SOURCEOFNAME).as(String.class);
    String nameStatus = getParameterChecked(PROPERTY_NAMESTATUS).as(String.class);
    String nativeness = getParameterChecked(PROPERTY_NATIVENESS).as(String.class);
    String gender = getParameterChecked(PROPERTY_GRAMMA_GENDER).as(String.class);
    String number = getParameterChecked(PROPERTY_GRAMMA_NUMBER).as(String.class);
    // get the script and transliteration parameters
    // should have the same order like source properties
    ListMultimap<String, ParameterValue> params = getParameters();
    List<ParameterValue> scripts = params.get(PROPERTY_SCRIPT);
    List<ParameterValue> trans = params.get(PROPERTY_TRANSLITERATION);
    if (inputs.size() != scripts.size() || inputs.size() != trans.size()) {
        throw new TransformationException("Number of inputs does not match number of configured spellings, can't determine script and transliteration of spellings.");
    // XXX could this be dealt with if the property "text" would be used
    // again for this purpose?
    }
    // definition of the target property
    TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
    // instance that can be changed (add property/instance as child)
    DefaultInstance targetInstance = new DefaultInstance(targetType, null);
    // search for the child named "GeographicalName"
    PropertyDefinition targetChildGeoName = Util.getChild("GeographicalName", targetType);
    // get type definition to create the "GeographicalName" instance
    TypeDefinition geoType = targetChildGeoName.getPropertyType();
    // name/GeographicalName/
    DefaultInstance geoInstance = new DefaultInstance(geoType, null);
    targetInstance.addProperty(targetChildGeoName.getName(), geoInstance);
    // name/GeographicalName/grammaticalGender/
    if (gender != null && !gender.isEmpty()) {
        PropertyDefinition geoChildGramGender = Util.getChild("grammaticalGender", geoType);
        TypeDefinition grammarGenderType = geoChildGramGender.getPropertyType();
        DefaultInstance grammarGenderInst = new DefaultInstance(grammarGenderType, null);
        grammarGenderInst.setValue(gender);
        geoInstance.addProperty(geoChildGramGender.getName(), grammarGenderInst);
    }
    // name/GeographicalName/grammaticalNumber
    if (number != null && !number.isEmpty()) {
        PropertyDefinition geoChildGramNumber = Util.getChild("grammaticalNumber", geoType);
        TypeDefinition grammarNumberType = geoChildGramNumber.getPropertyType();
        DefaultInstance grammarNumberInst = new DefaultInstance(grammarNumberType, null);
        // set value of the grammaticalNumber instance
        grammarNumberInst.setValue(number);
        geoInstance.addProperty(geoChildGramNumber.getName(), grammarNumberInst);
    }
    // name/GeographicalName/language
    if (language != null && !language.isEmpty()) {
        PropertyDefinition geoChildLanguage = Util.getChild("language", geoType);
        TypeDefinition languageType = geoChildLanguage.getPropertyType();
        DefaultInstance languageInstance = new DefaultInstance(languageType, null);
        // set value of the language instance
        languageInstance.setValue(language);
        geoInstance.addProperty(geoChildLanguage.getName(), languageInstance);
    }
    // name/GeographicalName/nameStatus
    if (nameStatus != null && !nameStatus.isEmpty()) {
        PropertyDefinition geoChildNameStatus = Util.getChild("nameStatus", geoType);
        TypeDefinition nameStatusType = geoChildNameStatus.getPropertyType();
        DefaultInstance nameStatusInstance = new DefaultInstance(nameStatusType, null);
        // set value of the nameStatus instance
        nameStatusInstance.setValue(nameStatus);
        geoInstance.addProperty(geoChildNameStatus.getName(), nameStatusInstance);
    }
    // name/GeographicalName/nativeness
    if (nativeness != null && !nativeness.isEmpty()) {
        PropertyDefinition geoChildNativeness = Util.getChild("nativeness", geoType);
        TypeDefinition nativenessType = geoChildNativeness.getPropertyType();
        DefaultInstance nativenessInstance = new DefaultInstance(nativenessType, null);
        // set value of the nativeness instance
        nativenessInstance.setValue(nativeness);
        geoInstance.addProperty(geoChildNativeness.getName(), nativenessInstance);
    }
    if ((ipa != null && !ipa.isEmpty()) || (sound != null && !sound.isEmpty())) {
        // name/GeographicalName/pronunciation
        PropertyDefinition geoChildPronun = Util.getChild("pronunciation", geoType);
        TypeDefinition pronunType = geoChildPronun.getPropertyType();
        DefaultInstance pronunInstance = new DefaultInstance(pronunType, null);
        geoInstance.addProperty(geoChildPronun.getName(), pronunInstance);
        // name/GeographicalName/pronunciation/PronunciationOfName
        PropertyDefinition pronunChildPronOfName = Util.getChild("PronunciationOfName", pronunType);
        TypeDefinition pronOfNameType = pronunChildPronOfName.getPropertyType();
        DefaultInstance pronOfNameInst = new DefaultInstance(pronOfNameType, null);
        pronunInstance.addProperty(pronunChildPronOfName.getName(), pronOfNameInst);
        if (ipa != null && !ipa.isEmpty()) {
            // name/GeographicalName/pronunciation/PronunciationOfName/pronunciationIPA
            PropertyDefinition pronOfNameChildIPA = Util.getChild("pronunciationIPA", pronOfNameType);
            TypeDefinition pronunIpaType = pronOfNameChildIPA.getPropertyType();
            DefaultInstance pronunIpaInstance = new DefaultInstance(pronunIpaType, null);
            pronunIpaInstance.setValue(ipa);
            pronOfNameInst.addProperty(pronOfNameChildIPA.getName(), pronunIpaInstance);
        }
        if (sound != null && !sound.isEmpty()) {
            // name/GeographicalName/pronunciation/PronunciationOfName/pronunciationSoundLink
            PropertyDefinition pronOfNameChildSound = Util.getChild("pronunciationSoundLink", pronOfNameType);
            TypeDefinition pronunSoundType = pronOfNameChildSound.getPropertyType();
            DefaultInstance pronunSoundInstance = new DefaultInstance(pronunSoundType, null);
            pronunSoundInstance.setValue(sound);
            pronOfNameInst.addProperty(pronOfNameChildSound.getName(), pronunSoundInstance);
        }
    }
    // name/GeographicalName/sourceOfName
    if (sourceOfName != null && !sourceOfName.isEmpty()) {
        PropertyDefinition geoChildSource = Util.getChild("sourceOfName", geoType);
        TypeDefinition sourceType = geoChildSource.getPropertyType();
        DefaultInstance sourceInstance = new DefaultInstance(sourceType, null);
        // set value of the sourceOfName instance
        sourceInstance.setValue(sourceOfName);
        geoInstance.addProperty(geoChildSource.getName(), sourceInstance);
    }
    // name/GeographicalName/spelling
    PropertyDefinition geoChildSpelling = Util.getChild("spelling", geoType);
    TypeDefinition spellingType = geoChildSpelling.getPropertyType();
    // name/GeographicalName/spelling/SpellingOfName
    PropertyDefinition spellingChildSpellOfName = Util.getChild("SpellingOfName", spellingType);
    TypeDefinition spellOfNameType = spellingChildSpellOfName.getPropertyType();
    // create a "spelling" instance for each spelling
    for (int i = 0; i < scripts.size(); i++) {
        DefaultInstance spellingInstance = new DefaultInstance(spellingType, null);
        DefaultInstance spellOfNameInst = new DefaultInstance(spellOfNameType, null);
        // name/GeographicalName/spelling/SpellingOfName/script
        PropertyDefinition spellOfNameChildScript = Util.getChild("script", spellOfNameType);
        TypeDefinition scriptType = spellOfNameChildScript.getPropertyType();
        DefaultInstance scriptInstance = new DefaultInstance(scriptType, null);
        // name/GeographicalName/spelling/SpellingOfName/text
        PropertyDefinition spellOfNameChildText = Util.getChild("text", spellOfNameType);
        // name/GeographicalName/spelling/SpellingOfName/transliterationScheme
        PropertyDefinition spellOfNameChildTransliteration = Util.getChild("transliterationScheme", spellOfNameType);
        TypeDefinition transliterationType = spellOfNameChildTransliteration.getPropertyType();
        DefaultInstance transliterationInstance = new DefaultInstance(transliterationType, null);
        // build the spelling instance
        scriptInstance.setValue(scripts.get(i).as(String.class));
        transliterationInstance.setValue(trans.get(i).as(String.class));
        spellOfNameInst.addProperty(spellOfNameChildScript.getName(), scriptInstance);
        // set text value from inputs
        spellOfNameInst.addProperty(spellOfNameChildText.getName(), inputs.get(i).getValue());
        spellOfNameInst.addProperty(spellOfNameChildTransliteration.getName(), transliterationInstance);
        spellingInstance.addProperty(spellingChildSpellOfName.getName(), spellOfNameInst);
        geoInstance.addProperty(geoChildSpelling.getName(), spellingInstance);
    }
    return targetInstance;
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)

Example 23 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class GeographicalNameExplanation method getExplanation.

@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
    // only one locale supported in this explanation (the function is
    // deprecated)
    Locale targetLocale = Locale.ENGLISH;
    Entity target = CellUtil.getFirstEntity(cell.getTarget());
    PropertyFunctionDefinition function = FunctionUtil.getPropertyFunction(ID, null);
    StringBuilder sb = new StringBuilder();
    sb.append("The {0} property is populated with an Inspire Geographical Name composed as follows:");
    addLineBreak(sb, html);
    addLineBreak(sb, html);
    // unique parameters
    if (html)
        sb.append("<ul>");
    addOptionalParameter(sb, cell, PROPERTY_NAMESTATUS, function, html);
    addOptionalParameter(sb, cell, PROPERTY_LANGUAGE, function, html);
    addOptionalParameter(sb, cell, PROPERTY_NATIVENESS, function, html);
    addOptionalParameter(sb, cell, PROPERTY_SOURCEOFNAME, function, html);
    addOptionalParameter(sb, cell, PROPERTY_PRONUNCIATIONIPA, function, html);
    addOptionalParameter(sb, cell, PROPERTY_PRONUNCIATIONSOUNDLINK, function, html);
    addOptionalParameter(sb, cell, PROPERTY_GRAMMA_GENDER, function, html);
    addOptionalParameter(sb, cell, PROPERTY_GRAMMA_NUMBER, function, html);
    if (html)
        sb.append("</ul>");
    addLineBreak(sb, html);
    // per source parameters
    List<? extends Entity> sources = cell.getSource().get(null);
    // PROPERTY_TEXT
    List<ParameterValue> scripts = cell.getTransformationParameters().get(PROPERTY_SCRIPT);
    List<ParameterValue> transs = cell.getTransformationParameters().get(PROPERTY_TRANSLITERATION);
    if (!sources.isEmpty()) {
        sb.append("For each source property a spelling is created, the spelling text is the value of the source property.");
        addLineBreak(sb, html);
        if (html) {
            sb.append("<table border=\"1\">");
            sb.append("<tr><th>Source property</th><th>Script</th><th>Transliteration</th></tr>");
        }
        int index = 0;
        for (Entity source : sources) {
            String script = (index < scripts.size()) ? (scripts.get(index).as(String.class)) : (null);
            String trans = (index < transs.size()) ? (transs.get(index).as(String.class)) : (null);
            if (html) {
                sb.append("<tr>");
                sb.append("<td>");
                sb.append(formatEntity(source, html, false, targetLocale));
                sb.append("</td>");
                sb.append("<td>");
                if (script != null) {
                    sb.append(script);
                }
                sb.append("</td>");
                sb.append("<td>");
                if (trans != null) {
                    sb.append(trans);
                }
                sb.append("</td>");
                sb.append("</tr>");
            } else {
                sb.append("Source: ");
                sb.append(formatEntity(source, html, false, targetLocale));
                addLineBreak(sb, html);
                if (script != null && !script.isEmpty()) {
                    sb.append("  Script: ");
                    sb.append(script);
                    addLineBreak(sb, html);
                }
                if (trans != null && !trans.isEmpty()) {
                    sb.append("  Transliteration: ");
                    sb.append(trans);
                    addLineBreak(sb, html);
                }
                addLineBreak(sb, html);
            }
            index++;
        }
        if (html) {
            sb.append("</table>");
        }
    }
    String result = sb.toString();
    if (target != null) {
        result = MessageFormat.format(result, formatEntity(target, html, true, targetLocale));
    }
    return result;
}
Also used : Locale(java.util.Locale) Entity(eu.esdihumboldt.hale.common.align.model.Entity) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) PropertyFunctionDefinition(eu.esdihumboldt.hale.common.align.extension.function.PropertyFunctionDefinition)

Example 24 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue 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 25 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class CityGMLXsltExport method init.

@Override
protected void init(XmlIndex sourceIndex, XmlIndex targetIndex) throws IOProviderConfigurationException {
    super.init(sourceIndex, targetIndex);
    // scan target schema for CityModel
    targetCityModel = findCityModel(targetIndex);
    if (targetCityModel != null) {
        QName name = targetCityModel.getName();
        setParameter(PARAM_ROOT_ELEMENT_NAMESPACE, new ParameterValue(name.getNamespaceURI()));
        setParameter(PARAM_ROOT_ELEMENT_NAME, new ParameterValue(name.getLocalPart()));
    } else {
        throw new IOProviderConfigurationException(MessageFormat.format("Element {0} not found in the target schema.", CITY_MODEL_ELEMENT));
    }
    // scan source schema for CityModel
    sourceCityModel = findCityModel(sourceIndex);
    if (sourceCityModel != null) {
        // create a custom source context
        sourceContext = new CityGMLSourceContext(sourceCityModel);
    // TODO copy envelope?
    }
}
Also used : IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) QName(javax.xml.namespace.QName)

Aggregations

ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)80 Test (org.junit.Test)29 Cell (eu.esdihumboldt.hale.common.align.model.Cell)28 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)21 AttributeMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType)14 AppSchemaMappingContext (eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)13 ArrayList (java.util.ArrayList)13 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)9 Property (eu.esdihumboldt.hale.common.align.model.Property)9 ClientProperty (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)9 Entity (eu.esdihumboldt.hale.common.align.model.Entity)8 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)8 Value (eu.esdihumboldt.hale.common.core.io.Value)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)7 Type (eu.esdihumboldt.hale.common.align.model.Type)6 AssignHandler (eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler)6 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)5 HashSet (java.util.HashSet)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4