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.FTPClientConfig; 22 import org.apache.commons.net.ftp.FTPFile; 23 24 /** 25 * Implementation of FTPFileEntryParser and FTPFileListParser for NT Systems. 26 * 27 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a> 28 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a> 29 * @version $Id: NTFTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $ 30 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions) 31 */ 32 public class NTFTPEntryParser extends ConfigurableFTPFileEntryParserImpl 33 { 34 35 private static final String DEFAULT_DATE_FORMAT 36 = "MM-dd-yy hh:mma"; //11-09-01 12:30PM 37 38 39 /** 40 * this is the regular expression used by this parser. 41 */ 42 private static final String REGEX = 43 "(\\S+)\\s+(\\S+)\\s+" 44 + "(?:(<DIR>)|([0-9]+))\\s+" 45 + "(\\S.*)"; 46 47 /** 48 * The sole constructor for an NTFTPEntryParser object. 49 * 50 * @exception IllegalArgumentException 51 * Thrown if the regular expression is unparseable. Should not be seen 52 * under normal conditions. It it is seen, this is a sign that 53 * <code>REGEX</code> is not a valid regular expression. 54 */ 55 public NTFTPEntryParser() 56 { 57 this(null); 58 } 59 60 /** 61 * This constructor allows the creation of an NTFTPEntryParser object 62 * with something other than the default configuration. 63 * 64 * @param config The {@link FTPClientConfig configuration} object used to 65 * configure this parser. 66 * @exception IllegalArgumentException 67 * Thrown if the regular expression is unparseable. Should not be seen 68 * under normal conditions. It it is seen, this is a sign that 69 * <code>REGEX</code> is not a valid regular expression. 70 * @since 1.4 71 */ 72 public NTFTPEntryParser(FTPClientConfig config) 73 { 74 super(REGEX); 75 configure(config); 76 } 77 78 /** 79 * Parses a line of an NT FTP server file listing and converts it into a 80 * usable format in the form of an <code> FTPFile </code> instance. If the 81 * file listing line doesn't describe a file, <code> null </code> is 82 * returned, otherwise a <code> FTPFile </code> instance representing the 83 * files in the directory is returned. 84 * <p> 85 * @param entry A line of text from the file listing 86 * @return An FTPFile instance corresponding to the supplied entry 87 */ 88 public FTPFile parseFTPEntry(String entry) 89 { 90 FTPFile f = new FTPFile(); 91 f.setRawListing(entry); 92 93 if (matches(entry)) 94 { 95 String datestr = group(1)+" "+group(2); 96 String dirString = group(3); 97 String size = group(4); 98 String name = group(5); 99 try 100 { 101 f.setTimestamp(super.parseTimestamp(datestr)); 102 } 103 catch (ParseException e) 104 { 105 // intentionally do nothing 106 } 107 108 if (null == name || name.equals(".") || name.equals("..")) 109 { 110 return (null); 111 } 112 f.setName(name); 113 114 115 if ("<DIR>".equals(dirString)) 116 { 117 f.setType(FTPFile.DIRECTORY_TYPE); 118 f.setSize(0); 119 } 120 else 121 { 122 f.setType(FTPFile.FILE_TYPE); 123 if (null != size) 124 { 125 f.setSize(Long.parseLong(size)); 126 } 127 } 128 return (f); 129 } 130 return null; 131 } 132 133 /** 134 * Defines a default configuration to be used when this class is 135 * instantiated without a {@link FTPClientConfig FTPClientConfig} 136 * parameter being specified. 137 * @return the default configuration for this parser. 138 */ 139 @Override 140 public FTPClientConfig getDefaultConfiguration() { 141 return new FTPClientConfig( 142 FTPClientConfig.SYST_NT, 143 DEFAULT_DATE_FORMAT, 144 null, null, null, null); 145 } 146 147 }