177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/* 277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Copyright 2008 the original author or authors. 377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * you may not use this file except in compliance with the License. 677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * You may obtain a copy of the License at 777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * http://www.apache.org/licenses/LICENSE-2.0 977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 1077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Unless required by applicable law or agreed to in writing, software 1177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * distributed under the License is distributed on an "AS IS" BASIS, 1277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * See the License for the specific language governing permissions and 1477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * limitations under the License. 1577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 1677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpackage org.mockftpserver.fake.filesystem; 1777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 1877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.util.Assert; 1977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 2077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/** 2177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Implementation of the {@link FileSystem} interface that simulates a Microsoft 2277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Windows file system. The rules for file and directory names include: 2377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <ul> 2477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>Filenames are case-insensitive (and normalized to lower-case)</li> 2577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>Either forward slashes (/) or backward slashes (\) are valid path separators (but are normalized to '\')</li> 2677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>An absolute path starts with a drive specifier (e.g. 'a:' or 'c:') followed 2777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * by '\' or '/', or else if it starts with "\\"</li> 2877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * </ul> 2977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <p/> 3077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * The <code>directoryListingFormatter</code> property is automatically initialized to an instance 3177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * of {@link WindowsDirectoryListingFormatter}. 3277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 3377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @author Chris Mair 3477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @version $Revision$ - $Date$ 3577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 3677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpublic class WindowsFakeFileSystem extends AbstractFakeFileSystem { 3777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 3877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair public static final char SEPARATOR = '\\'; 3977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair private static final String VALID_PATTERN = "\\p{Alpha}\\:" + "(\\\\|(\\\\[^\\\\\\:\\*\\?\\<\\>\\|\\\"]+)+)"; 4077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair //static final VALID_PATTERN = /\p{Alpha}\:(\\|(\\[^\\\:\*\?\<\>\|\"]+)+)/ 4177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair private static final String LAN_PREFIX = "\\\\"; 4277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 4377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 4477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Construct a new instance and initialize the directoryListingFormatter to a WindowsDirectoryListingFormatter. 4577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 4677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair public WindowsFakeFileSystem() { 4777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair this.setDirectoryListingFormatter(new WindowsDirectoryListingFormatter()); 4877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 4977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 5077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair //------------------------------------------------------------------------- 5177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // Abstract Or Overridden Method Implementations 5277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair //------------------------------------------------------------------------- 5377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 5477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 5577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Return the normalized and unique key used to access the file system entry. Windows is case-insensitive, 5677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * so normalize all paths to lower-case. 5777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 5877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @param path - the path 5977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @return the corresponding normalized key 6077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 6177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected String getFileSystemEntryKey(String path) { 6277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair return normalize(path).toLowerCase(); 6377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 6477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 6577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected char getSeparatorChar() { 6677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair return SEPARATOR; 6777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 6877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 6977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 7077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Return true if the specified path designates a valid (absolute) file path. For Windows 7177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * paths, a path is valid if it starts with a drive specifier followed by 7277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * '\' or '/', or if it starts with "\\". 7377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 7477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @param path - the path 7577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @return true if path is valid, false otherwise 7677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @throws AssertionError - if path is null 7777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 7877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected boolean isValidName(String path) { 7977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // \/:*?"<>| 8077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair Assert.notNull(path, "path"); 8177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair String standardized = path.replace('/', '\\'); 8277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair return standardized.matches(VALID_PATTERN) || standardized.startsWith(LAN_PREFIX); 8377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 8477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 8577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 8677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Return true if the specified char is a separator character ('\' or '/') 8777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * 8877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @param c - the character to test 8977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @return true if the specified char is a separator character ('\' or '/') 9077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 9177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected boolean isSeparator(char c) { 9277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair return c == '\\' || c == '/'; 9377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 9477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 9577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 9677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @return true if the specified path component is a root for this filesystem 9777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 9877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected boolean isRoot(String pathComponent) { 9977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair return pathComponent.indexOf(":") != -1; 10077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 10177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 10277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair}