示例#1
0
        //TODO onInit maken
        /// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
        public FilePathLayout(Layout layout, bool cleanupInvalidChars, FilePathKind filePathKind)
        {
            _layout = layout;
            _filePathKind = filePathKind;
            _cleanupInvalidChars = cleanupInvalidChars;

            if (_layout == null)
            {
                _filePathKind = FilePathKind.Unknown;
                return;
            }

            //do we have to the the layout?
            if (cleanupInvalidChars || _filePathKind == FilePathKind.Unknown)
            {
                //check if fixed 
                var pathLayout2 = layout as SimpleLayout;
                if (pathLayout2 != null)
                {
                    var isFixedText = pathLayout2.IsFixedText;
                    if (isFixedText)
                    {
                        cleanedFixedResult = pathLayout2.FixedText;
                        if (cleanupInvalidChars)
                        {
                            //clean first
                            cleanedFixedResult = CleanupInvalidFilePath(cleanedFixedResult);
                        }
                    }

                    //detect absolute
                    if (_filePathKind == FilePathKind.Unknown)
                    {
                        _filePathKind = DetectFilePathKind(pathLayout2);
                    }
                }
                else
                {
                    _filePathKind = FilePathKind.Unknown;
                }
            }
#if !SILVERLIGHT

            if (_filePathKind == FilePathKind.Relative)
            {
                _baseDir = AppDomainWrapper.CurrentDomain.BaseDirectory;
            }
#endif

        }
示例#2
0
        public override IReadOnlyList<string> NormalizeFragments(IReadOnlyList<string> fragments, FilePathKind pathKind)
        {
            var fragmentsArray = fragments.ToArray();

            if(pathKind == FilePathKind.Absolute && fragmentsArray.Length > 0 && fragmentsArray[0].Length > 1) {
                // ドライブ文字を大文字に
                fragmentsArray[0] = fragmentsArray[0].ToUpper().TrimEnd(':');
            }

            if(pathKind == FilePathKind.Relative) {
                fragmentsArray = FilePath.Resolve(fragmentsArray).ToArray();
            }

            return Array.AsReadOnly(fragmentsArray);
        }
示例#3
0
        /// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
        public FilePathLayout(Layout layout, bool cleanupInvalidChars, FilePathKind filePathKind)
        {
            _layout              = layout;
            _filePathKind        = filePathKind;
            _cleanupInvalidChars = cleanupInvalidChars;

            if (_layout == null)
            {
                _filePathKind = FilePathKind.Unknown;
                return;
            }

            //do we have to the the layout?
            if (cleanupInvalidChars || _filePathKind == FilePathKind.Unknown)
            {
                //check if fixed
                var pathLayout2 = layout as SimpleLayout;
                if (pathLayout2 != null)
                {
                    var isFixedText = pathLayout2.IsFixedText;
                    if (isFixedText)
                    {
                        _cleanedFixedResult = pathLayout2.FixedText;
                        if (cleanupInvalidChars)
                        {
                            //clean first
                            _cleanedFixedResult = CleanupInvalidFilePath(_cleanedFixedResult);
                        }
                    }

                    //detect absolute
                    if (_filePathKind == FilePathKind.Unknown)
                    {
                        _filePathKind = DetectFilePathKind(pathLayout2);
                    }
                }
                else
                {
                    _filePathKind = FilePathKind.Unknown;
                }
            }

            if (_filePathKind == FilePathKind.Relative)
            {
                _baseDir = LogFactory.CurrentAppDomain.BaseDirectory;
            }
        }
示例#4
0
        private static FilePathKind DetectKind(Layout layout, FilePathKind currentFilePathKind)
        {
            if (layout is SimpleLayout simpleLayout)
            {
                //detect absolute
                if (currentFilePathKind == FilePathKind.Unknown)
                {
                    return(DetectFilePathKind(simpleLayout));
                }
            }
            else
            {
                return(FilePathKind.Unknown);
            }

            return(currentFilePathKind);
        }
示例#5
0
        /// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
        public FilePathLayout(Layout layout, bool cleanupInvalidChars, FilePathKind filePathKind)
        {
            _layout              = layout;
            _filePathKind        = filePathKind;
            _cleanupInvalidChars = cleanupInvalidChars;

            if (_layout == null)
            {
                _filePathKind = FilePathKind.Unknown;
                return;
            }

            //do we have to the the layout?
            if (cleanupInvalidChars || _filePathKind == FilePathKind.Unknown)
            {
                _cleanedFixedResult = CreateCleanedFixedResult(cleanupInvalidChars, layout);
                _filePathKind       = DetectKind(layout, _filePathKind);
            }

            if (_filePathKind == FilePathKind.Relative)
            {
                _baseDir = LogFactory.CurrentAppDomain.BaseDirectory;
            }
        }
示例#6
0
 public void DetectFilePathKind(string path, FilePathKind expected)
 {
     Layout layout = path;
     var result = FilePathLayout.DetectFilePathKind(layout);
     Assert.Equal(expected, result);
 }
示例#7
0
 private async static Task<StorageFolder> GetFolderAsync(string directoryPath, FilePathKind filePathKind)
 {
     try
     {
         switch (filePathKind)
         {
             case FilePathKind.InstalledFolder:
                 return await Package.Current.InstalledLocation.GetFolderAsync(directoryPath);
             case FilePathKind.DataFolder:
                 return await ApplicationData.Current.LocalFolder.GetFolderAsync(directoryPath);
             case FilePathKind.AbsolutePath:
                 return await StorageFolder.GetFolderFromPathAsync(directoryPath);
             default:
                 throw new NotImplementedException();
         }
     }
     catch (FileNotFoundException)
     {
         return null;
     }
 }
示例#8
0
 private static async Task<Windows.Storage.StorageFile> CreateFileAsync(string key,
     FilePathKind fileKind = FilePathKind.DataFolder,
     CreationCollisionOption option = CreationCollisionOption.OpenIfExists)
 {
     switch (fileKind)
     {
         case FilePathKind.DataFolder:
             return await ApplicationData.Current.LocalFolder.CreateFileAsync(key, option);
         case FilePathKind.InstalledFolder:
             return await Package.Current.InstalledLocation.CreateFileAsync(key, option);
         case FilePathKind.AbsolutePath:
         default:
             throw new NotSupportedException(fileKind.ToString());
     }
 }
示例#9
0
        private async static Task<StorageFile> GetFileAsync(string filePath, FilePathKind filePathKind)
        {
            var directoryPath = Path.GetDirectoryName(filePath);
            var fileName = Path.GetFileName(filePath);
            var storageFolder = await GetFolderAsync(directoryPath, filePathKind);
            if (storageFolder == null)
            {
                return null;
            }

            try
            {
                return await storageFolder.GetFileAsync(fileName);
            }
            catch (Exception)
            {
                return null;
            }
        }
示例#10
0
 protected async static Task<Stream> OpenFileReadAsync(string filePath, FilePathKind filePathKind)
 {
     var file = await GetFileAsync(filePath, filePathKind);
     if (file == null)
     {
         return null;
     }
     var randomStream = await file.OpenReadAsync();
     return randomStream.AsStream();
 }
示例#11
0
        public override bool Parse(string path, FilePathKind pathKind, out string[] fragments)
        {
            if(!Enum.IsDefined(typeof(FilePathKind), pathKind)) {
                throw new ArgumentException("pathKind");
            }
            fragments = null;

            // 絶対パスの場合、ドライブ名をチェック
            if(pathKind == FilePathKind.Absolute) {
                if(!(path.Length > 0 && (('A' <= path[0] && path[0] <= 'Z') || ('a' <= path[0] && path[0] <= 'z')))) {
                    return false;
                }
                if(!IsAbsolute(path)) {
                    return false;
                }

                // ドライブ区切り除去
                var v = path.Split(new string[]{":"}, 2, StringSplitOptions.None);
                path = v[0] + v[1];
            } else {
                if(IsAbsolute(path)) {
                    return false;
                }
            }

            fragments = FilePath.Resolve(
                path.Split(
                    this.AltDirectorySeparators
                        .Concat(new string[]{this.DirectorySeparator})
                        .ToArray(),
                    StringSplitOptions.RemoveEmptyEntries
                )
            ).ToArray();

            return this.IsValid(fragments);
        }