Beispiel #1
0
        public void AddProtocolMappingSilentlyOverwritesExistingProtocol()
        {
            ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(FileSystemResource));
            // overwrite, must not complain...
            ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(AssemblyResource));
            IResource res = new ConfigurableResourceLoader().GetResource("beep://Oragon.Spring.Core.Tests/Spring/TestResource.txt");

            Assert.IsNotNull(res, "Resource must not be null");
            Assert.AreEqual(typeof(AssemblyResource), res.GetType(),
                            "The original IResource Type associated with the 'beep' protocol " +
                            "must have been overwritten; expecting an AssemblyResource 'cos " +
                            "we registered it last under the 'beep' protocol.");
        }
Beispiel #2
0
 public void SetUp()
 {
     loader = new ConfigurableResourceLoader();
 }
        /// <summary>
        /// Creates a new resource that is relative to this resource based on the
        /// supplied <paramref name="resourceName"/>.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This method can accept either a fully qualified resource name or a
        /// relative resource name as it's parameter.
        /// </p>
        /// <p>
        /// A fully qualified resource is one that has a protocol prefix and
        /// all elements of the resource name. All other resources are treated
        /// as relative to this resource, and the following rules are used to
        /// locate a relative resource:
        /// </p>
        /// <list type="bullet">
        ///     <item>
        ///     If the <paramref name="resourceName"/> starts with <c>'..'</c>,
        ///     the current resource path is navigated backwards before the
        ///     <paramref name="resourceName"/> is concatenated to the current
        ///     <see cref="Oragon.Spring.Core.IO.AbstractResource.ResourcePath"/> of
        ///     this resource.
        ///     </item>
        ///     <item>
        ///     If the <paramref name="resourceName"/> starts with '/', the
        ///     current resource path is ignored and a new resource name is
        ///     appended to the
        ///     <see cref="Oragon.Spring.Core.IO.AbstractResource.RootLocation"/> of
        ///     this resource.
        ///     </item>
        ///     <item>
        ///     If the <paramref name="resourceName"/> starts with '.' or a
        ///     letter, a new path is appended to the current
        ///     <see cref="Oragon.Spring.Core.IO.AbstractResource.ResourcePath"/> of
        ///     this resource.
        ///     </item>
        /// </list>
        /// </remarks>
        /// <param name="resourceName">
        /// The name of the resource to create.
        /// </param>
        /// <returns>The relative resource.</returns>
        /// <exception cref="System.UriFormatException">
        /// If the process of resolving the relative resource yielded an
        /// invalid URI.
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// If this resource does not support the resolution of relative
        /// resources (as determined by the value of the
        /// <see cref="Oragon.Spring.Core.IO.AbstractResource.SupportsRelativeResources"/>
        /// property).
        /// </exception>
        /// <seealso cref="Oragon.Spring.Core.IO.AbstractResource.ResourcePath"/>
        public virtual IResource CreateRelative(string resourceName)
        {
            AssertUtils.ArgumentNotNull(resourceName, "relativePath");

            // try to create fully qualified resource...
            IResourceLoader loader = GetResourceLoader();

            if (ConfigurableResourceLoader.HasProtocol(resourceName))
            {
                IResource resource = loader.GetResource(resourceName);
                if (resource != null)
                {
                    return(resource);
                }
            }
            if (!SupportsRelativeResources)
            {
                throw new NotSupportedException(GetType().Name +
                                                " does not support relative resources. Please use fully qualified resource name.");
            }

            StringBuilder fullResourceName = new StringBuilder(256);

            if (Protocol != null && Protocol != String.Empty)
            {
                fullResourceName.Append(Protocol).Append(ConfigurableResourceLoader.ProtocolSeparator);
            }

            if (!IsRelativeResource(resourceName))
            {
                fullResourceName.Append(resourceName);
            }
            else
            {
                string targetResource;
                string resourcePath;
                int    n = resourceName.LastIndexOfAny(new char[] { '/', '\\' });
                if (n >= 0)
                {
                    targetResource = resourceName.Substring(n + 1);
                    resourcePath   = CalculateResourcePath(resourceName.Substring(0, n + 1));
                }
                else // only resource name is specified, so current path should be used
                {
                    targetResource = resourceName;
                    resourcePath   = ResourcePath;
                }

                fullResourceName.Append(RootLocation.TrimEnd('\\', '/'));
                if (resourcePath != null && resourcePath != String.Empty)
                {
                    fullResourceName.Append('/').Append(resourcePath);
                }
                fullResourceName.Append('/').Append(targetResource);
            }

            string resultResourceName = fullResourceName.ToString();

            if (!ConfigurableResourceLoader.HasProtocol(resultResourceName))
            {
                // give derived resource classes a chance to create an instance on their own
                IResource resultResource = CreateResourceInstance(resultResourceName);
                if (resultResource != null)
                {
                    return(resultResource);
                }
            }

            // create resource instance using default loader
            return(loader.GetResource(resultResourceName));
        }
 /// <summary>
 /// Creates a new instance of the
 /// <see cref="Oragon.Spring.Core.IO.AbstractResource"/> class.
 /// </summary>
 /// <remarks>
 /// <p>
 /// This is an <see langword="abstract"/> class, and as such exposes no
 /// public constructors.
 /// </p>
 /// </remarks>
 /// <param name="resourceName">
 /// A string representation of the resource.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// If the supplied <paramref name="resourceName"/> is
 /// <see langword="null"/> or contains only whitespace character(s).
 /// </exception>
 protected AbstractResource(string resourceName)
 {
     AssertUtils.ArgumentHasText(resourceName, "resourceName");
     this.protocol     = ConfigurableResourceLoader.GetProtocol(resourceName);
     this.resourceName = resourceName;
 }