aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/hu.bme.mit.inf.yakinduModelExtractor/src/hu/bme/mit/inf/yakinduModelExtractor/ModelManager.java
blob: 951e44a32ef37739fc972fafd6e3da0c7d115d41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package hu.bme.mit.inf.yakinduModelExtractor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.yakindu.sct.model.sgraph.SGraphPackage;

public class ModelManager {
	ResourceSet resourceSet;
	
	public ModelManager() {
		init();
	}
	
	public void init() {
		SGraphPackage.eINSTANCE.eClass();
		NotationPackage.eINSTANCE.eClass();
		resourceSet = new ResourceSetImpl();
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
	}
	
	public List<String> loadAllModelPathsInDirectory(String path) {
		File directory = new File(path);
		if(directory.exists() && directory.isDirectory()) {
			List<String> filePaths = new LinkedList<>();
			for(File f  : directory.listFiles()) {
				if(f.isFile()) {
					String filePath =  f.getPath();
					if(filePath.endsWith("sct")) {
						filePaths.add(filePath);
					}
				}
			}
			return filePaths;
		} else {
			throw new IllegalArgumentException("invalid path");
		}
	}
	
	public EObject loadModel(String path) {
		Resource resource = this.resourceSet.getResource(URI.createFileURI(path), true);
		return resource.getContents().get(0);
	}
	
	public boolean saveModel(EObject root, String path) {
		Resource resource = this.resourceSet.createResource(URI.createURI(path));
		resource.getContents().add(root);
		try {
			resource.save(null);
			return true;
		} catch (IOException e) {
			System.err.println("Unable to save file: "+path);
			return false;
		}
	}
	
	public boolean saveFile(String path, String content) {
		try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "utf-8"))) {
		   writer.write(content.toString());
		   return true;
		} catch (IOException ex) {
			System.err.println("Unable to save file: "+path);
			return false;
		}
	}
}