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