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; 019import java.util.Calendar; 020 021import org.apache.commons.net.ftp.FTPFile; 022 023/** 024 * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce. 025 * Here is a sample of the sort of output line this parser processes: 026 * <pre> 027 * "-C--E-----FTP B QUA1I1 18128 41 Aug 12 13:56 QUADTEST" 028 * </pre> 029 * <p> 030 * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the 031 * DefaultFTPParserFactory by classname. It will not be chosen 032 * by the autodetection scheme. 033 * </p> 034 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions) 035 * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory 036 */ 037public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl 038{ 039 040 /** 041 * months abbreviations looked for by this parser. Also used 042 * to determine <b>which</b> month has been matched by the parser. 043 */ 044 private static final String MONTHS = 045 "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"; 046 047 /** 048 * this is the regular expression used by this parser. 049 */ 050 private static final String REGEX = 051 "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])" 052 + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))" 053 + "(\\S*)\\s*" // 12 054 + "(\\S+)\\s*" // 13 055 + "(\\S*)\\s*" // 14 user 056 + "(\\d*)\\s*" // 15 group 057 + "(\\d*)\\s*" // 16 filesize 058 + MONTHS // 17 month 059 + "\\s*" // TODO should the space be optional? 060 // TODO \\d* should be \\d? surely ? Otherwise 01111 is allowed 061 + "((?:[012]\\d*)|(?:3[01]))\\s*" // 18 date [012]\d* or 3[01] 062 + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s" 063 // 20 \d\d\d\d = year OR 064 // 21 [01]\d or 2[0123] hour + ':' 065 // 22 [012345]\d = minute 066 + "(\\S*)(\\s*.*)"; // 23 name 067 068 /** 069 * The sole constructor for a EnterpriseUnixFTPEntryParser object. 070 * 071 */ 072 public EnterpriseUnixFTPEntryParser() 073 { 074 super(REGEX); 075 } 076 077 /** 078 * Parses a line of a unix FTP server file listing and converts it into a 079 * usable format in the form of an <code> FTPFile </code> instance. If 080 * the file listing line doesn't describe a file, <code> null </code> is 081 * returned, otherwise a <code> FTPFile </code> instance representing the 082 * files in the directory is returned. 083 * 084 * @param entry A line of text from the file listing 085 * @return An FTPFile instance corresponding to the supplied entry 086 */ 087 @Override 088 public FTPFile parseFTPEntry(final String entry) 089 { 090 091 final FTPFile file = new FTPFile(); 092 file.setRawListing(entry); 093 094 if (matches(entry)) 095 { 096 final String usr = group(14); 097 final String grp = group(15); 098 final String filesize = group(16); 099 final String mo = group(17); 100 final String da = group(18); 101 final String yr = group(20); 102 final String hr = group(21); 103 final String min = group(22); 104 final String name = group(23); 105 106 file.setType(FTPFile.FILE_TYPE); 107 file.setUser(usr); 108 file.setGroup(grp); 109 try 110 { 111 file.setSize(Long.parseLong(filesize)); 112 } 113 catch (final NumberFormatException e) 114 { 115 // intentionally do nothing 116 } 117 118 final Calendar cal = Calendar.getInstance(); 119 cal.set(Calendar.MILLISECOND, 0); 120 cal.set(Calendar.SECOND, 0); 121 cal.set(Calendar.MINUTE, 0); 122 cal.set(Calendar.HOUR_OF_DAY, 0); 123 124 final int pos = MONTHS.indexOf(mo); 125 final int month = pos / 4; 126 final int missingUnit; // the first missing unit 127 try 128 { 129 130 if (yr != null) 131 { 132 // it's a year; there are no hours and minutes 133 cal.set(Calendar.YEAR, Integer.parseInt(yr)); 134 missingUnit = Calendar.HOUR_OF_DAY; 135 } 136 else 137 { 138 // it must be hour/minute or we wouldn't have matched 139 missingUnit = Calendar.SECOND; 140 int year = cal.get(Calendar.YEAR); 141 142 // if the month we're reading is greater than now, it must 143 // be last year 144 if (cal.get(Calendar.MONTH) < month) 145 { 146 year--; 147 } 148 cal.set(Calendar.YEAR, year); 149 cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr)); 150 cal.set(Calendar.MINUTE, Integer.parseInt(min)); 151 } 152 cal.set(Calendar.MONTH, month); 153 cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da)); 154 cal.clear(missingUnit); 155 file.setTimestamp(cal); 156 } 157 catch (final NumberFormatException e) 158 { 159 // do nothing, date will be uninitialized 160 } 161 file.setName(name); 162 163 return file; 164 } 165 return null; 166 } 167}