]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
918eccbbed34f679cb0c1f48febde7b8b1285ff3
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.representation.utils;\r
13 \r
14 import java.io.StringReader;\r
15 import java.util.HashMap;\r
16 import java.util.List;\r
17 import java.util.regex.Matcher;\r
18 import java.util.regex.Pattern;\r
19 \r
20 import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
21 import org.simantics.sysdyn.expressionParser.ParseException;\r
22 import org.simantics.sysdyn.expressionParser.Token;\r
23 import org.simantics.sysdyn.representation.Configuration;\r
24 import org.simantics.sysdyn.representation.IElement;\r
25 import org.simantics.sysdyn.representation.Sheet;\r
26 import org.simantics.sysdyn.representation.Variable;\r
27 \r
28 public class SheetFormatUtils {\r
29     \r
30     public static String reformatSheetReferences(Variable v, String expression) {\r
31         if(expression == null || !expression.contains("("))\r
32             return expression;\r
33         ExpressionParser parser = new ExpressionParser(new StringReader(expression));\r
34         try {\r
35             parser.expr();\r
36 \r
37             HashMap<String, List<Token>> functionCalls = parser.getFunctionCallReferences();\r
38             for(String key : functionCalls.keySet()) {\r
39                 String[] parts = key.split("\\.");\r
40                 Object current = v.getParentConfiguration();\r
41                 Object found = null;\r
42                 for(int i = 0; i < parts.length && current != null; i++) {\r
43                     found = null;\r
44                     if(current instanceof Configuration) {\r
45                         for(IElement e : ((Configuration)current).getElements()) {\r
46                             if(e instanceof Configuration &&\r
47                                     ((Variable)e).getName().equals(parts[i])) {\r
48                                 found = e;\r
49                                 break;\r
50                             } else if(e instanceof Variable &&\r
51                                     ((Variable)e).getName().equals(parts[i])) {\r
52                                 found = e;\r
53                                 break;\r
54                             } \r
55                         }\r
56                     }\r
57                     current = found;\r
58                 }\r
59                 \r
60                 if(current != null && current instanceof Sheet) {\r
61                     \r
62                     String tmp = "";\r
63                     int start = 0, end = 0, call = 0;\r
64                     String cellOrRange = null;\r
65                     while((call = expression.indexOf(key, end)) >= 0) {\r
66                         start = expression.indexOf("(", call);\r
67                         \r
68                         tmp += expression.substring(end, start);\r
69                         \r
70                         end = expression.indexOf(")", start) + 1;\r
71                         if(start < 0 || end < 0 || end < start) {\r
72                             break;\r
73                         }\r
74                         cellOrRange = expression.substring(start, end);\r
75                         cellOrRange = cellOrRange.substring(1, cellOrRange.length() - 1);\r
76                         cellOrRange = cellOrRange.trim();\r
77                         cellOrRange = cellOrRange.replace(" ", "");\r
78                         \r
79                         Pattern p = Pattern.compile("[-\\+\\*\\/\\(\\)\\{\\}\\[\\],\\.\\t\\n\\r\\f]");\r
80                         Matcher m = p.matcher(cellOrRange);\r
81                         if (m.find() || cellOrRange.split(":").length > 2 || cellOrRange.length() == 0) {\r
82                             continue;\r
83                         }\r
84                         \r
85                         // Use the cell or range in the sheet.\r
86                         ((Sheet)current).use(cellOrRange);\r
87                         \r
88                         cellOrRange = cellOrRange.replace(":", "_");\r
89                         \r
90                         tmp += "." + cellOrRange;\r
91                     }\r
92                     tmp += expression.substring(end, expression.length());\r
93                     return tmp;\r
94                 }\r
95             }\r
96 \r
97         } catch (ParseException e) {\r
98         }\r
99         return expression;\r
100     }\r
101 \r
102 }\r