]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.wiki.ui/src/org/simantics/wiki/ui/SimanticsDialect.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.wiki.ui / src / org / simantics / wiki / ui / SimanticsDialect.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.wiki.ui;
13
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
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;
27
28
29 public class SimanticsDialect implements MarkupTransform {
30     
31     final static public SimanticsDialect INSTANCE = new SimanticsDialect();
32
33     class TrendTransform implements MarkupTransform {
34
35         final Pattern startPattern = Pattern.compile("\\{trend(?::([^\\}]+))?\\}");
36         
37         @Override
38         public String apply(ReadGraph graph, Variable variable, String markup) {
39             
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);
45                 //String path = "";
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);
55                             /*                           
56                             if (key.equals("path")) {
57                                 path = value;
58                             }
59                             */
60                         }
61                     }
62                 }
63 //                sb.append("[[Image:" + path + "]]"); // FIXME: fetch value from somewhere...
64                 int width = 300;
65                 int height = 200;
66                 String title="Trend";
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+"\"/>");
71              
72             }
73             matcher.appendTail(sb);
74             return sb.toString();
75             
76         }
77         
78     }
79
80     public static class TimestampTransform implements MarkupTransform {
81
82         final Pattern startPattern = Pattern.compile("\\{timestamp(?::([^\\}]+))?\\}");
83         
84         @Override
85         public String apply(ReadGraph graph, Variable variable, String markup) {
86             
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);
92                 DateFormat df = null;
93                 if (options != null) {
94                         df = new SimpleDateFormat(options);
95                 } else {
96                         df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
97                 }
98                 sb.append(df.format(new Date()));
99             }
100             matcher.appendTail(sb);
101             return sb.toString();
102         }
103         
104     }
105
106     
107     /**
108      * 
109      * Syntax example: {diagram:model=Model 1|layer=Default}
110      * 
111      * @author J-P
112      *
113      */
114     class DiagramTransform implements MarkupTransform {
115
116         final Pattern startPattern = Pattern.compile("\\{diagram(?::([^\\}]+))?\\}");
117         
118         @Override
119         public String apply(ReadGraph graph, Variable variable, String markup) {
120             
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);
126                 //String path = "";
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);
136                             /*                           
137                             if (key.equals("path")) {
138                                 path = value;
139                             }
140                             */
141                         }
142                     }
143                 }
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>");
149             }
150             matcher.appendTail(sb);
151             return sb.toString();
152             
153         }
154         
155     }
156     
157     /**
158      * 
159      * Syntax example: {variable:#HasName}
160      * 
161      * @author Antti
162      *
163      */
164     class VariableTransform implements MarkupTransform {
165
166         final Pattern startPattern = Pattern.compile("\\{variable(?::([^\\}]+))?\\}");
167         
168         @Override
169         public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
170             
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());
178             }
179             matcher.appendTail(sb);
180             return sb.toString();
181             
182         }
183         
184     }
185     
186     /**
187      * 
188      * Syntax example: {include:http://company.org/Type}
189      * 
190      * @author Antti
191      *
192      */
193     class IncludeTransform implements MarkupTransform {
194
195         final Pattern startPattern = Pattern.compile("\\{include(?::([^\\}]+))?\\}");
196         
197         @Override
198         public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
199             
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);
208                 sb.append(desc);
209             }
210             matcher.appendTail(sb);
211             return sb.toString();
212             
213         }
214         
215     }
216
217     final private MarkupTransform[] transforms = { new TrendTransform(), new DiagramTransform(), new TimestampTransform(), new VariableTransform(), new IncludeTransform() };
218
219     @Override
220     public String apply(ReadGraph graph, Variable variable, String markup) throws DatabaseException {
221         for(MarkupTransform t : transforms) markup = t.apply(graph, variable, markup);
222         return markup;
223     }
224     
225 }