public IList <IList <T> > Find(UriTemplateLiteralPathSegment wireData) { IList <IList <T> > results = new List <IList <T> >(); for (int i = 0; i < this.items.Values.Count; i++) { List <T> sameOrderResults = null; for (int j = 0; j < this.items.Values[i].Count; j++) { if (this.items.Values[i][j].Segment.IsMatch(wireData)) { if (sameOrderResults == null) { sameOrderResults = new List <T>(); } sameOrderResults.Add(this.items.Values[i][j].Value); } } if (sameOrderResults != null) { results.Add(sameOrderResults); } } return(results); }
public IList <IList <T> > Find(UriTemplateLiteralPathSegment wireData) { IList <IList <T> > list = new List <IList <T> >(); for (int i = 0; i < this.items.Values.Count; i++) { List <T> list2 = null; for (int j = 0; j < this.items.Values[i].Count; j++) { CollectionItem <T> item = this.items.Values[i][j]; if (item.Segment.IsMatch(wireData)) { if (list2 == null) { list2 = new List <T>(); } CollectionItem <T> item2 = this.items.Values[i][j]; list2.Add(item2.Value); } } if (list2 != null) { list.Add(list2); } } return(list); }
public static UriTemplatePathSegment CreateFromUriTemplate(string segment, UriTemplate template) { // Identifying the type of segment - Literal|Compound|Variable switch (UriTemplateHelpers.IdentifyPartType(segment)) { case UriTemplatePartType.Literal: return(UriTemplateLiteralPathSegment.CreateFromUriTemplate(segment, template)); case UriTemplatePartType.Compound: return(UriTemplateCompoundPathSegment.CreateFromUriTemplate(segment, template)); case UriTemplatePartType.Variable: if (segment.EndsWith("/", StringComparison.Ordinal)) { string varName = template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 3)); return(new UriTemplateVariablePathSegment(segment, true, varName)); } else { string varName = template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 2)); return(new UriTemplateVariablePathSegment(segment, false, varName)); } default: Fx.Assert("Invalid value from IdentifyStringNature"); return(null); } }
bool ComputeRelativeSegmentsAndLookup(Uri uri, ICollection <string> relativePathSegments, // add to this ICollection <UriTemplateTableMatchCandidate> candidates) // matched candidates { string[] uriSegments = uri.Segments; int numRelativeSegments = uriSegments.Length - this.numSegmentsInBaseAddress; Fx.Assert(numRelativeSegments >= 0, "bad num segments"); UriTemplateLiteralPathSegment[] uSegments = new UriTemplateLiteralPathSegment[numRelativeSegments]; for (int i = 0; i < numRelativeSegments; ++i) { string seg = uriSegments[i + this.numSegmentsInBaseAddress]; // compute representation for matching UriTemplateLiteralPathSegment lps = UriTemplateLiteralPathSegment.CreateFromWireData(seg); uSegments[i] = lps; // compute representation to project out into results string relPathSeg = Uri.UnescapeDataString(seg); if (lps.EndsWithSlash) { Fx.Assert(relPathSeg.EndsWith("/", StringComparison.Ordinal), "problem with relative path segment"); relPathSeg = relPathSeg.Substring(0, relPathSeg.Length - 1); // trim slash } relativePathSegments.Add(relPathSeg); } return(rootNode.Match(uSegments, candidates)); }
public override bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash) { if (!ignoreTrailingSlash && (base.EndsWithSlash != segment.EndsWithSlash)) { return(false); } return(!segment.IsNullOrEmpty()); }
public override bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash) { if (!ignoreTrailingSlash && (this.EndsWithSlash != segment.EndsWithSlash)) { return(false); } return(TryLookup(segment.AsUnescapedString(), null)); }
private void AddFinalLiteralSegment(UriTemplateLiteralPathSegment lps, KeyValuePair <UriTemplate, object> kvp) { if ((this.finalLiteralSegment != null) && this.finalLiteralSegment.ContainsKey(lps)) { this.finalLiteralSegment[lps].Items.Add(kvp); } else { if (this.finalLiteralSegment == null) { this.finalLiteralSegment = new Dictionary <UriTemplateLiteralPathSegment, UriTemplatePathPartiallyEquivalentSet>(); } UriTemplatePathPartiallyEquivalentSet set = new UriTemplatePathPartiallyEquivalentSet(this.depth + 1); set.Items.Add(kvp); this.finalLiteralSegment.Add(lps, set); } }
void AddFinalLiteralSegment(UriTemplateLiteralPathSegment lps, KeyValuePair <UriTemplate, object> kvp) { Fx.Assert(lps != null, "must be - based on the segment nature"); if (this.finalLiteralSegment != null && this.finalLiteralSegment.ContainsKey(lps)) { this.finalLiteralSegment[lps].Items.Add(kvp); } else { if (this.finalLiteralSegment == null) { this.finalLiteralSegment = new Dictionary <UriTemplateLiteralPathSegment, UriTemplatePathPartiallyEquivalentSet>(); } UriTemplatePathPartiallyEquivalentSet pes = new UriTemplatePathPartiallyEquivalentSet(this.depth + 1); pes.Items.Add(kvp); this.finalLiteralSegment.Add(lps, pes); } }
private UriTemplateTrieNode AddNextLiteralSegment(UriTemplateLiteralPathSegment lps) { if ((this.nextLiteralSegment != null) && this.nextLiteralSegment.ContainsKey(lps)) { return(this.nextLiteralSegment[lps].node); } if (this.nextLiteralSegment == null) { this.nextLiteralSegment = new Dictionary <UriTemplateLiteralPathSegment, UriTemplateTrieLocation>(); } UriTemplateTrieNode n = new UriTemplateTrieNode(this.depth + 1) { onFailure = new UriTemplateTrieLocation(this, UriTemplateTrieIntraNodeLocation.AfterLiteral) }; this.nextLiteralSegment.Add(lps, new UriTemplateTrieLocation(n, UriTemplateTrieIntraNodeLocation.BeforeLiteral)); return(n); }
public static UriTemplatePathSegment CreateFromUriTemplate(string segment, UriTemplate template) { switch (UriTemplateHelpers.IdentifyPartType(segment)) { case UriTemplatePartType.Literal: return(UriTemplateLiteralPathSegment.CreateFromUriTemplate(segment, template)); case UriTemplatePartType.Compound: return(UriTemplateCompoundPathSegment.CreateFromUriTemplate(segment, template)); case UriTemplatePartType.Variable: if (!segment.EndsWith("/", StringComparison.Ordinal)) { return(new UriTemplateVariablePathSegment(segment, false, template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 2)))); } return(new UriTemplateVariablePathSegment(segment, true, template.AddPathVariable(UriTemplatePartType.Variable, segment.Substring(1, segment.Length - 3)))); } return(null); }
UriTemplateTrieNode AddNextLiteralSegment(UriTemplateLiteralPathSegment lps) { Fx.Assert(lps != null, "must be - based on the segment nature"); if (this.nextLiteralSegment != null && this.nextLiteralSegment.ContainsKey(lps)) { return(this.nextLiteralSegment[lps].node); } else { if (this.nextLiteralSegment == null) { this.nextLiteralSegment = new Dictionary <UriTemplateLiteralPathSegment, UriTemplateTrieLocation>(); } UriTemplateTrieNode newNode = new UriTemplateTrieNode(this.depth + 1); newNode.onFailure = new UriTemplateTrieLocation(this, UriTemplateTrieIntraNodeLocation.AfterLiteral); this.nextLiteralSegment.Add(lps, new UriTemplateTrieLocation(newNode, UriTemplateTrieIntraNodeLocation.BeforeLiteral)); return(newNode); } }
private bool ComputeRelativeSegmentsAndLookup(Uri uri, ICollection <string> relativePathSegments, ICollection <UriTemplateTableMatchCandidate> candidates) { string[] segments = uri.Segments; int num = segments.Length - this.numSegmentsInBaseAddress; UriTemplateLiteralPathSegment[] wireData = new UriTemplateLiteralPathSegment[num]; for (int i = 0; i < num; i++) { string str = segments[i + this.numSegmentsInBaseAddress]; UriTemplateLiteralPathSegment segment = UriTemplateLiteralPathSegment.CreateFromWireData(str); wireData[i] = segment; string item = Uri.UnescapeDataString(str); if (segment.EndsWithSlash) { item = item.Substring(0, item.Length - 1); } relativePathSegments.Add(item); } return(this.rootNode.Match(wireData, candidates)); }
public abstract bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash);
public bool IsMatch(UriTemplateLiteralPathSegment segment) { return(IsMatch(segment, false)); }
private static bool TryMatch(UriTemplateLiteralPathSegment[] wireUriSegments, UriTemplateTrieLocation currentLocation, out UriTemplatePathPartiallyEquivalentSet success, out SingleLocationOrLocationsSet nextStep) { IList <IList <UriTemplatePathPartiallyEquivalentSet> > list2; success = null; nextStep = new SingleLocationOrLocationsSet(); if (wireUriSegments.Length <= currentLocation.node.depth) { if (currentLocation.node.endOfPath.Items.Count != 0) { success = currentLocation.node.endOfPath; return(true); } if (currentLocation.node.star.Items.Count != 0) { success = currentLocation.node.star; return(true); } nextStep = new SingleLocationOrLocationsSet(currentLocation.node.onFailure); return(false); } UriTemplateLiteralPathSegment key = wireUriSegments[currentLocation.node.depth]; bool flag = false; bool flag2 = false; bool flag3 = false; bool flag4 = false; switch (currentLocation.locationWithin) { case UriTemplateTrieIntraNodeLocation.BeforeLiteral: flag = true; flag2 = true; flag3 = true; flag4 = true; break; case UriTemplateTrieIntraNodeLocation.AfterLiteral: flag = false; flag2 = true; flag3 = true; flag4 = true; break; case UriTemplateTrieIntraNodeLocation.AfterCompound: flag = false; flag2 = false; flag3 = true; flag4 = true; break; case UriTemplateTrieIntraNodeLocation.AfterVariable: flag = false; flag2 = false; flag3 = false; flag4 = true; break; } if (key.EndsWithSlash) { IList <IList <UriTemplateTrieLocation> > list; if ((flag && (currentLocation.node.nextLiteralSegment != null)) && currentLocation.node.nextLiteralSegment.ContainsKey(key)) { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.nextLiteralSegment[key]); return(false); } if ((flag2 && (currentLocation.node.nextCompoundSegment != null)) && AscendingSortedCompoundSegmentsCollection <UriTemplateTrieLocation> .Lookup(currentLocation.node.nextCompoundSegment, key, out list)) { nextStep = new SingleLocationOrLocationsSet(list); return(false); } if ((flag3 && (currentLocation.node.nextVariableSegment != null)) && !key.IsNullOrEmpty()) { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.nextVariableSegment); return(false); } if (flag4 && (currentLocation.node.star.Items.Count != 0)) { success = currentLocation.node.star; return(true); } nextStep = new SingleLocationOrLocationsSet(currentLocation.node.onFailure); return(false); } if ((flag && (currentLocation.node.finalLiteralSegment != null)) && currentLocation.node.finalLiteralSegment.ContainsKey(key)) { success = currentLocation.node.finalLiteralSegment[key]; return(true); } if ((flag2 && (currentLocation.node.finalCompoundSegment != null)) && AscendingSortedCompoundSegmentsCollection <UriTemplatePathPartiallyEquivalentSet> .Lookup(currentLocation.node.finalCompoundSegment, key, out list2)) { if (list2[0].Count == 1) { success = list2[0][0]; } else { success = new UriTemplatePathPartiallyEquivalentSet(currentLocation.node.depth + 1); for (int i = 0; i < list2[0].Count; i++) { success.Items.AddRange(list2[0][i].Items); } } return(true); } if (flag3 && (currentLocation.node.finalVariableSegment.Items.Count != 0)) { success = currentLocation.node.finalVariableSegment; return(true); } if (flag4 && (currentLocation.node.star.Items.Count != 0)) { success = currentLocation.node.star; return(true); } nextStep = new SingleLocationOrLocationsSet(currentLocation.node.onFailure); return(false); }
public static bool Lookup(AscendingSortedCompoundSegmentsCollection <T> collection, UriTemplateLiteralPathSegment wireData, out IList <IList <T> > results) { results = collection.Find(wireData); return((results != null) && (results.Count > 0)); }
static bool TryMatch(UriTemplateLiteralPathSegment[] wireUriSegments, UriTemplateTrieLocation currentLocation, out UriTemplatePathPartiallyEquivalentSet success, out SingleLocationOrLocationsSet nextStep) { // if returns true, success is set to answer // if returns false, nextStep is set to next place to look success = null; nextStep = new SingleLocationOrLocationsSet(); if (wireUriSegments.Length <= currentLocation.node.depth) { Fx.Assert(wireUriSegments.Length == 0 || wireUriSegments[wireUriSegments.Length - 1].EndsWithSlash, "we should not have traversed this deep into the trie unless the wire path ended in a slash"); if (currentLocation.node.endOfPath.Items.Count != 0) { // exact match of e.g. "path1/path2/" success = currentLocation.node.endOfPath; return(true); } else if (currentLocation.node.star.Items.Count != 0) { // inexact match of e.g. WIRE("path1/path2/") against TEMPLATE("path1/path2/*") success = currentLocation.node.star; return(true); } else { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.onFailure); return(false); } } else { UriTemplateLiteralPathSegment curWireSeg = wireUriSegments[currentLocation.node.depth]; bool considerLiteral = false; bool considerCompound = false; bool considerVariable = false; bool considerStar = false; switch (currentLocation.locationWithin) { case UriTemplateTrieIntraNodeLocation.BeforeLiteral: considerLiteral = true; considerCompound = true; considerVariable = true; considerStar = true; break; case UriTemplateTrieIntraNodeLocation.AfterLiteral: considerLiteral = false; considerCompound = true; considerVariable = true; considerStar = true; break; case UriTemplateTrieIntraNodeLocation.AfterCompound: considerLiteral = false; considerCompound = false; considerVariable = true; considerStar = true; break; case UriTemplateTrieIntraNodeLocation.AfterVariable: considerLiteral = false; considerCompound = false; considerVariable = false; considerStar = true; break; default: Fx.Assert("bad kind"); break; } if (curWireSeg.EndsWithSlash) { IList <IList <UriTemplateTrieLocation> > compoundLocationsSet; if (considerLiteral && currentLocation.node.nextLiteralSegment != null && currentLocation.node.nextLiteralSegment.ContainsKey(curWireSeg)) { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.nextLiteralSegment[curWireSeg]); return(false); } else if (considerCompound && currentLocation.node.nextCompoundSegment != null && AscendingSortedCompoundSegmentsCollection <UriTemplateTrieLocation> .Lookup(currentLocation.node.nextCompoundSegment, curWireSeg, out compoundLocationsSet)) { nextStep = new SingleLocationOrLocationsSet(compoundLocationsSet); return(false); } else if (considerVariable && currentLocation.node.nextVariableSegment != null && !curWireSeg.IsNullOrEmpty()) { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.nextVariableSegment); return(false); } else if (considerStar && currentLocation.node.star.Items.Count != 0) { // matches e.g. WIRE("path1/path2/path3") and TEMPLATE("path1/*") success = currentLocation.node.star; return(true); } else { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.onFailure); return(false); } } else { IList <IList <UriTemplatePathPartiallyEquivalentSet> > compoundPathEquivalentSets; Fx.Assert(!curWireSeg.EndsWithSlash, "!curWireSeg.EndsWithSlash"); Fx.Assert(!curWireSeg.IsNullOrEmpty(), "!curWireSeg.IsNullOrEmpty()"); if (considerLiteral && currentLocation.node.finalLiteralSegment != null && currentLocation.node.finalLiteralSegment.ContainsKey(curWireSeg)) { // matches e.g. WIRE("path1/path2") and TEMPLATE("path1/path2") success = currentLocation.node.finalLiteralSegment[curWireSeg]; return(true); } else if (considerCompound && currentLocation.node.finalCompoundSegment != null && AscendingSortedCompoundSegmentsCollection <UriTemplatePathPartiallyEquivalentSet> .Lookup(currentLocation.node.finalCompoundSegment, curWireSeg, out compoundPathEquivalentSets)) { // matches e.g. WIRE("path1/path2") and TEMPLATE("path1/p{var}th2") // we should take only the highest order match! Fx.Assert(compoundPathEquivalentSets.Count >= 1, "Lookup is expected to return false otherwise"); Fx.Assert(compoundPathEquivalentSets[0].Count > 0, "Find shouldn't return empty sublists"); if (compoundPathEquivalentSets[0].Count == 1) { success = compoundPathEquivalentSets[0][0]; } else { success = new UriTemplatePathPartiallyEquivalentSet(currentLocation.node.depth + 1); for (int i = 0; i < compoundPathEquivalentSets[0].Count; i++) { success.Items.AddRange(compoundPathEquivalentSets[0][i].Items); } } return(true); } else if (considerVariable && currentLocation.node.finalVariableSegment.Items.Count != 0) { // matches e.g. WIRE("path1/path2") and TEMPLATE("path1/{var}") success = currentLocation.node.finalVariableSegment; return(true); } else if (considerStar && currentLocation.node.star.Items.Count != 0) { // matches e.g. WIRE("path1/path2") and TEMPLATE("path1/*") success = currentLocation.node.star; return(true); } else { nextStep = new SingleLocationOrLocationsSet(currentLocation.node.onFailure); return(false); } } } }