]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/representation/representations/DefaultStringRepresentation2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / representation / representations / DefaultStringRepresentation2.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.layer0.utils.representation.representations;\r
13 \r
14 import java.io.IOException;\r
15 import java.lang.reflect.Array;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.databoard.Bindings;\r
19 import org.simantics.databoard.Databoard;\r
20 import org.simantics.databoard.binding.Binding;\r
21 import org.simantics.databoard.binding.mutable.Variant;\r
22 import org.simantics.databoard.parser.DataValuePrinter;\r
23 import org.simantics.databoard.type.Datatype;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.common.utils.NameUtils;\r
27 import org.simantics.db.common.utils.OrderedSetUtils;\r
28 import org.simantics.db.exception.BindingException;\r
29 import org.simantics.db.exception.DatabaseException;\r
30 import org.simantics.db.exception.DoesNotContainValueException;\r
31 import org.simantics.db.exception.ServiceException;\r
32 import org.simantics.db.exception.ValidationException;\r
33 import org.simantics.layer0.Layer0;\r
34 import org.simantics.layer0.utils.representation.StringRepresentation2;\r
35 \r
36 public class DefaultStringRepresentation2 implements StringRepresentation2 {\r
37 \r
38     private static final int MAX_ARRAY_VALUE_LENGTH_SHOWN = 32;\r
39 \r
40     Resource r;\r
41 \r
42     public DefaultStringRepresentation2(Resource r) {\r
43         this.r = r;\r
44     }\r
45 \r
46     @Override\r
47     public String get(ReadGraph g) throws DatabaseException {\r
48         return fullValueToString(g, r);\r
49     }\r
50 \r
51     @Override\r
52     public String get(ReadGraph g, int index) throws DatabaseException {\r
53         Layer0 L0 = Layer0.getInstance(g);\r
54         if (g.isInstanceOf(r, L0.OrderedSet)) {\r
55             List<Resource> l = OrderedSetUtils.toList(g, r);\r
56             return fullValueToString(g, l.get(index));\r
57         }\r
58 \r
59         Object value = g.getValue(r);\r
60         return Array.get(value, index).toString();\r
61     }\r
62 \r
63     @Override\r
64     public String get(ReadGraph g, int start, int size) throws DatabaseException {\r
65         Object value = g.getValue(r);\r
66         int valueSize = Array.getLength(value);\r
67         int end = start+size;\r
68         if (start > valueSize || end > valueSize)\r
69             throw new IndexOutOfBoundsException("value size is " + valueSize + ", requested range [" + start + "-" + (end-1) + "]");\r
70 \r
71         StringBuilder b = new StringBuilder();\r
72         boolean first = true;\r
73         for (int i = start; i < end; ++i) {\r
74             if (!first) {\r
75                 b.append(',');\r
76             }\r
77             first = false;\r
78             b.append(Array.get(value, i));\r
79         }\r
80         return b.toString();\r
81     }\r
82 \r
83     @Override\r
84     public int getArraySize(ReadGraph g) {\r
85         try {\r
86             Object value = g.getValue(r);\r
87             return Array.getLength(value);\r
88         } catch (DatabaseException e) {\r
89             return -1;\r
90         }\r
91     }\r
92 \r
93     /**\r
94      * @param graph\r
95      * @param resource\r
96      * @return\r
97      * @throws BindingException\r
98      */\r
99     public static String fullValueToString(ReadGraph graph, Resource resource) throws ValidationException, ServiceException, BindingException {\r
100         String representation = null;\r
101 \r
102         // First preference is name\r
103         final Layer0 L0 = Layer0.getInstance(graph);\r
104         if (graph.isInstanceOf(resource, L0.Relation)) {\r
105             representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);\r
106             if (representation == null)\r
107                 representation = graph.getPossibleRelatedValue(resource, L0.HasName);\r
108             if (representation == null) {\r
109                 Resource inverse = graph.getPossibleInverse(resource);\r
110                 if (inverse != null) {\r
111                     representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);\r
112                     if (representation == null)\r
113                         representation = graph.getPossibleRelatedValue(inverse, L0.HasName);\r
114                     if (representation != null)\r
115                         representation = "Inverse Of " + representation;\r
116                 }\r
117                 if (representation == null) {\r
118                     Resource single = graph.getPossibleObject(resource, L0.SubrelationOf);\r
119                     if(single != null) {\r
120                         String singleValue = fullValueToString(graph, single);\r
121                         representation = "<R " + singleValue;\r
122                     }\r
123                 }\r
124             }\r
125         }\r
126         if (representation == null) {\r
127             try {\r
128                 if (graph.isInstanceOf(resource, L0.Literal)) {\r
129                     representation = literalStr(graph, resource);\r
130                 } else {\r
131                     boolean first = true;\r
132                     // Try name property/properties\r
133                     for (Resource label : graph.getObjects(resource, L0.HasLabel)) {\r
134                         if (!first) {\r
135                             representation += ", ";\r
136                         } else {\r
137                             first = false;\r
138                         }\r
139                         if(graph.isInstanceOf(label, L0.Literal))\r
140                             representation = literalStr(graph, label);\r
141                     }\r
142                     if (representation == null) {\r
143                         for (Resource name : graph.getObjects(resource, L0.HasName)) {\r
144                             if (!first) {\r
145                                 representation += ", ";\r
146                             } else {\r
147                                 first = false;\r
148                             }\r
149                             representation = literalStr(graph, name);\r
150                         }\r
151                     }\r
152                     if (representation == null) {\r
153                         // Types\r
154                         representation = ": ";\r
155                         first = true;\r
156                         for (Resource t : graph.getPrincipalTypes(resource)) {\r
157                             String typeName = graph.getPossibleRelatedValue(t, L0.HasName);\r
158                             if (typeName == null)\r
159                                 typeName = "[unnamed type]";\r
160                             if (first)\r
161                                 first = false;\r
162                             else\r
163                                 representation += ", ";\r
164                             representation += typeName;\r
165                         }\r
166                     }\r
167                 }\r
168             } catch (DoesNotContainValueException e) {\r
169                 throw new ValidationException(e);\r
170             }\r
171         }\r
172 \r
173         return representation;\r
174 \r
175     }\r
176 \r
177     static Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);\r
178 \r
179     private static String literalStr(ReadGraph graph, Resource resource) throws DoesNotContainValueException,\r
180     ValidationException, ServiceException, BindingException {\r
181 \r
182         Layer0 L0 = Layer0.getInstance(graph);\r
183 \r
184         if (graph.isInstanceOf(resource, L0.Variant)) {\r
185             return arrayStr(graph, resource, "variant");\r
186         } else if (graph.isInstanceOf(resource, L0.String)) {\r
187             return arrayStr(graph, resource, "string");\r
188         } else if (graph.isInstanceOf(resource, L0.Integer)) {\r
189             return arrayStr(graph, resource, "integer");\r
190         } else if (graph.isInstanceOf(resource, L0.Long)) {\r
191             return arrayStr(graph, resource, "long");\r
192         } else if (graph.isInstanceOf(resource, L0.Boolean)) {\r
193             return arrayStr(graph, resource, "boolean");\r
194         } else if (graph.isInstanceOf(resource, L0.Double)) {\r
195             return arrayStr(graph, resource, "double");\r
196         } else if (graph.isInstanceOf(resource, L0.Float)) {\r
197             return arrayStr(graph, resource, "float");\r
198         } else if (graph.isInstanceOf(resource, L0.Byte)) {\r
199             return arrayStr(graph, resource, "byte");\r
200         } else if (graph.isInstanceOf(resource, L0.StringArray)) {\r
201             return arrayStr(graph, resource, "string");\r
202         } else if (graph.isInstanceOf(resource, L0.IntegerArray)) {\r
203             return arrayStr(graph, resource, "integer");\r
204         } else if (graph.isInstanceOf(resource, L0.LongArray)) {\r
205             return arrayStr(graph, resource, "long");\r
206         } else if (graph.isInstanceOf(resource, L0.BooleanArray)) {\r
207             return arrayStr(graph, resource, "boolean");\r
208         } else if (graph.isInstanceOf(resource, L0.DoubleArray)) {\r
209             return arrayStr(graph, resource, "double");\r
210         } else if (graph.isInstanceOf(resource, L0.FloatArray)) {\r
211             return arrayStr(graph, resource, "float");\r
212         } else if (graph.isInstanceOf(resource, L0.ByteArray)) {\r
213             return arrayStr(graph, resource, "byte");\r
214         } else if (graph.isInstanceOf(resource, L0.Literal)) {\r
215             try {\r
216                 // Print value using its datatype\r
217                 Datatype dt = graph.getPossibleRelatedValue(resource, L0.HasDataType, dataTypeBinding);\r
218                 if (dt != null) {\r
219                     Binding dtb = Bindings.getBinding(dt);\r
220                     //System.out.println("datatype: " + dt);\r
221                     Object value = graph.getPossibleValue(resource, dtb);\r
222                     if (value != null) {\r
223                         return DataValuePrinter.writeValueSingleLine(dtb, value);\r
224                     }\r
225                 } else {\r
226                     return arrayStr(graph, resource, "unknown");\r
227                 }\r
228                 return "[no value: " + NameUtils.getSafeName(graph, resource, true) + "]";\r
229             } catch (IOException e) {\r
230                 throw new ServiceException(e);\r
231             } catch (org.simantics.databoard.binding.error.BindingException e) {\r
232                 throw new ServiceException(e);\r
233             }\r
234         }\r
235         return "[unsupported literal type: " + NameUtils.getSafeName(graph, resource, true) + "]";\r
236     }\r
237 \r
238     private static String arrayStr(ReadGraph graph, Resource resource, String type) throws DoesNotContainValueException, ValidationException, ServiceException {\r
239         if ("variant".equals(type)) {\r
240             try {\r
241                 Databoard db = graph.getService(Databoard.class);\r
242                 Variant variant = graph.getPossibleValue(resource, db.VARIANT);\r
243                 if (variant != null)\r
244                     return variant.toString();\r
245             } catch (BindingException e) {\r
246                 return "BindingException: "+e.getMessage();\r
247             }\r
248         }\r
249         Object value = graph.getPossibleValue(resource);\r
250         if (value == null)\r
251             return "no value : " + type + "[]";\r
252 //        return value.toString();\r
253 \r
254         Class<?> vclass = value.getClass();\r
255         boolean isArray = vclass.isArray();\r
256         int length = 1;\r
257         if (isArray)\r
258             length = Array.getLength(value);\r
259 \r
260         if (!isArray)\r
261             return value.toString();\r
262         if (length == 1)\r
263             return String.valueOf(Array.get(value, 0));\r
264         if (length > MAX_ARRAY_VALUE_LENGTH_SHOWN)\r
265             return type + "[" + length + "]";\r
266 \r
267         StringBuilder b = new StringBuilder();\r
268         boolean first = true;\r
269         for (int i = 0; i < length; ++i) {\r
270             if (!first) {\r
271                 b.append(',');\r
272             }\r
273             first = false;\r
274             b.append(Array.get(value, i));\r
275         }\r
276         return b.toString();\r
277     }\r
278 \r
279 }\r