2 * Copyright (c) 2010-2021 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;
17 import java.util.Properties;
19 import org.openhab.core.transform.AbstractFileTransformationService;
20 import org.openhab.core.transform.TransformationException;
21 import org.openhab.core.transform.TransformationService;
22 import org.osgi.service.component.annotations.Component;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
28 * The implementation of {@link TransformationService} which simply maps strings to other strings
30 * @author Kai Kreuzer - Initial contribution and API
31 * @author Gaƫl L'hopital - Make it localizable
33 @Component(service = TransformationService.class, property = { "openhab.transform=MAP" })
34 public class MapTransformationService extends AbstractFileTransformationService<Properties> {
36 private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
40 * Transforms the input <code>source</code> by mapping it to another string. It expects the mappings to be read from
41 * a file which is stored under the 'configurations/transform' folder. This file should be in property syntax, i.e.
42 * simple lines with "key=value" pairs. To organize the various transformations one might use subfolders.
44 * @param properties the list of properties which contains the key value pairs for the mapping.
45 * @param source the input to transform
48 protected String internalTransform(Properties properties, String source) throws TransformationException {
49 String target = properties.getProperty(source);
52 target = properties.getProperty("");
54 throw new TransformationException("Target value not found in map for '" + source + "'");
58 logger.debug("Transformation resulted in '{}'", target);
63 protected Properties internalLoadTransform(String filename) throws TransformationException {
64 Properties result = new Properties();
65 try (FileReader reader = new FileReader(filename)) {
68 } catch (IOException e) {
69 throw new TransformationException("An error occurred while opening file.", e);