001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.net.ftp.parser; 017 import java.text.ParseException; 018 019 import org.apache.commons.net.ftp.FTPClientConfig; 020 import org.apache.commons.net.ftp.FTPFile; 021 022 /** 023 * Implementation of FTPFileEntryParser and FTPFileListParser for NT Systems. 024 * 025 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a> 026 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a> 027 * @version $Id: NTFTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $ 028 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions) 029 */ 030 public class NTFTPEntryParser extends ConfigurableFTPFileEntryParserImpl 031 { 032 033 private static final String DEFAULT_DATE_FORMAT 034 = "MM-dd-yy hh:mma"; //11-09-01 12:30PM 035 036 037 /** 038 * this is the regular expression used by this parser. 039 */ 040 private static final String REGEX = 041 "(\\S+)\\s+(\\S+)\\s+" 042 + "(<DIR>)?\\s*" 043 + "([0-9]+)?\\s+" 044 + "(\\S.*)"; 045 046 /** 047 * The sole constructor for an NTFTPEntryParser object. 048 * 049 * @exception IllegalArgumentException 050 * Thrown if the regular expression is unparseable. Should not be seen 051 * under normal conditions. It it is seen, this is a sign that 052 * <code>REGEX</code> is not a valid regular expression. 053 */ 054 public NTFTPEntryParser() 055 { 056 this(null); 057 } 058 059 /** 060 * This constructor allows the creation of an NTFTPEntryParser object 061 * with something other than the default configuration. 062 * 063 * @param config The {@link FTPClientConfig configuration} object used to 064 * configure this parser. 065 * @exception IllegalArgumentException 066 * Thrown if the regular expression is unparseable. Should not be seen 067 * under normal conditions. It it is seen, this is a sign that 068 * <code>REGEX</code> is not a valid regular expression. 069 * @since 1.4 070 */ 071 public NTFTPEntryParser(FTPClientConfig config) 072 { 073 super(REGEX); 074 configure(config); 075 } 076 077 /** 078 * Parses a line of an NT FTP server file listing and converts it into a 079 * usable format in the form of an <code> FTPFile </code> instance. If the 080 * 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 * <p> 084 * @param entry A line of text from the file listing 085 * @return An FTPFile instance corresponding to the supplied entry 086 */ 087 public FTPFile parseFTPEntry(String entry) 088 { 089 FTPFile f = new FTPFile(); 090 f.setRawListing(entry); 091 092 if (matches(entry)) 093 { 094 String datestr = group(1)+" "+group(2); 095 String dirString = group(3); 096 String size = group(4); 097 String name = group(5); 098 try 099 { 100 f.setTimestamp(super.parseTimestamp(datestr)); 101 } 102 catch (ParseException e) 103 { 104 return null; // this is a parsing failure too. 105 } 106 107 if (null == name || name.equals(".") || name.equals("..")) 108 { 109 return (null); 110 } 111 f.setName(name); 112 113 114 if ("<DIR>".equals(dirString)) 115 { 116 f.setType(FTPFile.DIRECTORY_TYPE); 117 f.setSize(0); 118 } 119 else 120 { 121 f.setType(FTPFile.FILE_TYPE); 122 if (null != size) 123 { 124 f.setSize(Long.parseLong(size)); 125 } 126 } 127 return (f); 128 } 129 return null; 130 } 131 132 /** 133 * Defines a default configuration to be used when this class is 134 * instantiated without a {@link FTPClientConfig FTPClientConfig} 135 * parameter being specified. 136 * @return the default configuration for this parser. 137 */ 138 public FTPClientConfig getDefaultConfiguration() { 139 return new FTPClientConfig( 140 FTPClientConfig.SYST_NT, 141 DEFAULT_DATE_FORMAT, 142 null, null, null, null); 143 } 144 145 }