001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.net.ftp.parser; 019 020import java.util.Calendar; 021 022import org.apache.commons.net.ftp.FTPFile; 023 024/** 025 * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce. Here is a sample of the sort of output line this parser processes: 026 * 027 * <pre> 028 * "-C--E-----FTP B QUA1I1 18128 41 Aug 12 13:56 QUADTEST" 029 * </pre> 030 * <p> 031 * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the DefaultFTPParserFactory by classname. It will not be chosen by the autodetection 032 * scheme. 033 * </p> 034 * 035 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions) 036 * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory 037 */ 038public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl { 039 040 /** 041 * months abbreviations looked for by this parser. Also used to determine <b>which</b> month has been matched by the parser. 042 */ 043 private static final String MONTHS = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"; 044 045 /** 046 * this is the regular expression used by this parser. 047 */ 048 private static final String REGEX = "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])" 049 + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))" + "(\\S*)\\s*" // 12 050 + "(\\S+)\\s*" // 13 051 + "(\\S*)\\s*" // 14 user 052 + "(\\d*)\\s*" // 15 group 053 + "(\\d*)\\s*" // 16 filesize 054 + MONTHS // 17 month 055 + "\\s*" // TODO should the space be optional? 056 // TODO \\d* should be \\d? surely ? Otherwise 01111 is allowed 057 + "((?:[012]\\d*)|(?:3[01]))\\s*" // 18 date [012]\d* or 3[01] 058 + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s" 059 // 20 \d\d\d\d = year OR 060 // 21 [01]\d or 2[0123] hour + ':' 061 // 22 [012345]\d = minute 062 + "(\\S*)(\\s*.*)"; // 23 name 063 064 /** 065 * The sole constructor for a EnterpriseUnixFTPEntryParser object. 066 * 067 */ 068 public EnterpriseUnixFTPEntryParser() { 069 super(REGEX); 070 } 071 072 /** 073 * Parses a line of a unix FTP server file listing and converts it into a usable format in the form of an <code> FTPFile </code> instance. If the file 074 * listing line doesn't describe a file, <code> null </code> is returned, otherwise a <code> FTPFile </code> instance representing the files in the 075 * directory is returned. 076 * 077 * @param entry A line of text from the file listing 078 * @return An FTPFile instance corresponding to the supplied entry 079 */ 080 @Override 081 public FTPFile parseFTPEntry(final String entry) { 082 083 final FTPFile file = new FTPFile(); 084 file.setRawListing(entry); 085 086 if (matches(entry)) { 087 final String usr = group(14); 088 final String grp = group(15); 089 final String filesize = group(16); 090 final String mo = group(17); 091 final String da = group(18); 092 final String yr = group(20); 093 final String hr = group(21); 094 final String min = group(22); 095 final String name = group(23); 096 097 file.setType(FTPFile.FILE_TYPE); 098 file.setUser(usr); 099 file.setGroup(grp); 100 try { 101 file.setSize(Long.parseLong(filesize)); 102 } catch (final NumberFormatException e) { 103 // intentionally do nothing 104 } 105 106 final Calendar cal = Calendar.getInstance(); 107 cal.set(Calendar.MILLISECOND, 0); 108 cal.set(Calendar.SECOND, 0); 109 cal.set(Calendar.MINUTE, 0); 110 cal.set(Calendar.HOUR_OF_DAY, 0); 111 112 final int pos = MONTHS.indexOf(mo); 113 final int month = pos / 4; 114 final int missingUnit; // the first missing unit 115 try { 116 117 if (yr != null) { 118 // it's a year; there are no hours and minutes 119 cal.set(Calendar.YEAR, Integer.parseInt(yr)); 120 missingUnit = Calendar.HOUR_OF_DAY; 121 } else { 122 // it must be hour/minute, or we wouldn't have matched 123 missingUnit = Calendar.SECOND; 124 int year = cal.get(Calendar.YEAR); 125 126 // if the month we're reading is greater than now, it must 127 // be last year 128 if (cal.get(Calendar.MONTH) < month) { 129 year--; 130 } 131 cal.set(Calendar.YEAR, year); 132 cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr)); 133 cal.set(Calendar.MINUTE, Integer.parseInt(min)); 134 } 135 cal.set(Calendar.MONTH, month); 136 cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da)); 137 cal.clear(missingUnit); 138 file.setTimestamp(cal); 139 } catch (final NumberFormatException e) { 140 // do nothing, date will be uninitialized 141 } 142 file.setName(name); 143 144 return file; 145 } 146 return null; 147 } 148}