Skip to content

Instantly share code, notes, and snippets.

@pvlasov
Created January 7, 2019 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvlasov/22b59513628e21b33cb0b270c999ad8c to your computer and use it in GitHub Desktop.
Save pvlasov/22b59513628e21b33cb0b270c999ad8c to your computer and use it in GitHub Desktop.
Demonstrates how to create an EMF resource loading/storing (configuration) model from/to YAML files. It can be used for creating configuration editors for Spring applications, e.g. microservices.
// --- Resource ---
public class YamlResourceImpl extends ResourceImpl {
private Map<String, Object> data;
public YamlResourceImpl(URI uri) {
super(uri);
}
@SuppressWarnings("unchecked")
@Override
protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException {
Yaml yaml = new Yaml();
data = (Map<String, Object>) yaml.load(inputStream);
// Create model content here ...
getContents().add(content);
}
@Override
protected void doSave(OutputStream outputStream, Map<?, ?> options) throws IOException {
if (data == null) {
data = new LinkedHashMap<>();
}
// Save to data.
getContents()...
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK);
dumperOptions.setIndent(4);
Yaml yaml = new Yaml(dumperOptions);
yaml.dump(data, new OutputStreamWriter(outputStream));
}
}
// --- Resource factory ---
public class YamlResourceFactoryImpl extends ResourceFactoryImpl {
@Override
public Resource createResource(URI uri) {
return new YamlResourceImpl(uri);
}
}
// --- Resource set - in wizard and editor - simple implementation ---
ResourceSet resourceSet = new ResourceSetImpl();
Resource.Factory.Registry resourceFactoryRegistry = new ResourceFactoryRegistryImpl() {
@Override
protected Resource.Factory delegatedGetFactory(URI uri, String contentTypeIdentifier) {
// This editor works only with YAML files which are assumed to be CCP configuration files.
return new YamlResourceFactoryImpl();
}
@Override
protected URIConverter getURIConverter() {
return resourceSet.getURIConverter();
}
};
resourceSet.setResourceFactoryRegistry(resourceFactoryRegistry);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment