]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.reflection/src/org/simantics/scl/reflection/internal/registry/BindingRegistry.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.reflection / src / org / simantics / scl / reflection / internal / registry / BindingRegistry.java
1 package org.simantics.scl.reflection.internal.registry;\r
2 \r
3 import gnu.trove.map.hash.THashMap;\r
4 import gnu.trove.procedure.TObjectObjectProcedure;\r
5 \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
17 \r
18 public class BindingRegistry {\r
19 \r
20     private static boolean DEBUG_INIT = false;\r
21 \r
22     private static THashMap<String, Namespace> namespaces =\r
23             new THashMap<String, Namespace>();\r
24         \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
30     }\r
31     \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
37     }   \r
38 \r
39     /*\r
40      * Called directly only for testing \r
41      */\r
42     public static void initialize() {\r
43         namespaces.clear();\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
49         }\r
50     }\r
51     \r
52     static {\r
53         initialize();\r
54         if (DEBUG_INIT) {\r
55             namespaces.forEachEntry(new TObjectObjectProcedure<String, Namespace>() {\r
56 \r
57                 @Override\r
58                 public boolean execute(String name, Namespace namespace) {\r
59                     System.out.println(name);\r
60                     namespace.print();\r
61                     return true;\r
62                 }\r
63             });\r
64         }\r
65     }\r
66     \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
72         }\r
73         else\r
74             return Platform.getBundle(contributor.getName());\r
75     }\r
76 \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
82         }\r
83         return namespace;\r
84     }\r
85     \r
86     private static void findBindings(\r
87             Bundle bundle,\r
88             String path,\r
89             IConfigurationElement element,\r
90             ImportSeq importSeq) {\r
91         String elementName = element.getName();        \r
92         if(elementName.equals("namespace")) {\r
93             if(path == null)\r
94                 path = OntologyVersions.getInstance().currentVersion(element.getAttribute("path"));\r
95             else\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
104                 }\r
105             for(IConfigurationElement childElement : element.getChildren())\r
106                 findBindings(bundle, path, childElement, importSeq);\r
107         }\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
112         }\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
118                     );\r
119         }\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
126                     );\r
127         }\r
128     } \r
129 }\r