| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 356 | public class UnixDirectoryListingFormatter implements DirectoryListingFormatter { |
| 33 | |
|
| 34 | 2 | 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 | 356 | private Locale locale = Locale.ENGLISH; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public String format(FileSystemEntry fileSystemEntry) { |
| 55 | 9 | DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, locale); |
| 56 | 9 | String dateStr = dateFormat.format(fileSystemEntry.getLastModified()); |
| 57 | 9 | String dirOrFile = fileSystemEntry.isDirectory() ? "d" : "-"; |
| 58 | 9 | Permissions permissions = fileSystemEntry.getPermissions() != null ? fileSystemEntry.getPermissions() : Permissions.DEFAULT; |
| 59 | 9 | String permissionsStr = StringUtil.padRight(permissions.asRwxString(), 9); |
| 60 | 9 | String linkCountStr = "1"; |
| 61 | 9 | String ownerStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getOwner()), OWNER_WIDTH); |
| 62 | 9 | String groupStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getGroup()), GROUP_WIDTH); |
| 63 | 9 | String sizeStr = StringUtil.padLeft(Long.toString(fileSystemEntry.getSize()), SIZE_WIDTH); |
| 64 | 9 | String listing = "" + dirOrFile + permissionsStr + " " + linkCountStr + " " + ownerStr + " " + groupStr + " " + sizeStr + " " + dateStr + " " + fileSystemEntry.getName(); |
| 65 | 9 | LOG.info("listing=[" + listing + "]"); |
| 66 | 9 | return listing; |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public void setLocale(Locale locale) { |
| 74 | 1 | this.locale = locale; |
| 75 | 1 | } |
| 76 | |
|
| 77 | |
private String stringOrNone(String string) { |
| 78 | 18 | return (string == null) ? NONE : string; |
| 79 | |
} |
| 80 | |
|
| 81 | |
} |