/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Everest.CmsServices.Models;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Services{
/// <summary>
///
/// </summary>
public class NamespaceObject
{
public string Namespace { get; set; }
/// <summary>
/// Gets or sets the full name.
/// </summary>
/// <value>The full name.</value>
public string FullName
{
get
{
if (StringExtensions.IsNullOrEmptyTrim(Namespace))
{
return this.Name;
}
else
{
return string.Format("{0}.{1}", Namespace, Name);
}
}
}
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
public string Name { get; set; }
}
/// <summary>
///
/// </summary>
public class NamespaceHelper
{
/// <summary>
/// Gets the child namespaces.
/// </summary>
/// <param name="parentNamespace">The parent namespace.</param>
/// <param name="fullChildNamespace">The full child namespace.</param>
/// <returns></returns>
public static IEnumerable<NamespaceObject> GetChildNamespaces(string parentNamespace, IEnumerable<string> fullChildNamespace)
{
IDictionary<string, NamespaceObject> dic = new Dictionary<string, NamespaceObject>();
foreach (var item in fullChildNamespace)
{
var namespaceObj = new NamespaceObject() { Namespace = parentNamespace };
string subNamespace = item;
if (!StringExtensions.IsNullOrEmptyTrim(parentNamespace))
{
subNamespace = item.Replace(parentNamespace, "");
}
namespaceObj.Name = GetFirstNamespaceNode(subNamespace);
if (!StringExtensions.IsNullOrEmptyTrim(namespaceObj.Name) && !dic.ContainsKey(namespaceObj.Name))
{
dic.Add(namespaceObj.Name, namespaceObj);
}
}
return dic.Values;
}
/// <summary>
/// Gets the first namespace node.
/// </summary>
/// <param name="s">The s.</param>
/// <returns></returns>
private static string GetFirstNamespaceNode(string s)
{
if (s.StartsWith("."))
{
s = s.Substring(1);
}
int dotIndex = s.IndexOf(".");
if (dotIndex != -1)
{
return s.Substring(0, dotIndex);
}
else
{
return s;
}
}
}
}
|