]> git.basschouten.com Git - openhab-addons.git/blob
6e6c466e1ecf096807d873055111ed51ce0c397b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.net.URI;
18 import java.util.Collection;
19 import java.util.Locale;
20 import java.util.Properties;
21 import java.util.stream.Collectors;
22
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;
33
34 /**
35  * <p>
36  * The implementation of {@link TransformationService} which simply maps strings to other strings
37  *
38  * @author Kai Kreuzer - Initial contribution and API
39  * @author GaĆ«l L'hopital - Make it localizable
40  */
41 @NonNullByDefault
42 @Component(service = { TransformationService.class, ConfigOptionProvider.class }, property = {
43         "openhab.transform=MAP" })
44 public class MapTransformationService extends AbstractFileTransformationService<Properties>
45         implements ConfigOptionProvider {
46
47     private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
48
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" };
52
53     /**
54      * <p>
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.
58      *
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.
62      */
63     @Override
64     protected @Nullable String internalTransform(Properties properties, String source) throws TransformationException {
65         String target = properties.getProperty(source);
66
67         if (target == null) {
68             target = properties.getProperty("");
69             if (target == null) {
70                 throw new TransformationException("Target value not found in map for '" + source + "'");
71             }
72         }
73
74         logger.debug("Transformation resulted in '{}'", target);
75         return target;
76     }
77
78     @Override
79     protected Properties internalLoadTransform(String filename) throws TransformationException {
80         Properties result = new Properties();
81         try (FileReader reader = new FileReader(filename)) {
82             result.load(reader);
83             return result;
84         } catch (IOException e) {
85             throw new TransformationException("An error occurred while opening file.", e);
86         }
87     }
88
89     @Override
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())) {
93             switch (param) {
94                 case CONFIG_PARAM_FUNCTION:
95                     return getFilenames(FILE_NAME_EXTENSIONS).stream().map(f -> new ParameterOption(f, f))
96                             .collect(Collectors.toList());
97             }
98         }
99         return null;
100     }
101 }