|
16 | 16 | import java.io.IOException; |
17 | 17 | import java.io.InputStreamReader; |
18 | 18 | import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.Collection; |
20 | 21 | import java.util.HashSet; |
| 22 | +import java.util.List; |
21 | 23 | import java.util.Locale; |
22 | 24 | import java.util.Map; |
23 | 25 | import java.util.Set; |
|
26 | 28 | import org.eclipse.jdt.annotation.NonNullByDefault; |
27 | 29 | import org.eclipse.jdt.annotation.Nullable; |
28 | 30 | import org.openhab.core.automation.Module; |
| 31 | +import org.openhab.core.automation.converter.RuleTemplateParser; |
29 | 32 | import org.openhab.core.automation.dto.RuleTemplateDTO; |
30 | 33 | import org.openhab.core.automation.dto.RuleTemplateDTOMapper; |
31 | 34 | import org.openhab.core.automation.parser.Parser; |
@@ -60,6 +63,7 @@ public class MarketplaceRuleTemplateProvider extends AbstractManagedProvider<Rul |
60 | 63 | implements RuleTemplateProvider { |
61 | 64 |
|
62 | 65 | private final Map<String, Parser<RuleTemplate>> parsers = new ConcurrentHashMap<>(); |
| 66 | + private final Map<String, RuleTemplateParser> fileConversionParsers = new ConcurrentHashMap<>(); |
63 | 67 | ObjectMapper yamlMapper; |
64 | 68 |
|
65 | 69 | @Activate |
@@ -94,6 +98,15 @@ public void removeParser(Parser<RuleTemplate> parser, Map<String, String> proper |
94 | 98 | parsers.remove(parserType); |
95 | 99 | } |
96 | 100 |
|
| 101 | + @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE) |
| 102 | + protected void addRuleTemplateParser(RuleTemplateParser templateParser) { |
| 103 | + fileConversionParsers.put(templateParser.getParserFormat(), templateParser); |
| 104 | + } |
| 105 | + |
| 106 | + protected void removeRuleTemplateParser(RuleTemplateParser templateParser) { |
| 107 | + fileConversionParsers.remove(templateParser.getParserFormat()); |
| 108 | + } |
| 109 | + |
97 | 110 | @Override |
98 | 111 | public @Nullable RuleTemplate getTemplate(String uid, @Nullable Locale locale) { |
99 | 112 | return get(uid); |
@@ -158,30 +171,62 @@ public void addTemplateAsYAML(String uid, String yaml) throws ParsingException, |
158 | 171 | * @throws ValidationException If the validation fails. |
159 | 172 | */ |
160 | 173 | protected void addTemplate(String uid, String content, String format) throws ParsingException, ValidationException { |
161 | | - Parser<RuleTemplate> parser = parsers.get(format); |
| 174 | + Set<RuleTemplate> templates; |
| 175 | + if (Parser.FORMAT_YAML.equals(format) && content.trim().startsWith("version:")) { |
| 176 | + // Use the "new YAML" parser |
| 177 | + RuleTemplateParser parser = fileConversionParsers.get("YAML"); |
| 178 | + |
| 179 | + // The parser might not have been registered yet |
| 180 | + if (parser == null) { |
| 181 | + throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, |
| 182 | + "No " + format.toUpperCase(Locale.ROOT) + " parser available", null)); |
| 183 | + } |
162 | 184 |
|
163 | | - // The parser might not have been registered yet |
164 | | - if (parser == null) { |
165 | | - throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, |
166 | | - "No " + format.toUpperCase(Locale.ROOT) + " parser available", null)); |
167 | | - } |
| 185 | + List<String> errors = new ArrayList<>(); |
| 186 | + List<String> warnings = new ArrayList<>(); |
| 187 | + String modelName = parser.startParsingFormat(content, errors, warnings); |
| 188 | + if (modelName == null || !errors.isEmpty()) { |
| 189 | + if (modelName != null) { |
| 190 | + parser.finishParsingFormat(modelName); |
| 191 | + } |
| 192 | + throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, |
| 193 | + "Parsing of " + format.toUpperCase(Locale.ROOT) + " failed: " + String.join(", ", errors), |
| 194 | + null)); |
| 195 | + } |
| 196 | + if (!warnings.isEmpty()) { |
| 197 | + logger.warn("Parsing of markedplace rule template add-on {} has warnings: {}", uid, |
| 198 | + String.join(", ", warnings)); |
| 199 | + } |
| 200 | + templates = Set.copyOf(parser.getParsedObjects(modelName)); |
| 201 | + parser.finishParsingFormat(modelName); |
| 202 | + } else { |
| 203 | + // Use the "old YAML" parser |
| 204 | + Parser<RuleTemplate> parser = parsers.get(format); |
| 205 | + |
| 206 | + // The parser might not have been registered yet |
| 207 | + if (parser == null) { |
| 208 | + throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, |
| 209 | + "No " + format.toUpperCase(Locale.ROOT) + " parser available", null)); |
| 210 | + } |
168 | 211 |
|
169 | | - try (InputStreamReader isr = new InputStreamReader( |
170 | | - new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))) { |
171 | | - Set<RuleTemplate> templates = parser.parse(isr); |
172 | | - |
173 | | - // Add a tag with the marketplace add-on ID to be able to identify the template in the registry |
174 | | - Set<String> tags; |
175 | | - for (RuleTemplate template : templates) { |
176 | | - validateTemplate(template); |
177 | | - tags = new HashSet<String>(template.getTags()); |
178 | | - tags.add(uid); |
179 | | - add(new RuleTemplate(template.getUID(), template.getLabel(), template.getDescription(), tags, |
180 | | - template.getTriggers(), template.getConditions(), template.getActions(), |
181 | | - template.getConfigurationDescriptions(), template.getVisibility())); |
| 212 | + try (InputStreamReader isr = new InputStreamReader( |
| 213 | + new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))) { |
| 214 | + templates = parser.parse(isr); |
| 215 | + } catch (IOException e) { |
| 216 | + // Impossible for ByteArrayInputStream |
| 217 | + templates = Set.of(); |
182 | 218 | } |
183 | | - } catch (IOException e) { |
184 | | - // Impossible for ByteArrayInputStream |
| 219 | + } |
| 220 | + |
| 221 | + // Add a tag with the marketplace add-on ID to be able to identify the template in the registry |
| 222 | + Set<String> tags; |
| 223 | + for (RuleTemplate template : templates) { |
| 224 | + validateTemplate(template); |
| 225 | + tags = new HashSet<String>(template.getTags()); |
| 226 | + tags.add(uid); |
| 227 | + add(new RuleTemplate(template.getUID(), template.getLabel(), template.getDescription(), tags, |
| 228 | + template.getTriggers(), template.getConditions(), template.getActions(), |
| 229 | + template.getConfigurationDescriptions(), template.getVisibility())); |
185 | 230 | } |
186 | 231 | } |
187 | 232 |
|
|
0 commit comments