//******************************************************************************************************
// EmbeddedResourceFileAttribute.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/04/2010 - Pinal C. Patel
// Generated original version of source code.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Text.RegularExpressions;
namespace GSF.Web.Hosting
{
///
/// Attribute indicating the location of an embedded resource that should be
/// served by the .
///
///
///
/// This attribute is used by the
/// module/path provider to retrieve the list of embedded resources from an assembly
/// that should be considered a part of the virtual filesystem.
///
///
///
///
/// Below is an example of what it might look like to embed a web form and a
/// user control in an assembly to be served up with the :
///
///
/// [assembly: EmbeddedResourceFileAttribute("MyNamespace.WebForm1.aspx", "MyNamespace")]
/// [assembly: EmbeddedResourceFileAttribute("MyNamespace.UserControl1.ascx", "MyNamespace")]
///
///
///
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class EmbeddedResourceFileAttribute : Attribute
{
#region [ Members ]
// Fields
///
/// Internal storage for the
///
/// property.
///
///
private readonly string m_resourceNamespace;
///
/// Internal storage for the
///
/// property.
///
///
private readonly string m_resourcePath;
#endregion
#region [ Constructors ]
///
/// Initializes a new instance of the class.
///
/// The path to the embedded resource. Used to get the resource as a stream from the assembly.
/// The namespace the resource is in. This will generally be removed from the full resource path to calculate the "application path" for the embedded resource.
///
/// Thrown if or is .
///
///
/// Thrown if is
/// or if it only consists of periods and/or spaces.
///
///
///
/// Both and
/// will be processed to have leading and trailing periods and spaces removed.
/// If ends up being empty, an
/// is thrown. No exception
/// is thrown if turns out empty.
///
///
///
///
/// If the is RootNS.SubNS.AppRoot.Folder.File.aspx
/// and the is RootNS.SubNS.AppRoot,
/// the virtual "path" to the embedded file will be Folder.File.aspx
/// (which will be converted by the
/// to ~/Folder/File.aspx).
///
///
///
public EmbeddedResourceFileAttribute(string resourcePath, string resourceNamespace)
{
if (resourcePath == null)
{
throw new ArgumentNullException(nameof(resourcePath));
}
if (resourcePath.Length == 0)
{
throw new ArgumentOutOfRangeException(nameof(resourcePath));
}
if (resourceNamespace == null)
{
throw new ArgumentNullException(nameof(resourceNamespace));
}
m_resourcePath = RemoveMalformedEndChars(resourcePath);
if (m_resourcePath.Length == 0)
{
throw new ArgumentOutOfRangeException(nameof(resourcePath), resourcePath, "The resource path is invalid for mapping.");
}
m_resourceNamespace = RemoveMalformedEndChars(resourceNamespace);
if (m_resourceNamespace.Length == 0)
{
throw new ArgumentOutOfRangeException(nameof(resourceNamespace), resourceNamespace, "The resource namespace is invalid for mapping.");
}
}
#endregion
#region [ Properties ]
///
/// Gets the namespace for the embedded resource.
///
///
/// A with the embedded resource namespace.
///
///
///
/// This namespace will be removed from the full
/// to create the virtual application path for the resource.
///
///
///
public string ResourceNamespace
{
get
{
return m_resourceNamespace;
}
}
///
/// Gets the path to the embedded resource.
///
///
/// A with the full path to an embedded resource in
/// the associated assembly.
///
///
///
/// This path will be used to retrieve the resource from the assembly and serve
/// it up.
///
///
///
public string ResourcePath
{
get
{
return m_resourcePath;
}
}
#endregion
#region [ Methods ]
///
/// Removes leading and trailing dots and spaces from a string.
///
/// The to be cleaned up.
/// A version of with leading and trailing dots and spaces removed.
///
private static string RemoveMalformedEndChars(string toFix)
{
return MalformedEndCharExpression.Replace(toFix, "$1");
}
#endregion
#region [ Static ]
// Static Fields
///
/// Regular expression indicating the characters that can't start or end a resource.
///
///
///
private static readonly Regex MalformedEndCharExpression = new Regex("^[. ]*(.*?)[. ]*$", RegexOptions.Compiled);
#endregion
}
}