Skip to main content
summaryrefslogtreecommitdiffstats
blob: b2ff33291a6c8b6916676eb0b6c4fc3cdee3d258 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.emf.compare.epatch.tests.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
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;

/**
 * @author Moritz Eysholdt - Initial contribution and API
 */
public class DynEObject {
	protected EObject obj;

	public DynEObject(EObject obj) {
		super();
		this.obj = obj;
	}

	public EObject eObj() {
		return obj;
	}

	public DynEObject(Resource res) {
		super();
		this.obj = res.getContents().get(0);
	}

	public EStructuralFeature eGetFeature(String name) {
		EStructuralFeature f = obj.eClass().getEStructuralFeature(name);
		if (f != null)
			return f;
		ArrayList<String> features = new ArrayList<String>();
		for (EStructuralFeature sf : obj.eClass().getEAllStructuralFeatures())
			features.add(sf.getName());
		throw new RuntimeException("Feature '" + name + "' not found. Available features: " + features
				+ " for class " + obj.eClass().getName());
	}

	public static DynEObject eGetResource(ResourceSet rs, String part) {
		for (Resource r : rs.getResources())
			if (r.getURI().toString().contains(part))
				return new DynEObject(r.getContents().get(0));
		throw new RuntimeException("Resource " + part + " not found!");
	};

	public static Resource eLoadResource(ResourceSet rs, Class<?> loader, String model) {
		final String path = loader.getPackage().getName().replace('.', '/');
		// final String file = "/" + path + "/" + model;
		// final URL url = loader.getResource(file);
		// if (url == null)
		// throw new RuntimeException(file + " not found on classpath.");
		// return rs.getResource(URI.createURI(url.toString()), true);
		URI file = URI.createURI(model);
		URL url = loader.getResource("/" + path + "/" + file);
		if (url != null)
			file = URI.createURI(url.toString());
		return rs.getResource(file, true);
	}

	public static ResourceSet eCreateResourceSet(Class<?> loader, String... models) {
		// final String path = loader.getPackage().getName().replace('.', '/');
		final ResourceSet rs = new ResourceSetImpl();
		Map<String, Object> map = rs.getResourceFactoryRegistry().getExtensionToFactoryMap();
		map.put("ecore", new XMIResourceFactoryImpl());
		map.put("xmi", new XMIResourceFactoryImpl());
		for (String m : models)
			eLoadResource(rs, loader, m);
		// URI file = URI.createURI(m);
		// URL url = loader.getResource("/" + path + "/" + file);
		// if (url != null)
		// file = URI.createURI(url.toString());
		// rs.getResource(file, true);
		// }
		return rs;
	}

	public DynEObject eGetObj(String feat, int index) {
		return toDynObj(listGet(obj.eGet(eGetFeature(feat)), index), feat);
	}

	@SuppressWarnings("unchecked")
	public DynEObject eGetObj(String feature, String matchFeature, String matchValue) {
		Object list = eGetVal(feature);
		if (list instanceof List) {
			for (EObject o : (List<EObject>)list) {
				EStructuralFeature f = o.eClass().getEStructuralFeature(matchFeature);
				if (f != null && matchValue.equals(o.eGet(f)))
					return new DynEObject(o);
			}
			return null;
		} else
			throw new RuntimeException("This is not a list: " + list);
	}

	@SuppressWarnings("unchecked")
	private Object listGet(Object list, int index) {
		if (list instanceof List)
			return ((List<Object>)list).get(index);
		throw new RuntimeException("This is not a list: " + list);
	}

	private DynEObject toDynObj(Object o, String feat) {
		if (o instanceof EObject)
			return new DynEObject((EObject)o);
		String t = (o != null) ? o.getClass().getName() : "null";
		throw new RuntimeException("The from " + obj.eClass().getName() + "." + feat
				+ " is not an EObject, but a " + t);

	}

	public DynEObject eGetObj(String feat) {
		return toDynObj(obj.eGet(eGetFeature(feat)), feat);
	}

	public DynEObject eUnset(String feat) {
		obj.eUnset(eGetFeature(feat));
		return this;
	}

	public DynEObject eSet(String feat, Object value) {
		obj.eSet(eGetFeature(feat), value);
		return this;
	}

	public DynEObject eSetNew(String feat, String typeName) {
		DynEObject o = newObj(typeName);
		obj.eSet(eGetFeature(feat), o.obj);
		return o;
	}

	public DynEObject eSetType(String feat, String typeName) {
		EClassifier o = eGetType(typeName);
		obj.eSet(eGetFeature(feat), o);
		return this;
	}

	public Object eGetVal(String feat) {
		return obj.eGet(eGetFeature(feat));
	}

	@SuppressWarnings("unchecked")
	public void eAdd(String feat, Object value) {
		((EList<Object>)obj.eGet(eGetFeature(feat))).add(value);
	}

	@SuppressWarnings("unchecked")
	public void eAdd(String feat, int index, Object value) {
		((EList<Object>)obj.eGet(eGetFeature(feat))).add(index, value);
	}

	@SuppressWarnings("unchecked")
	public void eSet(String feat, int index, Object value) {
		((EList<Object>)obj.eGet(eGetFeature(feat))).set(index, value);
	}

	@SuppressWarnings("unchecked")
	public void eMove(String feat, int newPosition, int oldPosition) {
		((EList<Object>)obj.eGet(eGetFeature(feat))).move(newPosition, oldPosition);
	}

	@SuppressWarnings("unchecked")
	public void eRemove(String feat, int index) {
		((EList<Object>)obj.eGet(eGetFeature(feat))).remove(index);
	}

	@SuppressWarnings("unchecked")
	public void eRemove(String feat, String feature, String matchValue) {
		EList<EObject> list = (EList<EObject>)obj.eGet(eGetFeature(feat));
		for (Iterator<EObject> i = list.iterator(); i.hasNext();) {
			EObject o = i.next();
			EStructuralFeature f = o.eClass().getEStructuralFeature(feature);
			if (f != null && matchValue.equals(o.eGet(f)))
				i.remove();
		}
	}

	public DynEObject eAddNew(String feat, String type) {
		DynEObject o = newObj(type);
		eAdd(feat, o.obj);
		return o;
	}

	public EClassifier eGetType(String typeName) {
		EPackage pkg = obj.eClass().getEPackage();
		EClassifier cls = pkg.getEClassifier(typeName);
		if (cls == null) {
			ArrayList<String> types = new ArrayList<String>();
			for (EClassifier c : pkg.getEClassifiers())
				types.add(c.getName());
			throw new RuntimeException("Type '" + typeName + "' not found in package " + pkg.getName()
					+ ". Available types:" + types + " Package namespace:" + pkg.getNsURI());
		}
		return cls;
	}

	public DynEObject newObj(String typeName) {
		EClassifier cls = eGetType(typeName);
		if (cls instanceof EClass) {
			return new DynEObject(cls.getEPackage().getEFactoryInstance().create((EClass)cls));
		} else
			throw new RuntimeException("Type '" + typeName + " is not an EClass, but an "
					+ cls.eClass().getName());
	}

}

Back to the top