2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.map.internal;
15 import java.io.FileReader;
16 import java.io.IOException;
18 import java.util.Collection;
19 import java.util.Locale;
20 import java.util.Properties;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.config.core.ConfigOptionProvider;
26 import org.openhab.core.config.core.ParameterOption;
27 import org.openhab.core.transform.AbstractFileTransformationService;
28 import org.openhab.core.transform.TransformationException;
29 import org.openhab.core.transform.TransformationService;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 * The implementation of {@link TransformationService} which simply maps strings to other strings
38 * @author Kai Kreuzer - Initial contribution and API
39 * @author Gaƫl L'hopital - Make it localizable
42 @Component(service = { TransformationService.class, ConfigOptionProvider.class }, property = {
43 "openhab.transform=MAP" })
44 public class MapTransformationService extends AbstractFileTransformationService<Properties>
45 implements ConfigOptionProvider {
47 private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
49 private static final String PROFILE_CONFIG_URI = "profile:transform:MAP";
50 private static final String CONFIG_PARAM_FUNCTION = "function";
51 private static final String[] FILE_NAME_EXTENSIONS = { "map" };
55 * Transforms the input <code>source</code> by mapping it to another string. It expects the mappings to be read from
56 * a file which is stored under the 'configurations/transform' folder. This file should be in property syntax, i.e.
57 * simple lines with "key=value" pairs. To organize the various transformations one might use subfolders.
59 * @param properties the list of properties which contains the key value pairs for the mapping.
60 * @param source the input to transform
61 * @return the transformed result or null if the transformation couldn't be completed for any reason.
64 protected @Nullable String internalTransform(Properties properties, String source) throws TransformationException {
65 String target = properties.getProperty(source);
68 target = properties.getProperty("");
70 throw new TransformationException("Target value not found in map for '" + source + "'");
74 logger.debug("Transformation resulted in '{}'", target);
79 protected Properties internalLoadTransform(String filename) throws TransformationException {
80 Properties result = new Properties();
81 try (FileReader reader = new FileReader(filename)) {
84 } catch (IOException e) {
85 throw new TransformationException("An error occurred while opening file.", e);
90 public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String param, @Nullable String context,
91 @Nullable Locale locale) {
92 if (PROFILE_CONFIG_URI.equals(uri.toString())) {
94 case CONFIG_PARAM_FUNCTION:
95 return getFilenames(FILE_NAME_EXTENSIONS).stream().map(f -> new ParameterOption(f, f))
96 .collect(Collectors.toList());