private Node FixPrecedence(Node node) { if (node is ICollectionNode) { ICollectionNode op = (ICollectionNode)node; //First, fix precedence of childs for (int i = 0; i < op.Length; i++) { if (op[i] != null) { FixPrecedence(op[i]); } } } //Then the current one if (node is IPrecedenceFixer) { return(((IPrecedenceFixer)node).FixPrecedence()); } else { return(node); } }
private async Task <CollectionActionResult> ExecuteAsync( Uri sourceUrl, ICollectionNode sourceNode, TMissing target, CancellationToken cancellationToken) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Collect properties for operation on collection {sourceUrl} and create target {target.DestinationUrl}."); } var properties = await GetWriteableProperties(sourceNode.Collection, cancellationToken).ConfigureAwait(false); TCollection newColl; try { newColl = await target.CreateCollectionAsync(cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.LogDebug($"{target.DestinationUrl}: Create failed with exception {ex.Message}"); return(new CollectionActionResult(ActionStatus.CreateFailed, target) { Exception = ex, }); } return(await ExecuteAsync(sourceUrl, sourceNode, newColl, properties, cancellationToken).ConfigureAwait(false)); }
public void ReplaceAsParent(int[] path, Node node, int childIndex) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (!(node is ICollectionNode)) { throw new Exception("Invalid node"); } ICollectionNode collectionNode = (ICollectionNode)node; if (collectionNode == null) { throw new ArgumentNullException(nameof(collectionNode)); } if (!(collectionNode is Node)) { throw new ArgumentException("The given node is not a Calculator Node"); } //Get the current parent of the Old node int[] targetParentPath = new int[path.Length - 1]; Array.Copy(path, targetParentPath, targetParentPath.Length); ICollectionNode targetParent = (ICollectionNode)this.GetNode(targetParentPath); //Set the old element as the child of the new collectionNode[childIndex] = targetParent[path[path.Length - 1]]; //Set the new element as the child of the old's parent targetParent[path[path.Length - 1]] = (Node)collectionNode; }
internal void SwapWithChild(int childIndex, int targetIndex) { ICollectionNode coll = this[childIndex] as ICollectionNode; if (coll == null) { throw new Exception("The given child is not a collection"); } Node child = this[childIndex]; //Save the child's child Node tmp = coll[targetIndex]; //Add the child to the old parent of this this[childIndex] = null; //Un-parent child from this ICollectionNode parent = this.Parent != null ? (ICollectionNode)this.Parent : null; if (parent != null) { int thisIndex = parent.GetIndex(this); parent[thisIndex] = child; } //Set this as child of old child coll[targetIndex] = this; //Set the tmp as new child this[childIndex] = tmp; }
public static string Print(ICollectionNode <INode> nodes) { var printer = new Printer(); var context = new PrinterContext(); var walker = new ReadOnlyDocumentWalker <PrinterContext>( new[] { printer }, context ); walker.Visit(nodes); return(context.ToString()); }
private void VisitCollection(ICollectionNode <INode>?nodes) { if (nodes == null) { return; } var arrayState = _context.PushArrayState(nodes); EnterNode(nodes); for (var i = 0; i < nodes.Count; i++) { var node = nodes[i]; arrayState.Index = i; Visit(node); } ExitNode(nodes); _context.PopArrayState(); }
private async Task <CollectionActionResult> ExecuteAsync( Uri sourceUrl, ICollectionNode sourceNode, TCollection target, CancellationToken cancellationToken) { if (_allowOverwrite && _handler.ExistingTargetBehaviour == RecursiveTargetBehaviour.DeleteTarget) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Delete existing target {target.DestinationUrl} for operation on collection {sourceUrl}."); } // Only delete an existing collection when the client allows an overwrite TMissing missing; try { missing = await target.DeleteAsync(cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.LogDebug($"{target.DestinationUrl}: Delete failed with exception {ex.Message}"); return(new CollectionActionResult(ActionStatus.TargetDeleteFailed, target) { Exception = ex, }); } return(await ExecuteAsync(sourceUrl, sourceNode, missing, cancellationToken).ConfigureAwait(false)); } if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Collect properties for operation on collection {sourceUrl} with existing target {target.DestinationUrl}."); } var properties = await GetWriteableProperties(sourceNode.Collection, cancellationToken).ConfigureAwait(false); return(await ExecuteAsync(sourceUrl, sourceNode, target, properties, cancellationToken).ConfigureAwait(false)); }
public void SetNode(int[] path, Node node) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (path.Length == 0) { this.Root = node; return; } if (!(this.Root is ICollectionNode)) { throw new InvalidPathException(); } ICollectionNode current = (ICollectionNode)this.Root; for (int i = 0; i < path.Length; i++) { if (i < path.Length - 1) { Node next = current[path[i]]; if (!(next is ICollectionNode)) { throw new InvalidPathException(); } current = (ICollectionNode)next; } else { current[path[i]] = node; return; } } throw new InvalidPathException(); }
private Node OptimizeInternal(Node node) { if (node is ICollectionNode) { ICollectionNode op = (ICollectionNode)node; //First, fix precedence of childs for (int i = 0; i < op.Length; i++) { if (op[i] != null) { Node optimizedChild = OptimizeInternal(op[i]); if (optimizedChild != op[i]) { op[i] = optimizedChild; } } } } //Then the current one Node optimized = node.Optimize(); return(optimized); }
public ArrayState(ICollectionNode <INode> array) { Array = array; }
private async Task <CollectionActionResult> ExecuteAsync( Uri sourceUrl, ICollectionNode sourceNode, TCollection target, IReadOnlyCollection <IUntypedWriteableProperty> properties, CancellationToken cancellationToken) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Perform operation on collection {sourceUrl} with existing target {target.DestinationUrl}."); } var documentActionResults = ImmutableList <ActionResult> .Empty; var collectionActionResults = ImmutableList <CollectionActionResult> .Empty; var subNodeProperties = new Dictionary <string, IReadOnlyCollection <IUntypedWriteableProperty> >(); foreach (var childNode in sourceNode.Nodes) { var subProperties = await GetWriteableProperties(childNode.Collection, cancellationToken).ConfigureAwait(false); subNodeProperties.Add(childNode.Name, subProperties); } foreach (var document in sourceNode.Documents) { var docUrl = sourceUrl.Append(document); if (target.Created) { // Collection was created by us - we just assume that the document doesn't exist var missingTarget = target.NewMissing(document.Name); var docResult = await ExecuteAsync(docUrl, document, missingTarget, cancellationToken).ConfigureAwait(false); documentActionResults = documentActionResults.Add(docResult); } else { var foundTarget = await target.GetAsync(document.Name, cancellationToken).ConfigureAwait(false); var docTarget = foundTarget as TDocument; if (docTarget != null) { // We found a document: Business as usual when we're allowed to overwrite it var docResult = await ExecuteAsync(docUrl, document, docTarget, cancellationToken).ConfigureAwait(false); documentActionResults = documentActionResults.Add(docResult); } else { var collTarget = foundTarget as TCollection; if (collTarget != null) { // We found a collection instead of a document _logger.LogDebug($"{target.DestinationUrl}: Found a collection instead of a document"); var docResult = new ActionResult(ActionStatus.OverwriteFailed, foundTarget); documentActionResults = documentActionResults.Add(docResult); } else { // We didn't find anything: Business as usual var missingTarget = (TMissing)foundTarget; var docResult = await ExecuteAsync(docUrl, document, missingTarget, cancellationToken).ConfigureAwait(false); documentActionResults = documentActionResults.Add(docResult); } } } } foreach (var childNode in sourceNode.Nodes) { var childProperties = subNodeProperties[childNode.Name]; var collection = childNode.Collection; var docUrl = sourceUrl.Append(childNode.Collection); if (target.Created) { // Collection was created by us - we just assume that the sub collection doesn't exist var missingTarget = target.NewMissing(childNode.Name); var newColl = await missingTarget.CreateCollectionAsync(cancellationToken).ConfigureAwait(false); var collResult = await ExecuteAsync(docUrl, childNode, newColl, childProperties, cancellationToken).ConfigureAwait(false); collectionActionResults = collectionActionResults.Add(collResult); } else { // Test if the target node exists var foundTarget = await target.GetAsync(collection.Name, cancellationToken).ConfigureAwait(false); var docTarget = foundTarget as TDocument; if (docTarget != null) { // We found a document instead of a collection _logger.LogDebug($"{target.DestinationUrl}: Found a document instead of a collection"); var collResult = new CollectionActionResult(ActionStatus.OverwriteFailed, foundTarget); collectionActionResults = collectionActionResults.Add(collResult); } else { var collTarget = foundTarget as TCollection; if (collTarget != null) { // We found a collection: Business as usual var collResult = await ExecuteAsync(docUrl, childNode, collTarget, childProperties, cancellationToken).ConfigureAwait(false); collectionActionResults = collectionActionResults.Add(collResult); } else { // We didn't find anything: Business as usual var missingTarget = (TMissing)foundTarget; var newColl = await missingTarget.CreateCollectionAsync(cancellationToken).ConfigureAwait(false); var collResult = await ExecuteAsync(docUrl, childNode, newColl, childProperties, cancellationToken).ConfigureAwait(false); collectionActionResults = collectionActionResults.Add(collResult); } } } } try { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Set properties on collection {target.DestinationUrl}."); } var failedPropertyNames = await target.SetPropertiesAsync(properties, cancellationToken).ConfigureAwait(false); if (failedPropertyNames.Count != 0) { _logger.LogDebug($"{target.DestinationUrl}: Failed setting properties {string.Join(", ", failedPropertyNames.Select(x => x.ToString()))}"); return(new CollectionActionResult(ActionStatus.PropSetFailed, target) { FailedProperties = failedPropertyNames, CollectionActionResults = collectionActionResults, DocumentActionResults = documentActionResults, }); } await _handler.ExecuteAsync(sourceNode.Collection, target, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.LogDebug($"{sourceNode.Collection.Path}: Cleanup failed with exception {ex.Message}"); return(new CollectionActionResult(ActionStatus.CleanupFailed, target) { Exception = ex, CollectionActionResults = collectionActionResults, DocumentActionResults = documentActionResults, }); } return(new CollectionActionResult(ActionStatus.Created, target) { CollectionActionResults = collectionActionResults, DocumentActionResults = documentActionResults, }); }
/// <summary> /// Visits a <see cref="ICollectionNode{T}"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collectionNode">The presentation collection.</param> public void VisitCollection <T>(ICollectionNode <T> collectionNode) { collectionNode.NotifyOfPropertyChange("IsValid"); }