]> git.basschouten.com Git - openhab-addons.git/blob
342a116fe7abe4bbab2d6be52ca7cca0868b6462
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.map.internal;
14
15 import java.io.FileReader;
16 import java.io.IOException;
17 import java.util.Properties;
18
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;
25
26 /**
27  * <p>
28  * The implementation of {@link TransformationService} which simply maps strings to other strings
29  *
30  * @author Kai Kreuzer - Initial contribution and API
31  * @author GaĆ«l L'hopital - Make it localizable
32  */
33 @Component(service = TransformationService.class, property = { "smarthome.transform=MAP" })
34 public class MapTransformationService extends AbstractFileTransformationService<Properties> {
35
36     private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
37
38     /**
39      * <p>
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.
43      *
44      * @param properties the list of properties which contains the key value pairs for the mapping.
45      * @param source the input to transform
46      */
47     @Override
48     protected String internalTransform(Properties properties, String source) throws TransformationException {
49         String target = properties.getProperty(source);
50
51         if (target == null) {
52             target = properties.getProperty("");
53             if (target == null) {
54                 throw new TransformationException("Target value not found in map for '" + source + "'");
55             }
56         }
57
58         logger.debug("Transformation resulted in '{}'", target);
59         return target;
60     }
61
62     @Override
63     protected Properties internalLoadTransform(String filename) throws TransformationException {
64         Properties result = new Properties();
65         try (FileReader reader = new FileReader(filename)) {
66             result.load(reader);
67             return result;
68         } catch (IOException e) {
69             throw new TransformationException("An error occurred while opening file.", e);
70         }
71     }
72 }