Пример #1
0
        public override Composite Convert(Bundle.EntryComponent entry, int resourceIndex)
        {
            // create the resource root using the type of the resource and iteration of the resource
            var resourceRoot = new Composite(MappedTermsDictionary[entry.Resource.TypeName] + $":{resourceIndex}");

            resourceRoot = Helpers.ReflectionHelper.GetObjectComposite(entry.Resource, resourceRoot, MappedTermsDictionary, SkipNodeNames);

            // prune nodes
            var matchingNodes = resourceRoot.All(new List <Component>()).Where(x => x.GetType() == typeof(Leaf) && NodePruneList.Any(t => x.PathToRoot.Contains(t))).ToList();

            foreach (var node in matchingNodes)
            {
                // get the parent node of the node at the start of the path
                var currentNode = node.Parent;

                // calculate how many nodes we need to traverse backwards
                var iterationCount = NodePruneList.OrderByDescending(x => x.Length)
                                     .First(t => node.PathToRoot.Contains(t))?.Split('/').Length;

                // traverse nodes to get to the sub root, removing the old ones as we go
                for (var i = 0; i < iterationCount; i++)
                {
                    var oldNode = currentNode;
                    currentNode = currentNode.Parent;
                    currentNode.Remove(oldNode);
                }

                // add the leaf from the end of the path to the start of the path
                resourceRoot.FindById(currentNode.Id).Add(new Leaf(node.Name));
            }

            // done, return resource root
            return(resourceRoot);
        }