2 * Copyright (c) 2010-2023 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.xslt.internal.profiles;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.StringType;
17 import org.openhab.core.thing.profiles.ProfileCallback;
18 import org.openhab.core.thing.profiles.ProfileContext;
19 import org.openhab.core.thing.profiles.ProfileTypeUID;
20 import org.openhab.core.thing.profiles.StateProfile;
21 import org.openhab.core.transform.TransformationException;
22 import org.openhab.core.transform.TransformationHelper;
23 import org.openhab.core.transform.TransformationService;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Profile to offer the XsltTransformationservice on an ItemChannelLink
33 * @author Stefan Triller - initial contribution
37 public class XSLTTransformationProfile implements StateProfile {
39 public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
40 TransformationService.TRANSFORM_PROFILE_SCOPE, "XSLT");
42 private final Logger logger = LoggerFactory.getLogger(XSLTTransformationProfile.class);
44 private final TransformationService service;
45 private final ProfileCallback callback;
47 private static final String FUNCTION_PARAM = "function";
48 private static final String SOURCE_FORMAT_PARAM = "sourceFormat";
51 private final String function;
53 private final String sourceFormat;
55 public XSLTTransformationProfile(ProfileCallback callback, ProfileContext context, TransformationService service) {
56 this.service = service;
57 this.callback = callback;
59 Object paramFunction = context.getConfiguration().get(FUNCTION_PARAM);
60 Object paramSource = context.getConfiguration().get(SOURCE_FORMAT_PARAM);
62 logger.debug("Profile configured with '{}'='{}', '{}'={}", FUNCTION_PARAM, paramFunction, SOURCE_FORMAT_PARAM,
64 // SOURCE_FORMAT_PARAM is an advanced parameter and we assume "%s" if it is not set
65 if (paramSource == null) {
68 if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) {
70 sourceFormat = pFormat;
72 logger.warn("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM,
80 public ProfileTypeUID getProfileTypeUID() {
81 return PROFILE_TYPE_UID;
85 public void onStateUpdateFromItem(State state) {
89 public void onCommandFromItem(Command command) {
90 callback.handleCommand(command);
94 public void onCommandFromHandler(Command command) {
95 if (function == null || sourceFormat == null) {
97 "Please specify a function and a source format for this Profile in the '{}', and '{}' parameters. Returning the original command now.",
98 FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
99 callback.sendCommand(command);
102 callback.sendCommand((Command) transformState(command));
106 public void onStateUpdateFromHandler(State state) {
107 if (function == null || sourceFormat == null) {
109 "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters. Returning the original state now.",
110 FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
111 callback.sendUpdate(state);
114 callback.sendUpdate((State) transformState(state));
117 private Type transformState(Type state) {
118 String result = state.toFullString();
120 result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString());
121 } catch (TransformationException e) {
122 logger.debug("Could not transform state '{}' with function '{}' and format '{}'", state, function,
125 StringType resultType = new StringType(result);
126 logger.debug("Transformed '{}' into '{}'", state, resultType);