--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.layer0.utils.representation.representations;\r
+\r
+import java.io.IOException;\r
+import java.lang.reflect.Array;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Databoard;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.parser.DataValuePrinter;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.BindingException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.DoesNotContainValueException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.representation.StringRepresentation2;\r
+\r
+public class DefaultStringRepresentation2 implements StringRepresentation2 {\r
+\r
+ private static final int MAX_ARRAY_VALUE_LENGTH_SHOWN = 32;\r
+\r
+ Resource r;\r
+\r
+ public DefaultStringRepresentation2(Resource r) {\r
+ this.r = r;\r
+ }\r
+\r
+ @Override\r
+ public String get(ReadGraph g) throws DatabaseException {\r
+ return fullValueToString(g, r);\r
+ }\r
+\r
+ @Override\r
+ public String get(ReadGraph g, int index) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(g);\r
+ if (g.isInstanceOf(r, L0.OrderedSet)) {\r
+ List<Resource> l = OrderedSetUtils.toList(g, r);\r
+ return fullValueToString(g, l.get(index));\r
+ }\r
+\r
+ Object value = g.getValue(r);\r
+ return Array.get(value, index).toString();\r
+ }\r
+\r
+ @Override\r
+ public String get(ReadGraph g, int start, int size) throws DatabaseException {\r
+ Object value = g.getValue(r);\r
+ int valueSize = Array.getLength(value);\r
+ int end = start+size;\r
+ if (start > valueSize || end > valueSize)\r
+ throw new IndexOutOfBoundsException("value size is " + valueSize + ", requested range [" + start + "-" + (end-1) + "]");\r
+\r
+ StringBuilder b = new StringBuilder();\r
+ boolean first = true;\r
+ for (int i = start; i < end; ++i) {\r
+ if (!first) {\r
+ b.append(',');\r
+ }\r
+ first = false;\r
+ b.append(Array.get(value, i));\r
+ }\r
+ return b.toString();\r
+ }\r
+\r
+ @Override\r
+ public int getArraySize(ReadGraph g) {\r
+ try {\r
+ Object value = g.getValue(r);\r
+ return Array.getLength(value);\r
+ } catch (DatabaseException e) {\r
+ return -1;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @param graph\r
+ * @param resource\r
+ * @return\r
+ * @throws BindingException\r
+ */\r
+ public static String fullValueToString(ReadGraph graph, Resource resource) throws ValidationException, ServiceException, BindingException {\r
+ String representation = null;\r
+\r
+ // First preference is name\r
+ final Layer0 L0 = Layer0.getInstance(graph);\r
+ if (graph.isInstanceOf(resource, L0.Relation)) {\r
+ representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);\r
+ if (representation == null)\r
+ representation = graph.getPossibleRelatedValue(resource, L0.HasName);\r
+ if (representation == null) {\r
+ Resource inverse = graph.getPossibleInverse(resource);\r
+ if (inverse != null) {\r
+ representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);\r
+ if (representation == null)\r
+ representation = graph.getPossibleRelatedValue(inverse, L0.HasName);\r
+ if (representation != null)\r
+ representation = "Inverse Of " + representation;\r
+ }\r
+ if (representation == null) {\r
+ Resource single = graph.getPossibleObject(resource, L0.SubrelationOf);\r
+ if(single != null) {\r
+ String singleValue = fullValueToString(graph, single);\r
+ representation = "<R " + singleValue;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ if (representation == null) {\r
+ try {\r
+ if (graph.isInstanceOf(resource, L0.Literal)) {\r
+ representation = literalStr(graph, resource);\r
+ } else {\r
+ boolean first = true;\r
+ // Try name property/properties\r
+ for (Resource label : graph.getObjects(resource, L0.HasLabel)) {\r
+ if (!first) {\r
+ representation += ", ";\r
+ } else {\r
+ first = false;\r
+ }\r
+ if(graph.isInstanceOf(label, L0.Literal))\r
+ representation = literalStr(graph, label);\r
+ }\r
+ if (representation == null) {\r
+ for (Resource name : graph.getObjects(resource, L0.HasName)) {\r
+ if (!first) {\r
+ representation += ", ";\r
+ } else {\r
+ first = false;\r
+ }\r
+ representation = literalStr(graph, name);\r
+ }\r
+ }\r
+ if (representation == null) {\r
+ // Types\r
+ representation = ": ";\r
+ first = true;\r
+ for (Resource t : graph.getPrincipalTypes(resource)) {\r
+ String typeName = graph.getPossibleRelatedValue(t, L0.HasName);\r
+ if (typeName == null)\r
+ typeName = "[unnamed type]";\r
+ if (first)\r
+ first = false;\r
+ else\r
+ representation += ", ";\r
+ representation += typeName;\r
+ }\r
+ }\r
+ }\r
+ } catch (DoesNotContainValueException e) {\r
+ throw new ValidationException(e);\r
+ }\r
+ }\r
+\r
+ return representation;\r
+\r
+ }\r
+\r
+ static Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);\r
+\r
+ private static String literalStr(ReadGraph graph, Resource resource) throws DoesNotContainValueException,\r
+ ValidationException, ServiceException, BindingException {\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+ if (graph.isInstanceOf(resource, L0.Variant)) {\r
+ return arrayStr(graph, resource, "variant");\r
+ } else if (graph.isInstanceOf(resource, L0.String)) {\r
+ return arrayStr(graph, resource, "string");\r
+ } else if (graph.isInstanceOf(resource, L0.Integer)) {\r
+ return arrayStr(graph, resource, "integer");\r
+ } else if (graph.isInstanceOf(resource, L0.Long)) {\r
+ return arrayStr(graph, resource, "long");\r
+ } else if (graph.isInstanceOf(resource, L0.Boolean)) {\r
+ return arrayStr(graph, resource, "boolean");\r
+ } else if (graph.isInstanceOf(resource, L0.Double)) {\r
+ return arrayStr(graph, resource, "double");\r
+ } else if (graph.isInstanceOf(resource, L0.Float)) {\r
+ return arrayStr(graph, resource, "float");\r
+ } else if (graph.isInstanceOf(resource, L0.Byte)) {\r
+ return arrayStr(graph, resource, "byte");\r
+ } else if (graph.isInstanceOf(resource, L0.StringArray)) {\r
+ return arrayStr(graph, resource, "string");\r
+ } else if (graph.isInstanceOf(resource, L0.IntegerArray)) {\r
+ return arrayStr(graph, resource, "integer");\r
+ } else if (graph.isInstanceOf(resource, L0.LongArray)) {\r
+ return arrayStr(graph, resource, "long");\r
+ } else if (graph.isInstanceOf(resource, L0.BooleanArray)) {\r
+ return arrayStr(graph, resource, "boolean");\r
+ } else if (graph.isInstanceOf(resource, L0.DoubleArray)) {\r
+ return arrayStr(graph, resource, "double");\r
+ } else if (graph.isInstanceOf(resource, L0.FloatArray)) {\r
+ return arrayStr(graph, resource, "float");\r
+ } else if (graph.isInstanceOf(resource, L0.ByteArray)) {\r
+ return arrayStr(graph, resource, "byte");\r
+ } else if (graph.isInstanceOf(resource, L0.Literal)) {\r
+ try {\r
+ // Print value using its datatype\r
+ Datatype dt = graph.getPossibleRelatedValue(resource, L0.HasDataType, dataTypeBinding);\r
+ if (dt != null) {\r
+ Binding dtb = Bindings.getBinding(dt);\r
+ //System.out.println("datatype: " + dt);\r
+ Object value = graph.getPossibleValue(resource, dtb);\r
+ if (value != null) {\r
+ return DataValuePrinter.writeValueSingleLine(dtb, value);\r
+ }\r
+ } else {\r
+ return arrayStr(graph, resource, "unknown");\r
+ }\r
+ return "[no value: " + NameUtils.getSafeName(graph, resource, true) + "]";\r
+ } catch (IOException e) {\r
+ throw new ServiceException(e);\r
+ } catch (org.simantics.databoard.binding.error.BindingException e) {\r
+ throw new ServiceException(e);\r
+ }\r
+ }\r
+ return "[unsupported literal type: " + NameUtils.getSafeName(graph, resource, true) + "]";\r
+ }\r
+\r
+ private static String arrayStr(ReadGraph graph, Resource resource, String type) throws DoesNotContainValueException, ValidationException, ServiceException {\r
+ if ("variant".equals(type)) {\r
+ try {\r
+ Databoard db = graph.getService(Databoard.class);\r
+ Variant variant = graph.getPossibleValue(resource, db.VARIANT);\r
+ if (variant != null)\r
+ return variant.toString();\r
+ } catch (BindingException e) {\r
+ return "BindingException: "+e.getMessage();\r
+ }\r
+ }\r
+ Object value = graph.getPossibleValue(resource);\r
+ if (value == null)\r
+ return "no value : " + type + "[]";\r
+// return value.toString();\r
+\r
+ Class<?> vclass = value.getClass();\r
+ boolean isArray = vclass.isArray();\r
+ int length = 1;\r
+ if (isArray)\r
+ length = Array.getLength(value);\r
+\r
+ if (!isArray)\r
+ return value.toString();\r
+ if (length == 1)\r
+ return String.valueOf(Array.get(value, 0));\r
+ if (length > MAX_ARRAY_VALUE_LENGTH_SHOWN)\r
+ return type + "[" + length + "]";\r
+\r
+ StringBuilder b = new StringBuilder();\r
+ boolean first = true;\r
+ for (int i = 0; i < length; ++i) {\r
+ if (!first) {\r
+ b.append(',');\r
+ }\r
+ first = false;\r
+ b.append(Array.get(value, i));\r
+ }\r
+ return b.toString();\r
+ }\r
+\r
+}\r