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