public static UdiRange Parse(string s) { Uri uri; if (Uri.IsWellFormedUriString(s, UriKind.Absolute) == false || Uri.TryCreate(s, UriKind.Absolute, out uri) == false) { //if (tryParse) return false; throw new FormatException(string.Format("String \"{0}\" is not a valid udi range.", s)); } var udiUri = uri.Query == string.Empty ? uri : new UriBuilder(uri) { Query = string.Empty }.Uri; return(new UdiRange(Udi.Create(udiUri), uri.Query.TrimStart(Constants.CharArrays.QuestionMark))); }
/// <summary> /// Initializes a new instance of the <see cref="UdiRange"/> class with a <see cref="Core.Udi"/> and an optional selector. /// </summary> /// <param name="udi">A <see cref="Udi"/>.</param> /// <param name="selector">An optional selector.</param> public UdiRange(Udi udi, string selector = Constants.DeploySelector.This) { Udi = udi; switch (selector) { case Constants.DeploySelector.This: Selector = selector; _uriValue = udi.UriValue; break; case Constants.DeploySelector.ChildrenOfThis: case Constants.DeploySelector.DescendantsOfThis: case Constants.DeploySelector.ThisAndChildren: case Constants.DeploySelector.ThisAndDescendants: Selector = selector; _uriValue = new Uri(Udi + "?" + selector); break; default: throw new ArgumentException(string.Format("Invalid selector \"{0}\".", selector)); } }
/// <summary> /// Converts the string representation of an entity identifier into the equivalent Udi instance. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="udi">An Udi instance that contains the value that was parsed.</param> /// <returns>A boolean value indicating whether the string could be parsed.</returns> public static bool TryParse(string s, out Udi udi) { return(ParseInternal(s, true, false, out udi)); }
private static bool ParseInternal(string s, bool tryParse, bool knownTypes, out Udi udi) { udi = null; Uri uri; if (Uri.IsWellFormedUriString(s, UriKind.Absolute) == false || Uri.TryCreate(s, UriKind.Absolute, out uri) == false) { if (tryParse) { return(false); } throw new FormatException(string.Format("String \"{0}\" is not a valid udi.", s)); } var entityType = uri.Host; if (UdiTypes.TryGetValue(entityType, out var udiType) == false) { if (knownTypes) { // not knowing the type is not an error // just return the unknown type udi udi = UnknownTypeUdi.Instance; return(false); } if (tryParse) { return(false); } throw new FormatException(string.Format("Unknown entity type \"{0}\".", entityType)); } var path = uri.AbsolutePath.TrimStart('/'); if (udiType == UdiType.GuidUdi) { if (path == string.Empty) { udi = GetRootUdi(uri.Host); return(true); } if (Guid.TryParse(path, out var guid) == false) { if (tryParse) { return(false); } throw new FormatException(string.Format("String \"{0}\" is not a valid udi.", s)); } udi = new GuidUdi(uri.Host, guid); return(true); } if (udiType == UdiType.StringUdi) { udi = path == string.Empty ? GetRootUdi(uri.Host) : new StringUdi(uri.Host, Uri.UnescapeDataString(path)); return(true); } if (tryParse) { return(false); } throw new InvalidOperationException(string.Format("Invalid udi type \"{0}\".", udiType)); }
/// <summary> /// Converts the string representation of an entity identifier into the equivalent Udi instance. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="knownTypes">A value indicating whether to only deal with known types.</param> /// <param name="udi">An Udi instance that contains the value that was parsed.</param> /// <returns>A boolean value indicating whether the string could be parsed.</returns> /// <remarks> /// <para>If <paramref name="knownTypes"/> is <c>true</c>, and the string could not be parsed because /// the entity type was not known, the method returns <c>false</c> but still sets <c>udi</c> /// to an <see cref="UnknownTypeUdi"/> value.</para> /// <para>If <paramref name="knownTypes"/> is <c>true</c>, assemblies are not scanned for types, /// and therefore only builtin types may be known. Unless scanning already took place.</para> /// </remarks> public static bool TryParse(string s, bool knownTypes, out Udi udi) { return(ParseInternal(s, true, knownTypes, out udi)); }
/// <summary> /// Converts the string representation of an entity identifier into the equivalent Udi instance. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="udi">An Udi instance that contains the value that was parsed.</param> /// <returns>A boolean value indicating whether the string could be parsed.</returns> public static bool TryParse(string s, [MaybeNullWhen(returnValue: false)] out Udi udi) { return(ParseInternal(s, true, false, out udi)); }