/// <summary> /// Validates the component properties. /// </summary> /// <remarks> /// Throws an exception if any of the properties is invalid. /// </remarks> protected override List <string> Validate() { List <string> errors = new List <string>(); if (this.XPathProperties == null || this.XPathProperties.Count == 0) { errors.Add(Resources.XPathPropertiesValidationMessage); } for (int i = 0; i < this.XPathProperties.Count; i++) { XPathProperty xpathProperty = this.XPathProperties[i]; if (string.IsNullOrEmpty(xpathProperty.Name) == true) { errors.Add(string.Format("Property {0} - Name cannot be empty.", i)); } if (string.IsNullOrEmpty(xpathProperty.Namespace) == true) { errors.Add(string.Format("Property {0} - Namespace cannot be empty.", i)); } if (string.IsNullOrEmpty(xpathProperty.XPath) == true) { errors.Add(string.Format("Property {0} - XPath expression cannot be empty.", i)); } } return(errors); }
/// <summary> /// Evaluates an xpath property. /// </summary> /// <param name="xpathProperty">XPathProperty instance.</param> /// <param name="navigator">XPathNavigator instance.</param> /// <returns>XPath expression result.</returns> private object Evaluate(XPathProperty xpathProperty, XPathNavigator navigator) { object result = null; // create an xpath expression XPathExpression expression = XPathExpression.Compile(xpathProperty.XPath); switch (expression.ReturnType) { case XPathResultType.String: case XPathResultType.Number: result = navigator.Evaluate(expression); break; case XPathResultType.NodeSet: XPathNodeIterator ni = navigator.Select(expression); if (ni.Count == 0 && this.RaiseXPathExceptions == true) { throw new ApplicationException(string.Format(Resources.XPathNodeNotFoundException, xpathProperty.XPath)); } if (ni.Count > 1 && this.RaiseXPathExceptions == true) { throw new ApplicationException(string.Format(Resources.XPathMultipleResultsException, xpathProperty.XPath)); } if (ni.Count == 1 && ni.MoveNext() == true) { result = ni.Current.ToString(); } break; case XPathResultType.Boolean: result = (bool)navigator.Evaluate(expression); break; } return(result); }
public static StateBase ConvertXpathToStates(string xPath) { int len = xPath.Length; StateBase state = null; var levels = xPath.Split('.'); for (int i = 0; i < levels.Length; i++) { levels[i] = levels[i].Trim(); StateBase newState = null; if (levels[i].Contains("/")) { newState = XPathProperty.Parser(levels[i]); } else if (levels[i].Contains("(")) { newState = MethodObject.Parser(levels[i]); } else if (levels[i].Contains("[")) { newState = ArrayListProperty.Parser(levels[i]); } else { newState = DirectProperty.Parser(levels[i]); } newState.Parent = state; state = newState; } return(state); //for (int i = 0; i < len; i++) //{ // if(xPath[i]=='/' || i==0 ) // { // int xPathStart = i; // int xPathEnd = i; // while (xPath[i] != '.' && i != len) // { // } // xPathEnd = i-1; // var value = xPath.Substring(xPathStart, xPathEnd- xPathStart); // value = value.Trim('.', ' '); // StateBase newState = new XPathProperty(value); // } // else // { // } //} }