private void ApplySetValueVerb(XPathNavigator rootNavigator, ReferenceXPathItem xpathVisibility, BuildLogger logger) { if (!String.Equals(xpathVisibility.Verb, "SetValue", StringComparison.OrdinalIgnoreCase)) { return; } string newValue = xpathVisibility.Value; if (newValue == null) { return; } XPathNodeIterator iterator = rootNavigator.Select(xpathVisibility.Expression); if (iterator == null || iterator.Count == 0) { return; } int itemCount = 0; string condition = xpathVisibility.Condition; string attribute = xpathVisibility.Attribute; if (String.IsNullOrEmpty(condition)) { foreach (XPathNavigator navigator in iterator) { if (String.IsNullOrEmpty(attribute)) { navigator.SetValue(newValue); itemCount++; } else if (navigator.MoveToAttribute(attribute, String.Empty)) { navigator.SetValue(newValue); itemCount++; } } } else { XPathExpression xpathCondition = XPathExpression.Compile(condition); object evaluateResults = xpathVisibility.Results; foreach (XPathNavigator navigator in iterator) { switch (xpathCondition.ReturnType) { case XPathResultType.Number: if (evaluateResults != null && evaluateResults == navigator.Evaluate(xpathCondition)) { if (String.IsNullOrEmpty(attribute)) { navigator.SetValue(newValue); itemCount++; } else if (navigator.MoveToAttribute(attribute, String.Empty)) { navigator.SetValue(newValue); itemCount++; } } break; case XPathResultType.String: if (evaluateResults != null && String.Equals((string)evaluateResults, (string)navigator.Evaluate(xpathCondition), StringComparison.Ordinal)) { if (String.IsNullOrEmpty(attribute)) { navigator.SetValue(newValue); itemCount++; } else if (navigator.MoveToAttribute(attribute, String.Empty)) { navigator.SetValue(newValue); itemCount++; } } break; case XPathResultType.Boolean: if (evaluateResults != null) { if (Convert.ToBoolean(evaluateResults) == (bool)navigator.Evaluate(xpathCondition)) { if (String.IsNullOrEmpty(attribute)) { navigator.SetValue(newValue); itemCount++; } else if (navigator.MoveToAttribute(attribute, String.Empty)) { navigator.SetValue(newValue); itemCount++; } } } else if ((bool)navigator.Evaluate(xpathCondition)) { if (String.IsNullOrEmpty(attribute)) { navigator.SetValue(newValue); itemCount++; } else if (navigator.MoveToAttribute(attribute, String.Empty)) { navigator.SetValue(newValue); itemCount++; } } break; case XPathResultType.NodeSet: XPathNodeIterator selectIterator = navigator.Select(xpathCondition); if (selectIterator != null && selectIterator.Count != 0) { XPathNavigator[] selectNavigators = ToClonedArray(selectIterator); foreach (XPathNavigator selectNavigator in selectNavigators) { if (String.IsNullOrEmpty(attribute)) { selectNavigator.SetValue(newValue); itemCount++; } else if (navigator.MoveToAttribute(attribute, String.Empty)) { selectNavigator.SetValue(newValue); itemCount++; } } } break; } } } if (itemCount > 0 && logger != null) { logger.WriteLine(itemCount.ToString().PadRight(5) + " Value of matched items changed.", BuildLoggerLevel.Info); } }
/// <summary> /// This reads and sets its state or attributes stored in a <c>XML</c> format /// with the given reader. /// </summary> /// <param name="reader"> /// The reader with which the <c>XML</c> attributes of this object are accessed. /// </param> /// <exception cref="ArgumentNullException"> /// If the <paramref name="reader"/> is <see langword="null"/>. /// </exception> public override void ReadXml(XmlReader reader) { BuildExceptions.NotNull(reader, "reader"); Debug.Assert(reader.NodeType == XmlNodeType.Element); if (reader.NodeType != XmlNodeType.Element) { return; } if (!String.Equals(reader.Name, TagName, StringComparison.OrdinalIgnoreCase)) { Debug.Assert(false, String.Format( "The element name '{0}' does not match the expected '{1}'.", reader.Name, TagName)); return; } string tempText = reader.GetAttribute("name"); if (String.IsNullOrEmpty(tempText) || !String.Equals(tempText, ConfigurationName, StringComparison.OrdinalIgnoreCase)) { throw new BuildException(String.Format( "ReadXml: The current name '{0}' does not match the expected name '{1}'.", tempText, ConfigurationName)); } if (reader.IsEmptyElement) { return; } if (_xpathExpressions == null) { _xpathExpressions = new BuildList <ReferenceXPathItem>(); } while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (String.Equals(reader.Name, "propertyGroup", StringComparison.OrdinalIgnoreCase)) { this.ReadXmlGeneral(reader); } else if (String.Equals(reader.Name, ReferenceXPathItem.TagName, StringComparison.OrdinalIgnoreCase)) { ReferenceXPathItem item = new ReferenceXPathItem(); item.ReadXml(reader); if (!item.IsEmpty) { _xpathExpressions.Add(item); } } } else if (reader.NodeType == XmlNodeType.EndElement) { if (String.Equals(reader.Name, TagName, StringComparison.OrdinalIgnoreCase)) { break; } } } }
private void ApplyXPath(XPathNavigator documentNavigator, XPathNavigator rootNavigator) { IList <ReferenceXPathItem> xpathExpressions = _xpathConfig.Expressions; if (xpathExpressions == null || xpathExpressions.Count == 0) { return; } BuildContext context = this.Context; Debug.Assert(context != null); if (context == null) { return; } BuildLogger logger = context.Logger; if (logger != null) { logger.WriteLine("Begin Applying Path Document Visibility.", BuildLoggerLevel.Info); } int itemCount = xpathExpressions.Count; for (int i = 0; i < itemCount; i++) { ReferenceXPathItem xpathExpression = xpathExpressions[i]; if (xpathExpression != null && !xpathExpression.IsEmpty) { XPathNodeIterator iterator = rootNavigator.Select( xpathExpression.Expression); if (iterator != null && iterator.Count != 0) { string actionVerb = xpathExpression.Verb; if (String.Equals(actionVerb, "DeleteSelf", StringComparison.OrdinalIgnoreCase)) { if (xpathExpression.UseApiNode) { this.ApplyDeleteVerb(rootNavigator, xpathExpression, logger); } else { this.ApplyDeleteVerb(documentNavigator, xpathExpression, logger); } } else if (String.Equals(actionVerb, "SetValue", StringComparison.OrdinalIgnoreCase)) { if (xpathExpression.UseApiNode) { this.ApplySetValueVerb(rootNavigator, xpathExpression, logger); } else { this.ApplySetValueVerb(documentNavigator, xpathExpression, logger); } } } } } if (logger != null) { logger.WriteLine("Completed Applying Path Document Visibility.", BuildLoggerLevel.Info); } }