|
| 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 | +} |
0 commit comments