1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.net.ftp.parser; 19 import java.text.ParseException; 20 21 import org.apache.commons.net.ftp.Configurable; 22 import org.apache.commons.net.ftp.FTPClientConfig; 23 import org.apache.commons.net.ftp.FTPFile; 24 25 /** 26 * Implementation of FTPFileEntryParser and FTPFileListParser for NT Systems. 27 * 28 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a> 29 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a> 30 * @version $Id: NTFTPEntryParser.java 1489361 2013-06-04 09:48:36Z sebb $ 31 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions) 32 */ 33 public class NTFTPEntryParser extends ConfigurableFTPFileEntryParserImpl 34 { 35 36 private static final String DEFAULT_DATE_FORMAT 37 = "MM-dd-yy hh:mma"; //11-09-01 12:30PM 38 39 private static final String DEFAULT_DATE_FORMAT2 40 = "MM-dd-yy kk:mm"; //11-09-01 18:30 41 42 private final FTPTimestampParser timestampParser; 43 44 /** 45 * this is the regular expression used by this parser. 46 */ 47 private static final String REGEX = 48 "(\\S+)\\s+(\\S+)\\s+" // MM-dd-yy whitespace hh:mma|kk:mm; swallow trailing spaces 49 + "(?:(<DIR>)|([0-9]+))\\s+" // <DIR> or ddddd; swallow trailing spaces 50 + "(\\S.*)"; // First non-space followed by rest of line (name) 51 52 /** 53 * The sole constructor for an NTFTPEntryParser object. 54 * 55 * @exception IllegalArgumentException 56 * Thrown if the regular expression is unparseable. Should not be seen 57 * under normal conditions. It it is seen, this is a sign that 58 * <code>REGEX</code> is not a valid regular expression. 59 */ 60 public NTFTPEntryParser() 61 { 62 this(null); 63 } 64 65 /** 66 * This constructor allows the creation of an NTFTPEntryParser object 67 * with something other than the default configuration. 68 * 69 * @param config The {@link FTPClientConfig configuration} object used to 70 * configure this parser. 71 * @exception IllegalArgumentException 72 * Thrown if the regular expression is unparseable. Should not be seen 73 * under normal conditions. It it is seen, this is a sign that 74 * <code>REGEX</code> is not a valid regular expression. 75 * @since 1.4 76 */ 77 public NTFTPEntryParser(FTPClientConfig config) 78 { 79 super(REGEX); 80 configure(config); 81 FTPClientConfig config2 = new FTPClientConfig( 82 FTPClientConfig.SYST_NT, 83 DEFAULT_DATE_FORMAT2, 84 null, null, null, null); 85 config2.setDefaultDateFormatStr(DEFAULT_DATE_FORMAT2); 86 this.timestampParser = new FTPTimestampParserImpl(); 87 ((Configurable)this.timestampParser).configure(config2); 88 } 89 90 /** 91 * Parses a line of an NT FTP server file listing and converts it into a 92 * usable format in the form of an <code> FTPFile </code> instance. If the 93 * file listing line doesn't describe a file, <code> null </code> is 94 * returned, otherwise a <code> FTPFile </code> instance representing the 95 * files in the directory is returned. 96 * <p> 97 * @param entry A line of text from the file listing 98 * @return An FTPFile instance corresponding to the supplied entry 99 */ 100 // @Override 101 public FTPFile parseFTPEntry(String entry) 102 { 103 FTPFile f = new FTPFile(); 104 f.setRawListing(entry); 105 106 if (matches(entry)) 107 { 108 String datestr = group(1)+" "+group(2); 109 String dirString = group(3); 110 String size = group(4); 111 String name = group(5); 112 try 113 { 114 f.setTimestamp(super.parseTimestamp(datestr)); 115 } 116 catch (ParseException e) 117 { 118 // parsing fails, try the other date format 119 try 120 { 121 f.setTimestamp(timestampParser.parseTimestamp(datestr)); 122 } 123 catch (ParseException e2) 124 { 125 // intentionally do nothing 126 } 127 } 128 129 if (null == name || name.equals(".") || name.equals("..")) 130 { 131 return (null); 132 } 133 f.setName(name); 134 135 136 if ("<DIR>".equals(dirString)) 137 { 138 f.setType(FTPFile.DIRECTORY_TYPE); 139 f.setSize(0); 140 } 141 else 142 { 143 f.setType(FTPFile.FILE_TYPE); 144 if (null != size) 145 { 146 f.setSize(Long.parseLong(size)); 147 } 148 } 149 return (f); 150 } 151 return null; 152 } 153 154 /** 155 * Defines a default configuration to be used when this class is 156 * instantiated without a {@link FTPClientConfig FTPClientConfig} 157 * parameter being specified. 158 * @return the default configuration for this parser. 159 */ 160 @Override 161 public FTPClientConfig getDefaultConfiguration() { 162 return new FTPClientConfig( 163 FTPClientConfig.SYST_NT, 164 DEFAULT_DATE_FORMAT, 165 null, null, null, null); 166 } 167 168 }