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