예제 #1
0
        public static bool TryParse(
            Uri identifier,
            IReadOnlyCollection <IExtension> extensions,
            out IUniformResourceIdentifier parsedIdentifier)
        {
            parsedIdentifier = null;

            if (null == identifier)
            {
                throw new ArgumentNullException(UniformResourceIdentifier.ArgumentNameIdentifier);
            }

            IReadOnlyCollection <IExtension> effectiveExtensions =
                extensions ?? Array.Empty <IExtension>();

            IResourceQuery query = new ResourceQuery(identifier);

            IReadOnlyCollection <string> alternatePathCollection =
                effectiveExtensions
                .Select(
                    (IExtension item) =>
                    string.Format(CultureInfo.InvariantCulture, UniformResourceIdentifier.AlternatePathTemplate, item.Path))
                .ToArray();
            string alternatePaths = string.Join(string.Empty, alternatePathCollection);

            string retrievalPattern =
                string.Format(CultureInfo.InvariantCulture, UniformResourceIdentifier.RetrievalPatternTemplate, alternatePaths);
            Regex retrievalExpression = new Regex(retrievalPattern, RegexOptions.IgnoreCase);
            Match expressionMatch     = retrievalExpression.Match(identifier.AbsoluteUri);

            if (!expressionMatch.Success)
            {
                return(false);
            }

            string type = expressionMatch.Groups[UniformResourceIdentifier.ExpressionGroupNameType].Value;

            if (string.IsNullOrWhiteSpace(type))
            {
                return(false);
            }

            string schemaIdentifier;

            switch (type)
            {
            case ProtocolConstants.PathGroups:
                schemaIdentifier = SchemaIdentifiers.Core2Group;
                break;

            case ProtocolConstants.PathUsers:
                schemaIdentifier = SchemaIdentifiers.Core2EnterpriseUser;
                break;

            default:
                if (null == extensions)
                {
                    return(false);
                }

                schemaIdentifier =
                    effectiveExtensions
                    .Where(
                        (IExtension item) =>
                        string.Equals(item.Path, type, StringComparison.OrdinalIgnoreCase))
                    .Select(
                        (IExtension item) =>
                        item.SchemaIdentifier)
                    .SingleOrDefault();
                if (string.IsNullOrWhiteSpace(schemaIdentifier))
                {
                    return(false);
                }
                break;
            }

            IResourceIdentifier resourceIdentifier = new ResourceIdentifier();

            resourceIdentifier.SchemaIdentifier = schemaIdentifier;

            string resourceIdentifierValue = expressionMatch.Groups[UniformResourceIdentifier.ExpressionGroupNameIdentifier].Value;

            if (!string.IsNullOrWhiteSpace(resourceIdentifierValue))
            {
                string unescapedIdentifier = Uri.UnescapeDataString(resourceIdentifierValue);
                resourceIdentifier.Identifier = unescapedIdentifier;
            }

            parsedIdentifier = new UniformResourceIdentifier(resourceIdentifier, query);
            return(true);
        }
 /// <inheritdoc />
 int IComparable <IUniformResourceIdentifier> .CompareTo(IUniformResourceIdentifier other) => ComparableImplementations.ImplementCompareTo(DefaultComparer, this, other);
예제 #3
0
 /// <summary>
 /// Converts to a <see cref="Uri"/>.
 /// </summary>
 public static Uri ToUri(this IUniformResourceIdentifier @this) => new Uri(@this.ToString(), UriKind.Absolute);
 /// <inheritdoc />
 bool IEquatable <IUniformResourceIdentifier> .Equals(IUniformResourceIdentifier other) => ComparableImplementations.ImplementEquals(DefaultComparer, this, other);
예제 #5
0
 /// <summary>
 /// Converts to a <see cref="Uri"/>.
 /// </summary>
 public static Uri ToUri(this IUniformResourceIdentifier @this)
 {
     _ = @this ?? throw new ArgumentNullException(nameof(@this));
     return(new Uri(@this.ToString(), UriKind.Absolute));
 }