]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFileGenerator.java
428dcfcba287f9e9f7e8cd685ba44cd3a64940cd
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / resourceFiles / ResourceFileGenerator.java
1 package org.simantics.graph.compiler.internal.resourceFiles;\r
2 \r
3 import gnu.trove.set.hash.THashSet;\r
4 \r
5 import java.util.ArrayList;\r
6 import java.util.Collection;\r
7 import java.util.Collections;\r
8 import java.util.List;\r
9 \r
10 import org.simantics.databoard.Bindings;\r
11 import org.simantics.databoard.adapter.AdaptException;\r
12 import org.simantics.databoard.binding.mutable.Variant;\r
13 import org.simantics.graph.IResourceFile;\r
14 import org.simantics.graph.query.Path;\r
15 import org.simantics.graph.query.PathChild;\r
16 import org.simantics.graph.query.Paths;\r
17 import org.simantics.graph.store.GraphStore;\r
18 \r
19 public class ResourceFileGenerator {\r
20         public static Collection<IResourceFile> generate(Paths paths, GraphStore store) {\r
21                 int HasResourceClass = store.identities.pathToId(paths.HasResourceClass);\r
22                 if(HasResourceClass < 0)\r
23                         return Collections.emptyList();\r
24                 \r
25                 Collection<IResourceFile> result = new ArrayList<IResourceFile>();\r
26                 \r
27                 int resourceCount = store.identities.getResourceCount();\r
28                 for(int ontology=0;ontology<resourceCount;++ontology) {\r
29                         for(int v : store.statements.getObjects(ontology, HasResourceClass).toArray()) {\r
30                                 Variant value = store.values.getByteValue(v);\r
31                                 if(value == null)\r
32                                         continue;\r
33                                 try {\r
34                                         String className = (String)value.getValue(Bindings.STRING);\r
35                                         result.add(generate(paths, store, ontology, className));\r
36                                 } catch (AdaptException e) {\r
37                 }\r
38                         }\r
39                 }\r
40                 \r
41                 return result;\r
42         }\r
43 \r
44         private static ResourceFile generate(Paths paths,\r
45                 GraphStore store,\r
46                         int ontology,\r
47                         String fullClassName) {\r
48                 int p = fullClassName.lastIndexOf('.');\r
49                 String packageName;\r
50                 String className;\r
51                 if(p>0) {\r
52                         packageName = fullClassName.substring(0, p);\r
53                         className = fullClassName.substring(p+1);\r
54                 }\r
55                 else {\r
56                         packageName = "";\r
57                         className = fullClassName;\r
58                 }\r
59                 \r
60                 List<ResourceRef> resources = new ArrayList<ResourceRef>(); \r
61                 findResources(store,\r
62                                 store.identities.pathToId(paths.Deprecated),\r
63                                 store.identities.idToPath(ontology),\r
64                                 ontology, \r
65                                 resources\r
66                         );\r
67                 \r
68                 ResourceFile file = new ResourceFile(packageName, className, resources);\r
69                 file.sort();\r
70                 return file;\r
71         }\r
72 \r
73         private static void findResources(GraphStore store,\r
74                         int deprecatedId,\r
75                         Path root,\r
76                         int parent,\r
77                         List<ResourceRef> resources) {\r
78                 for(int child : store.identities.getChildren(parent)) {\r
79                         ResourceRef ref = createResource(root, store.identities.idToPath(child));\r
80                         if(!store.statements.getObjects(child, deprecatedId).isEmpty())\r
81                                 ref.deprecated = true;\r
82                         resources.add(ref);\r
83                         findResources(store, deprecatedId, root, child, resources);\r
84                 }\r
85         }\r
86         \r
87         static THashSet<String> KEYWORDS = new THashSet<String>();\r
88         \r
89         static {\r
90                 for(String s : new String[] {\r
91                                 "abstract", "continue", "for", "new", "switch",\r
92                                 "assert", "default", "goto", "package", "synchronized",\r
93                                 "boolean", "do", "if", "private", "this",\r
94                                 "break", "double", "implements", "protected", "throw",\r
95                                 "byte", "else", "import", "public", "throws",\r
96                                 "case", "enum", "instanceof", "return", "transient",\r
97                                 "catch", "extends", "int", "short", "try",\r
98                                 "char", "final", "interface", "static", "void",\r
99                                 "class", "finally", "long", "strictfp", "volatile",\r
100                                 "const", "float", "native", "super", "while",\r
101                                 "true", "false", "null"\r
102                 })\r
103                         KEYWORDS.add(s);\r
104         }\r
105 \r
106         private static ResourceRef createResource(Path root, Path path) {\r
107                 StringBuilder b = new StringBuilder();\r
108                 javaName(b, root, path);\r
109                 String javaName = b.toString();\r
110                 if(KEYWORDS.contains(javaName))\r
111                         javaName = javaName + "_";\r
112                 else if(!Character.isJavaIdentifierStart(javaName.charAt(0)))\r
113                         javaName = "_" + javaName;\r
114                 return new ResourceRef(javaName, path.toString());\r
115         }\r
116         \r
117         private static void javaName(StringBuilder b, Path root, Path path) {\r
118                 if(!root.equals(path)) {\r
119                         PathChild pc = (PathChild)path;\r
120                         javaName(b, root, pc.parent);\r
121                         if(b.length() > 0)\r
122                                 b.append('_');\r
123                         for(int i=0;i<pc.name.length();++i) {\r
124                                 char c = pc.name.charAt(i);\r
125                                 if(Character.isJavaIdentifierPart(c))\r
126                                         b.append(c);\r
127                                 else if(c==' ')\r
128                                         b.append('_');\r
129                                 else\r
130                                         b.append('$');\r
131                         }\r
132                 }               \r
133         }\r
134 }\r