blob: 410eb54996270c3f6be858a400f6faf44f4e0d22 (
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
|
/*******************************************************************************
* Copyright (c) 2005-2009 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.xtend.typesystem.emf;
import java.lang.reflect.Field;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.mwe.core.ConfigurationException;
import org.eclipse.emf.mwe.core.resources.ResourceLoaderFactory;
/**
* Initializes EMF support. Allows to register additional Packages.
*/
public class Setup {
/**
* Default Constructor. Registers the Ecore Package.
*
*/
public Setup() {
addEPackageClass(EcorePackage.eINSTANCE.getClass().getName());
}
public void addEPackageDescriptor(final String descr) {
EcoreUtil2.getEPackageByDescriptorClassName(descr);
}
public void addEPackageFile(final String descr) {
EcoreUtil2.getEPackage(descr);
}
public void addEPackageClass(final String clazz) {
EcoreUtil2.getEPackageByClassName(clazz);
}
@SuppressWarnings("unchecked")
public void addUriMap(final Mapping uriMap) {
final URI baseUri = URI.createURI(uriMap.getFrom());
final URI mappedUri = EcoreUtil2.getURI(uriMap.getTo());
if (mappedUri == null)
throw new ConfigurationException("cannot make URI out of " + uriMap.getTo());
else {
URIConverter.URI_MAP.put(baseUri, mappedUri);
}
}
/**
* Adds an extension
* @param m <tt>from</tt>: extension name, <tt>to</tt> factory classname
* @throws ConfigurationException
* <ul>
* <li> The factory class for the extension cannot be found
* <li> The inner factory class for the extension cannot be found
* </ul>
*/
@SuppressWarnings("unchecked")
public void addExtensionMap(final Mapping m) throws ConfigurationException {
try {
// locate the factory class of the extension
Class factoryClass = ResourceLoaderFactory.createResourceLoader().loadClass(m.getTo());
if (factoryClass == null)
throw new ConfigurationException("cannot find class " + m.getTo() + " for extension " + m.getFrom());
Object factoryInstance = null;
if (factoryClass.isInterface()) {
final Class[] innerClasses = factoryClass.getDeclaredClasses();
factoryClass = null;
for (int j = 0; j < innerClasses.length; j++) {
if (Resource.Factory.class.isAssignableFrom(innerClasses[j])) {
factoryClass = innerClasses[j];
}
}
if (factoryClass == null)
throw new ConfigurationException("cannot find inner factory class " + m.getTo() + " for extension " + m.getFrom());
final Field instanceField = factoryClass.getField("INSTANCE");
factoryInstance = instanceField.get(null);
} else {
factoryInstance = factoryClass.newInstance();
}
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(m.getFrom(), factoryInstance);
} catch (final Exception e) {
throw new ConfigurationException(e);
}
}
}
|