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