private TextItem GetTextItem(InternalMatch match) { string text = match.RegexMatch.Value; switch (match.TextItemType) { case TextItemType.WebLink: Uri webUri; if (Uri.TryCreate(text, UriKind.Absolute, out webUri)) { return(new TextItem(text, TextItemType.WebLink, webUri)); } return(new TextItem(text, TextItemType.Text)); case TextItemType.HashTag: Uri twitterHashUri; if (TryGetHashTagUri(text, out twitterHashUri)) { return(new TextItem(text, TextItemType.HashTag, twitterHashUri)); } return(new TextItem(text, TextItemType.Text)); default: return(new TextItem(text, match.TextItemType)); } }
/// <summary> /// Parses the resource name and gets the registered resource handler and /// any path params. /// </summary> /// <param name="resourceName">Resource name</param> /// <returns>Returns found match, or null if there is no match.</returns> public Match GetHandler(string resourceName) { InternalMatch match = new InternalMatch(); string subrname = resourceName; int pl = Pattern.Length; if (pl > 0) { int rl = resourceName.Length; if (pl == rl) { if (Pattern != resourceName) { return(null); } subrname = ""; } else { if (pl > rl || !resourceName.StartsWith(Pattern) || resourceName[pl] != '.') { return(null); } subrname = resourceName.Substring(pl + 1); } } if (subrname.Length == 0) { if (root.Handler == null) { return(null); } return(new Match(root.Handler, root.EventHandler, null, root.Group.ToString(resourceName, null, 0))); } string[] tokens = subrname.Split(BTSEP); matchNode(root, tokens, 0, 0, match); return(match.Node == null ? null : new Match( match.Node.Handler, match.Node.EventHandler, match.Params, match.Node.Group.ToString(resourceName, tokens, match.MountIdx))); }
public IEnumerable <TextItem> Split(string text, TextPatterns patterns) { List <TextItem> result = new List <TextItem>(); try { // check if there is a chance that the text contains any parts that need to be treated specially: if (string.IsNullOrEmpty(text) || !SpecialConstructs.Any(text.Contains)) { result.Add(new TextItem(text ?? string.Empty, TextItemType.Text)); } else { IEnumerable <InternalMatch> specialMatches = new InternalMatch[] { }; if ((patterns & TextPatterns.Urls) == TextPatterns.Urls) { specialMatches = specialMatches .Union(GetMatches(text, HttpUrlPattern, TextItemType.WebLink)); } if ((patterns & TextPatterns.Hashtags) == TextPatterns.Hashtags) { specialMatches = specialMatches .Union(GetMatches(text, HashTagPattern, TextItemType.HashTag)); } specialMatches = specialMatches .OrderBy(im => im.RegexMatch.Index) .ToArray(); if (!specialMatches.Any()) { result.Add(new TextItem(text, TextItemType.Text)); } else { bool thereAreMoreSpecialFeatures; int matchIndex = 0; InternalMatch match; int currentStartIndex = 0; do { thereAreMoreSpecialFeatures = null != (match = specialMatches.Skip(matchIndex).FirstOrDefault()); if (!thereAreMoreSpecialFeatures) { // Return the remaining text as a regular text item. result.Add(new TextItem(text.Substring(currentStartIndex), TextItemType.Text)); } else { Match regexMatch = match.RegexMatch; if (regexMatch.Index > currentStartIndex) { result.Add(new TextItem(text.Substring(currentStartIndex, regexMatch.Index - currentStartIndex), TextItemType.Text)); } result.Add(GetTextItem(match)); matchIndex++; currentStartIndex = regexMatch.Index + regexMatch.Length; } }while (thereAreMoreSpecialFeatures); } } return(result); } catch (Exception ex) { logger.LogError(ex, $"Error in {nameof(Split)}({nameof(text)} = '{text}'):\n{ex.ToString()}"); throw; } }
private bool matchNode(Node current, string[] tokens, int tokenIdx, int mountIdx, InternalMatch nodeMatch) { Node next = null; if (current.Nodes != null) { current.Nodes.TryGetValue(tokens[tokenIdx], out next); } if (current.IsMounted) { mountIdx = tokenIdx; } tokenIdx++; int c = 2; // A counter to run the code below twice while (c > 0) { // Does the node exist if (next != null) { // Check if it is the last token if (tokens.Length == tokenIdx) { // Check if this node has handlers if (next.Handler != null) { nodeMatch.Node = next; nodeMatch.MountIdx = mountIdx; // Check if we have path parameters for the handlers if (next.Params != null) { // Create a map with path parameter values nodeMatch.Params = new Dictionary <string, string>(next.Params.Count); foreach (PathParam pp in next.Params) { nodeMatch.Params[pp.Name] = tokens[pp.Idx + mountIdx]; } } return(true); } } else { // Match against next node if (matchNode(next, tokens, tokenIdx, mountIdx, nodeMatch)) { return(true); } } } // To avoid repeating code above, set node to test to l.param // and run it all again. next = current.Param; c--; } // Check full wild card if (current.Wild != null) { next = current.Wild; nodeMatch.Node = next; nodeMatch.MountIdx = mountIdx; if (next.Params != null) { // Create a map with path parameter values nodeMatch.Params = new Dictionary <string, string>(next.Params.Count); foreach (PathParam pp in next.Params) { nodeMatch.Params[pp.Name] = tokens[pp.Idx + mountIdx]; } } return(true); } return(false); }