1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.representation.utils;
\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
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
28 public class SheetFormatUtils {
\r
30 public static String reformatSheetReferences(Variable v, String expression) {
\r
31 if(expression == null || !expression.contains("("))
\r
33 ExpressionParser parser = new ExpressionParser(new StringReader(expression));
\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
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
50 } else if(e instanceof Variable &&
\r
51 ((Variable)e).getName().equals(parts[i])) {
\r
60 if(current != null && current instanceof Sheet) {
\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
68 tmp += expression.substring(end, start);
\r
70 end = expression.indexOf(")", start) + 1;
\r
71 if(start < 0 || end < 0 || end < start) {
\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
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
85 // Use the cell or range in the sheet.
\r
86 ((Sheet)current).use(cellOrRange);
\r
88 cellOrRange = cellOrRange.replace(":", "_");
\r
90 tmp += "." + cellOrRange;
\r
92 tmp += expression.substring(end, expression.length());
\r
97 } catch (ParseException e) {
\r