160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/*
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2008 the original author or authors.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License");
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License.
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *      http://www.apache.org/licenses/LICENSE-2.0
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.fake.filesystem;
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.util.Assert;
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Implementation of the {@link FileSystem} interface that simulates a Microsoft
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Windows file system. The rules for file and directory names include:
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <ul>
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>Filenames are case-insensitive (and normalized to lower-case)</li>
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>Either forward slashes (/) or backward slashes (\) are valid path separators (but are normalized to '\')</li>
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>An absolute path starts with a drive specifier (e.g. 'a:' or 'c:') followed
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * by '\' or '/', or else if it starts with "\\"</li>
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * </ul>
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <p/>
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * The <code>directoryListingFormatter</code> property is automatically initialized to an instance
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * of {@link WindowsDirectoryListingFormatter}.
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic class WindowsFakeFileSystem extends AbstractFakeFileSystem {
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public static final char SEPARATOR = '\\';
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String VALID_PATTERN = "\\p{Alpha}\\:" + "(\\\\|(\\\\[^\\\\\\:\\*\\?\\<\\>\\|\\\"]+)+)";
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //static final VALID_PATTERN = /\p{Alpha}\:(\\|(\\[^\\\:\*\?\<\>\|\"]+)+)/
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String LAN_PREFIX = "\\\\";
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Construct a new instance and initialize the directoryListingFormatter to a WindowsDirectoryListingFormatter.
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public WindowsFakeFileSystem() {
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        this.setDirectoryListingFormatter(new WindowsDirectoryListingFormatter());
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Abstract Or Overridden Method Implementations
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Return the normalized and unique key used to access the file system entry. Windows is case-insensitive,
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * so normalize all paths to lower-case.
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     *
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param path - the path
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @return the corresponding normalized key
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected String getFileSystemEntryKey(String path) {
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return normalize(path).toLowerCase();
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected char getSeparatorChar() {
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return SEPARATOR;
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Return true if the specified path designates a valid (absolute) file path. For Windows
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * paths, a path is valid if it starts with a drive specifier followed by
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * '\' or '/', or if it starts with "\\".
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     *
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param path - the path
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @return true if path is valid, false otherwise
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @throws AssertionError - if path is null
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected boolean isValidName(String path) {
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // \/:*?"<>|
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        Assert.notNull(path, "path");
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        String standardized = path.replace('/', '\\');
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return standardized.matches(VALID_PATTERN) || standardized.startsWith(LAN_PREFIX);
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Return true if the specified char is a separator character ('\' or '/')
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     *
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param c - the character to test
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @return true if the specified char is a separator character ('\' or '/')
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected boolean isSeparator(char c) {
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return c == '\\' || c == '/';
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @return true if the specified path component is a root for this filesystem
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected boolean isRoot(String pathComponent) {
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return pathComponent.indexOf(":") != -1;
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}