15eefc0033edb232f9febcc1d32ede04bc901067cchrismair/* 25eefc0033edb232f9febcc1d32ede04bc901067cchrismair * Copyright 2008 the original author or authors. 35eefc0033edb232f9febcc1d32ede04bc901067cchrismair * 45eefc0033edb232f9febcc1d32ede04bc901067cchrismair * Licensed under the Apache License, Version 2.0 (the "License"); 55eefc0033edb232f9febcc1d32ede04bc901067cchrismair * you may not use this file except in compliance with the License. 65eefc0033edb232f9febcc1d32ede04bc901067cchrismair * You may obtain a copy of the License at 75eefc0033edb232f9febcc1d32ede04bc901067cchrismair * 85eefc0033edb232f9febcc1d32ede04bc901067cchrismair * http://www.apache.org/licenses/LICENSE-2.0 95eefc0033edb232f9febcc1d32ede04bc901067cchrismair * 105eefc0033edb232f9febcc1d32ede04bc901067cchrismair * Unless required by applicable law or agreed to in writing, software 115eefc0033edb232f9febcc1d32ede04bc901067cchrismair * distributed under the License is distributed on an "AS IS" BASIS, 125eefc0033edb232f9febcc1d32ede04bc901067cchrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135eefc0033edb232f9febcc1d32ede04bc901067cchrismair * See the License for the specific language governing permissions and 145eefc0033edb232f9febcc1d32ede04bc901067cchrismair * limitations under the License. 155eefc0033edb232f9febcc1d32ede04bc901067cchrismair */ 165eefc0033edb232f9febcc1d32ede04bc901067cchrismairpackage org.mockftpserver.fake.filesystem; 175eefc0033edb232f9febcc1d32ede04bc901067cchrismair 18dfa40a06dff44f29d8d5e1d3186055ad325fc7b9chrismairimport org.slf4j.Logger; 19dfa40a06dff44f29d8d5e1d3186055ad325fc7b9chrismairimport org.slf4j.LoggerFactory; 205eefc0033edb232f9febcc1d32ede04bc901067cchrismairimport org.mockftpserver.core.util.StringUtil; 215eefc0033edb232f9febcc1d32ede04bc901067cchrismair 225eefc0033edb232f9febcc1d32ede04bc901067cchrismairimport java.text.DateFormat; 235eefc0033edb232f9febcc1d32ede04bc901067cchrismairimport java.text.SimpleDateFormat; 2440186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismairimport java.util.Locale; 255eefc0033edb232f9febcc1d32ede04bc901067cchrismair 265eefc0033edb232f9febcc1d32ede04bc901067cchrismair/** 275eefc0033edb232f9febcc1d32ede04bc901067cchrismair * Unix-specific implementation of the DirectoryListingFormatter interface. 285eefc0033edb232f9febcc1d32ede04bc901067cchrismair * 295eefc0033edb232f9febcc1d32ede04bc901067cchrismair * @author Chris Mair 302a0a3f946dba517a01cc26278f905156857c9c91chrismair * @version $Revision$ - $Date$ 315eefc0033edb232f9febcc1d32ede04bc901067cchrismair */ 325eefc0033edb232f9febcc1d32ede04bc901067cchrismairpublic class UnixDirectoryListingFormatter implements DirectoryListingFormatter { 335eefc0033edb232f9febcc1d32ede04bc901067cchrismair 34dfa40a06dff44f29d8d5e1d3186055ad325fc7b9chrismair private static final Logger LOG = LoggerFactory.getLogger(UnixDirectoryListingFormatter.class); 355eefc0033edb232f9febcc1d32ede04bc901067cchrismair 365eefc0033edb232f9febcc1d32ede04bc901067cchrismair private static final String DATE_FORMAT = "MMM dd yyyy"; 375eefc0033edb232f9febcc1d32ede04bc901067cchrismair private static final int SIZE_WIDTH = 15; 385eefc0033edb232f9febcc1d32ede04bc901067cchrismair private static final int OWNER_WIDTH = 8; 395eefc0033edb232f9febcc1d32ede04bc901067cchrismair private static final int GROUP_WIDTH = 8; 405eefc0033edb232f9febcc1d32ede04bc901067cchrismair private static final String NONE = "none"; 415eefc0033edb232f9febcc1d32ede04bc901067cchrismair 4240186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair private Locale locale = Locale.ENGLISH; 4340186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair 445eefc0033edb232f9febcc1d32ede04bc901067cchrismair // "-rw-rw-r-- 1 ftp ftp 254 Feb 23 2007 robots.txt" 455eefc0033edb232f9febcc1d32ede04bc901067cchrismair // "-rw-r--r-- 1 ftp ftp 30014925 Apr 15 00:19 md5.sums.gz" 46e5e13f42437c20ad460316e2144e2d5a39681db8chrismair // "-rwxr-xr-x 1 henry users 5778 Dec 1 2005 planaccess.sql" 475eefc0033edb232f9febcc1d32ede04bc901067cchrismair 485eefc0033edb232f9febcc1d32ede04bc901067cchrismair /** 495eefc0033edb232f9febcc1d32ede04bc901067cchrismair * Format the directory listing for a single file/directory entry. 505eefc0033edb232f9febcc1d32ede04bc901067cchrismair * 515eefc0033edb232f9febcc1d32ede04bc901067cchrismair * @param fileSystemEntry - the FileSystemEntry for a single file system entry 525eefc0033edb232f9febcc1d32ede04bc901067cchrismair * @return the formatted directory listing 535eefc0033edb232f9febcc1d32ede04bc901067cchrismair */ 545eefc0033edb232f9febcc1d32ede04bc901067cchrismair public String format(FileSystemEntry fileSystemEntry) { 5540186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, locale); 565eefc0033edb232f9febcc1d32ede04bc901067cchrismair String dateStr = dateFormat.format(fileSystemEntry.getLastModified()); 575eefc0033edb232f9febcc1d32ede04bc901067cchrismair String dirOrFile = fileSystemEntry.isDirectory() ? "d" : "-"; 585eefc0033edb232f9febcc1d32ede04bc901067cchrismair Permissions permissions = fileSystemEntry.getPermissions() != null ? fileSystemEntry.getPermissions() : Permissions.DEFAULT; 595eefc0033edb232f9febcc1d32ede04bc901067cchrismair String permissionsStr = StringUtil.padRight(permissions.asRwxString(), 9); 605eefc0033edb232f9febcc1d32ede04bc901067cchrismair String linkCountStr = "1"; 615eefc0033edb232f9febcc1d32ede04bc901067cchrismair String ownerStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getOwner()), OWNER_WIDTH); 625eefc0033edb232f9febcc1d32ede04bc901067cchrismair String groupStr = StringUtil.padRight(stringOrNone(fileSystemEntry.getGroup()), GROUP_WIDTH); 635eefc0033edb232f9febcc1d32ede04bc901067cchrismair String sizeStr = StringUtil.padLeft(Long.toString(fileSystemEntry.getSize()), SIZE_WIDTH); 645eefc0033edb232f9febcc1d32ede04bc901067cchrismair String listing = "" + dirOrFile + permissionsStr + " " + linkCountStr + " " + ownerStr + " " + groupStr + " " + sizeStr + " " + dateStr + " " + fileSystemEntry.getName(); 655eefc0033edb232f9febcc1d32ede04bc901067cchrismair LOG.info("listing=[" + listing + "]"); 665eefc0033edb232f9febcc1d32ede04bc901067cchrismair return listing; 675eefc0033edb232f9febcc1d32ede04bc901067cchrismair } 685eefc0033edb232f9febcc1d32ede04bc901067cchrismair 6940186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair /** 7040186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair * Set the Locale to be used in formatting the date within file/directory listings 7140186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair * @param locale - the Locale instance 7240186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair */ 7340186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair public void setLocale(Locale locale) { 7440186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair this.locale = locale; 7540186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair } 7640186f5d22de9dfbf27e9ddcbcac0e7c54f18d9dchrismair 775eefc0033edb232f9febcc1d32ede04bc901067cchrismair private String stringOrNone(String string) { 785eefc0033edb232f9febcc1d32ede04bc901067cchrismair return (string == null) ? NONE : string; 795eefc0033edb232f9febcc1d32ede04bc901067cchrismair } 805eefc0033edb232f9febcc1d32ede04bc901067cchrismair 81dfa40a06dff44f29d8d5e1d3186055ad325fc7b9chrismair} 82