Search in sources :

Example 1 with TransformationService

use of org.eclipse.smarthome.core.transform.TransformationService in project smarthome by eclipse.

the class ItemUIRegistryImpl method transform.

/*
     * check if there is a status value being displayed on the right side of the
     * label (the right side is signified by being enclosed in square brackets [].
     * If so, check if the value starts with the call to a transformation service
     * (e.g. "[MAP(en.map):%s]") and execute the transformation in this case.
     * If the value does not start with the call to a transformation service,
     * we return the label with the mapped option value if provided (not null).
     */
private String transform(String label, String labelMappedOption) {
    String ret = label;
    if (getFormatPattern(label) != null) {
        Matcher matcher = EXTRACT_TRANSFORMFUNCTION_PATTERN.matcher(label);
        if (matcher.find()) {
            String type = matcher.group(1);
            String pattern = matcher.group(2);
            String value = matcher.group(3);
            TransformationService transformation = TransformationHelper.getTransformationService(UIActivator.getContext(), type);
            if (transformation != null) {
                try {
                    ret = label.substring(0, label.indexOf("[") + 1) + transformation.transform(pattern, value) + "]";
                } catch (TransformationException e) {
                    logger.error("transformation throws exception [transformation={}, value={}]", transformation, value, e);
                    ret = label.substring(0, label.indexOf("[") + 1) + value + "]";
                }
            } else {
                logger.warn("couldn't transform value in label because transformationService of type '{}' is unavailable", type);
                ret = label.substring(0, label.indexOf("[") + 1) + value + "]";
            }
        } else if (labelMappedOption != null) {
            ret = labelMappedOption;
        }
    }
    return ret;
}
Also used : TransformationException(org.eclipse.smarthome.core.transform.TransformationException) Matcher(java.util.regex.Matcher) TransformationService(org.eclipse.smarthome.core.transform.TransformationService)

Example 2 with TransformationService

use of org.eclipse.smarthome.core.transform.TransformationService in project smarthome by eclipse.

the class Transformation method transform.

/**
 * Applies a transformation of a given type with some function to a value.
 *
 * @param type the transformation type, e.g. REGEX or MAP
 * @param function the function to call, this value depends on the transformation type
 * @param value the value to apply the transformation to
 * @return the transformed value or the original one, if there was no service registered for the
 *         given type or a transformation exception occurred.
 */
public static String transform(String type, String function, String value) {
    String result;
    TransformationService service = TransformationHelper.getTransformationService(TransformationActivator.getContext(), type);
    Logger logger = LoggerFactory.getLogger(Transformation.class);
    if (service != null) {
        try {
            result = service.transform(function, value);
        } catch (TransformationException e) {
            logger.error("Error executing the transformation '{}': {}", type, e.getMessage());
            result = value;
        }
    } else {
        logger.warn("No transformation service '{}' could be found.", type);
        result = value;
    }
    return result;
}
Also used : TransformationException(org.eclipse.smarthome.core.transform.TransformationException) TransformationService(org.eclipse.smarthome.core.transform.TransformationService) Logger(org.slf4j.Logger)

Aggregations

TransformationException (org.eclipse.smarthome.core.transform.TransformationException)2 TransformationService (org.eclipse.smarthome.core.transform.TransformationService)2 Matcher (java.util.regex.Matcher)1 Logger (org.slf4j.Logger)1