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