Reorganize file conversion classes/interfaces (#5370)

Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
This commit is contained in:
Nadahar
2026-03-15 21:23:04 +01:00
committed by GitHub
parent a8fd50ef51
commit 0bc16195cb
19 changed files with 470 additions and 413 deletions
@@ -64,8 +64,8 @@ import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.MetadataKey;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.items.fileconverter.ItemFileGenerator;
import org.openhab.core.items.fileconverter.ItemFileParser;
import org.openhab.core.items.fileconverter.ItemParser;
import org.openhab.core.items.fileconverter.ItemSerializer;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
@@ -76,8 +76,8 @@ import org.openhab.core.thing.binding.ThingFactory;
import org.openhab.core.thing.dto.ChannelDTO;
import org.openhab.core.thing.dto.ThingDTO;
import org.openhab.core.thing.dto.ThingDTOMapper;
import org.openhab.core.thing.fileconverter.ThingFileGenerator;
import org.openhab.core.thing.fileconverter.ThingFileParser;
import org.openhab.core.thing.fileconverter.ThingParser;
import org.openhab.core.thing.fileconverter.ThingSerializer;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.type.BridgeType;
@@ -257,10 +257,10 @@ public class FileFormatResource implements RESTResource {
private final ThingTypeRegistry thingTypeRegistry;
private final ChannelTypeRegistry channelTypeRegistry;
private final ConfigDescriptionRegistry configDescRegistry;
private final Map<String, ItemFileGenerator> itemFileGenerators = new ConcurrentHashMap<>();
private final Map<String, ItemFileParser> itemFileParsers = new ConcurrentHashMap<>();
private final Map<String, ThingFileGenerator> thingFileGenerators = new ConcurrentHashMap<>();
private final Map<String, ThingFileParser> thingFileParsers = new ConcurrentHashMap<>();
private final Map<String, ItemSerializer> itemSerializers = new ConcurrentHashMap<>();
private final Map<String, ItemParser> itemParsers = new ConcurrentHashMap<>();
private final Map<String, ThingSerializer> thingSerializers = new ConcurrentHashMap<>();
private final Map<String, ThingParser> thingParsers = new ConcurrentHashMap<>();
private int counter;
@@ -291,39 +291,39 @@ public class FileFormatResource implements RESTResource {
}
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE)
protected void addItemFileGenerator(ItemFileGenerator itemFileGenerator) {
itemFileGenerators.put(itemFileGenerator.getFileFormatGenerator(), itemFileGenerator);
protected void addItemSerializer(ItemSerializer itemSerializer) {
itemSerializers.put(itemSerializer.getGeneratedFormat(), itemSerializer);
}
protected void removeItemFileGenerator(ItemFileGenerator itemFileGenerator) {
itemFileGenerators.remove(itemFileGenerator.getFileFormatGenerator());
protected void removeItemSerializer(ItemSerializer itemSerializer) {
itemSerializers.remove(itemSerializer.getGeneratedFormat());
}
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE)
protected void addItemFileParser(ItemFileParser itemFileParser) {
itemFileParsers.put(itemFileParser.getFileFormatParser(), itemFileParser);
protected void addItemParser(ItemParser itemParser) {
itemParsers.put(itemParser.getParserFormat(), itemParser);
}
protected void removeItemFileParser(ItemFileParser itemFileParser) {
itemFileParsers.remove(itemFileParser.getFileFormatParser());
protected void removeItemParser(ItemParser itemParser) {
itemParsers.remove(itemParser.getParserFormat());
}
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE)
protected void addThingFileGenerator(ThingFileGenerator thingFileGenerator) {
thingFileGenerators.put(thingFileGenerator.getFileFormatGenerator(), thingFileGenerator);
protected void addThingSerializer(ThingSerializer thingSerializer) {
thingSerializers.put(thingSerializer.getGeneratedFormat(), thingSerializer);
}
protected void removeThingFileGenerator(ThingFileGenerator thingFileGenerator) {
thingFileGenerators.remove(thingFileGenerator.getFileFormatGenerator());
protected void removeThingSerializer(ThingSerializer thingSerializer) {
thingSerializers.remove(thingSerializer.getGeneratedFormat());
}
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE)
protected void addThingFileParser(ThingFileParser thingFileParser) {
thingFileParsers.put(thingFileParser.getFileFormatParser(), thingFileParser);
protected void addThingParser(ThingParser thingParser) {
thingParsers.put(thingParser.getParserFormat(), thingParser);
}
protected void removeThingFileParser(ThingFileParser thingFileParser) {
thingFileParsers.remove(thingFileParser.getFileFormatParser());
protected void removeThingParser(ThingParser thingParser) {
thingParsers.remove(thingParser.getParserFormat());
}
@POST
@@ -343,8 +343,8 @@ public class FileFormatResource implements RESTResource {
@Parameter(description = "Array of item names. If empty or omitted, return all Items.") @Nullable List<String> itemNames) {
String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT);
logger.debug("createFileFormatForItems: mediaType = {}, itemNames = {}", acceptHeader, itemNames);
ItemFileGenerator generator = getItemFileGenerator(acceptHeader);
if (generator == null) {
ItemSerializer serializer = getItemSerializer(acceptHeader);
if (serializer == null) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported media type '" + acceptHeader + "'!").build();
}
@@ -363,7 +363,7 @@ public class FileFormatResource implements RESTResource {
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String genId = newIdForGeneration();
String genId = newIdForSerialization();
Map<String, String> stateFormatters = new HashMap<>();
items.forEach(item -> {
StateDescription stateDescr = item.getStateDescription();
@@ -372,8 +372,8 @@ public class FileFormatResource implements RESTResource {
stateFormatters.put(item.getName(), format);
}
});
generator.setItemsToBeGenerated(genId, items, getMetadata(items), stateFormatters, hideDefaultParameters);
generator.generateFileFormat(genId, outputStream);
serializer.setItemsToBeSerialized(genId, items, getMetadata(items), stateFormatters, hideDefaultParameters);
serializer.generateFormat(genId, outputStream);
return Response.ok(new String(outputStream.toByteArray())).build();
}
@@ -394,8 +394,8 @@ public class FileFormatResource implements RESTResource {
@Parameter(description = "Array of Thing UIDs. If empty or omitted, return all Things from the Registry.") @Nullable List<String> thingUIDs) {
String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT);
logger.debug("createFileFormatForThings: mediaType = {}, thingUIDs = {}", acceptHeader, thingUIDs);
ThingFileGenerator generator = getThingFileGenerator(acceptHeader);
if (generator == null) {
ThingSerializer serializer = getThingSerializer(acceptHeader);
if (serializer == null) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported media type '" + acceptHeader + "'!").build();
}
@@ -410,9 +410,9 @@ public class FileFormatResource implements RESTResource {
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String genId = newIdForGeneration();
generator.setThingsToBeGenerated(genId, things, true, hideDefaultParameters);
generator.generateFileFormat(genId, outputStream);
String genId = newIdForSerialization();
serializer.setThingsToBeSerialized(genId, things, true, hideDefaultParameters);
serializer.generateFormat(genId, outputStream);
return Response.ok(new String(outputStream.toByteArray())).build();
}
@@ -446,44 +446,44 @@ public class FileFormatResource implements RESTResource {
return Response.status(Response.Status.BAD_REQUEST).entity(String.join("\n", errors)).build();
}
ThingFileGenerator thingGenerator = getThingFileGenerator(acceptHeader);
ItemFileGenerator itemGenerator = getItemFileGenerator(acceptHeader);
ThingSerializer thingSerializer = getThingSerializer(acceptHeader);
ItemSerializer itemSerializer = getItemSerializer(acceptHeader);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String genId = newIdForGeneration();
String genId = newIdForSerialization();
switch (acceptHeader) {
case "text/vnd.openhab.dsl.thing":
if (thingGenerator == null) {
if (thingSerializer == null) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported media type '" + acceptHeader + "'!").build();
} else if (things.isEmpty()) {
return Response.status(Response.Status.BAD_REQUEST).entity("No thing loaded from input").build();
}
thingGenerator.setThingsToBeGenerated(genId, things, hideDefaultChannels, hideDefaultParameters);
thingGenerator.generateFileFormat(genId, outputStream);
thingSerializer.setThingsToBeSerialized(genId, things, hideDefaultChannels, hideDefaultParameters);
thingSerializer.generateFormat(genId, outputStream);
break;
case "text/vnd.openhab.dsl.item":
if (itemGenerator == null) {
if (itemSerializer == null) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported media type '" + acceptHeader + "'!").build();
} else if (items.isEmpty()) {
return Response.status(Response.Status.BAD_REQUEST).entity("No item loaded from input").build();
}
itemGenerator.setItemsToBeGenerated(genId, items, hideChannelLinksAndMetadata ? List.of() : metadata,
itemSerializer.setItemsToBeSerialized(genId, items, hideChannelLinksAndMetadata ? List.of() : metadata,
stateFormatters, hideDefaultParameters);
itemGenerator.generateFileFormat(genId, outputStream);
itemSerializer.generateFormat(genId, outputStream);
break;
case "application/yaml":
if (thingGenerator != null) {
thingGenerator.setThingsToBeGenerated(genId, things, hideDefaultChannels, hideDefaultParameters);
if (thingSerializer != null) {
thingSerializer.setThingsToBeSerialized(genId, things, hideDefaultChannels, hideDefaultParameters);
}
if (itemGenerator != null) {
itemGenerator.setItemsToBeGenerated(genId, items,
if (itemSerializer != null) {
itemSerializer.setItemsToBeSerialized(genId, items,
hideChannelLinksAndMetadata ? List.of() : metadata, stateFormatters, hideDefaultParameters);
}
if (thingGenerator != null) {
thingGenerator.generateFileFormat(genId, outputStream);
} else if (itemGenerator != null) {
itemGenerator.generateFileFormat(genId, outputStream);
if (thingSerializer != null) {
thingSerializer.generateFormat(genId, outputStream);
} else if (itemSerializer != null) {
itemSerializer.generateFormat(genId, outputStream);
}
break;
default:
@@ -519,8 +519,8 @@ public class FileFormatResource implements RESTResource {
Map<String, String> stateFormatters = Map.of();
List<String> errors = new ArrayList<>();
List<String> warnings = new ArrayList<>();
ThingFileParser thingParser = getThingFileParser(contentTypeHeader);
ItemFileParser itemParser = getItemFileParser(contentTypeHeader);
ThingParser thingParser = getThingParser(contentTypeHeader);
ItemParser itemParser = getItemParser(contentTypeHeader);
String modelName = null;
String modelName2 = null;
switch (contentTypeHeader) {
@@ -529,13 +529,13 @@ public class FileFormatResource implements RESTResource {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported content type '" + contentTypeHeader + "'!").build();
}
modelName = thingParser.startParsingFileFormat(input, errors, warnings);
modelName = thingParser.startParsingFormat(input, errors, warnings);
if (modelName == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.join("\n", errors)).build();
}
things = thingParser.getParsedThings(modelName);
things = thingParser.getParsedObjects(modelName);
if (things.isEmpty()) {
thingParser.finishParsingFileFormat(modelName);
thingParser.finishParsingFormat(modelName);
return Response.status(Response.Status.BAD_REQUEST).entity("No thing loaded from input").build();
}
break;
@@ -544,13 +544,13 @@ public class FileFormatResource implements RESTResource {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported content type '" + contentTypeHeader + "'!").build();
}
modelName2 = itemParser.startParsingFileFormat(input, errors, warnings);
modelName2 = itemParser.startParsingFormat(input, errors, warnings);
if (modelName2 == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.join("\n", errors)).build();
}
items = itemParser.getParsedItems(modelName2);
items = itemParser.getParsedObjects(modelName2);
if (items.isEmpty()) {
itemParser.finishParsingFileFormat(modelName2);
itemParser.finishParsingFormat(modelName2);
return Response.status(Response.Status.BAD_REQUEST).entity("No item loaded from input").build();
}
metadata = itemParser.getParsedMetadata(modelName2);
@@ -563,24 +563,24 @@ public class FileFormatResource implements RESTResource {
break;
case "application/yaml":
if (thingParser != null) {
modelName = thingParser.startParsingFileFormat(input, errors, warnings);
modelName = thingParser.startParsingFormat(input, errors, warnings);
if (modelName == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.join("\n", errors)).build();
}
things = thingParser.getParsedThings(modelName);
things = thingParser.getParsedObjects(modelName);
channelLinks = thingParser.getParsedChannelLinks(modelName);
}
if (itemParser != null) {
// Avoid parsing the input a second time
if (modelName == null) {
modelName2 = itemParser.startParsingFileFormat(input, errors, warnings);
modelName2 = itemParser.startParsingFormat(input, errors, warnings);
if (modelName2 == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.join("\n", errors))
.build();
}
}
String modelNameToUse = modelName != null ? modelName : Objects.requireNonNull(modelName2);
items = itemParser.getParsedItems(modelNameToUse);
items = itemParser.getParsedObjects(modelNameToUse);
metadata = itemParser.getParsedMetadata(modelNameToUse);
stateFormatters = itemParser.getParsedStateFormatters(modelNameToUse);
}
@@ -592,15 +592,15 @@ public class FileFormatResource implements RESTResource {
ExtendedFileFormatDTO result = convertToFileFormatDTO(things, items, metadata, stateFormatters, channelLinks,
warnings);
if (modelName != null && thingParser != null) {
thingParser.finishParsingFileFormat(modelName);
thingParser.finishParsingFormat(modelName);
}
if (modelName2 != null && itemParser != null) {
itemParser.finishParsingFileFormat(modelName2);
itemParser.finishParsingFormat(modelName2);
}
return Response.ok(result).build();
}
private String newIdForGeneration() {
private String newIdForSerialization() {
return GEN_ID_PATTERN.formatted(++counter);
}
@@ -748,35 +748,35 @@ public class FileFormatResource implements RESTResource {
configDescRegistry);
}
private @Nullable ItemFileGenerator getItemFileGenerator(String mediaType) {
private @Nullable ItemSerializer getItemSerializer(String mediaType) {
return switch (mediaType) {
case "text/vnd.openhab.dsl.item" -> itemFileGenerators.get("DSL");
case "application/yaml" -> itemFileGenerators.get("YAML");
case "text/vnd.openhab.dsl.item" -> itemSerializers.get("DSL");
case "application/yaml" -> itemSerializers.get("YAML");
default -> null;
};
}
private @Nullable ThingFileGenerator getThingFileGenerator(String mediaType) {
private @Nullable ThingSerializer getThingSerializer(String mediaType) {
return switch (mediaType) {
case "text/vnd.openhab.dsl.thing" -> thingFileGenerators.get("DSL");
case "application/yaml" -> thingFileGenerators.get("YAML");
case "text/vnd.openhab.dsl.thing" -> thingSerializers.get("DSL");
case "application/yaml" -> thingSerializers.get("YAML");
default -> null;
};
}
private @Nullable ItemFileParser getItemFileParser(String contentType) {
private @Nullable ItemParser getItemParser(String contentType) {
return switch (contentType) {
case "text/vnd.openhab.dsl.item" -> itemFileParsers.get("DSL");
case "application/yaml" -> itemFileParsers.get("YAML");
case "text/vnd.openhab.dsl.item" -> itemParsers.get("DSL");
case "application/yaml" -> itemParsers.get("YAML");
default -> null;
};
}
private @Nullable ThingFileParser getThingFileParser(String contentType) {
private @Nullable ThingParser getThingParser(String contentType) {
return switch (contentType) {
case "text/vnd.openhab.dsl.thing" -> thingFileParsers.get("DSL");
case "text/vnd.openhab.dsl.item" -> thingFileParsers.get("DSL");
case "application/yaml" -> thingFileParsers.get("YAML");
case "text/vnd.openhab.dsl.thing" -> thingParsers.get("DSL");
case "text/vnd.openhab.dsl.item" -> thingParsers.get("DSL");
case "application/yaml" -> thingParsers.get("YAML");
default -> null;
};
}
@@ -37,9 +37,9 @@ import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemUtil;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.fileconverter.AbstractItemFileGenerator;
import org.openhab.core.items.fileconverter.ItemFileGenerator;
import org.openhab.core.items.fileconverter.ItemFileParser;
import org.openhab.core.items.fileconverter.AbstractItemSerializer;
import org.openhab.core.items.fileconverter.ItemParser;
import org.openhab.core.items.fileconverter.ItemSerializer;
import org.openhab.core.model.core.ModelRepository;
import org.openhab.core.model.item.internal.GenericItemProvider;
import org.openhab.core.model.item.internal.GenericMetadataProvider;
@@ -56,16 +56,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link DslItemFileConverter} is the DSL file converter for {@link Item} object
* with the capabilities of parsing and generating file.
* {@link DslItemConverter} is the DSL converter for {@link Item} objects
* with the capabilities of parsing and serializing files.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
@Component(immediate = true, service = { ItemFileGenerator.class, ItemFileParser.class })
public class DslItemFileConverter extends AbstractItemFileGenerator implements ItemFileParser {
@Component(immediate = true, service = { ItemSerializer.class, ItemParser.class })
public class DslItemConverter extends AbstractItemSerializer implements ItemParser {
private final Logger logger = LoggerFactory.getLogger(DslItemFileConverter.class);
private final Logger logger = LoggerFactory.getLogger(DslItemConverter.class);
private final Map<String, ItemModel> elementsToGenerate = new ConcurrentHashMap<>();
@@ -75,7 +75,7 @@ public class DslItemFileConverter extends AbstractItemFileGenerator implements I
private final ConfigDescriptionRegistry configDescriptionRegistry;
@Activate
public DslItemFileConverter(final @Reference ModelRepository modelRepository,
public DslItemConverter(final @Reference ModelRepository modelRepository,
final @Reference GenericItemProvider itemProvider,
final @Reference GenericMetadataProvider metadataProvider,
final @Reference ConfigDescriptionRegistry configDescriptionRegistry) {
@@ -86,12 +86,12 @@ public class DslItemFileConverter extends AbstractItemFileGenerator implements I
}
@Override
public String getFileFormatGenerator() {
public String getGeneratedFormat() {
return "DSL";
}
@Override
public void setItemsToBeGenerated(String id, List<Item> items, Collection<Metadata> metadata,
public void setItemsToBeSerialized(String id, List<Item> items, Collection<Metadata> metadata,
Map<String, String> stateFormatters, boolean hideDefaultParameters) {
if (items.isEmpty()) {
return;
@@ -105,7 +105,7 @@ public class DslItemFileConverter extends AbstractItemFileGenerator implements I
}
@Override
public void generateFileFormat(String id, OutputStream out) {
public void generateFormat(String id, OutputStream out) {
ItemModel model = elementsToGenerate.remove(id);
if (model != null) {
modelRepository.generateFileFormat(out, "items", model);
@@ -299,18 +299,18 @@ public class DslItemFileConverter extends AbstractItemFileGenerator implements I
}
@Override
public String getFileFormatParser() {
public String getParserFormat() {
return "DSL";
}
@Override
public @Nullable String startParsingFileFormat(String syntax, List<String> errors, List<String> warnings) {
public @Nullable String startParsingFormat(String syntax, List<String> errors, List<String> warnings) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(syntax.getBytes());
return modelRepository.createIsolatedModel("items", inputStream, errors, warnings);
}
@Override
public Collection<Item> getParsedItems(String modelName) {
public Collection<Item> getParsedObjects(String modelName) {
return itemProvider.getAllFromModel(modelName);
}
@@ -325,7 +325,7 @@ public class DslItemFileConverter extends AbstractItemFileGenerator implements I
}
@Override
public void finishParsingFileFormat(String modelName) {
public void finishParsingFormat(String modelName) {
modelRepository.removeModel(modelName);
}
}
@@ -41,9 +41,9 @@ import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.fileconverter.AbstractThingFileGenerator;
import org.openhab.core.thing.fileconverter.ThingFileGenerator;
import org.openhab.core.thing.fileconverter.ThingFileParser;
import org.openhab.core.thing.fileconverter.AbstractThingSerializer;
import org.openhab.core.thing.fileconverter.ThingParser;
import org.openhab.core.thing.fileconverter.ThingSerializer;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.type.ChannelTypeRegistry;
@@ -57,16 +57,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link DslThingFileConverter} is the DSL file converter for {@link Thing} object
* with the capabilities of parsing and generating file.
* {@link DslThingConverter} is the DSL file converter for {@link Thing} objects
* with the capabilities of parsing and serializing files.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
@Component(immediate = true, service = { ThingFileGenerator.class, ThingFileParser.class })
public class DslThingFileConverter extends AbstractThingFileGenerator implements ThingFileParser {
@Component(immediate = true, service = { ThingSerializer.class, ThingParser.class })
public class DslThingConverter extends AbstractThingSerializer implements ThingParser {
private final Logger logger = LoggerFactory.getLogger(DslThingFileConverter.class);
private final Logger logger = LoggerFactory.getLogger(DslThingConverter.class);
private final ModelRepository modelRepository;
private final GenericThingProvider thingProvider;
@@ -76,7 +76,7 @@ public class DslThingFileConverter extends AbstractThingFileGenerator implements
private final Map<String, ThingModel> elementsToGenerate = new ConcurrentHashMap<>();
@Activate
public DslThingFileConverter(final @Reference ModelRepository modelRepository,
public DslThingConverter(final @Reference ModelRepository modelRepository,
final @Reference GenericThingProvider thingProvider,
final @Reference GenericItemChannelLinkProvider itemChannelLinkProvider,
final @Reference ThingTypeRegistry thingTypeRegistry,
@@ -91,12 +91,12 @@ public class DslThingFileConverter extends AbstractThingFileGenerator implements
}
@Override
public String getFileFormatGenerator() {
public String getGeneratedFormat() {
return "DSL";
}
@Override
public void setThingsToBeGenerated(String id, List<Thing> things, boolean hideDefaultChannels,
public void setThingsToBeSerialized(String id, List<Thing> things, boolean hideDefaultChannels,
boolean hideDefaultParameters) {
if (things.isEmpty()) {
return;
@@ -114,7 +114,7 @@ public class DslThingFileConverter extends AbstractThingFileGenerator implements
}
@Override
public void generateFileFormat(String id, OutputStream out) {
public void generateFormat(String id, OutputStream out) {
ThingModel model = elementsToGenerate.remove(id);
if (model != null) {
// Double quotes are unexpectedly generated in thing UID when the segment contains a -.
@@ -235,18 +235,18 @@ public class DslThingFileConverter extends AbstractThingFileGenerator implements
}
@Override
public String getFileFormatParser() {
public String getParserFormat() {
return "DSL";
}
@Override
public @Nullable String startParsingFileFormat(String syntax, List<String> errors, List<String> warnings) {
public @Nullable String startParsingFormat(String syntax, List<String> errors, List<String> warnings) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(syntax.getBytes());
return modelRepository.createIsolatedModel("things", inputStream, errors, warnings);
}
@Override
public Collection<Thing> getParsedThings(String modelName) {
public Collection<Thing> getParsedObjects(String modelName) {
return thingProvider.getAllFromModel(modelName);
}
@@ -256,7 +256,7 @@ public class DslThingFileConverter extends AbstractThingFileGenerator implements
}
@Override
public void finishParsingFileFormat(String modelName) {
public void finishParsingFormat(String modelName) {
modelRepository.removeModel(modelName);
}
}
@@ -252,7 +252,7 @@ public class YamlModelRepositoryImpl implements WatchService.WatchEventListener,
List<String> newElementNames = new ArrayList<>();
// get sub-elements
Iterator<Map.Entry<String, JsonNode>> it = fileContent.fields();
Iterator<Map.Entry<String, JsonNode>> it = fileContent.properties().iterator();
while (it.hasNext()) {
Map.Entry<String, JsonNode> element = it.next();
String elementName = element.getKey();
@@ -39,9 +39,9 @@ import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemUtil;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.fileconverter.AbstractItemFileGenerator;
import org.openhab.core.items.fileconverter.ItemFileGenerator;
import org.openhab.core.items.fileconverter.ItemFileParser;
import org.openhab.core.items.fileconverter.AbstractItemSerializer;
import org.openhab.core.items.fileconverter.ItemParser;
import org.openhab.core.items.fileconverter.ItemSerializer;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.model.yaml.YamlElement;
import org.openhab.core.model.yaml.YamlModelRepository;
@@ -57,13 +57,13 @@ import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* {@link YamlItemFileConverter} is the YAML file converter for {@link Item} object.
* {@link YamlItemConverter} is the YAML converter for {@link Item} objects.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
@Component(immediate = true, service = { ItemFileGenerator.class, ItemFileParser.class })
public class YamlItemFileConverter extends AbstractItemFileGenerator implements ItemFileParser {
@Component(immediate = true, service = { ItemSerializer.class, ItemParser.class })
public class YamlItemConverter extends AbstractItemSerializer implements ItemParser {
private final YamlModelRepository modelRepository;
private final YamlItemProvider itemProvider;
@@ -72,7 +72,7 @@ public class YamlItemFileConverter extends AbstractItemFileGenerator implements
private final ConfigDescriptionRegistry configDescriptionRegistry;
@Activate
public YamlItemFileConverter(final @Reference YamlModelRepository modelRepository,
public YamlItemConverter(final @Reference YamlModelRepository modelRepository,
final @Reference YamlItemProvider itemProvider, final @Reference YamlMetadataProvider metadataProvider,
final @Reference YamlChannelLinkProvider channelLinkProvider,
final @Reference ConfigDescriptionRegistry configDescRegistry) {
@@ -84,12 +84,12 @@ public class YamlItemFileConverter extends AbstractItemFileGenerator implements
}
@Override
public String getFileFormatGenerator() {
public String getGeneratedFormat() {
return "YAML";
}
@Override
public void setItemsToBeGenerated(String id, List<Item> items, Collection<Metadata> metadata,
public void setItemsToBeSerialized(String id, List<Item> items, Collection<Metadata> metadata,
Map<String, String> stateFormatters, boolean hideDefaultParameters) {
List<YamlElement> elements = new ArrayList<>();
items.forEach(item -> {
@@ -100,7 +100,7 @@ public class YamlItemFileConverter extends AbstractItemFileGenerator implements
}
@Override
public void generateFileFormat(String id, OutputStream out) {
public void generateFormat(String id, OutputStream out) {
modelRepository.generateFileFormat(id, out);
}
@@ -283,18 +283,18 @@ public class YamlItemFileConverter extends AbstractItemFileGenerator implements
}
@Override
public String getFileFormatParser() {
public String getParserFormat() {
return "YAML";
}
@Override
public @Nullable String startParsingFileFormat(String syntax, List<String> errors, List<String> warnings) {
public @Nullable String startParsingFormat(String syntax, List<String> errors, List<String> warnings) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(syntax.getBytes());
return modelRepository.createIsolatedModel(inputStream, errors, warnings);
}
@Override
public Collection<Item> getParsedItems(String modelName) {
public Collection<Item> getParsedObjects(String modelName) {
return itemProvider.getAllFromModel(modelName);
}
@@ -318,7 +318,7 @@ public class YamlItemFileConverter extends AbstractItemFileGenerator implements
}
@Override
public void finishParsingFileFormat(String modelName) {
public void finishParsingFormat(String modelName) {
modelRepository.removeIsolatedModel(modelName);
}
}
@@ -35,9 +35,9 @@ import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.fileconverter.AbstractThingFileGenerator;
import org.openhab.core.thing.fileconverter.ThingFileGenerator;
import org.openhab.core.thing.fileconverter.ThingFileParser;
import org.openhab.core.thing.fileconverter.AbstractThingSerializer;
import org.openhab.core.thing.fileconverter.ThingParser;
import org.openhab.core.thing.fileconverter.ThingSerializer;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.type.ChannelType;
@@ -50,13 +50,13 @@ import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* {@link YamlThingFileConverter} is the YAML file converter for {@link Thing} object.
* {@link YamlThingConverter} is the YAML converter for {@link Thing} objects.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
@Component(immediate = true, service = { ThingFileGenerator.class, ThingFileParser.class })
public class YamlThingFileConverter extends AbstractThingFileGenerator implements ThingFileParser {
@Component(immediate = true, service = { ThingSerializer.class, ThingParser.class })
public class YamlThingConverter extends AbstractThingSerializer implements ThingParser {
private final YamlModelRepository modelRepository;
private final YamlThingProvider thingProvider;
@@ -64,7 +64,7 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator implement
private final LocaleProvider localeProvider;
@Activate
public YamlThingFileConverter(final @Reference YamlModelRepository modelRepository,
public YamlThingConverter(final @Reference YamlModelRepository modelRepository,
final @Reference YamlThingProvider thingProvider,
final @Reference YamlChannelLinkProvider itemChannelLinkProvider,
final @Reference ThingTypeRegistry thingTypeRegistry,
@@ -79,12 +79,12 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator implement
}
@Override
public String getFileFormatGenerator() {
public String getGeneratedFormat() {
return "YAML";
}
@Override
public void setThingsToBeGenerated(String id, List<Thing> things, boolean hideDefaultChannels,
public void setThingsToBeSerialized(String id, List<Thing> things, boolean hideDefaultChannels,
boolean hideDefaultParameters) {
List<YamlElement> elements = new ArrayList<>();
things.forEach(thing -> {
@@ -94,7 +94,7 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator implement
}
@Override
public void generateFileFormat(String id, OutputStream out) {
public void generateFormat(String id, OutputStream out) {
modelRepository.generateFileFormat(id, out);
}
@@ -165,18 +165,18 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator implement
}
@Override
public String getFileFormatParser() {
public String getParserFormat() {
return "YAML";
}
@Override
public @Nullable String startParsingFileFormat(String syntax, List<String> errors, List<String> warnings) {
public @Nullable String startParsingFormat(String syntax, List<String> errors, List<String> warnings) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(syntax.getBytes());
return modelRepository.createIsolatedModel(inputStream, errors, warnings);
}
@Override
public Collection<Thing> getParsedThings(String modelName) {
public Collection<Thing> getParsedObjects(String modelName) {
return thingProvider.getAllFromModel(modelName);
}
@@ -186,7 +186,7 @@ public class YamlThingFileConverter extends AbstractThingFileGenerator implement
}
@Override
public void finishParsingFileFormat(String modelName) {
public void finishParsingFormat(String modelName) {
modelRepository.removeIsolatedModel(modelName);
}
}
@@ -37,7 +37,7 @@ import org.openhab.core.model.yaml.internal.items.YamlItemProvider;
import org.openhab.core.model.yaml.internal.items.YamlMetadataProvider;
@NonNullByDefault
public class YamlItemFileConverterTest {
public class YamlItemConverterTest {
@Test
public void testExpireMetadataConvertedToShortForm() {
@@ -100,7 +100,7 @@ public class YamlItemFileConverterTest {
private YamlItemDTO convertWithMetadata(Metadata metadata, String itemType) {
CapturingYamlModelRepository repository = new CapturingYamlModelRepository();
YamlItemFileConverter converter = new YamlItemFileConverter(repository, mock(YamlItemProvider.class),
YamlItemConverter converter = new YamlItemConverter(repository, mock(YamlItemProvider.class),
mock(YamlMetadataProvider.class), mock(YamlChannelLinkProvider.class),
mock(ConfigDescriptionRegistry.class));
@@ -112,7 +112,7 @@ public class YamlItemFileConverterTest {
when(item.getGroupNames()).thenReturn(List.of());
when(item.getTags()).thenReturn(Set.of());
converter.setItemsToBeGenerated("id", List.of(item), List.of(metadata), Map.of(), false);
converter.setItemsToBeSerialized("id", List.of(item), List.of(metadata), Map.of(), false);
List<YamlElement> elements = repository.getElements();
assertEquals(1, elements.size());
@@ -37,19 +37,19 @@ import org.openhab.core.thing.type.ThingTypeRegistry;
import org.osgi.service.component.annotations.Activate;
/**
* {@link AbstractThingFileGenerator} is the base class for any {@link Thing} file generator.
* {@link AbstractThingSerializer} is the base class for any {@link Thing} serializer.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public abstract class AbstractThingFileGenerator implements ThingFileGenerator {
public abstract class AbstractThingSerializer implements ThingSerializer {
protected final ThingTypeRegistry thingTypeRegistry;
protected final ChannelTypeRegistry channelTypeRegistry;
protected final ConfigDescriptionRegistry configDescRegistry;
@Activate
public AbstractThingFileGenerator(ThingTypeRegistry thingTypeRegistry, ChannelTypeRegistry channelTypeRegistry,
public AbstractThingSerializer(ThingTypeRegistry thingTypeRegistry, ChannelTypeRegistry channelTypeRegistry,
ConfigDescriptionRegistry configDescRegistry) {
this.thingTypeRegistry = thingTypeRegistry;
this.channelTypeRegistry = channelTypeRegistry;
@@ -1,54 +0,0 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.fileconverter;
import java.io.OutputStream;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.Thing;
/**
* {@link ThingFileGenerator} is the interface to implement by any file generator for {@link Thing} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ThingFileGenerator {
/**
* Returns the format of the file.
*
* @return the file format
*/
String getFileFormatGenerator();
/**
* Define the list of things to be generated and associate them to an identifier.
*
* @param id the identifier of the file format generation
* @param things the things
* @param hideDefaultChannels true to hide the non extensible channels having a default configuration
* @param hideDefaultParameters true to hide the configuration parameters having the default value
*/
void setThingsToBeGenerated(String id, List<Thing> things, boolean hideDefaultChannels,
boolean hideDefaultParameters);
/**
* Generate the file format for all data that were associated to the provided identifier.
*
* @param id the identifier of the file format generation
* @param out the output stream to write to
*/
void generateFileFormat(String id, OutputStream out);
}
@@ -1,71 +0,0 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.fileconverter;
import java.util.Collection;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.link.ItemChannelLink;
/**
* {@link ThingFileParser} is the interface to implement by any file parser for {@link Thing} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ThingFileParser {
/**
* Returns the format of the syntax.
*
* @return the syntax format
*/
String getFileFormatParser();
/**
* Parse the provided syntax in file format without impacting the thing registry.
*
* @param syntax the syntax in file format
* @param errors the list to be used to fill the errors
* @param warnings the list to be used to fill the warnings
* @return the model name used for parsing if the parsing succeeded without errors; null otherwise
*/
@Nullable
String startParsingFileFormat(String syntax, List<String> errors, List<String> warnings);
/**
* Get the {@link Thing} objects found when parsing the file format.
*
* @param modelName the model name used for parsing
* @return the collection of things
*/
Collection<Thing> getParsedThings(String modelName);
/**
* Get the {@link ItemChannelLink} objects found when parsing the file format.
*
* @param modelName the model name used for parsing
* @return the collection of items channel links
*/
Collection<ItemChannelLink> getParsedChannelLinks(String modelName);
/**
* Release the data from a previously started file format parsing.
*
* @param modelName the model name used for parsing
*/
void finishParsingFileFormat(String modelName);
}
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.fileconverter;
import java.util.Collection;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.converter.ObjectParser;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.link.ItemChannelLink;
/**
* {@link ThingParser} is the interface to implement by any {@link Thing} parser.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ThingParser extends ObjectParser<Thing> {
/**
* Parse the provided {@code syntax} string without impacting the thing registry.
*
* @param syntax the syntax in format.
* @param errors the {@link List} to use to report errors.
* @param warnings the {@link List} to be used to report warnings.
* @return The model name used for parsing if the parsing succeeded without errors; {@code null} otherwise.
*/
@Override
@Nullable
String startParsingFormat(String syntax, List<String> errors, List<String> warnings);
/**
* Get the {@link Thing} objects found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return The {@link Collection} of {@link Thing}s.
*/
@Override
Collection<Thing> getParsedObjects(String modelName);
/**
* Get the {@link ItemChannelLink} objects found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return The {@link Collection} of {@link ItemChannelLink}s.
*/
Collection<ItemChannelLink> getParsedChannelLinks(String modelName);
}
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.fileconverter;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.converter.ObjectSerializer;
import org.openhab.core.thing.Thing;
/**
* {@link ThingSerializer} is the interface to implement by any file generator for {@link Thing} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ThingSerializer extends ObjectSerializer<Thing> {
/**
* Specify the {@link List} of {@link Thing}s to serialize and associate them with an identifier.
*
* @param id the identifier of the {@link Thing} format generation.
* @param things the {@link List} of {@link Thing}s to serialize.
* @param hideDefaultChannels {@code true} to hide the non extensible channels having a default configuration.
* @param hideDefaultParameters {@code true} to hide the configuration parameters having a default value.
*/
void setThingsToBeSerialized(String id, List<Thing> things, boolean hideDefaultChannels,
boolean hideDefaultParameters);
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.converter;
import java.util.Collection;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* A generic interface for parsers that parse strings into specific object types like Things, Items, Rules etc.
*
* @param <T> The object type.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public interface ObjectParser<T> {
/**
* Get the name of the format.
*
* @return The format name.
*/
String getParserFormat();
/**
* Parse the provided syntax in format without impacting any object registries.
*
* @param syntax the syntax in format.
* @param errors the {@link List} to use to report errors.
* @param warnings the {@link List} to be used to report warnings.
* @return The model name used for parsing if the parsing succeeded without errors; {@code null} otherwise.
*/
@Nullable
String startParsingFormat(String syntax, List<String> errors, List<String> warnings);
/**
* Get the objects found when parsing the format.
*
* @param modelName the model name whose objects to get.
* @return The {@link Collection} of objects.
*/
Collection<T> getParsedObjects(String modelName);
/**
* Release the resources from a previously started format parsing.
*
* @param modelName the model name whose resources to release.
*/
void finishParsingFormat(String modelName);
}
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.converter;
import java.io.OutputStream;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* A generic interface for serializers that serialize specific object types like Things, Items, Rules etc. into a
* serialized representation that is written to an {@link OutputStream}.
*
* @param <T> The object type.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public interface ObjectSerializer<T> {
/**
* Get the name of the format.
*
* @return The format name.
*/
String getGeneratedFormat();
/**
* Generate the format for all data that were associated to the provided identifier.
*
* @param id the identifier of the format generation.
* @param out The {@link OutputStream} to write to.
*/
void generateFormat(String id, OutputStream out);
}
@@ -26,14 +26,14 @@ import org.openhab.core.items.Metadata;
import org.openhab.core.library.CoreItemFactory;
/**
* {@link AbstractItemFileGenerator} is the base class for any {@link Item} file generator.
* {@link AbstractItemSerializer} is the base class for any {@link Item} serializer.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public abstract class AbstractItemFileGenerator implements ItemFileGenerator {
public abstract class AbstractItemSerializer implements ItemSerializer {
public AbstractItemFileGenerator() {
public AbstractItemSerializer() {
}
/**
@@ -1,59 +0,0 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.items.fileconverter;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.items.Item;
import org.openhab.core.items.Metadata;
/**
* {@link ItemFileGenerator} is the interface to implement by any file generator for {@link Item} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ItemFileGenerator {
/**
* Returns the format of the file.
*
* @return the file format
*/
String getFileFormatGenerator();
/**
* Define the list of items (including metadata and channel links) to be generated and associate them
* to an identifier.
*
* @param id the identifier of the file format generation
* @param items the items
* @param metadata the provided collection of metadata for these items (including channel links)
* @param stateFormatters the optional state formatter for each item
* @param hideDefaultParameters true to hide the configuration parameters having the default value
*/
void setItemsToBeGenerated(String id, List<Item> items, Collection<Metadata> metadata,
Map<String, String> stateFormatters, boolean hideDefaultParameters);
/**
* Generate the file format for all data that were associated to the provided identifier.
*
* @param id the identifier of the file format generation
* @param out the output stream to write to
*/
void generateFileFormat(String id, OutputStream out);
}
@@ -1,80 +0,0 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.items.fileconverter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.items.Metadata;
/**
* {@link ItemFileParser} is the interface to implement by any file parser for {@link Item} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ItemFileParser {
/**
* Returns the format of the syntax.
*
* @return the syntax format
*/
String getFileFormatParser();
/**
* Parse the provided syntax in file format without impacting the item and metadata registries.
*
* @param syntax the syntax in file format
* @param errors the list to be used to fill the errors
* @param warnings the list to be used to fill the warnings
* @return the model name used for parsing if the parsing succeeded without errors; null otherwise
*/
@Nullable
String startParsingFileFormat(String syntax, List<String> errors, List<String> warnings);
/**
* Get the {@link Item} objects found when parsing the file format.
*
* @param modelName the model name used for parsing
* @return the collection of items
*/
Collection<Item> getParsedItems(String modelName);
/**
* Get the {@link Metadata} objects found when parsing the file format.
*
* @param modelName the model name used for parsing
* @return the collection of metadata
*/
Collection<Metadata> getParsedMetadata(String modelName);
/**
* Get the state formatters found when parsing the file format.
*
* @param modelName the model name used for parsing
* @return the state formatters as a Map per item name
*/
Map<String, String> getParsedStateFormatters(String modelName);
/**
* Release the data from a previously started file format parsing.
*
* @param modelName the model name used for parsing
*/
void finishParsingFileFormat(String modelName);
}
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.items.fileconverter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.converter.ObjectParser;
import org.openhab.core.items.Item;
import org.openhab.core.items.Metadata;
/**
* {@link ItemParser} is the interface to implement by any file parser for {@link Item} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ItemParser extends ObjectParser<Item> {
/**
* Parse the provided {@code syntax} string without impacting the item and metadata registries.
*
* @param syntax the syntax in format.
* @param errors the {@link List} to use to report errors.
* @param warnings the {@link List} to be used to report warnings.
* @return The model name used for parsing if the parsing succeeded without errors; {@code null} otherwise.
*/
@Override
@Nullable
String startParsingFormat(String syntax, List<String> errors, List<String> warnings);
/**
* Get the {@link Item} objects found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return The {@link Collection} of {@link Item}s.
*/
@Override
Collection<Item> getParsedObjects(String modelName);
/**
* Get the {@link Metadata} objects found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return The {@link Collection} of {@link Metadata}.
*/
Collection<Metadata> getParsedMetadata(String modelName);
/**
* Get the state formatters found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return the state formatters as a {@link Map} per item name.
*/
Map<String, String> getParsedStateFormatters(String modelName);
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.items.fileconverter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.converter.ObjectSerializer;
import org.openhab.core.items.Item;
import org.openhab.core.items.Metadata;
/**
* {@link ItemSerializer} is the interface to implement by any file generator for {@link Item} object.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ItemSerializer extends ObjectSerializer<Item> {
/**
* Specify the {@link List} of {@link Item}s (including {@link Metadata} and channel links) to be serialized and
* associate them with an identifier.
*
* @param id the identifier of the {@link Item} format generation.
* @param items the {@link List} of {@link Item}s to serialize.
* @param metadata the provided {@link Collection} of {@link Metadata} for the {@link Item}s (including channel
* links).
* @param stateFormatters a {@link Map} of {@link Item} name and state formatter for each {@link Item}. Callers
* should pass an empty map if no state formatters are provided.
* @param hideDefaultParameters {@code true} to hide the configuration parameters having a default value.
*/
void setItemsToBeSerialized(String id, List<Item> items, Collection<Metadata> metadata,
Map<String, String> stateFormatters, boolean hideDefaultParameters);
}