Provides utility methods for interface and feed URIs.
Example #1
0
        //--------------------//

        #region Normalize
        /// <summary>
        /// Sets missing default values and handles legacy elements.
        /// </summary>
        /// <param name="feedUri">The feed the data was originally loaded from; can be <see langword="null"/>.</param>
        /// <remarks>This method should be called to prepare a <see cref="Feed"/> for solver processing. Do not call it if you plan on serializing the feed again since it may loose some of its structure.</remarks>
        public override void Normalize(FeedUri feedUri = null)
        {
            base.Normalize(feedUri);

            // Merge the version modifier into the normal version attribute
            if (!string.IsNullOrEmpty(VersionModifier))
            {
                Version         = new ImplementationVersion(Version + VersionModifier);
                VersionModifier = null;
            }

            // Default stability rating to testing
            if (Stability == Stability.Unset)
            {
                Stability = Stability.Testing;
            }

            // Make local paths absolute
            if (!string.IsNullOrEmpty(LocalPath))
            {
                LocalPath = ModelUtils.GetAbsolutePath(LocalPath, feedUri);
            }
            else if (!string.IsNullOrEmpty(ID) && ID.StartsWith(".")) // Get local path from ID
            {
                LocalPath = ID = ModelUtils.GetAbsolutePath(ID, feedUri);
            }

            // Parse manifest digest from ID if missing
            if (!string.IsNullOrEmpty(ID))
            {
                _manifestDigest.ParseID(ID);
            }
        }
Example #2
0
        /// <summary>
        /// Creates a new implementation version from a a string.
        /// </summary>
        /// <param name="value">The string containing the version information.</param>
        /// <exception cref="FormatException"><paramref name="value"/> is not a valid version string.</exception>
        public ImplementationVersion([NotNull] string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new FormatException(Resources.MustStartWithDottedList);
            }

            if (ModelUtils.ContainsTemplateVariables(value))
            {
                _verbatimString  = value;
                _additionalParts = new VersionPart[0];
                return;
            }

            string[] parts = value.Split('-');

            // Ensure the first part is a dotted list
            if (!VersionDottedList.IsValid(parts[0]))
            {
                throw new FormatException(Resources.MustStartWithDottedList);
            }
            _firstPart = new VersionDottedList(parts[0]);

            // Iterate through all additional parts
            _additionalParts = new VersionPart[parts.Length - 1];
            for (int i = 1; i < parts.Length; i++)
            {
                _additionalParts[i - 1] = new VersionPart(parts[i]);
            }
        }
Example #3
0
 public void TestContainsTemplateVariables()
 {
     ModelUtils.ContainsTemplateVariables("").Should().BeFalse();
     ModelUtils.ContainsTemplateVariables("x").Should().BeFalse();
     ModelUtils.ContainsTemplateVariables("}{").Should().BeFalse();
     ModelUtils.ContainsTemplateVariables("{}").Should().BeTrue();
     ModelUtils.ContainsTemplateVariables("{var}").Should().BeTrue();
     ModelUtils.ContainsTemplateVariables("x{var}x").Should().BeTrue();
 }
        //--------------------//

        #region Normalize
        /// <inheritdoc/>
        public override void Normalize(FeedUri feedUri = null)
        {
            base.Normalize(feedUri);

            if (Href != null)
            {
                Href = ModelUtils.GetAbsoluteHref(Href, feedUri);
            }
        }
Example #5
0
 public void TestContainsTemplateVariables()
 {
     Assert.IsFalse(ModelUtils.ContainsTemplateVariables(""));
     Assert.IsFalse(ModelUtils.ContainsTemplateVariables("x"));
     Assert.IsFalse(ModelUtils.ContainsTemplateVariables("}{"));
     Assert.IsTrue(ModelUtils.ContainsTemplateVariables("{}"));
     Assert.IsTrue(ModelUtils.ContainsTemplateVariables("{var}"));
     Assert.IsTrue(ModelUtils.ContainsTemplateVariables("x{var}x"));
 }
Example #6
0
        public void TestGetAbsoluteHref()
        {
            Uri absoluteHref = WindowsUtils.IsWindows ? new Uri("file:///C:/local/subdir/file") : new Uri("file:///local/subdir/file");

            var result = ModelUtils.GetAbsoluteHref(new Uri("subdir/file", UriKind.Relative), new FeedUri(WindowsUtils.IsWindows ? @"C:\local\feed.xml" : "/local/feed.xml"));

            Assert.IsTrue(result.IsAbsoluteUri);
            Assert.AreEqual(absoluteHref, result);

            Assert.AreEqual(absoluteHref, ModelUtils.GetAbsoluteHref(absoluteHref), "Should ignore source if href is already absolute.");
        }
Example #7
0
        public void TestGetAbsolutePath()
        {
            string absolutePath = WindowsUtils.IsWindows ? @"C:\local\subdir\file" : "/local/subdir/file";

            string result = ModelUtils.GetAbsolutePath("subdir/file", new FeedUri(WindowsUtils.IsWindows ? @"C:\local\" : "/local/"));

            Assert.IsTrue(Path.IsPathRooted(result));
            Assert.AreEqual(absolutePath, result);

            Assert.AreEqual(absolutePath, ModelUtils.GetAbsolutePath(absolutePath), "Should ignore source if path is already absolute.");
        }
Example #8
0
        public void TestGetAbsoluteHref()
        {
            Uri absoluteHref = WindowsUtils.IsWindows ? new Uri("file:///C:/local/subdir/file") : new Uri("file:///local/subdir/file");

            var result = ModelUtils.GetAbsoluteHref(new Uri("subdir/file", UriKind.Relative), new FeedUri(WindowsUtils.IsWindows ? @"C:\local\feed.xml" : "/local/feed.xml"));

            result.IsAbsoluteUri.Should().BeTrue();
            result.Should().Be(absoluteHref);

            ModelUtils.GetAbsoluteHref(absoluteHref)
            .Should().Be(absoluteHref, because: "Should ignore source if href is already absolute.");
        }
Example #9
0
        public void TestGetAbsolutePath()
        {
            string absolutePath = WindowsUtils.IsWindows ? @"C:\local\subdir\file" : "/local/subdir/file";

            string result = ModelUtils.GetAbsolutePath("subdir/file", new FeedUri(WindowsUtils.IsWindows ? @"C:\local\" : "/local/"));

            Path.IsPathRooted(result).Should().BeTrue();
            result.Should().Be(absolutePath);

            ModelUtils.GetAbsolutePath(absolutePath)
            .Should().Be(absolutePath, because: "Should ignore source if path is already absolute.");
        }
Example #10
0
        /// <summary>
        /// Sets missing default values and handles legacy elements.
        /// </summary>
        /// <param name="feedUri">The feed the data was originally loaded from.</param>
        /// <remarks>This method should be called to prepare a <see cref="Feed"/> for solver processing. Do not call it if you plan on serializing the feed again since it may loose some of its structure.</remarks>
        public override void Normalize(FeedUri feedUri)
        {
            #region Sanity checks
            if (feedUri == null)
            {
                throw new ArgumentNullException(nameof(feedUri));
            }
            #endregion

            base.Normalize(feedUri);

            // Merge the version modifier into the normal version attribute
            if (!string.IsNullOrEmpty(VersionModifier))
            {
                Version         = new ImplementationVersion(Version + VersionModifier);
                VersionModifier = null;
            }

            // Default stability rating to testing
            if (Stability == Stability.Unset)
            {
                Stability = Stability.Testing;
            }

            // Make local paths absolute
            try
            {
                if (!string.IsNullOrEmpty(LocalPath))
                {
                    LocalPath = ModelUtils.GetAbsolutePath(LocalPath, feedUri);
                }
                else if (!string.IsNullOrEmpty(ID) && ID.StartsWith(".")) // Get local path from ID
                {
                    LocalPath = ID = ModelUtils.GetAbsolutePath(ID, feedUri);
                }
            }
            #region Error handling
            catch (UriFormatException ex)
            {
                Log.Error(ex);
                LocalPath = null;
            }
            #endregion

            // Parse manifest digest from ID if missing
            if (!string.IsNullOrEmpty(ID))
            {
                _manifestDigest.ParseID(ID);
            }
        }
Example #11
0
        /// <inheritdoc/>
        public override void Normalize(FeedUri feedUri)
        {
            #region Sanity checks
            if (feedUri == null)
            {
                throw new ArgumentNullException(nameof(feedUri));
            }
            #endregion

            base.Normalize(feedUri);

            if (Href != null)
            {
                Href = ModelUtils.GetAbsoluteHref(Href, feedUri);
            }
        }
Example #12
0
 public void TestGetAbsoluteHrefException()
 {
     Assert.Throws <UriFormatException>(() => ModelUtils.GetAbsoluteHref(new Uri("subdir/file", UriKind.Relative)));
     Assert.Throws <UriFormatException>(() => ModelUtils.GetAbsoluteHref(new Uri("subdir/file", UriKind.Relative), new FeedUri("http://remote/")));
 }
Example #13
0
 public void TestGetAbsolutePathException()
 {
     Assert.Throws <UriFormatException>(() => ModelUtils.GetAbsolutePath("subdir/file"));
     Assert.Throws <UriFormatException>(() => ModelUtils.GetAbsolutePath("subdir/file", new FeedUri("http://remote/")));
 }