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