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