The file format is documented [here](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-).
To organize the various transformations one might use subfolders.
-A default value can be provided if no matching entry is found by using "=value" syntax
+A default value can be provided if no matching entry is found by using "=value" syntax.
+Defining this default value using `_source_` would then return the non transformed input string.
+
## Example
| `white space` | `using escape` |
| `anything` | `default` |
+
## Usage as a Profile
The functionality of this `TransformationService` can be used in a `Profile` on an `ItemChannelLink` too.
--- /dev/null
+CLOSED=closed
+OPEN=open
+-=-
+
+# Default mapping for missing keys
+=_source_
"openhab.transform=MAP" })
public class MapTransformationService
implements TransformationService, ConfigOptionProvider, RegistryChangeListener<Transformation> {
- private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
-
+ private static final String SOURCE_VALUE = "_source_";
private static final String PROFILE_CONFIG_URI = "profile:transform:MAP";
private static final String CONFIG_PARAM_FUNCTION = "function";
private static final Set<String> SUPPORTED_CONFIGURATION_TYPES = Set.of("map");
+ private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
private final TransformationRegistry transformationRegistry;
private final Map<String, Properties> cachedTransformations = new ConcurrentHashMap<>();
target = properties.getProperty("");
if (target == null) {
throw new TransformationException("Target value not found in map for '" + source + "'");
+ } else if (SOURCE_VALUE.equals(target)) {
+ target = source;
}
}
private static final String NON_DEFAULTED_TRANSFORMATION_DE = "map" + File.separator + "doorstatus_de.map";
private static final String NON_DEFAULTED_TRANSFORMATION_FR = "map" + File.separator + "doorstatus_fr.map";
private static final String DEFAULTED_TRANSFORMATION = "map" + File.separator + "doorstatus_defaulted.map";
+ private static final String FALLBACK_TRANSFORMATION = "map" + File.separator + "doorstatus_fallback.map";
private static final String UNKNOWN_TRANSFORMATION = "map" + File.separator + "de.map";
private static final String SRC_FOLDER = "conf" + File.separator + "transform";
assertEquals("Default Value", processor.transform(DEFAULTED_TRANSFORMATION, SOURCE_UNKNOWN));
}
+ @Test
+ public void testTransformSucceedsWithFallbackDefault() throws TransformationException {
+ assertEquals(SOURCE_UNKNOWN, processor.transform(FALLBACK_TRANSFORMATION, SOURCE_UNKNOWN));
+ }
+
@Test
public void testTransformFailsOnUnknownTransformation() {
assertThrows(TransformationException.class, () -> processor.transform(UNKNOWN_TRANSFORMATION, SOURCE_CLOSED));
public void setTransformationIsNotUpdatedIfOldElementMissing() throws TransformationException {
// update configuration
Transformation transformationDE = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_DE));
- Transformation transformationFR = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_FR));
Transformation transformationModified = new Transformation(transformationDE.getUID(),
transformationDE.getLabel(), transformationDE.getType(), transformationDE.getConfiguration());
processor.updated(transformationDE, transformationModified);