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 Unix
22c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * file system. The rules for file and directory names include:
23c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <ul>
24c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <li>Filenames are case-sensitive</li>
25c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <li>Forward slashes (/) are the only valid path separators</li>
26c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * </ul>
27c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * <p/>
28c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * The <code>directoryListingFormatter</code> property is automatically initialized to an instance
29c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * of {@link UnixDirectoryListingFormatter}.
30c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
31c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @author Chris Mair
32c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @version $Revision$ - $Date$
33c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
34c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpublic class UnixFakeFileSystem extends AbstractFakeFileSystem {
35c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
36c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public static final char SEPARATOR = '/';
37c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
38c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
39c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Construct a new instance and initialize the directoryListingFormatter to a UnixDirectoryListingFormatter.
40c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
41c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public UnixFakeFileSystem() {
42c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        this.setDirectoryListingFormatter(new UnixDirectoryListingFormatter());
43c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
44c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
45c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    //-------------------------------------------------------------------------
46c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    // Abstract Method Implementations
47c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    //-------------------------------------------------------------------------
48c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
49c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    protected char getSeparatorChar() {
50c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return SEPARATOR;
51c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
52c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
53c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
54c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Return true if the specified path designates a valid (absolute) file path. For Unix,
55c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * a path is valid if it starts with the '/' character, followed by an optional sequence of
56c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * any characters except '/'.
57c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
58c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param path - the path
59c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if path is valid, false otherwise
60c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @throws AssertionError - if path is null
61c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
62c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    protected boolean isValidName(String path) {
63c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Assert.notNull(path, "path");
64c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        // Any character but '/'
65c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return path.matches("\\/|(\\/[^\\/]+)+");
66c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
67c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
68c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
69c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Return true if the specified char is a separator character ('\' or '/')
70c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
71c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param c - the character to test
72c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if the specified char is a separator character ('\' or '/')
73c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
74c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    protected boolean isSeparator(char c) {
75c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return c == SEPARATOR;
76c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
77c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
78c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
79c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if the specified path component is a root for this filesystem
80c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
81c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    protected boolean isRoot(String pathComponent) {
82c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return pathComponent.indexOf(":") != -1;
83c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
84c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
85c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair}