]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Fix a problem in variable finding utility which caused variables inside modules to...
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 24 Apr 2015 13:10:19 +0000 (13:10 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 24 Apr 2015 13:10:19 +0000 (13:10 +0000)
fixes #5794

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31210 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java

index cac1aea8f188382406a4d54b109b1e1a75e9d9f0..5462572025d33feddbf4a9ad8f2af201de8ba72e 100644 (file)
@@ -161,35 +161,49 @@ public class RepresentationUtils {
 \r
 \r
        public static Variable getVariable(Configuration configuration, String name) {\r
+               if (name == null || name.isEmpty()) {\r
+                       return null;\r
+               }\r
+               \r
+               // this function seems to be used with both whitespaced and\r
+               // underscored versions of the variable name\r
+               String[] components = name.replace('_', ' ').split("\\.");\r
+               \r
                Configuration conf = configuration;\r
-               // This function seems to be used with both whitespaced and\r
-               // underscored versions of the variable name.\r
-               String whitespacedName = name.replace('_', ' ');\r
-               String[] components = whitespacedName.split("\\.");\r
-\r
-               for(String component : components) {\r
-\r
-                       Configuration newConf = null;\r
-\r
-                       for(IElement element : conf.getElements()) {\r
-                               if(element instanceof Variable) {\r
-                                       Variable variable = (Variable) element;\r
-                                       if(variable.getName().equals(component)) {\r
-                                               return variable;\r
-                                       }\r
-                               } else if(element instanceof Module) {\r
-                                       Module m = (Module)element;\r
-                                       if(m.getName().equals(component)) {\r
-                                               newConf = m.getType().getConfiguration();\r
-                                       }\r
+               \r
+               for (int i = 0; i < components.length-1; i++) {\r
+                       Module module = findModule(conf, components[i]);\r
+                       if (module == null) {\r
+                               // requested module could not be found\r
+                               return null;\r
+                       }\r
+                       conf = module.getType().getConfiguration();\r
+               }\r
+               \r
+               return findVariable(conf, components[components.length-1]);\r
+       }\r
+       \r
+       private static Module findModule(Configuration conf, String name) {\r
+               for (IElement element : conf.getElements()) {\r
+                       if (element instanceof Module) {\r
+                               Module module = (Module)element;\r
+                               if (module.getName().equals(name)) {\r
+                                       return module;\r
+                               }\r
+                       }\r
+               }\r
+               return null;\r
+       }\r
+       \r
+       private static Variable findVariable(Configuration conf, String name) {\r
+               for (IElement element : conf.getElements()) {\r
+                       if (element instanceof Variable) {\r
+                               Variable variable = (Variable)element;\r
+                               if (variable.getName().equals(name)) {\r
+                                       return variable;\r
                                }\r
                        }\r
-\r
-                       // If variable or configuration has not been found, return null\r
-                       if(newConf == null)\r
-                               return null;\r
                }\r
-\r
                return null;\r
        }\r
 \r