<commit_before>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2014-2015 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary 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
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Zongsoft.IO
{
	public static class FileSystem
	{
		#region 成员字段
		private static readonly IFile _file;
		private static readonly IDirectory _directory;
		private static Zongsoft.Services.IServiceProvider _providers;
		#endregion

		#region 构造函数
		static FileSystem()
		{
			_file = new FileProvider();
			_directory = new DirectoryProvider();
		}
		#endregion

		#region 公共属性
		public static IFile File
		{
			get
			{
				return _file;
			}
		}

		public static IDirectory Directory
		{
			get
			{
				return _directory;
			}
		}

		public static Zongsoft.Services.IServiceProvider Providers
		{
			get
			{
				if(_providers == null)
					System.Threading.Interlocked.CompareExchange(ref _providers, new Zongsoft.Services.ServiceProvider(), null);

				return _providers;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_providers = value;
			}
		}
		#endregion

		#region 公共方法
		public static string GetUrl(string virtualPath)
		{
			if(string.IsNullOrEmpty(virtualPath))
				return virtualPath;

			//如果传入的虚拟路径参数是一个URI格式，则直接返回它作为结果
			if(Zongsoft.Text.TextRegular.Uri.Url.IsMatch(virtualPath))
				return virtualPath;

			Path path;
			var fs = GetFileSystem(virtualPath, out path);
			return fs.GetUrl(path.FullPath);
		}
		#endregion

		#region 私有方法
		private static IFileSystem GetFileSystem(string text, out Path path)
		{
			if(_providers == null)
				throw new InvalidOperationException("The value of 'Services' property is null.");

			if(string.IsNullOrWhiteSpace(text))
				throw new ArgumentNullException("text");

			path = Path.Parse(text);

			var fileSystem = _providers.Resolve<IFileSystem>(path.Schema);

			if(fileSystem == null)
				throw new InvalidOperationException(string.Format("Can not obtain the File or Directory provider by the '{0}' path.", path));

			return fileSystem;<commit_msg>:card_index:  调整 FileSystem.GetUrl(...) 方法的处理规则。 :rugby_football:<commit_after>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2014-2015 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary 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
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Zongsoft.IO
{
	public static class FileSystem
	{
		#region 成员字段
		private static readonly IFile _file;
		private static readonly IDirectory _directory;
		private static Zongsoft.Services.IServiceProvider _providers;
		#endregion

		#region 构造函数
		static FileSystem()
		{
			_file = new FileProvider();
			_directory = new DirectoryProvider();
		}
		#endregion

		#region 公共属性
		public static IFile File
		{
			get
			{
				return _file;
			}
		}

		public static IDirectory Directory
		{
			get
			{
				return _directory;
			}
		}

		public static Zongsoft.Services.IServiceProvider Providers
		{
			get
			{
				if(_providers == null)
					System.Threading.Interlocked.CompareExchange(ref _providers, new Zongsoft.Services.ServiceProvider(), null);

				return _providers;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_providers = value;
			}
		}
		#endregion

		#region 公共方法
		public static string GetUrl(string virtualPath)
		{
			if(string.IsNullOrWhiteSpace(virtualPath))
				return virtualPath;

			//如果传入的虚拟路径参数是一个URI格式，则直接返回它作为结果
			if(Zongsoft.Text.TextRegular.Uri.Url.IsMatch(virtualPath))
				return virtualPath;

			Path path;
			var fs = GetFileSystem(virtualPath, out path);

			return fs == null ? virtualPath : fs.GetUrl(path.FullPath);
		}
		#endregion

		#region 私有方法
		private static IFileSystem GetFileSystem(string text, out Path path)
		{
			if(_providers == null)
				throw new InvalidOperationException("The value of 'Services' property is null.");

			if(string.IsNullOrWhiteSpace(text))
				throw new ArgumentNullException("text");

			//如果路径解析失败则返回空
			if(!Path.TryParse(text, out path))
				return null;

			var fileSystem = _providers.Resolve<IFileSystem>(path.Schema);

			if(fileSystem == null)
				throw new InvalidOperationException(string.Format("Can not obtain the File or Directory provider by the '{0}' path.", path));

			return fileSystem;
{'fraction_non_alphanumeric': 0.08386889460154241, 'fraction_numerical': 0.007069408740359897, 'mean_word_length': 3.726651480637813, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2931132', 'n_tokens_mistral': 2172, 'n_tokens_neox': 1893, 'n_words': 616}