X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2FresourceFiles%2FResourceFileGenerator.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2FresourceFiles%2FResourceFileGenerator.java;h=428dcfcba287f9e9f7e8cd685ba44cd3a64940cd;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFileGenerator.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFileGenerator.java new file mode 100644 index 000000000..428dcfcba --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFileGenerator.java @@ -0,0 +1,134 @@ +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 generate(Paths paths, GraphStore store) { + int HasResourceClass = store.identities.pathToId(paths.HasResourceClass); + if(HasResourceClass < 0) + return Collections.emptyList(); + + Collection result = new ArrayList(); + + int resourceCount = store.identities.getResourceCount(); + for(int ontology=0;ontology0) { + packageName = fullClassName.substring(0, p); + className = fullClassName.substring(p+1); + } + else { + packageName = ""; + className = fullClassName; + } + + List resources = new ArrayList(); + 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 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 KEYWORDS = new THashSet(); + + 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