From eb9c58c4e7385579f9625913858254931997f489 Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Thu, 24 Mar 2022 11:00:54 +0100
Subject: [PATCH] Expose createResource(), add saveResource()

Issue-Ref: 4223
Issue-Url: https://git.fortiss.org/af3/af3/-/issues/4223

Signed-off-by: Simon Barner <barner@fortiss.org>
---
 .../org/fortiss/tooling/kernel/utils/.ratings  |  2 +-
 .../kernel/utils/EcoreSerializerBase.java      | 18 ++++++++++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings
index db56108a5..1f61338d2 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings
@@ -1,6 +1,6 @@
 CompositionUtils.java 34c0a191bd0fb4176c94b4d61abb5c88a679d5e8 GREEN
 EMFResourceUtils.java 68e6712a52349548bf85346900b17aa65b5f0ea9 GREEN
-EcoreSerializerBase.java 0a0c2969d793d2e68094c55c8f7b0a662ef6e5d5 GREEN
+EcoreSerializerBase.java f906e6578ca512e0c83a822ebf6ad17ca5bd3b76 YELLOW
 EcoreUtils.java a62460d445203b69ec6029479929da77e887866a GREEN
 ExtensionPointUtils.java 7ce63242b49eb9a7cd4eaadd223f5ebce1dfd75b GREEN
 HierarchicalNameComparator.java 6face1b673126701a0721af48ead2f9766c17d46 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/EcoreSerializerBase.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/EcoreSerializerBase.java
index 0a0c2969d..f906e6578 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/EcoreSerializerBase.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/EcoreSerializerBase.java
@@ -19,6 +19,7 @@ import static org.eclipse.emf.ecore.util.EcoreUtil.getIdentification;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.Map;
@@ -288,10 +289,23 @@ public abstract class EcoreSerializerBase<R extends EObject> {
 
 		validate(rootElement);
 
+		saveResource(resource, null);
+	}
+
+	/**
+	 * Saves the given {@link Resource} with the options provided by {@link #getOptions()}. In case
+	 * the provided {@link OutputStream} is not {@code null}, the resource is saved to it.
+	 * Otherwise, it is saved to the URI as configured during the creation of the {@link Resource}.
+	 */
+	protected void saveResource(Resource resource, OutputStream ostream) throws IOException {
 		if(resource instanceof XMLResource) {
 			((XMLResource)resource).setEncoding(charset.name());
 		}
-		resource.save(getOptions());
+		if(ostream == null) {
+			resource.save(getOptions());
+		} else {
+			resource.save(ostream, getOptions());
+		}
 	}
 
 	/**
@@ -424,7 +438,7 @@ public abstract class EcoreSerializerBase<R extends EObject> {
 	 * 
 	 * @throws IOException
 	 */
-	private final ResourceSet createResourceSet() throws IOException {
+	protected final ResourceSet createResourceSet() throws IOException {
 		ResourceSet resourceSet = new ResourceSetImpl();
 		resourceSet.getPackageRegistry().put(eNsURI, ePackage);
 
-- 
GitLab