1. /*
  2. * Copyright 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.parser;
  17. import org.apache.commons.net.ftp.FTPFileEntryParser;
  18. import org.apache.commons.net.ftp.FTPFile;
  19. import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
  20. /**
  21. * This implementation allows to pack some FileEntryParsers together
  22. * and handle the case where to returned dirstyle isnt clearly defined.
  23. * The matching parser will be cached.
  24. * If the cached parser wont match due to the server changed the dirstyle,
  25. * a new matching parser will be searched.
  26. *
  27. * @author Mario Ivankovits <mario@ops.co.at>
  28. */
  29. public class CompositeFileEntryParser extends FTPFileEntryParserImpl
  30. {
  31. private final FTPFileEntryParser[] ftpFileEntryParsers;
  32. private FTPFileEntryParser cachedFtpFileEntryParser;
  33. public CompositeFileEntryParser(FTPFileEntryParser[] ftpFileEntryParsers)
  34. {
  35. super();
  36. this.cachedFtpFileEntryParser = null;
  37. this.ftpFileEntryParsers = ftpFileEntryParsers;
  38. }
  39. public FTPFile parseFTPEntry(String listEntry)
  40. {
  41. if (cachedFtpFileEntryParser != null)
  42. {
  43. FTPFile matched = cachedFtpFileEntryParser.parseFTPEntry(listEntry);
  44. if (matched != null)
  45. {
  46. return matched;
  47. }
  48. }
  49. else
  50. {
  51. for (int iterParser=0; iterParser < ftpFileEntryParsers.length; iterParser++)
  52. {
  53. FTPFileEntryParser ftpFileEntryParser = ftpFileEntryParsers[iterParser];
  54. FTPFile matched = ftpFileEntryParser.parseFTPEntry(listEntry);
  55. if (matched != null)
  56. {
  57. cachedFtpFileEntryParser = ftpFileEntryParser;
  58. return matched;
  59. }
  60. }
  61. }
  62. return null;
  63. }
  64. }