1 package org.simantics.graph.compiler.internal.resourceFiles;
\r
3 import gnu.trove.set.hash.THashSet;
\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
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
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
25 Collection<IResourceFile> result = new ArrayList<IResourceFile>();
\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
34 String className = (String)value.getValue(Bindings.STRING);
\r
35 result.add(generate(paths, store, ontology, className));
\r
36 } catch (AdaptException e) {
\r
44 private static ResourceFile generate(Paths paths,
\r
47 String fullClassName) {
\r
48 int p = fullClassName.lastIndexOf('.');
\r
52 packageName = fullClassName.substring(0, p);
\r
53 className = fullClassName.substring(p+1);
\r
57 className = fullClassName;
\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
68 ResourceFile file = new ResourceFile(packageName, className, resources);
\r
73 private static void findResources(GraphStore store,
\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
83 findResources(store, deprecatedId, root, child, resources);
\r
87 static THashSet<String> KEYWORDS = new THashSet<String>();
\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
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
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
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