Beispiel #1
0
        /**
         * Note : Calling this method MAY modify the tree object by adding new Maps and Lists as needed
         *  for the traversal.  This is determined by the behavior of the implementations of the
         *  abstract methods of this class.
         */
        public JToken Get(JToken tree, IList <string> keys)
        {
            if (keys.Count != _traversaLength)
            {
                throw new TraversrException("Traversal Path and number of keys mismatch, _traversaLength:" + _traversaLength + " numKeys:" + keys.Count);
            }

            return(_root.Traverse(tree, TraversalStepOperation.GET, keys.GetEnumerator(), null));
        }
        public JToken Traverse(JToken tree, TraversalStepOperation op, IEnumerator <string> keys, JToken data)
        {
            if (tree == null)
            {
                return(null);
            }

            if (GetStepType().IsAssignableFrom(tree.GetType()))
            {
                keys.MoveNext();
                string key = keys.Current;

                if (_child == null)
                {
                    // End of the Traversal so do the set or get
                    switch (op)
                    {
                    case TraversalStepOperation.GET:
                        return(Get(tree, key));

                    case TraversalStepOperation.SET:
                        return(_traversr.HandleFinalSet(this, tree, key, data));

                    case TraversalStepOperation.REMOVE:
                        return(Remove(tree, key));

                    default:
                        throw new InvalidOperationException("Invalid op:" + op.ToString());
                    }
                }
                else
                {
                    // We just an intermediate step, so traverse and then hand over control to our child
                    var optSub = _traversr.HandleIntermediateGet(this, tree, key, op);

                    if (optSub != null)
                    {
                        return(_child.Traverse(optSub, op, keys, data));
                    }
                }
            }

            return(null);
        }