1 package org.simantics.scl.reflection.internal.registry;
\r
3 import gnu.trove.map.hash.THashMap;
\r
4 import gnu.trove.procedure.TObjectObjectProcedure;
\r
6 import org.eclipse.core.runtime.IConfigurationElement;
\r
7 import org.eclipse.core.runtime.IContributor;
\r
8 import org.eclipse.core.runtime.Platform;
\r
9 import org.eclipse.core.runtime.spi.RegistryContributor;
\r
10 import org.osgi.framework.Bundle;
\r
11 import org.simantics.scl.compiler.types.TCon;
\r
12 import org.simantics.scl.reflection.OntologyVersions;
\r
13 import org.simantics.scl.reflection.TypeNotFoundException;
\r
14 import org.simantics.scl.reflection.TypedValue;
\r
15 import org.simantics.scl.reflection.ValueNotFoundException;
\r
16 import org.simantics.scl.reflection.internal.Activator;
\r
18 public class BindingRegistry {
\r
20 private static boolean DEBUG_INIT = false;
\r
22 private static THashMap<String, Namespace> namespaces =
\r
23 new THashMap<String, Namespace>();
\r
25 public static TypedValue getValue(String path, String name) throws ValueNotFoundException {
\r
26 Namespace namespace = namespaces.get(path);
\r
27 if(namespace == null)
\r
28 throw new ValueNotFoundException("Didn't find namespace " + path + ".");
\r
29 return namespace.getValue(name);
\r
32 public static Class<?> getClass(TCon type) throws TypeNotFoundException {
\r
33 Namespace namespace = namespaces.get(type.module);
\r
34 if(namespace == null)
\r
35 throw new TypeNotFoundException("Didn't find namespace " + type.module + ".");
\r
36 return namespace.getClass(type.name);
\r
40 * Called directly only for testing
\r
42 public static void initialize() {
\r
44 IConfigurationElement[] elements =
\r
45 Platform.getExtensionRegistry().getConfigurationElementsFor("org.simantics.scl.reflection.binding");
\r
46 for(IConfigurationElement element : elements) {
\r
47 Bundle bundle = getBundle(element);
\r
48 findBindings(bundle, null, element, null);
\r
55 namespaces.forEachEntry(new TObjectObjectProcedure<String, Namespace>() {
\r
58 public boolean execute(String name, Namespace namespace) {
\r
59 System.out.println(name);
\r
67 private static Bundle getBundle(IConfigurationElement element) {
\r
68 IContributor contributor = element.getContributor();
\r
69 if(contributor instanceof RegistryContributor) {
\r
70 long id = Long.parseLong(((RegistryContributor) contributor).getActualId());
\r
71 return Activator.getInstance().getContext().getBundle(id);
\r
74 return Platform.getBundle(contributor.getName());
\r
77 private static Namespace getOrCreateNamespace(String path, ImportSeq importSeq) {
\r
78 Namespace namespace = namespaces.get(path);
\r
79 if(namespace == null) {
\r
80 namespace = new Namespace(path, importSeq);
\r
81 namespaces.put(path, namespace);
\r
86 private static void findBindings(
\r
89 IConfigurationElement element,
\r
90 ImportSeq importSeq) {
\r
91 String elementName = element.getName();
\r
92 if(elementName.equals("namespace")) {
\r
94 path = OntologyVersions.getInstance().currentVersion(element.getAttribute("path"));
\r
96 path = OntologyVersions.getInstance().currentVersion(path + "/" + element.getAttribute("path"));
\r
97 if(path.endsWith("/"))
\r
98 path = path.substring(0, path.length()-1);
\r
99 for(IConfigurationElement childElement : element.getChildren())
\r
100 if(childElement.getName().equals("import")) {
\r
101 String path_ = OntologyVersions.getInstance().currentVersion(childElement.getAttribute("path"));
\r
102 String localName = childElement.getAttribute("localName");
\r
103 importSeq = new ImportSeq(importSeq, localName, path_);
\r
105 for(IConfigurationElement childElement : element.getChildren())
\r
106 findBindings(bundle, path, childElement, importSeq);
\r
108 else if(elementName.equals("class")) {
\r
109 String className = element.getAttribute("className");
\r
110 getOrCreateNamespace(path, importSeq)
\r
111 .addClass(new Entry(bundle, className));
\r
113 else if(elementName.equals("externalClass")) {
\r
114 String className = element.getAttribute("className");
\r
115 String alternativeName = element.getAttribute("alternativeName");
\r
116 getOrCreateNamespace(path, importSeq).addExternalClass(
\r
117 new ExternalClass(bundle, className, alternativeName)
\r
120 else if(elementName.equals("externalMethod")) {
\r
121 String className = element.getAttribute("className");
\r
122 String methodName = element.getAttribute("methodName");
\r
123 String alternativeName = element.getAttribute("alternativeName");
\r
124 getOrCreateNamespace(path, importSeq).addExternalMethod(
\r
125 new ExternalMethod(bundle, className, methodName, alternativeName)
\r