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 zero or more names
56     * (a sequence of any characters except '/'), delimited by '/'. The path may optionally
57     * contain a terminating '/'.
58     *
59     * @param path - the path
60     * @return true if path is valid, false otherwise
61     * @throws AssertionError - if path is null
62     */
63    protected boolean isValidName(String path) {
64        Assert.notNull(path, "path");
65        // Any character but '/'
66        return path.matches("\\/|(\\/[^\\/]+\\/?)+");
67
68    }
69
70    /**
71     * Return true if the specified char is a separator character ('\' or '/')
72     *
73     * @param c - the character to test
74     * @return true if the specified char is a separator character ('\' or '/')
75     */
76    protected boolean isSeparator(char c) {
77        return c == SEPARATOR;
78    }
79
80    /**
81     * @return true if the specified path component is a root for this filesystem
82     */
83    protected boolean isRoot(String pathComponent) {
84        return pathComponent.indexOf(":") != -1;
85    }
86
87}