--- /dev/null
+package org.simantics.graph.compiler.internal.resourceFiles;\r
+\r
+import gnu.trove.set.hash.THashSet;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.graph.IResourceFile;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.PathChild;\r
+import org.simantics.graph.query.Paths;\r
+import org.simantics.graph.store.GraphStore;\r
+\r
+public class ResourceFileGenerator {\r
+ public static Collection<IResourceFile> generate(Paths paths, GraphStore store) {\r
+ int HasResourceClass = store.identities.pathToId(paths.HasResourceClass);\r
+ if(HasResourceClass < 0)\r
+ return Collections.emptyList();\r
+ \r
+ Collection<IResourceFile> result = new ArrayList<IResourceFile>();\r
+ \r
+ int resourceCount = store.identities.getResourceCount();\r
+ for(int ontology=0;ontology<resourceCount;++ontology) {\r
+ for(int v : store.statements.getObjects(ontology, HasResourceClass).toArray()) {\r
+ Variant value = store.values.getByteValue(v);\r
+ if(value == null)\r
+ continue;\r
+ try {\r
+ String className = (String)value.getValue(Bindings.STRING);\r
+ result.add(generate(paths, store, ontology, className));\r
+ } catch (AdaptException e) {\r
+ }\r
+ }\r
+ }\r
+ \r
+ return result;\r
+ }\r
+\r
+ private static ResourceFile generate(Paths paths,\r
+ GraphStore store,\r
+ int ontology,\r
+ String fullClassName) {\r
+ int p = fullClassName.lastIndexOf('.');\r
+ String packageName;\r
+ String className;\r
+ if(p>0) {\r
+ packageName = fullClassName.substring(0, p);\r
+ className = fullClassName.substring(p+1);\r
+ }\r
+ else {\r
+ packageName = "";\r
+ className = fullClassName;\r
+ }\r
+ \r
+ List<ResourceRef> resources = new ArrayList<ResourceRef>(); \r
+ findResources(store,\r
+ store.identities.pathToId(paths.Deprecated),\r
+ store.identities.idToPath(ontology),\r
+ ontology, \r
+ resources\r
+ );\r
+ \r
+ ResourceFile file = new ResourceFile(packageName, className, resources);\r
+ file.sort();\r
+ return file;\r
+ }\r
+\r
+ private static void findResources(GraphStore store,\r
+ int deprecatedId,\r
+ Path root,\r
+ int parent,\r
+ List<ResourceRef> resources) {\r
+ for(int child : store.identities.getChildren(parent)) {\r
+ ResourceRef ref = createResource(root, store.identities.idToPath(child));\r
+ if(!store.statements.getObjects(child, deprecatedId).isEmpty())\r
+ ref.deprecated = true;\r
+ resources.add(ref);\r
+ findResources(store, deprecatedId, root, child, resources);\r
+ }\r
+ }\r
+ \r
+ static THashSet<String> KEYWORDS = new THashSet<String>();\r
+ \r
+ static {\r
+ for(String s : new String[] {\r
+ "abstract", "continue", "for", "new", "switch",\r
+ "assert", "default", "goto", "package", "synchronized",\r
+ "boolean", "do", "if", "private", "this",\r
+ "break", "double", "implements", "protected", "throw",\r
+ "byte", "else", "import", "public", "throws",\r
+ "case", "enum", "instanceof", "return", "transient",\r
+ "catch", "extends", "int", "short", "try",\r
+ "char", "final", "interface", "static", "void",\r
+ "class", "finally", "long", "strictfp", "volatile",\r
+ "const", "float", "native", "super", "while",\r
+ "true", "false", "null"\r
+ })\r
+ KEYWORDS.add(s);\r
+ }\r
+\r
+ private static ResourceRef createResource(Path root, Path path) {\r
+ StringBuilder b = new StringBuilder();\r
+ javaName(b, root, path);\r
+ String javaName = b.toString();\r
+ if(KEYWORDS.contains(javaName))\r
+ javaName = javaName + "_";\r
+ else if(!Character.isJavaIdentifierStart(javaName.charAt(0)))\r
+ javaName = "_" + javaName;\r
+ return new ResourceRef(javaName, path.toString());\r
+ }\r
+ \r
+ private static void javaName(StringBuilder b, Path root, Path path) {\r
+ if(!root.equals(path)) {\r
+ PathChild pc = (PathChild)path;\r
+ javaName(b, root, pc.parent);\r
+ if(b.length() > 0)\r
+ b.append('_');\r
+ for(int i=0;i<pc.name.length();++i) {\r
+ char c = pc.name.charAt(i);\r
+ if(Character.isJavaIdentifierPart(c))\r
+ b.append(c);\r
+ else if(c==' ')\r
+ b.append('_');\r
+ else\r
+ b.append('$');\r
+ }\r
+ } \r
+ }\r
+}\r