1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.wiki.ui;
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.HashMap;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.layer0.Layer0;
29 public class SimanticsDialect implements MarkupTransform {
31 final static public SimanticsDialect INSTANCE = new SimanticsDialect();
33 class TrendTransform implements MarkupTransform {
35 final Pattern startPattern = Pattern.compile("\\{trend(?::([^\\}]+))?\\}");
38 public String apply(ReadGraph graph, Variable variable, String markup) {
40 StringBuffer sb = new StringBuffer();
41 Matcher matcher = startPattern.matcher(markup);
42 while(matcher.find()) {
43 matcher.appendReplacement(sb, "");
44 String options = matcher.group(1);
46 Map<String, String> params = new HashMap<String, String>();
47 if (options != null) {
48 String[] optionPairs = options.split("\\s*\\|\\s*");
49 for (String optionPair: optionPairs) {
50 String[] keyValue = optionPair.split("\\s*=\\s*");
51 if (keyValue.length == 2) {
52 String key = keyValue[0].trim();
53 String value = keyValue[1].trim();
54 params.put(key, value);
56 if (key.equals("path")) {
63 // sb.append("[[Image:" + path + "]]"); // FIXME: fetch value from somewhere...
67 if(params.containsKey("width")) width = Integer.parseInt(params.get("width"));
68 if(params.containsKey("height")) height = Integer.parseInt(params.get("height"));
69 if(params.containsKey("title")) title = params.get("title");
70 sb.append("<object classid=\"org.simantics.wiki.ui.objects.TrendRenderer\" title=\""+title+"\" data=\""+params.get("data")+"\" width=\""+width+"\" height=\""+height+"\"/>");
73 matcher.appendTail(sb);
80 public static class TimestampTransform implements MarkupTransform {
82 final Pattern startPattern = Pattern.compile("\\{timestamp(?::([^\\}]+))?\\}");
85 public String apply(ReadGraph graph, Variable variable, String markup) {
87 StringBuffer sb = new StringBuffer();
88 Matcher matcher = startPattern.matcher(markup);
89 while(matcher.find()) {
90 matcher.appendReplacement(sb, "");
91 String options = matcher.group(1);
93 if (options != null) {
94 df = new SimpleDateFormat(options);
96 df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
98 sb.append(df.format(new Date()));
100 matcher.appendTail(sb);
101 return sb.toString();
109 * Syntax example: {diagram:model=Model 1|layer=Default}
114 class DiagramTransform implements MarkupTransform {
116 final Pattern startPattern = Pattern.compile("\\{diagram(?::([^\\}]+))?\\}");
119 public String apply(ReadGraph graph, Variable variable, String markup) {
121 StringBuffer sb = new StringBuffer();
122 Matcher matcher = startPattern.matcher(markup);
123 while(matcher.find()) {
124 matcher.appendReplacement(sb, "");
125 String options = matcher.group(1);
127 Map<String, String> params = new HashMap<String, String>();
128 if (options != null) {
129 String[] optionPairs = options.split("\\s*\\|\\s*");
130 for (String optionPair: optionPairs) {
131 String[] keyValue = optionPair.split("\\s*=\\s*");
132 if (keyValue.length == 2) {
133 String key = keyValue[0].trim();
134 String value = keyValue[1].trim();
135 params.put(key, value);
137 if (key.equals("path")) {
144 // sb.append("[[Image:" + path + "]]"); // FIXME: fetch value from somewhere...
145 String data = "resource="+params.get("resource");
146 if(params.containsKey("layer"))
147 data += "|layer="+params.get("layer");
148 sb.append("<p style=\"height:768\"><object classid=\"org.simantics.wiki.ui.objects.DiagramRenderer\" data=\""+data+"\" width=\"1024\" height=\"768\"/></p>");
150 matcher.appendTail(sb);
151 return sb.toString();
159 * Syntax example: {variable:#HasName}
164 class VariableTransform implements MarkupTransform {
166 final Pattern startPattern = Pattern.compile("\\{variable(?::([^\\}]+))?\\}");
169 public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
171 StringBuffer sb = new StringBuffer();
172 Matcher matcher = startPattern.matcher(markup);
173 while(matcher.find()) {
174 matcher.appendReplacement(sb, "");
175 String rvi = matcher.group(1);
176 Object value = variable.browse(graph, rvi).getValue(graph);
177 sb.append(value.toString());
179 matcher.appendTail(sb);
180 return sb.toString();
188 * Syntax example: {include:http://company.org/Type}
193 class IncludeTransform implements MarkupTransform {
195 final Pattern startPattern = Pattern.compile("\\{include(?::([^\\}]+))?\\}");
198 public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
200 StringBuffer sb = new StringBuffer();
201 Matcher matcher = startPattern.matcher(markup);
202 while(matcher.find()) {
203 matcher.appendReplacement(sb, "");
204 String uri = matcher.group(1);
205 Layer0 L0 = Layer0.getInstance(graph);
206 Resource resource = graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri));
207 String desc = graph.getPossibleRelatedValue(resource, L0.HasDescription);
210 matcher.appendTail(sb);
211 return sb.toString();
217 final private MarkupTransform[] transforms = { new TrendTransform(), new DiagramTransform(), new TimestampTransform(), new VariableTransform(), new IncludeTransform() };
220 public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
221 for(MarkupTransform t : transforms) markup = t.apply(graph, variable, markup);