private static string ParseXPathString(XPathLexer lexer, XmlNamespaceManager namespaceManager, bool throwOnFailure) { int firstTokenChar = lexer.FirstTokenChar; if (lexer.MoveNext()) { XPathToken token = lexer.Token; StringBuilder builder = new StringBuilder(ParseXPathString(lexer, namespaceManager, throwOnFailure)); if (XPathTokenID.NameTest == token.TokenID) { string prefix = token.Prefix; if (!string.IsNullOrEmpty(prefix)) { string str3 = namespaceManager.LookupNamespace(prefix); if (!string.IsNullOrEmpty(str3)) { builder = builder.Replace(prefix, str3, firstTokenChar, prefix.Length); } else if (throwOnFailure) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new IndexOutOfRangeException(System.ServiceModel.SR.GetString("ConfigXPathNamespacePrefixNotFound", new object[] { prefix }))); } } } return(builder.ToString()); } return(lexer.ConsumedSubstring()); }
/// <summary> /// Basic XPath tokenizer with cache support /// </summary> /// <param name="xpath">The XPath string</param> /// <returns>List of tokens extracted from the string</returns> XPathToken[] tokenize(string xpath) { if (cache.ContainsKey(xpath)) { return(cache[xpath]); } Regex reg = new Regex("([a-zA-Z:\\-_0-9]+)|(\\[[0-9]+\\])+"); MatchCollection matches = reg.Matches(xpath); XPathToken[] res = new XPathToken[matches.Count]; for (int i = 0; i < matches.Count; i++) { XPathToken token = new XPathToken(); if (Regex.IsMatch(matches[i].Value, "\\[[0-9]+\\]")) { token.type = XPathTokenType.INDEX; token.name = matches[i].Value.Trim('[', ']'); } else if (Regex.IsMatch(matches[i].Value, "[a-zA-Z:\\-_0-9]+")) { token.type = XPathTokenType.KEY; token.name = matches[i].Value.TrimStart('/'); } res[i] = token; } cache[xpath] = res; return(res); }
public void ParseOrOperator() { var tokens = XPathToken.Parse("Relationships/Item[@type = 'File' or @type = 'Folder']").ToList(); Assert.AreEqual("Relationships", tokens[0].Value); Assert.AreEqual("Item", tokens[2].Value); Assert.IsTrue(tokens[4].TryGetAxis(out var axis1)); Assert.AreEqual(XPathAxis.Attribute, axis1); Assert.IsTrue(tokens[7].TryGetString(out var str)); Assert.AreEqual("File", str); Assert.AreEqual(XPathTokenType.Operator, tokens[8].Type); }
public void ParseRelationships() { var tokens = XPathToken.Parse("Relationships/Item[@type = 'File'][@id]").ToList(); Assert.AreEqual(13, tokens.Count); Assert.AreEqual("Relationships", tokens[0].Value); Assert.AreEqual("Item", tokens[2].Value); Assert.IsTrue(tokens[4].TryGetAxis(out var axis1)); Assert.AreEqual(XPathAxis.Attribute, axis1); Assert.IsTrue(tokens[7].TryGetString(out var str)); Assert.AreEqual("File", str); Assert.IsTrue(tokens[10].TryGetAxis(out var axis2)); Assert.AreEqual(XPathAxis.Attribute, axis2); }
private static OperationElement Parse(string methodName, IEnumerable <XElement> elements) { var method = new OperationElement { Name = methodName }; var builder = new StringBuilder(); method.Summary.AddRange(elements .Where(e => e.Name.LocalName == "summary") .SelectMany(e => ParseDoc(e.Nodes()))); foreach (var elem in elements.Where(e => e.Name.LocalName == "inheritdoc")) { if (Standard.TryGetValue((string)elem.Attribute("cref"), out var doc)) { method.WithAttributes(doc.Attributes); method.WithElements(doc.Elements.ToArray()); if (method.Summary.Count < 1 && doc.Summary.Count > 0) { method.Summary.AddRange(doc.Summary); } } } foreach (var elem in elements.Where(e => e.Name.LocalName == "param")) { var xPath = new List <XPathToken>(); try { xPath = XPathToken.Parse((string)elem.Attribute("name")).ToList(); } catch (Exception) { } var stack = new Stack <OperationElement>(); stack.Push(method); var idx = 0; while (idx < xPath.Count) { if (xPath[idx].TryGetAxis(out var axis) && axis == XPathAxis.Attribute && idx + 1 < xPath.Count) { var attrDoc = stack.Peek().GetOrAddAttribute(xPath[idx + 1].Value); stack.Push(attrDoc); idx++; }
static string ParseXPathString(XPathLexer lexer, XmlNamespaceManager namespaceManager, bool throwOnFailure) { string retVal = String.Empty; int currentTokenStart = lexer.FirstTokenChar; if (lexer.MoveNext()) { XPathToken token = lexer.Token; StringBuilder xPathString = new StringBuilder(XPathMessageFilterElementComparer.ParseXPathString(lexer, namespaceManager, throwOnFailure)); if (XPathTokenID.NameTest == token.TokenID) { string nsPrefix = token.Prefix; if (!String.IsNullOrEmpty(nsPrefix)) { string ns = namespaceManager.LookupNamespace(nsPrefix); if (!String.IsNullOrEmpty(ns)) { xPathString = xPathString.Replace(nsPrefix, ns, currentTokenStart, nsPrefix.Length); } else { if (throwOnFailure) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new IndexOutOfRangeException(SR.GetString(SR.ConfigXPathNamespacePrefixNotFound, nsPrefix))); } } } } retVal = xPathString.ToString(); } else { retVal = lexer.ConsumedSubstring(); } return(retVal); }