aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java')
-rw-r--r--Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java
new file mode 100644
index 00000000..85fd208c
--- /dev/null
+++ b/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java
@@ -0,0 +1,79 @@
1package hu.bme.mit.inf.yakinduModelExtractor;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStreamWriter;
8import java.io.Writer;
9import java.util.LinkedList;
10import java.util.List;
11
12import org.eclipse.emf.common.util.URI;
13import org.eclipse.emf.ecore.EObject;
14import org.eclipse.emf.ecore.resource.Resource;
15import org.eclipse.emf.ecore.resource.ResourceSet;
16import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
17import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
18import org.eclipse.gmf.runtime.notation.NotationPackage;
19import org.yakindu.sct.model.sgraph.SGraphPackage;
20
21public class ModelManager {
22 ResourceSet resourceSet;
23
24 public ModelManager() {
25 init();
26 }
27
28 public void init() {
29 SGraphPackage.eINSTANCE.eClass();
30 NotationPackage.eINSTANCE.eClass();
31 resourceSet = new ResourceSetImpl();
32 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
33 }
34
35 public List<String> loadAllModelPathsInDirectory(String path) {
36 File directory = new File(path);
37 if(directory.exists() && directory.isDirectory()) {
38 List<String> filePaths = new LinkedList<>();
39 for(File f : directory.listFiles()) {
40 if(f.isFile()) {
41 String filePath = f.getPath();
42 if(filePath.endsWith("sct")) {
43 filePaths.add(filePath);
44 }
45 }
46 }
47 return filePaths;
48 } else {
49 throw new IllegalArgumentException("invalid path");
50 }
51 }
52
53 public EObject loadModel(String path) {
54 Resource resource = this.resourceSet.getResource(URI.createFileURI(path), true);
55 return resource.getContents().get(0);
56 }
57
58 public boolean saveModel(EObject root, String path) {
59 Resource resource = this.resourceSet.createResource(URI.createURI(path));
60 resource.getContents().add(root);
61 try {
62 resource.save(null);
63 return true;
64 } catch (IOException e) {
65 System.err.println("Unable to save file: "+path);
66 return false;
67 }
68 }
69
70 public boolean saveFile(String path, String content) {
71 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "utf-8"))) {
72 writer.write(content.toString());
73 return true;
74 } catch (IOException ex) {
75 System.err.println("Unable to save file: "+path);
76 return false;
77 }
78 }
79}