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 * https://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.text.ParseException; 021 022import org.apache.commons.net.ftp.Configurable; 023import org.apache.commons.net.ftp.FTPClientConfig; 024import org.apache.commons.net.ftp.FTPFile; 025import org.apache.commons.net.ftp.FTPFileEntryParser; 026 027/** 028 * Implements {@link FTPFileEntryParser} and {@link Configurable} for OS/2 Systems. 029 * 030 * @see FTPFileEntryParser Usage instructions. 031 */ 032public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl { 033 034 private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy HH:mm"; // 11-09-01 12:30 035 036 /** 037 * this is the regular expression used by this parser. 038 */ 039 private static final String REGEX = "\\s*([0-9]+)\\s*" + "(\\s+|[A-Z]+)\\s*" + "(DIR|\\s+)\\s*" + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */ 040 + "(\\S.*)"; 041 042 /** 043 * Constructs a new instance. 044 * 045 * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a 046 * sign that {@code REGEX} is not a valid regular expression. 047 */ 048 public OS2FTPEntryParser() { 049 this(null); 050 } 051 052 /** 053 * Constructs a new instance with something other than the default configuration. 054 * 055 * @param config The {@link FTPClientConfig configuration} object used to configure this parser. 056 * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a 057 * sign that {@code REGEX} is not a valid regular expression. 058 * @since 1.4 059 */ 060 public OS2FTPEntryParser(final FTPClientConfig config) { 061 super(REGEX); 062 configure(config); 063 } 064 065 /** 066 * Gets a new default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified. 067 * 068 * @return the default configuration for this parser. 069 */ 070 @Override 071 protected FTPClientConfig getDefaultConfiguration() { 072 return new FTPClientConfig(FTPClientConfig.SYST_OS2, DEFAULT_DATE_FORMAT, null); 073 } 074 075 /** 076 * Parses a line of an OS2 FTP server file listing and converts it into a usable format in the form of an {@code FTPFile} instance. If the file 077 * listing line doesn't describe a file, {@code null} is returned, otherwise a {@code FTPFile} instance representing the files in the 078 * directory is returned. 079 * 080 * @param entry A line of text from the file listing 081 * @return An FTPFile instance corresponding to the supplied entry 082 */ 083 @Override 084 public FTPFile parseFTPEntry(final String entry) { 085 086 final FTPFile f = new FTPFile(); 087 if (matches(entry)) { 088 final String size = group(1); 089 final String attrib = group(2); 090 final String dirString = group(3); 091 final String datestr = group(4) + " " + group(5); 092 final String name = group(6); 093 try { 094 f.setTimestamp(super.parseTimestamp(datestr)); 095 } catch (final ParseException e) { 096 // intentionally do nothing 097 } 098 099 // is it a DIR or a file 100 if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR")) { 101 f.setType(FTPFile.DIRECTORY_TYPE); 102 } else { 103 f.setType(FTPFile.FILE_TYPE); 104 } 105 106 // set the name 107 f.setName(name.trim()); 108 109 // set the size 110 f.setSize(Long.parseLong(size.trim())); 111 112 return f; 113 } 114 return null; 115 116 } 117 118}