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