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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.wiki.ui;
\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
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
29 public class SimanticsDialect implements MarkupTransform {
\r
31 final static public SimanticsDialect INSTANCE = new SimanticsDialect();
\r
33 class TrendTransform implements MarkupTransform {
\r
35 final Pattern startPattern = Pattern.compile("\\{trend(?::([^\\}]+))?\\}");
\r
38 public String apply(ReadGraph graph, Variable variable, String markup) {
\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
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
56 if (key.equals("path")) {
\r
63 // sb.append("[[Image:" + path + "]]"); // FIXME: fetch value from somewhere...
\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
73 matcher.appendTail(sb);
\r
74 return sb.toString();
\r
80 public static class TimestampTransform implements MarkupTransform {
\r
82 final Pattern startPattern = Pattern.compile("\\{timestamp(?::([^\\}]+))?\\}");
\r
85 public String apply(ReadGraph graph, Variable variable, String markup) {
\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
96 df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
\r
98 sb.append(df.format(new Date()));
\r
100 matcher.appendTail(sb);
\r
101 return sb.toString();
\r
109 * Syntax example: {diagram:model=Model 1|layer=Default}
\r
114 class DiagramTransform implements MarkupTransform {
\r
116 final Pattern startPattern = Pattern.compile("\\{diagram(?::([^\\}]+))?\\}");
\r
119 public String apply(ReadGraph graph, Variable variable, String markup) {
\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
137 if (key.equals("path")) {
\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
150 matcher.appendTail(sb);
\r
151 return sb.toString();
\r
159 * Syntax example: {variable:#HasName}
\r
164 class VariableTransform implements MarkupTransform {
\r
166 final Pattern startPattern = Pattern.compile("\\{variable(?::([^\\}]+))?\\}");
\r
169 public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
\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
179 matcher.appendTail(sb);
\r
180 return sb.toString();
\r
188 * Syntax example: {include:http://company.org/Type}
\r
193 class IncludeTransform implements MarkupTransform {
\r
195 final Pattern startPattern = Pattern.compile("\\{include(?::([^\\}]+))?\\}");
\r
198 public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
\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
210 matcher.appendTail(sb);
\r
211 return sb.toString();
\r
217 final private MarkupTransform[] transforms = { new TrendTransform(), new DiagramTransform(), new TimestampTransform(), new VariableTransform(), new IncludeTransform() };
\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