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