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; 017 import java.io.BufferedReader; 018 import java.io.IOException; 019 import java.io.InputStream; 020 import java.util.Iterator; 021 import java.util.List; 022 023 /** 024 * This abstract class implements both the older FTPFileListParser and 025 * newer FTPFileEntryParser interfaces with default functionality. 026 * All the classes in the parser subpackage inherit from this. 027 * 028 */ 029 public abstract class FTPFileEntryParserImpl 030 implements FTPFileEntryParser, FTPFileListParser 031 { 032 /** 033 * The constructor for a FTPFileEntryParserImpl object. 034 */ 035 public FTPFileEntryParserImpl() 036 { 037 } 038 039 040 /*** 041 * Parses an FTP server file listing and converts it into a usable format 042 * in the form of an array of <code> FTPFile </code> instances. If the 043 * file list contains no files, <code> null </code> should be 044 * returned, otherwise an array of <code> FTPFile </code> instances 045 * representing the files in the directory is returned. 046 * <p> 047 * @param listStream The InputStream from which the file list should be 048 * read. 049 * @return The list of file information contained in the given path. null 050 * if the list could not be obtained or if there are no files in 051 * the directory. 052 * @exception java.io.IOException If an I/O error occurs reading the listStream. 053 ***/ 054 public FTPFile[] parseFileList(InputStream listStream, String encoding) throws IOException 055 { 056 FTPFileList ffl = FTPFileList.create(listStream, this, encoding); 057 return ffl.getFiles(); 058 059 } 060 061 /*** 062 * Parses an FTP server file listing and converts it into a usable format 063 * in the form of an array of <code> FTPFile </code> instances. If the 064 * file list contains no files, <code> null </code> should be 065 * returned, otherwise an array of <code> FTPFile </code> instances 066 * representing the files in the directory is returned. 067 * <p> 068 * @param listStream The InputStream from which the file list should be 069 * read. 070 * @return The list of file information contained in the given path. null 071 * if the list could not be obtained or if there are no files in 072 * the directory. 073 * @exception java.io.IOException If an I/O error occurs reading the listStream. 074 * 075 * @deprecated The version of this method which takes an encoding should be used. 076 ***/ 077 public FTPFile[] parseFileList(InputStream listStream) throws IOException 078 { 079 return parseFileList(listStream, null); 080 } 081 082 /** 083 * Reads the next entry using the supplied BufferedReader object up to 084 * whatever delemits one entry from the next. This default implementation 085 * simply calls BufferedReader.readLine(). 086 * 087 * @param reader The BufferedReader object from which entries are to be 088 * read. 089 * 090 * @return A string representing the next ftp entry or null if none found. 091 * @exception java.io.IOException thrown on any IO Error reading from the reader. 092 */ 093 public String readNextEntry(BufferedReader reader) throws IOException 094 { 095 return reader.readLine(); 096 } 097 /** 098 * This method is a hook for those implementors (such as 099 * VMSVersioningFTPEntryParser, and possibly others) which need to 100 * perform some action upon the FTPFileList after it has been created 101 * from the server stream, but before any clients see the list. 102 * 103 * This default implementation is a no-op. 104 * 105 * @param original Original list after it has been created from the server stream 106 * 107 * @return <code>original</code> unmodified. 108 */ 109 public List preParse(List original) { 110 Iterator it = original.iterator(); 111 while (it.hasNext()){ 112 String entry = (String) it.next(); 113 if (null == parseFTPEntry(entry)) { 114 it.remove(); 115 } else { 116 break; 117 } 118 } 119 return original; 120 } 121 } 122 123 /* Emacs configuration 124 * Local variables: ** 125 * mode: java ** 126 * c-basic-offset: 4 ** 127 * indent-tabs-mode: nil ** 128 * End: ** 129 */