]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.wiki.ui/src/org/simantics/wiki/ui/SimanticsDialect.java
Fixing modelled STS tests to not include nested JUnit suites
[simantics/platform.git] / bundles / org.simantics.wiki.ui / src / org / simantics / wiki / ui / SimanticsDialect.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.wiki.ui;\r
13 \r
14 import java.text.DateFormat;\r
15 import java.text.SimpleDateFormat;\r
16 import java.util.Date;\r
17 import java.util.HashMap;\r
18 import java.util.Map;\r
19 import java.util.regex.Matcher;\r
20 import java.util.regex.Pattern;\r
21 \r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.layer0.variable.Variable;\r
26 import org.simantics.layer0.Layer0;\r
27 \r
28 \r
29 public class SimanticsDialect implements MarkupTransform {\r
30     \r
31     final static public SimanticsDialect INSTANCE = new SimanticsDialect();\r
32 \r
33     class TrendTransform implements MarkupTransform {\r
34 \r
35         final Pattern startPattern = Pattern.compile("\\{trend(?::([^\\}]+))?\\}");\r
36         \r
37         @Override\r
38         public String apply(ReadGraph graph, Variable variable, String markup) {\r
39             \r
40             StringBuffer sb = new StringBuffer();\r
41             Matcher matcher = startPattern.matcher(markup);\r
42             while(matcher.find()) {\r
43                 matcher.appendReplacement(sb, "");\r
44                 String options = matcher.group(1);\r
45                 //String path = "";\r
46                 Map<String, String> params = new HashMap<String, String>();\r
47                 if (options != null) {\r
48                     String[] optionPairs = options.split("\\s*\\|\\s*");\r
49                     for (String optionPair: optionPairs) {\r
50                         String[] keyValue = optionPair.split("\\s*=\\s*");\r
51                         if (keyValue.length == 2) {\r
52                             String key = keyValue[0].trim();\r
53                             String value = keyValue[1].trim();\r
54                             params.put(key, value);\r
55                             /*                           \r
56                             if (key.equals("path")) {\r
57                                 path = value;\r
58                             }\r
59                             */\r
60                         }\r
61                     }\r
62                 }\r
63 //                sb.append("[[Image:" + path + "]]"); // FIXME: fetch value from somewhere...\r
64                 int width = 300;\r
65                 int height = 200;\r
66                 String title="Trend";\r
67                 if(params.containsKey("width")) width = Integer.parseInt(params.get("width"));\r
68                 if(params.containsKey("height")) height = Integer.parseInt(params.get("height"));\r
69                 if(params.containsKey("title")) title = params.get("title");\r
70                 sb.append("<object classid=\"org.simantics.wiki.ui.objects.TrendRenderer\" title=\""+title+"\" data=\""+params.get("data")+"\" width=\""+width+"\" height=\""+height+"\"/>");\r
71              \r
72             }\r
73             matcher.appendTail(sb);\r
74             return sb.toString();\r
75             \r
76         }\r
77         \r
78     }\r
79 \r
80     public static class TimestampTransform implements MarkupTransform {\r
81 \r
82         final Pattern startPattern = Pattern.compile("\\{timestamp(?::([^\\}]+))?\\}");\r
83         \r
84         @Override\r
85         public String apply(ReadGraph graph, Variable variable, String markup) {\r
86             \r
87             StringBuffer sb = new StringBuffer();\r
88             Matcher matcher = startPattern.matcher(markup);\r
89             while(matcher.find()) {\r
90                 matcher.appendReplacement(sb, "");\r
91                 String options = matcher.group(1);\r
92                 DateFormat df = null;\r
93                 if (options != null) {\r
94                         df = new SimpleDateFormat(options);\r
95                 } else {\r
96                         df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");\r
97                 }\r
98                 sb.append(df.format(new Date()));\r
99             }\r
100             matcher.appendTail(sb);\r
101             return sb.toString();\r
102         }\r
103         \r
104     }\r
105 \r
106     \r
107     /**\r
108      * \r
109      * Syntax example: {diagram:model=Model 1|layer=Default}\r
110      * \r
111      * @author J-P\r
112      *\r
113      */\r
114     class DiagramTransform implements MarkupTransform {\r
115 \r
116         final Pattern startPattern = Pattern.compile("\\{diagram(?::([^\\}]+))?\\}");\r
117         \r
118         @Override\r
119         public String apply(ReadGraph graph, Variable variable, String markup) {\r
120             \r
121             StringBuffer sb = new StringBuffer();\r
122             Matcher matcher = startPattern.matcher(markup);\r
123             while(matcher.find()) {\r
124                 matcher.appendReplacement(sb, "");\r
125                 String options = matcher.group(1);\r
126                 //String path = "";\r
127                 Map<String, String> params = new HashMap<String, String>();\r
128                 if (options != null) {\r
129                     String[] optionPairs = options.split("\\s*\\|\\s*");\r
130                     for (String optionPair: optionPairs) {\r
131                         String[] keyValue = optionPair.split("\\s*=\\s*");\r
132                         if (keyValue.length == 2) {\r
133                             String key = keyValue[0].trim();\r
134                             String value = keyValue[1].trim();\r
135                             params.put(key, value);\r
136                             /*                           \r
137                             if (key.equals("path")) {\r
138                                 path = value;\r
139                             }\r
140                             */\r
141                         }\r
142                     }\r
143                 }\r
144 //                sb.append("[[Image:" + path + "]]"); // FIXME: fetch value from somewhere...\r
145                 String data = "resource="+params.get("resource");\r
146                 if(params.containsKey("layer"))\r
147                         data += "|layer="+params.get("layer");\r
148                 sb.append("<p style=\"height:768\"><object classid=\"org.simantics.wiki.ui.objects.DiagramRenderer\" data=\""+data+"\" width=\"1024\" height=\"768\"/></p>");\r
149             }\r
150             matcher.appendTail(sb);\r
151             return sb.toString();\r
152             \r
153         }\r
154         \r
155     }\r
156     \r
157     /**\r
158      * \r
159      * Syntax example: {variable:#HasName}\r
160      * \r
161      * @author Antti\r
162      *\r
163      */\r
164     class VariableTransform implements MarkupTransform {\r
165 \r
166         final Pattern startPattern = Pattern.compile("\\{variable(?::([^\\}]+))?\\}");\r
167         \r
168         @Override\r
169         public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {\r
170             \r
171             StringBuffer sb = new StringBuffer();\r
172             Matcher matcher = startPattern.matcher(markup);\r
173             while(matcher.find()) {\r
174                 matcher.appendReplacement(sb, "");\r
175                 String rvi = matcher.group(1);\r
176                 Object value = variable.browse(graph, rvi).getValue(graph);\r
177                 sb.append(value.toString());\r
178             }\r
179             matcher.appendTail(sb);\r
180             return sb.toString();\r
181             \r
182         }\r
183         \r
184     }\r
185     \r
186     /**\r
187      * \r
188      * Syntax example: {include:http://company.org/Type}\r
189      * \r
190      * @author Antti\r
191      *\r
192      */\r
193     class IncludeTransform implements MarkupTransform {\r
194 \r
195         final Pattern startPattern = Pattern.compile("\\{include(?::([^\\}]+))?\\}");\r
196         \r
197         @Override\r
198         public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {\r
199             \r
200             StringBuffer sb = new StringBuffer();\r
201             Matcher matcher = startPattern.matcher(markup);\r
202             while(matcher.find()) {\r
203                 matcher.appendReplacement(sb, "");\r
204                 String uri = matcher.group(1);\r
205                 Layer0 L0 = Layer0.getInstance(graph);\r
206                 Resource resource = graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri));\r
207                 String desc = graph.getPossibleRelatedValue(resource, L0.HasDescription);\r
208                 sb.append(desc);\r
209             }\r
210             matcher.appendTail(sb);\r
211             return sb.toString();\r
212             \r
213         }\r
214         \r
215     }\r
216 \r
217     final private MarkupTransform[] transforms = { new TrendTransform(), new DiagramTransform(), new TimestampTransform(), new VariableTransform(), new IncludeTransform() };\r
218 \r
219     @Override\r
220     public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {\r
221         for(MarkupTransform t : transforms) markup = t.apply(graph, variable, markup);\r
222         return markup;\r
223     }\r
224     \r
225 }