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