Skip to content

Commit d0afa4d

Browse files
author
Ravi Nadahar
committed
MIMETypeAliases
1 parent 878dedd commit d0afa4d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2010-2025 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.core.model.yaml.internal.rules;
14+
15+
import java.util.Collections;
16+
import java.util.HashMap;
17+
import java.util.Locale;
18+
import java.util.Map;
19+
20+
import org.eclipse.jdt.annotation.NonNullByDefault;
21+
22+
/**
23+
* This class provides translations to and from aliases for scripting language MIME types.
24+
*
25+
* @author Ravi Nadahar - Initial contribution
26+
*/
27+
@NonNullByDefault
28+
public class MIMETypeAliases {
29+
30+
private static final Map<String, String> ALIAS_IDX;
31+
private static final Map<String, String> MIME_TYPE_IDX;
32+
33+
static {
34+
/*
35+
* The table below is formatted as follows:
36+
* - The first column is the MIME type
37+
* - The second column is the "primary code" that will be used when translating to alias
38+
* - The remaining columns are optional aliases that will only work when translation to MIME type
39+
*/
40+
String[][] table = { //
41+
{ "application/vnd.openhab.dsl.rule", "RuleDSL", "DSL" }, //
42+
{ "application/x-groovy", "Groovy" }, //
43+
{ "application/javascript", "JavaScript", "JS", "GraalJS" }, //
44+
{ "application/python", "Python", "Python3", "PY", "PY3" }, //
45+
{ "application/x-python2", "Jython", "JythonPY", "PY2" }, { "application/x-ruby", "Ruby", "RB" }, //
46+
{ "application/javascript;version=ECMAScript-5.1", "NashornJS", "NashornJavaScript" } //
47+
};
48+
49+
Map<String, String> aliasIdx = new HashMap<>();
50+
Map<String, String> mimeTypeIdx = new HashMap<>();
51+
for (String[] entry : table) {
52+
mimeTypeIdx.put(entry[0], entry[1]);
53+
for (int i = 1; i < entry.length; i++) {
54+
aliasIdx.put(entry[i].toLowerCase(Locale.ROOT), entry[0]);
55+
}
56+
}
57+
ALIAS_IDX = Collections.unmodifiableMap(aliasIdx);
58+
MIME_TYPE_IDX = Collections.unmodifiableMap(mimeTypeIdx);
59+
}
60+
61+
/**
62+
* Not to be instantiated
63+
*/
64+
private MIMETypeAliases() {
65+
}
66+
67+
/**
68+
* Translates an alias to a scripting language MIME type, or returns the argument if the argument doesn't
69+
* match an alias.
70+
*
71+
* @param alias the potential alias to translate.
72+
* @return The corresponding scripting language MIME type or the unmodified argument the it's not an alias.
73+
*/
74+
public static String aliasToType(String alias) {
75+
return ALIAS_IDX.getOrDefault(alias.toLowerCase(Locale.ROOT), alias);
76+
}
77+
78+
/**
79+
* Translates a scripting language MIME type to an alias if such a mapping exists, otherwise returns the argument.
80+
*
81+
* @param type scripting language MIME type to potentially translate to an alias.
82+
* @return The corresponding alias or the unmodified argument if no matching mapping was found.
83+
*/
84+
public static String mimeTypeToAlias(String type) {
85+
return MIME_TYPE_IDX.getOrDefault(type, type);
86+
}
87+
}

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/rules/YamlRuleDTO.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public Visibility getVisibility() {
139139
result.config = partial.config;
140140
result.configDescriptions = partial.configDescriptions;
141141

142+
Map<@NonNull String, @NonNull Object> config;
143+
String translatedType;
142144
if (partial.actions != null && !partial.actions.isEmpty()) {
143145
if (!partial.actions.isArray()) {
144146
throw new SerializationException("Expected actions to be an array node");
@@ -150,6 +152,12 @@ public Visibility getVisibility() {
150152
actionNode = iterator.next();
151153
action = mapper.treeToValue(actionNode, YamlActionDTO.class);
152154
action.type = ModuleTypeAliases.aliasToType(Action.class, action.type);
155+
if ((config = action.config) != null && config.containsKey("script")
156+
&& config.get("type") instanceof String type) {
157+
if (!type.equals(translatedType = MIMETypeAliases.aliasToType(type))) {
158+
config.put("type", translatedType);
159+
}
160+
}
153161
actions.add(action);
154162
}
155163
result.actions = actions;
@@ -165,6 +173,12 @@ public Visibility getVisibility() {
165173
conditionNode = iterator.next();
166174
condition = mapper.treeToValue(conditionNode, YamlConditionDTO.class);
167175
condition.type = ModuleTypeAliases.aliasToType(Condition.class, condition.type);
176+
if ((config = condition.config) != null && config.containsKey("script")
177+
&& config.get("type") instanceof String type) {
178+
if (!type.equals(translatedType = MIMETypeAliases.aliasToType(type))) {
179+
config.put("type", translatedType);
180+
}
181+
}
168182
conditions.add(condition);
169183
}
170184
result.conditions = conditions;
@@ -180,6 +194,12 @@ public Visibility getVisibility() {
180194
triggerNode = iterator.next();
181195
trigger = mapper.treeToValue(triggerNode, YamlModuleDTO.class);
182196
trigger.type = ModuleTypeAliases.aliasToType(Trigger.class, trigger.type);
197+
if ((config = trigger.config) != null && config.containsKey("script")
198+
&& config.get("type") instanceof String type) {
199+
if (!type.equals(translatedType = MIMETypeAliases.aliasToType(type))) {
200+
config.put("type", translatedType);
201+
}
202+
}
183203
triggers.add(trigger);
184204
}
185205
result.triggers = triggers;

0 commit comments

Comments
 (0)