WindowsFakeFileSystem.java revision c1de24f1bfa6699e54b069e300af5e4246b34a34
1c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/* 2c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Copyright 2008 the original author or authors. 3c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 4c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * you may not use this file except in compliance with the License. 6c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * You may obtain a copy of the License at 7c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 8c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * http://www.apache.org/licenses/LICENSE-2.0 9c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 10c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Unless required by applicable law or agreed to in writing, software 11c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * See the License for the specific language governing permissions and 14c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * limitations under the License. 15c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 16c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpackage org.mockftpserver.fake.filesystem; 17c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 18c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.util.Assert; 19c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 20c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/** 21c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Implementation of the {@link FileSystem} interface that simulates a Microsoft 22c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Windows file system. The rules for file and directory names include: 23c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <ul> 24c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <li>Filenames are case-insensitive (and normalized to lower-case)</li> 25c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <li>Either forward slashes (/) or backward slashes (\) are valid path separators (but are normalized to '\')</li> 26c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <li>An absolute path starts with a drive specifier (e.g. 'a:' or 'c:') followed 27c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * by '\' or '/', or else if it starts with "\\"</li> 28c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * </ul> 29c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <p/> 30c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * The <code>directoryListingFormatter</code> property is automatically initialized to an instance 31c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * of {@link WindowsDirectoryListingFormatter}. 32c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 33c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @author Chris Mair 34c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @version $Revision$ - $Date$ 35c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 36c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpublic class WindowsFakeFileSystem extends AbstractFakeFileSystem { 37c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 38c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair public static final char SEPARATOR = '\\'; 39c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair private static final String VALID_PATTERN = "\\p{Alpha}\\:" + "(\\\\|(\\\\[^\\\\\\:\\*\\?\\<\\>\\|\\\"]+)+)"; 40c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair //static final VALID_PATTERN = /\p{Alpha}\:(\\|(\\[^\\\:\*\?\<\>\|\"]+)+)/ 41c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair private static final String LAN_PREFIX = "\\\\"; 42c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 43c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair /** 44c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Construct a new instance and initialize the directoryListingFormatter to a WindowsDirectoryListingFormatter. 45c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 46c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair public WindowsFakeFileSystem() { 47c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair this.setDirectoryListingFormatter(new WindowsDirectoryListingFormatter()); 48c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair } 49c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 50c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair //------------------------------------------------------------------------- 51c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair // Abstract Or Overridden Method Implementations 52c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair //------------------------------------------------------------------------- 53c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 54c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair /** 55c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Return the normalized and unique key used to access the file system entry. Windows is case-insensitive, 56c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * so normalize all paths to lower-case. 57c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 58c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @param path - the path 59c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @return the corresponding normalized key 60c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 61c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair protected String getFileSystemEntryKey(String path) { 62c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair return normalize(path).toLowerCase(); 63c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair } 64c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 65c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair protected char getSeparatorChar() { 66c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair return SEPARATOR; 67c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair } 68c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 69c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair /** 70c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Return true if the specified path designates a valid (absolute) file path. For Windows 71c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * paths, a path is valid if it starts with a drive specifier followed by 72c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * '\' or '/', or if it starts with "\\". 73c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 74c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @param path - the path 75c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @return true if path is valid, false otherwise 76c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @throws AssertionError - if path is null 77c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 78c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair protected boolean isValidName(String path) { 79c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair // \/:*?"<>| 80c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair Assert.notNull(path, "path"); 81c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair String standardized = path.replace('/', '\\'); 82c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair return standardized.matches(VALID_PATTERN) || standardized.startsWith(LAN_PREFIX); 83c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair } 84c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 85c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair /** 86c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Return true if the specified char is a separator character ('\' or '/') 87c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * 88c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @param c - the character to test 89c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @return true if the specified char is a separator character ('\' or '/') 90c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 91c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair protected boolean isSeparator(char c) { 92c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair return c == '\\' || c == '/'; 93c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair } 94c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 95c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair /** 96c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @return true if the specified path component is a root for this filesystem 97c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */ 98c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair protected boolean isRoot(String pathComponent) { 99c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair return pathComponent.indexOf(":") != -1; 100c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair } 101c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair 102c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair}