1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.reference;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.StringTokenizer;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.databoard.adapter.AdaptException;
23 import org.simantics.databoard.annotations.Optional;
24 import org.simantics.databoard.annotations.Union;
25 import org.simantics.databoard.binding.Binding;
26 import org.simantics.databoard.binding.mutable.MutableVariant;
27 import org.simantics.databoard.type.ArrayType;
28 import org.simantics.databoard.type.Datatype;
29 import org.simantics.databoard.type.MapType;
30 import org.simantics.databoard.type.OptionalType;
31 import org.simantics.databoard.type.RecordType;
32 import org.simantics.databoard.type.UnionType;
33 import org.simantics.databoard.util.URIUtil;
36 * Path is a single or multi-level reference to a child node in the tree
37 * representation of data value. <p>
39 * ComponentReference[] is a path from a node to a child or decendent node
40 * in the tree representation of the container. <p>
42 * Reference has three serializable formats: Binary, URL compatible Path.
44 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
50 ComponentReference.class,
53 public abstract class ChildReference implements Cloneable {
57 * Create a concatenation of two references. Prefix part is cloned,
60 * @param pathToBeCloned prefix path, or <tt>null</tt>
61 * @param ref suffix path, or <tt>null</tt>
64 public static ChildReference concatenate(ChildReference pathToBeCloned, ChildReference ref) {
65 if (pathToBeCloned==null) return ref;
66 ChildReference result = pathToBeCloned.clone();
67 if (ref==null) return result;
68 result.tail().setChildReference(ref);
73 * Creates a compilation of individual a references into a one refence.
76 * @return reference or <code>null</code> if there are no elements
78 public static ChildReference compile(ChildReference ... refs) {
79 if (refs.length==0) return null;
80 ChildReference first = refs[0].clone();
81 ChildReference r = first;
82 for (int i=1; i<refs.length; i++) {
83 ChildReference next = refs[i].clone();
84 r.setChildReference( next );
86 while(r.childReference!=null) r = r.childReference;
92 * Creates a compilation of individual a references into a one refence.
95 * @return reference or <code>null</code> if there are no elements
97 public static ChildReference compile(Collection<ChildReference> refs) {
98 if (refs.isEmpty()) return null;
99 Iterator<ChildReference> itr = refs.iterator();
100 ChildReference first = itr.next().clone();
101 ChildReference r = first;
102 for (; itr.hasNext(); ) {
103 ChildReference next = itr.next().clone();
104 r.setChildReference( next );
106 while(r.childReference!=null) r = r.childReference;
112 * Get reference path from AccessorReference path.
113 * <a href="http://dev.simantics.org/index.php/Databoard_Specification#Path_Notation">Path Notation</a>
116 * @return reference path or <code>null</code> if there is no path
118 public static ChildReference parsePath(String path) {
119 StringTokenizer st = new StringTokenizer(path, "/", false);
120 if (!st.hasMoreTokens()) return null;
121 ChildReference first = createSingleReference( st.nextToken() );
122 ChildReference ref = first;
123 while (st.hasMoreTokens()) {
124 ref.childReference = createSingleReference(st.nextToken());
125 ref = ref.childReference;
132 * Attempt to convert value reference to type reference.
136 * @return type reference or null
137 * @throws IllegalArgumentException if conversion fails.
139 public static ChildReference toTypeReference(ChildReference vref, Datatype type)
140 throws IllegalArgumentException
142 if (vref==null) return null;
143 if (type instanceof ArrayType) {
144 if (vref instanceof IndexReference || vref instanceof LabelReference) {
145 ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );
146 return new ComponentReference( tail );
150 if (type instanceof MapType) {
151 if (vref instanceof KeyReference) {
152 ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(1) );
153 return new IndexReference( 1, tail );
157 if (type instanceof OptionalType) {
158 if (vref instanceof ComponentReference) {
159 ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );
160 return new ComponentReference( tail );
162 if (vref instanceof LabelReference) {
163 LabelReference lr = (LabelReference) vref;
164 if (lr.label.equals("v")) {
165 ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );
166 return new ComponentReference( tail );
171 if (type instanceof RecordType) {
172 RecordType rt = (RecordType) type;
173 if (vref instanceof IndexReference) {
174 IndexReference ir = (IndexReference) vref;
175 ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );
176 return new IndexReference( ir.index, tail );
179 if (vref instanceof NameReference) {
180 NameReference ir = (NameReference) vref;
181 ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.name) );
182 return new NameReference( ir.name, tail );
185 if (vref instanceof LabelReference) {
186 LabelReference ir = (LabelReference) vref;
187 ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.label) );
188 return new NameReference( ir.label, tail );
192 if (type instanceof UnionType) {
193 UnionType ut = (UnionType) type;
194 if (vref instanceof IndexReference) {
195 IndexReference ir = (IndexReference) vref;
196 ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );
197 return new IndexReference( ir.index, tail );
200 if (vref instanceof NameReference) {
201 NameReference ir = (NameReference) vref;
202 ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.name) );
203 return new NameReference( ir.name, tail );
206 if (vref instanceof LabelReference) {
207 LabelReference ir = (LabelReference) vref;
208 ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.label) );
209 return new NameReference( ir.label, tail );
214 throw new IllegalArgumentException();
217 public static ChildReference parseBinary(byte[] binaryRef)
220 Binding binding = Bindings.getBindingUnchecked(ChildReference.class);
221 ChildReference result;
222 result = (ChildReference) Bindings.getSerializerUnchecked( binding ).deserialize(binaryRef);
226 public final static Pattern INDEX_PATTERN = Pattern.compile("i-(\\d*)");
227 public final static Pattern MAP_PATTERN = Pattern.compile("k-(\\p{ASCII}*)");
228 public final static Pattern NAME_PATTERN = Pattern.compile("n-(\\p{ASCII}*)");
230 public @Optional ChildReference childReference;
232 protected ChildReference() {}
234 protected ChildReference(ChildReference childReference) {
235 this.childReference = childReference;
238 public ChildReference getChildReference() {
239 return childReference;
242 public boolean hasChildReference() {
243 return childReference != null;
246 public void setChildReference(ChildReference childReference) {
247 this.childReference = childReference;
250 public int getDepth() {
252 ChildReference r = this;
253 while ( r.childReference != null ) {
254 r = r.childReference;
260 public String toPath() {
265 * Converts the reference path into string representation.
267 * @param labelReference if true return label references.
268 * @return path string representation
270 public String toPath(boolean labelReference) {
271 if (childReference == null) return toString();
272 StringBuilder sb = new StringBuilder();
273 ChildReference ref = this;
275 if (sb.length() > 0) sb.append("/");
276 sb.append( ref.toString(labelReference) );
277 ref = ref.getChildReference();
279 return sb.toString();
283 * Convert the reference into its string representation
285 * @return reference string representation
287 public String toString() {
288 return toString(true);
292 * Convert the reference into string representation.<p>
294 * If <code>labelReference</code> is true, the string representation is
295 * more user readable but has weaker typing. It serializes into
296 * instances of LabelReference.
298 * For instance Record Field Reference is "n-Children", but label reference "Children".
300 * Some references cannot be converted into LabelReference.
301 * E.g. string representation of FieldNameReference("i-5") is ambiguous with ArrayIndexReference(5).
303 * @param labelReference if true returns
304 * @return string representation
306 public abstract String toString(boolean labelReference);
308 public abstract ChildReference clone();
310 public ChildReference tail() {
311 ChildReference result = this;
312 while (result.childReference!=null) result = result.childReference;
317 * Create accessor reference from string representation.
318 * This doesn't parse path separators.
320 * @see https://www.simantics.org/wiki/index.php/Databoard_Specification#Accessor_Reference
325 static ChildReference createSingleReference(String ref) {
328 m = INDEX_PATTERN.matcher( ref );
330 return new IndexReference( Integer.parseInt( m.group(1) ) );
333 m = MAP_PATTERN.matcher( ref );
335 String keyStr = m.group(1);
338 key = (MutableVariant) Bindings.adapt(keyStr, Bindings.STRING, Bindings.MUTABLE_VARIANT);
339 } catch (AdaptException e) {
340 throw new IllegalArgumentException("Not string variant "+ref, e);
342 return new KeyReference(key);
345 m = NAME_PATTERN.matcher( ref );
347 String encoded = m.group(1);
348 String name = URIUtil.decodeURI(encoded);
349 return new NameReference( name );
352 if (ref.equals("v")) {
353 return new ComponentReference();
356 String text = URIUtil.decodeURI( ref );
357 return new LabelReference( text );