1b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair/* 2b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Copyright 2008 the original author or authors. 3b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * 4b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * you may not use this file except in compliance with the License. 6b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * You may obtain a copy of the License at 7b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * 8b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * http://www.apache.org/licenses/LICENSE-2.0 9b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * 10b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Unless required by applicable law or agreed to in writing, software 11b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * See the License for the specific language governing permissions and 14b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * limitations under the License. 15b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair */ 16b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairpackage org.mockftpserver.fake.filesystem; 17b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 18b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairimport org.apache.log4j.Logger; 19b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairimport org.mockftpserver.core.util.StringUtil; 20b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 21b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairimport java.text.DateFormat; 22b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairimport java.text.SimpleDateFormat; 23b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 24b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair/** 25b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Unix-specific implementation of the DirectoryListingFormatter interface. 26b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * 27b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @author Chris Mair 28b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @version $Revision$ - $Date$ 29b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair */ 30b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismairpublic class UnixDirectoryListingFormatter implements DirectoryListingFormatter { 31b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 32b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private static final Logger LOG = Logger.getLogger(UnixDirectoryListingFormatter.class); 33b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 34b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private static final String DATE_FORMAT = "MMM dd yyyy"; 35b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private static final int SIZE_WIDTH = 15; 36b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private static final int OWNER_WIDTH = 8; 37b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private static final int GROUP_WIDTH = 8; 38b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private static final String NONE = "none"; 39b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 40b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair // "-rw-rw-r-- 1 ftp ftp 254 Feb 23 2007 robots.txt" 41b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair // "-rw-r--r-- 1 ftp ftp 30014925 Apr 15 00:19 md5.sums.gz" 42b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair // "-rwxr-xr-x 1 henry users 5778 Dec 1 2005 planaccess.sql" 43b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 44b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair /** 45b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * Format the directory listing for a single file/directory entry. 46b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * 47b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @param fileSystemEntry - the FileSystemEntry for a single file system entry 48b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair * @return the formatted directory listing 49b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair */ 50b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair public String format(FileSystemEntry fileSystemEntry) { 51b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 52b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String dateStr = dateFormat.format(fileSystemEntry.getLastModified()); 53b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String dirOrFile = fileSystemEntry.isDirectory() ? "d" : "-"; 54b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair Permissions permissions = fileSystemEntry.getPermissions() != null ? fileSystemEntry.getPermissions() : Permissions.DEFAULT; 55b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String permissionsStr = StringUtil.padRight(permissions.asRwxString(), 9); 56b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String linkCountStr = "1"; 57b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String ownerStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getOwner()), OWNER_WIDTH); 58b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String groupStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getGroup()), GROUP_WIDTH); 59b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String sizeStr = StringUtil.padLeft(Long.toString(fileSystemEntry.getSize()), SIZE_WIDTH); 60b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair String listing = "" + dirOrFile + permissionsStr + " " + linkCountStr + " " + ownerStr + " " + groupStr + " " + sizeStr + " " + dateStr + " " + fileSystemEntry.getName(); 61b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair LOG.info("listing=[" + listing + "]"); 62b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair return listing; 63b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair } 64b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 65b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair private String stringOrNone(String string) { 66b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair return (string == null) ? NONE : string; 67b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair } 68b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair 69b2f4a2dfc590c250e42b21eb40d9539ac135b495chrismair}