public List <InstancePathElement> findSingleObjectPath(object root, object object2Find)
        {
            bool found = false;
            ObjectExplorerImpl          explorer    = new ObjectExplorerImpl();
            Stack <InstancePathElement> currentPath = new Stack <InstancePathElement>();

            MoveAway down = delegate(Object from, string propertyName, Object to, bool isIndexed, int?index)
            {
                if (found)
                {
                    return(false);
                }
                InstancePathElement element;
                InstancePathElement thepeek = currentPath.Count > 0 ? currentPath.Peek() : null;
                bool parentIsIndexed        = currentPath.Count > 0 && currentPath.Peek().IsIndexed;
                if (parentIsIndexed)
                {
                    IndexPathElement ielement = new IndexPathElement();
                    ielement.Index = (int)index;
                    element        = ielement;
                }
                else
                {
                    NamePathElement nElement = new NamePathElement();
                    nElement.Name = propertyName;
                    element       = nElement;
                }
                element.IsIndexed = isIndexed;
                currentPath.Push(element);
                found = object2Find == to;
                return(!found);
            };
            MoveBack up = (from, propertyName, to, isIndexed) =>
            {
                if (!found)
                {
                    currentPath.Pop();
                }
            };

            OnLeaf onLeaf = (from, propertyName, to, index) =>
            {
            };

            PropertyReflectionNodeExpander expander = new PropertyReflectionNodeExpander();

            expander.ExcludeReadOnlyProperties = true;
            explorer.NodeExpander = expander;

            explorer.explore(root, down, up, onLeaf);
            List <InstancePathElement> result = null;

            if (found)
            {
                result = new List <InstancePathElement>(currentPath.ToArray().Reverse());
                // remove the first element because it is to the root
                result.RemoveAt(0);
            }
            return(result);
        }
        public void testExploration()
        {
            NodeExpander       nodeExpander = new PropertyReflectionNodeExpander();
            TestData           testData     = new TestData();
            ObjectExplorerImpl explorer     = new ObjectExplorerImpl();

            explorer.NodeExpander = nodeExpander;
            Object   valueFound = null;
            int      depth      = 0;
            int?     depthFound = null;
            MoveBack up         = delegate(Object from, String propertyName, Object to, bool isIndexed)
            {
                depth--;
            };
            MoveAway down = delegate(Object from, String propertyName, Object to, bool isIndexed, int?index)
            {
                depth++;
                return(true);
            };
            OnLeaf leaf = delegate(Object from, String propertyName, Object to, int?index)
            {
                if (propertyName != null && propertyName.Equals("Greeting"))
                {
                    valueFound = to;
                    depthFound = depth + 1;
                }
            };

            explorer.explore(testData, down, up, leaf);
            Assert.AreEqual(testData.TheSub.TheSubSub.Greeting, valueFound, "expected greeting ");
            Assert.AreEqual(4, depthFound, "expected depth");
        }
Ejemplo n.º 3
0
 /// \\
 /// Walk any leaves, possibly this node itself, if it's terminal.
 /// \\
 public void WalkAnyLeaves(OnLeaf leafWalker)
 {
     if (IsLeaf)
     {
         leafWalker(AsLeaf());
     }
     else
     {
         AsInner().WalkLeaves(leafWalker);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Walk any leaves, possibly this node itself, if it's terminal.
 /// </summary>
 public void WalkAnyLeaves(OnLeaf leafWalker)
 {
     if (IsLeaf)
     {
         leafWalker(AsLeaf());
     }
     else
     {
         AsInner().WalkLeaves(leafWalker);
     }
 }
Ejemplo n.º 5
0
 public void WalkLeaves(OnLeaf leafWalker)
 {
     foreach (var branch in Branches.Where(branch =\>\ branch != null))
     {
         if (branch.IsInner)
         {
             branch.AsInner().WalkLeaves(leafWalker);
         }
         else if (branch.IsLeaf)
         {
             leafWalker(branch.AsLeaf());
         }
     }
 }
Ejemplo n.º 6
0
 public void WalkLeaves(OnLeaf leafWalker)
 {
     foreach (var branch in Branches.Where(branch => branch != null))
     {
         if (branch.IsInner)
         {
             branch.AsInner().WalkLeaves(leafWalker);
         }
         else if (branch.IsLeaf)
         {
             leafWalker(branch.AsLeaf());
         }
     }
 }
        public void explore(object root, MoveAway down, MoveBack up, OnLeaf leaf)
        {
            if (CheckPermissionByName==null)
            {
                //todo write test for this
                throw new SecurityException("cant check permissions no Check Permission By Name code");
            }
            if (UnderlyingExplorer == null)
            {
                throw new SecurityException("cant check permissions no UnderlyingExplorer");
            }

            OnLeaf filteredOnLeaf = (from, propertyName, to, index) =>
                {
                    if (!isDeniedByPermission(from, propertyName))
                    {
                        leaf(from, propertyName, to, index);
                    }
                };

            MoveAway filteredMoveAway = ( from, propertyName, to, isIndexed, index) =>
                {
                    if (!isDeniedByPermission(from, propertyName))
                    {
                        return down(from, propertyName, to, isIndexed, index);
                    }
                    else
                    {
                        return false;
                    }
                };

            MoveBack filteredMoveBack = (from,  propertyName,  to,  isIndexed) =>
            {
                if (!isDeniedByPermission(from, propertyName))
                {
                    up(from, propertyName, to, isIndexed);
                }
            };

            UnderlyingExplorer.explore(root, filteredMoveAway, filteredMoveBack, filteredOnLeaf);
        }
Ejemplo n.º 8
0
        public void explore(object root, MoveAway down, MoveBack up, OnLeaf leaf)
        {
            if (CheckPermissionByName == null)
            {
                //todo write test for this
                throw new SecurityException("cant check permissions no Check Permission By Name code");
            }
            if (UnderlyingExplorer == null)
            {
                throw new SecurityException("cant check permissions no UnderlyingExplorer");
            }

            OnLeaf filteredOnLeaf = (from, propertyName, to, index) =>
            {
                if (!isDeniedByPermission(from, propertyName))
                {
                    leaf(from, propertyName, to, index);
                }
            };

            MoveAway filteredMoveAway = (from, propertyName, to, isIndexed, index) =>
            {
                if (!isDeniedByPermission(from, propertyName))
                {
                    return(down(from, propertyName, to, isIndexed, index));
                }
                else
                {
                    return(false);
                }
            };

            MoveBack filteredMoveBack = (from, propertyName, to, isIndexed) =>
            {
                if (!isDeniedByPermission(from, propertyName))
                {
                    up(from, propertyName, to, isIndexed);
                }
            };

            UnderlyingExplorer.explore(root, filteredMoveAway, filteredMoveBack, filteredOnLeaf);
        }
        //, TypeAliaser aliaser
        public LeafDefaultSet getDefaultsForAllLinkedObjects(Object root, NodeExpander nodeExpander)
        {
            LeafDefaultSet result = new LeafDefaultSet();

            ObjectExplorerImpl explorer = new ObjectExplorerImpl();
            MoveAway           down     = (from, propertyName, to, isIndexed, index) =>
            {
                return(isIndexed || (to != null && !result.Type2Defaults.ContainsKey(to.GetType())));
            };
            MoveBack up = (from, propertyName, to, isIndexed) =>
            {
            };

            OnLeaf leaf = (from, propertyName, to, index) =>
            {
                Defaults4Class defaults = null;
                if (from == null)
                {
                    return;
                }
                Type fromType = from.GetType();
                if (!result.Type2Defaults.ContainsKey(fromType))
                {
                    defaults = new Defaults4Class();
                    defaults.FullClassName         = fromType.FullName;
                    result.Type2Defaults[fromType] = defaults;
                }
                else
                {
                    defaults = result.Type2Defaults[fromType];
                }
                defaults.PropertyName2DefaultValue[propertyName] = to;
            };

            explorer.NodeExpander = nodeExpander;
            explorer.explore(root, down, up, leaf);
            return(result);
        }
        /** carries out exploration
         * if o is a leaf leaf is called
         * if o is a standard object down is called the item is expanded anf then up is called
         * if o is a dictionary or indexed object (e.g. array) every contained object is treated as a
         * leaf or standard object
         *
         * determination of leaf is in isLeaf and is based on c# properties
         *
         * determination if indexed type is in getEnumeratorIfIndexedType
         *
         * determination of map type is in toDictionary
         **/
        public void explore(object o, MoveAway down, MoveBack up, OnLeaf leaf)
        {
            if (NodeExpander == null)
            {
                throw new Exception("explore is not possible unless a node expander is specified");
            }
            OnChildNode onChildNode = null;

            onChildNode = delegate(Object from, String name, Object to)
            {
                IEnumerator           en           = null;
                Type                  toType       = to == null?null:to.GetType();
                IDictionaryNonGeneric asDictionary = null;
                if (isLeaf(to, toType))
                {
                    leaf(from, name, to);
                }
                else if (null != (asDictionary = toDictionary(to, toType)))
                {
                    bool doExpand =
                        down(from, name, to, false);
                    en = asDictionary.Keys.GetEnumerator();
                    for (int done = 0; doExpand && en.MoveNext(); done++)
                    {
                        Object oKey     = en.Current;
                        string strKey   = dictionaryKey2String(oKey);
                        Object oVal     = asDictionary[oKey];
                        Type   oValType = oVal == null ? null : oVal.GetType();
                        if (isLeaf(oVal, oValType))
                        {
                            leaf(to, strKey, oVal);
                        }
                        else
                        {
                            bool doSubExpand = down(to, strKey, oVal, false, done);
                            if (doSubExpand)
                            {
                                NodeExpander.expand(oVal, onChildNode);
                            }
                            up(from, strKey, oVal, false);
                        }
                    }
                    up(from, name, to, false);
                }
                else if (null != (en = getEnumeratorIfIndexedType(to)))
                {
                    bool doExpand =
                        down(from, name, to, true);

                    for (int done = 0; doExpand && en.MoveNext(); done++)
                    {
                        Object oVal = en.Current;
                        if (isLeaf(oVal, oVal == null?null:oVal.GetType()))
                        {
                            leaf(to, null, oVal);
                        }
                        else
                        {
                            bool doSubExpand = down(null, null, oVal, false, done);
                            if (doSubExpand)
                            {
                                NodeExpander.expand(oVal, onChildNode);
                            }
                            up(null, null, oVal, false);
                        }
                    }
                    up(from, name, to, true);
                }
                else
                {
                    bool doExpand = down(from, name, to, false);
                    if (doExpand)
                    {
                        NodeExpander.expand(to, onChildNode);
                    }
                    up(from, name, to, false);
                }
            };
            onChildNode(null, null, o);
        }
        /** carries out exploration
         if o is a leaf leaf is called
         if o is a standard object down is called the item is expanded anf then up is called
         if o is a dictionary or indexed object (e.g. array) every contained object is treated as a
         leaf or standard object
         *
         * determination of leaf is in isLeaf and is based on c# properties
         *
         * determination if indexed type is in getEnumeratorIfIndexedType
         *
         * determination of map type is in toDictionary
        **/
        public void explore(object o, MoveAway down, MoveBack up, OnLeaf leaf)
        {
            if (NodeExpander == null) throw new Exception("explore is not possible unless a node expander is specified");
            OnChildNode onChildNode = null;
            onChildNode = delegate(Object from, String name, Object to)
             {
                 IEnumerator en = null;
                 Type toType= to==null?null:to.GetType();
                 IDictionaryNonGeneric asDictionary=null;
                 if (isLeaf(to, toType))
                 {
                     leaf(from, name, to);
                 }
                 else if (null!=(asDictionary=toDictionary(to, toType)))
                 {
                     bool doExpand =
                     down(from, name, to, false);
                     en = asDictionary.Keys.GetEnumerator();
                     for (int done = 0; doExpand && en.MoveNext(); done++)
                     {
                         Object oKey = en.Current;
                         string strKey = dictionaryKey2String(oKey);
                         Object oVal = asDictionary[oKey];
                         Type oValType = oVal == null ? null : oVal.GetType();
                         if (isLeaf(oVal, oValType))
                             leaf(to, strKey, oVal);
                         else
                         {
                             bool doSubExpand = down(to, strKey, oVal, false, done);
                             if (doSubExpand) NodeExpander.expand(oVal, onChildNode);
                             up(from, strKey, oVal, false);
                         }
                     }
                     up(from, name, to, false);
                 }
                 else if (null!=(en=getEnumeratorIfIndexedType(to)))
                 {
                     bool doExpand =
                     down(from, name, to, true);

                     for  (int done=0;doExpand && en.MoveNext();done++)
                     {
                         Object oVal = en.Current;
                         if (isLeaf(oVal, oVal==null?null:oVal.GetType()))
                             leaf(to, null, oVal);
                         else
                         {
                             bool doSubExpand = down(null, null, oVal, false, done);
                             if (doSubExpand) NodeExpander.expand(oVal, onChildNode);
                             up(null, null, oVal, false);
                         }
                     }
                     up(from, name, to, true);
                 }
                 else
                 {
                     bool doExpand = down(from, name, to, false);
                     if (doExpand) NodeExpander.expand(to, onChildNode);
                     up(from, name, to, false);
                 }
             };
            onChildNode(null, null, o);
        }
Ejemplo n.º 12
0
        public void writeAsJson(Object o, TextWriter writer, TypeAliaser typeAliaser = null)
        {
            if (LeafDefaultSet != null && OmitDefaultLeafValuesInJs && OmitMarkAsArrayFunction)
            {
                throw new Exception("Leaf defaulting requires Array marker for js code");
            }


            ObjectIDGenerator idGenerator = null;

            if (UseReferences)
            {
                idGenerator = new ObjectIDGenerator();
            }
            Stack <ExploreStackFrame> exploreStack = new Stack <ExploreStackFrame>();
            Explorer explorerImpl = ExplorerFactory();

            ((Explorer)explorerImpl).NodeExpander = NodeExpander;
            MoveAway down = delegate(Object from, string propertyName, Object to, bool isIndexed, int?index)
            {
                ExploreStackFrame currentFrame = exploreStack.Count > 0 ? exploreStack.Peek():null;
                if (currentFrame != null)
                {
                    if (currentFrame.propertyCount > 0)
                    {
                        writer.Write(", ");
                    }
                    currentFrame.propertyCount++;
                }
                if (from != null && propertyName != null)
                {
                    writePropertyName(propertyName, writer);
                    writer.Write(":");
                }
                ExploreStackFrame childFrame = new ExploreStackFrame();
                exploreStack.Push(childFrame);
                writeIndent(writer, exploreStack);
                if (UseReferences)
                {
                    bool firstTime;
                    long objectid = idGenerator.GetId(to, out firstTime);
                    if (firstTime)
                    {
                        // could be done like this ! (function() {var x=[1,2]; x.id="uuu";return x;})()
                        if (!isIndexed)
                        {
                            writer.Write("{" + this.IdTag + ":" + objectid + ' ');
                            childFrame.propertyCount++;
                            childFrame.propertyCount += writeTypeAliasProperty(writer, to, typeAliaser, childFrame.propertyCount);
                        }
                        else
                        {
                            // no need for type alias
                            writer.Write(AttachId2ArrayFunctionName + "(" + objectid + ",[");
                        }
                    }
                    else
                    {
                        writer.Write("{" + this.ReferenceTag + ":" + objectid);
                        return(false);
                    }
                }
                else // !Use References
                {
                    if (!isIndexed)
                    {
                        writer.Write('{');
                        // todo -- check this out ............
                        childFrame.propertyCount += writeTypeAliasProperty(writer, to, typeAliaser, childFrame.propertyCount);
                    }
                    else
                    {
                        if (!OmitMarkAsArrayFunction)
                        {
                            writer.Write(markAsArrayFunctionName);
                            writer.Write("([");
                        }
                        else
                        {
                            writer.Write("[");
                        }
                    }
                }

                return(true);
            };

            MoveBack up = (from, propertyName, to, isIndexed) =>
            {
                if (!isIndexed)
                {
                    writer.Write('}');
                }
                else
                {
                    writer.Write(']');
                    // is there a function wrapper ?
                    if (!OmitMarkAsArrayFunction || UseReferences)
                    {
                        writer.Write(")");
                    }
                }
                exploreStack.Pop();
                writeIndent(writer, exploreStack);
            };

            OnLeaf leaf = (from, propertyName, to, index) =>
            {
                //check for default leaf values
                if (!this.OmitDefaultLeafValuesInJs ||
                    !isDefaultLeafValue(from, propertyName, to, LeafDefaultSet))
                {
                    ExploreStackFrame currentFrame = exploreStack.Peek();
                    if (currentFrame.propertyCount > 0)
                    {
                        writer.Write(", ");
                    }
                    currentFrame.propertyCount++;
                    if (propertyName != null)
                    {
                        writePropertyName(propertyName, writer);
                        writer.Write(":");
                    }
                    writeLeafValue(writer, to, propertyName);
                }
            };

            explorerImpl.explore(o, down, up, leaf);
        }