/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.layer0.utils.representation.representations; import java.io.IOException; import java.lang.reflect.Array; import java.util.List; import org.simantics.databoard.Bindings; import org.simantics.databoard.Databoard; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.mutable.Variant; import org.simantics.databoard.parser.DataValuePrinter; import org.simantics.databoard.type.Datatype; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.BindingException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.DoesNotContainValueException; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.representation.StringRepresentation2; public class DefaultStringRepresentation2 implements StringRepresentation2 { private static final int MAX_ARRAY_VALUE_LENGTH_SHOWN = 32; Resource r; public DefaultStringRepresentation2(Resource r) { this.r = r; } @Override public String get(ReadGraph g) throws DatabaseException { return fullValueToString(g, r); } @Override public String get(ReadGraph g, int index) throws DatabaseException { Layer0 L0 = Layer0.getInstance(g); if (g.isInstanceOf(r, L0.OrderedSet)) { List l = OrderedSetUtils.toList(g, r); return fullValueToString(g, l.get(index)); } Object value = g.getValue(r); return Array.get(value, index).toString(); } @Override public String get(ReadGraph g, int start, int size) throws DatabaseException { Object value = g.getValue(r); int valueSize = Array.getLength(value); int end = start+size; if (start > valueSize || end > valueSize) throw new IndexOutOfBoundsException("value size is " + valueSize + ", requested range [" + start + "-" + (end-1) + "]"); StringBuilder b = new StringBuilder(); boolean first = true; for (int i = start; i < end; ++i) { if (!first) { b.append(','); } first = false; b.append(Array.get(value, i)); } return b.toString(); } @Override public int getArraySize(ReadGraph g) { try { Object value = g.getValue(r); return Array.getLength(value); } catch (DatabaseException e) { return -1; } } /** * @param graph * @param resource * @return * @throws BindingException */ public static String fullValueToString(ReadGraph graph, Resource resource) throws ValidationException, ServiceException, BindingException { String representation = null; // First preference is name final Layer0 L0 = Layer0.getInstance(graph); if (graph.isInstanceOf(resource, L0.Relation)) { representation = graph.getPossibleRelatedValue(resource, L0.HasLabel); if (representation == null) representation = graph.getPossibleRelatedValue(resource, L0.HasName); if (representation == null) { Resource inverse = graph.getPossibleInverse(resource); if (inverse != null) { representation = graph.getPossibleRelatedValue(resource, L0.HasLabel); if (representation == null) representation = graph.getPossibleRelatedValue(inverse, L0.HasName); if (representation != null) representation = "Inverse Of " + representation; } if (representation == null) { Resource single = graph.getPossibleObject(resource, L0.SubrelationOf); if(single != null) { String singleValue = fullValueToString(graph, single); representation = " vclass = value.getClass(); boolean isArray = vclass.isArray(); int length = 1; if (isArray) length = Array.getLength(value); if (!isArray) return value.toString(); if (length == 1) return String.valueOf(Array.get(value, 0)); if (length > MAX_ARRAY_VALUE_LENGTH_SHOWN) return type + "[" + length + "]"; StringBuilder b = new StringBuilder(); boolean first = true; for (int i = 0; i < length; ++i) { if (!first) { b.append(','); } first = false; b.append(Array.get(value, i)); } return b.toString(); } }