1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.ftp;
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. /**
  23. * This abstract class implements both the older FTPFileListParser and
  24. * newer FTPFileEntryParser interfaces with default functionality.
  25. * All the classes in the parser subpackage inherit from this.
  26. *
  27. * @author Steve Cohen <scohen@apache.org>
  28. */
  29. public abstract class FTPFileEntryParserImpl
  30. implements FTPFileEntryParser, FTPFileListParser
  31. {
  32. /**
  33. * The constructor for a FTPFileEntryParserImpl object.
  34. */
  35. public FTPFileEntryParserImpl()
  36. {
  37. }
  38. /***
  39. * Parses an FTP server file listing and converts it into a usable format
  40. * in the form of an array of <code> FTPFile </code> instances. If the
  41. * file list contains no files, <code> null </code> should be
  42. * returned, otherwise an array of <code> FTPFile </code> instances
  43. * representing the files in the directory is returned.
  44. * <p>
  45. * @param listStream The InputStream from which the file list should be
  46. * read.
  47. * @return The list of file information contained in the given path. null
  48. * if the list could not be obtained or if there are no files in
  49. * the directory.
  50. * @exception java.io.IOException If an I/O error occurs reading the listStream.
  51. ***/
  52. public FTPFile[] parseFileList(InputStream listStream) throws IOException
  53. {
  54. FTPFileList ffl = FTPFileList.create(listStream, this);
  55. return ffl.getFiles();
  56. }
  57. /**
  58. * Reads the next entry using the supplied BufferedReader object up to
  59. * whatever delemits one entry from the next. This default implementation
  60. * simply calls BufferedReader.readLine().
  61. *
  62. * @param reader The BufferedReader object from which entries are to be
  63. * read.
  64. *
  65. * @return A string representing the next ftp entry or null if none found.
  66. * @exception java.io.IOException thrown on any IO Error reading from the reader.
  67. */
  68. public String readNextEntry(BufferedReader reader) throws IOException
  69. {
  70. return reader.readLine();
  71. }
  72. /**
  73. * This method is a hook for those implementors (such as
  74. * VMSVersioningFTPEntryParser, and possibly others) which need to
  75. * perform some action upon the FTPFileList after it has been created
  76. * from the server stream, but before any clients see the list.
  77. *
  78. * This default implementation is a no-op.
  79. *
  80. * @param original Original list after it has been created from the server stream
  81. *
  82. * @return <code>original</code> unmodified.
  83. */
  84. public List preParse(List original) {
  85. Iterator it = original.iterator();
  86. while (it.hasNext()){
  87. String entry = (String) it.next();
  88. if (null == parseFTPEntry(entry)) {
  89. it.remove();
  90. } else {
  91. break;
  92. }
  93. }
  94. return original;
  95. }
  96. }
  97. /* Emacs configuration
  98. * Local variables: **
  99. * mode: java **
  100. * c-basic-offset: 4 **
  101. * indent-tabs-mode: nil **
  102. * End: **
  103. */