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.apache.log4j.Logger;
19import org.mockftpserver.core.util.StringUtil;
20
21import java.text.DateFormat;
22import java.text.SimpleDateFormat;
23import java.util.Locale;
24
25/**
26 * Unix-specific implementation of the DirectoryListingFormatter interface.
27 *
28 * @author Chris Mair
29 * @version $Revision$ - $Date$
30 */
31public class UnixDirectoryListingFormatter implements DirectoryListingFormatter {
32
33    private static final Logger LOG = Logger.getLogger(UnixDirectoryListingFormatter.class);
34
35    private static final String DATE_FORMAT = "MMM dd  yyyy";
36    private static final int SIZE_WIDTH = 15;
37    private static final int OWNER_WIDTH = 8;
38    private static final int GROUP_WIDTH = 8;
39    private static final String NONE = "none";
40
41    private Locale locale = Locale.ENGLISH;
42
43    // "-rw-rw-r--    1 ftp      ftp           254 Feb 23  2007 robots.txt"
44    // "-rw-r--r--    1 ftp      ftp      30014925 Apr 15 00:19 md5.sums.gz"
45    // "-rwxr-xr-x   1 henry    users       5778 Dec  1  2005 planaccess.sql"
46
47    /**
48     * Format the directory listing for a single file/directory entry.
49     *
50     * @param fileSystemEntry - the FileSystemEntry for a single file system entry
51     * @return the formatted directory listing
52     */
53    public String format(FileSystemEntry fileSystemEntry) {
54        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, locale);
55        String dateStr = dateFormat.format(fileSystemEntry.getLastModified());
56        String dirOrFile = fileSystemEntry.isDirectory() ? "d" : "-";
57        Permissions permissions = fileSystemEntry.getPermissions() != null ? fileSystemEntry.getPermissions() : Permissions.DEFAULT;
58        String permissionsStr = StringUtil.padRight(permissions.asRwxString(), 9);
59        String linkCountStr = "1";
60        String ownerStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getOwner()), OWNER_WIDTH);
61        String groupStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getGroup()), GROUP_WIDTH);
62        String sizeStr = StringUtil.padLeft(Long.toString(fileSystemEntry.getSize()), SIZE_WIDTH);
63        String listing = "" + dirOrFile + permissionsStr + "  " + linkCountStr + " " + ownerStr + " " + groupStr + " " + sizeStr + " " + dateStr + " " + fileSystemEntry.getName();
64        LOG.info("listing=[" + listing + "]");
65        return listing;
66    }
67
68    /**
69     * Set the Locale to be used in formatting the date within file/directory listings
70     * @param locale - the Locale instance
71     */
72    public void setLocale(Locale locale) {
73        this.locale = locale;
74    }
75
76    private String stringOrNone(String string) {
77        return (string == null) ? NONE : string;
78    }
79
80}