]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/reference/ChildReference.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / reference / ChildReference.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/reference/ChildReference.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/reference/ChildReference.java
new file mode 100644 (file)
index 0000000..afb2edf
--- /dev/null
@@ -0,0 +1,361 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  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.databoard.accessor.reference;
+
+import java.io.IOException;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.StringTokenizer;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.annotations.Optional;\r
+import org.simantics.databoard.annotations.Union;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.MutableVariant;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.MapType;\r
+import org.simantics.databoard.type.OptionalType;\r
+import org.simantics.databoard.type.RecordType;\r
+import org.simantics.databoard.type.UnionType;\r
+import org.simantics.databoard.util.URIUtil;\r
+
+/**
+ * Path is a single or multi-level reference to a child node in the tree \r
+ * representation of data value. <p>
+ * 
+ * ComponentReference[] is a path from a node to a child or decendent node 
+ * in the tree representation of the container. <p>   
+ *  
+ * Reference has three serializable formats: Binary, URL compatible Path.
+ * 
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+@Union({
+               IndexReference.class,
+               KeyReference.class, 
+               NameReference.class,
+               ComponentReference.class, \r
+               LabelReference.class
+       })
+public abstract class ChildReference implements Cloneable {
+\r
+       \r
+       /**\r
+        * Create a concatenation of two references. Prefix part is cloned,\r
+        * suffix is linked.\r
+        * \r
+        * @param pathToBeCloned prefix path, or <tt>null</tt>\r
+        * @param ref suffix path, or <tt>null</tt>\r
+        * @return path\r
+        */\r
+       public static ChildReference concatenate(ChildReference pathToBeCloned, ChildReference ref) {\r
+               if (pathToBeCloned==null) return ref;\r
+               ChildReference result = pathToBeCloned.clone();\r
+               if (ref==null) return result;\r
+               result.tail().setChildReference(ref);\r
+               return result;\r
+       }\r
+       \r
+       /**\r
+        * Creates a compilation of individual a references into a one refence. \r
+        *  \r
+        * @param refs\r
+        * @return reference or <code>null</code> if there are no elements\r
+        */\r
+       public static ChildReference compile(ChildReference ... refs) {\r
+               if (refs.length==0) return null;\r
+               ChildReference first = refs[0].clone();\r
+               ChildReference r = first;\r
+               for (int i=1; i<refs.length; i++) {\r
+                       ChildReference next = refs[i].clone();\r
+                       r.setChildReference( next );\r
+                       r = next;\r
+                       while(r.childReference!=null) r = r.childReference;\r
+               }\r
+               return first;\r
+       }\r
+\r
+       /**\r
+        * Creates a compilation of individual a references into a one refence. \r
+        *  \r
+        * @param refs\r
+        * @return reference or <code>null</code> if there are no elements\r
+        */\r
+       public static ChildReference compile(Collection<ChildReference> refs) {\r
+               if (refs.isEmpty()) return null;\r
+               Iterator<ChildReference> itr = refs.iterator();\r
+               ChildReference first = itr.next().clone();\r
+               ChildReference r = first;\r
+               for (; itr.hasNext(); ) {\r
+                       ChildReference next = itr.next().clone();\r
+                       r.setChildReference( next );\r
+                       r = next;\r
+                       while(r.childReference!=null) r = r.childReference;\r
+               }\r
+               return first;\r
+       }\r
+\r
+       /**\r
+        * Get reference path from AccessorReference path.\r
+        * <a href="http://dev.simantics.org/index.php/Databoard_Specification#Path_Notation">Path Notation</a>\r
+        * \r
+        * @param path\r
+        * @return reference path or <code>null</code> if there is no path\r
+        */\r
+       public static ChildReference parsePath(String path) {\r
+               StringTokenizer st = new StringTokenizer(path, "/", false);\r
+               if (!st.hasMoreTokens()) return null;\r
+               ChildReference first = createSingleReference( st.nextToken() );         \r
+               ChildReference ref = first;\r
+               while (st.hasMoreTokens()) {\r
+                       ref.childReference = createSingleReference(st.nextToken());\r
+                       ref = ref.childReference;\r
+               }\r
+               \r
+               return first;\r
+       }\r
+       \r
+       /**\r
+        * Attempt to convert value reference to type reference.  \r
+        * \r
+        * @param vref\r
+        * @param type\r
+        * @return type reference or null\r
+        * @throws IllegalArgumentException if conversion fails.\r
+        */\r
+       public static ChildReference toTypeReference(ChildReference vref, Datatype type) \r
+       throws IllegalArgumentException \r
+       {\r
+               if (vref==null) return null;\r
+               if (type instanceof ArrayType) {\r
+                       if (vref instanceof IndexReference || vref instanceof LabelReference) {\r
+                               ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );\r
+                               return new ComponentReference( tail );\r
+                       }\r
+               }\r
+               \r
+               if (type instanceof MapType) {\r
+                       if (vref instanceof KeyReference) {\r
+                               ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(1) );\r
+                               return new IndexReference( 1, tail );                           \r
+                       }\r
+               }\r
+               \r
+               if (type instanceof OptionalType) {\r
+                       if (vref instanceof ComponentReference) {\r
+                               ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );\r
+                               return new ComponentReference( tail );                          \r
+                       }\r
+                       if (vref instanceof LabelReference) {\r
+                               LabelReference lr = (LabelReference) vref;\r
+                               if (lr.label.equals("v")) {\r
+                                       ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );\r
+                                       return new ComponentReference( tail );\r
+                               }\r
+                       }                       \r
+               }\r
+               \r
+               if (type instanceof RecordType) {\r
+                       RecordType rt = (RecordType) type;\r
+                       if (vref instanceof IndexReference) {\r
+                               IndexReference ir = (IndexReference) vref;                              \r
+                               ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );\r
+                               return new IndexReference( ir.index, tail );\r
+                       }\r
+                       \r
+                       if (vref instanceof NameReference) {\r
+                               NameReference ir = (NameReference) vref;                                \r
+                               ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.name) );\r
+                               return new NameReference( ir.name, tail );\r
+                       }\r
+                       \r
+                       if (vref instanceof LabelReference) {\r
+                               LabelReference ir = (LabelReference) vref;                              \r
+                               ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.label) );\r
+                               return new NameReference( ir.label, tail );\r
+                       }                       \r
+               }\r
+               \r
+               if (type instanceof UnionType) {\r
+                       UnionType ut = (UnionType) type;\r
+                       if (vref instanceof IndexReference) {\r
+                               IndexReference ir = (IndexReference) vref;                              \r
+                               ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );\r
+                               return new IndexReference( ir.index, tail );\r
+                       }\r
+                       \r
+                       if (vref instanceof NameReference) {\r
+                               NameReference ir = (NameReference) vref;                                \r
+                               ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.name) );\r
+                               return new NameReference( ir.name, tail );\r
+                       }\r
+                       \r
+                       if (vref instanceof LabelReference) {\r
+                               LabelReference ir = (LabelReference) vref;                              \r
+                               ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.label) );\r
+                               return new NameReference( ir.label, tail );\r
+                       }                       \r
+               }\r
+               \r
+               \r
+               throw new IllegalArgumentException();\r
+       }\r
+       \r
+       public static ChildReference parseBinary(byte[] binaryRef) \r
+       throws IOException \r
+       {\r
+               Binding binding = Bindings.getBindingUnchecked(ChildReference.class);\r
+               ChildReference result;\r
+               result = (ChildReference) Bindings.getSerializerUnchecked( binding ).deserialize(binaryRef);\r
+               return result;\r
+       }\r
+       \r
+       public final static Pattern INDEX_PATTERN = Pattern.compile("i-(\\d*)");\r
+       public final static Pattern MAP_PATTERN = Pattern.compile("k-(\\p{ASCII}*)");\r
+       public final static Pattern NAME_PATTERN = Pattern.compile("n-(\\p{ASCII}*)");\r
+       
+       public @Optional ChildReference childReference;
+       
+       protected ChildReference() {}
+       
+       protected ChildReference(ChildReference childReference) {
+               this.childReference = childReference;
+       }
+       
+       public ChildReference getChildReference() {
+               return childReference;
+       }\r
+       \r
+       public boolean hasChildReference() {\r
+               return childReference != null;\r
+       }\r
+               
+       public void setChildReference(ChildReference childReference) {
+               this.childReference = childReference;
+       }\r
+\r
+       public int getDepth() {\r
+               int result = 1;\r
+               ChildReference r = this;\r
+               while ( r.childReference != null ) {\r
+                       r = r.childReference;\r
+                       result++;\r
+               }\r
+               return result;\r
+       }\r
+       \r
+       public String toPath() {\r
+               return toPath(true);\r
+       }\r
+       \r
+       /**\r
+        * Converts the reference path into string representation.\r
+        * \r
+        * @param labelReference if true return label references.\r
+        * @return path string representation\r
+        */\r
+       public String toPath(boolean labelReference) {\r
+               if (childReference == null) return toString();\r
+               StringBuilder sb = new StringBuilder();\r
+               ChildReference ref = this;\r
+               while (ref!=null) {\r
+                       if (sb.length() > 0) sb.append("/");\r
+                       sb.append( ref.toString(labelReference) );\r
+                       ref = ref.getChildReference();\r
+               }\r
+               return sb.toString();\r
+       }\r
+       \r
+       /**\r
+        * Convert the reference into its string representation\r
+        * \r
+        * @return reference string representation\r
+        */
+       public String toString() {\r
+               return toString(true);\r
+       }\r
+       \r
+       /**\r
+        * Convert the reference into string representation.<p>\r
+        *\r
+        * If <code>labelReference</code> is true, the string representation is\r
+        * more user readable but has weaker typing. It serializes into\r
+        * instances of LabelReference.\r
+        * \r
+        * For instance Record Field Reference is "n-Children", but label reference "Children".\r
+        * \r
+        * Some references cannot be converted into LabelReference. \r
+        * E.g. string representation of FieldNameReference("i-5") is ambiguous with ArrayIndexReference(5). \r
+        * \r
+        * @param labelReference if true returns  \r
+        * @return string representation \r
+        */\r
+       public abstract String toString(boolean labelReference); \r
+\r
+       public abstract ChildReference clone();
+       
+       public ChildReference tail() {
+               ChildReference result = this;
+               while (result.childReference!=null) result = result.childReference;
+               return result;
+       }
+       \r
+       /**\r
+        * Create accessor reference from string representation.\r
+        * This doesn't parse path separators.\r
+        * \r
+        * @see https://www.simantics.org/wiki/index.php/Databoard_Specification#Accessor_Reference\r
+        * \r
+        * @param ref\r
+        * @return\r
+        */\r
+       static ChildReference createSingleReference(String ref) {\r
+               Matcher m;\r
+               \r
+               m = INDEX_PATTERN.matcher( ref );\r
+               if (m.matches()) {\r
+                       return new IndexReference( Integer.parseInt( m.group(1) ) );\r
+               }\r
+               \r
+               m = MAP_PATTERN.matcher( ref );\r
+               if (m.matches()) {\r
+                       String keyStr = m.group(1);\r
+                       MutableVariant key;\r
+                       try {\r
+                               key = (MutableVariant) Bindings.adapt(keyStr, Bindings.STRING, Bindings.MUTABLE_VARIANT);\r
+                       } catch (AdaptException e) {\r
+                               throw new IllegalArgumentException("Not string variant "+ref, e);\r
+                       }\r
+                       return new KeyReference(key);\r
+               }\r
+               \r
+               m = NAME_PATTERN.matcher( ref );\r
+               if (m.matches()) {\r
+                       String encoded = m.group(1);\r
+                       String name = URIUtil.decodeURI(encoded);\r
+                       return new NameReference( name );\r
+               }\r
+               \r
+               if (ref.equals("v")) {\r
+                       return new ComponentReference();\r
+               }\r
+               \r
+               String text = URIUtil.decodeURI( ref );\r
+               return new LabelReference( text );              \r
+       }
+       
+}
+