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. /**
  19. * This is the default implementation of the
  20. * FTPFileEntryParserFactory interface. This is the
  21. * implementation that will be used by
  22. * org.apache.commons.net.ftp.FTPClient.listFiles()
  23. * if no other implementation has been specified.
  24. *
  25. * @see org.apache.commons.net.ftp.FTPClient#listFiles
  26. * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
  27. */
  28. public class DefaultFTPFileEntryParserFactory
  29. implements FTPFileEntryParserFactory
  30. {
  31. /**
  32. * This default implementation of the FTPFileEntryParserFactory
  33. * interface works according to the following logic:
  34. * First it attempts to interpret the supplied key as a fully
  35. * qualified classname of a class implementing the
  36. * FTPFileEntryParser interface. If that succeeds, a parser
  37. * object of this class is instantiated and is returned.
  38. * <p/>
  39. * If <code>key</code> is not recognized as a fully qualified
  40. * classname known to the system, this method will then attempt
  41. * to see whether it <b>contains</b> a string identifying one of
  42. * the known parsers. This comparison is <b>case-insensitive</b>.
  43. * The intent here is where possible, to select as keys strings
  44. * which are returned by the SYST command on the systems which
  45. * the corresponding parser successfully parses. This enables
  46. * this factory to be used in the auto-detection system.
  47. * <p/>
  48. *
  49. * @param key should be a fully qualified classname corresponding to
  50. * a class implementing the FTPFileEntryParser interface<br/>
  51. * OR<br/>
  52. * a string containing (case-insensitively) one of the
  53. * following keywords:
  54. * <ul>
  55. * <li><code>unix</code></li>
  56. * <li><code>windows</code></li>
  57. * <li><code>os/2</code></li>
  58. * <li><code>vms</code></li>
  59. * </ul>
  60. * @return the FTPFileEntryParser corresponding to the supplied key.
  61. * @throws ParserInitializationException thrown if for any reason the factory cannot resolve
  62. * the supplied key into an FTPFileEntryParser.
  63. * @see FTPFileEntryParser
  64. */
  65. public FTPFileEntryParser createFileEntryParser(String key)
  66. {
  67. Class parserClass = null;
  68. try
  69. {
  70. parserClass = Class.forName(key);
  71. return (FTPFileEntryParser) parserClass.newInstance();
  72. }
  73. catch (ClassNotFoundException e)
  74. {
  75. String ukey = null;
  76. if (null != key)
  77. {
  78. ukey = key.toUpperCase();
  79. }
  80. if (ukey.indexOf("UNIX") >= 0)
  81. {
  82. return createUnixFTPEntryParser();
  83. }
  84. else if (ukey.indexOf("VMS") >= 0)
  85. {
  86. return createVMSVersioningFTPEntryParser();
  87. }
  88. else if (ukey.indexOf("WINDOWS") >= 0)
  89. {
  90. return createNTFTPEntryParser();
  91. }
  92. else if (ukey.indexOf("OS/2") >= 0)
  93. {
  94. return createOS2FTPEntryParser();
  95. }
  96. else if (ukey.indexOf("OS/400") >= 0)
  97. {
  98. return createOS400FTPEntryParser();
  99. }
  100. else
  101. {
  102. throw new ParserInitializationException("Unknown parser type: " + key);
  103. }
  104. }
  105. catch (ClassCastException e)
  106. {
  107. throw new ParserInitializationException(parserClass.getName()
  108. + " does not implement the interface "
  109. + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
  110. }
  111. catch (Throwable e)
  112. {
  113. throw new ParserInitializationException("Error initializing parser", e);
  114. }
  115. }
  116. public FTPFileEntryParser createUnixFTPEntryParser()
  117. {
  118. return new UnixFTPEntryParser();
  119. }
  120. public FTPFileEntryParser createVMSVersioningFTPEntryParser()
  121. {
  122. return new VMSVersioningFTPEntryParser();
  123. }
  124. public FTPFileEntryParser createNTFTPEntryParser()
  125. {
  126. return new CompositeFileEntryParser(new FTPFileEntryParser[]
  127. {
  128. new NTFTPEntryParser(),
  129. new UnixFTPEntryParser()
  130. });
  131. }
  132. public FTPFileEntryParser createOS2FTPEntryParser()
  133. {
  134. return new OS2FTPEntryParser();
  135. }
  136. public FTPFileEntryParser createOS400FTPEntryParser()
  137. {
  138. return new CompositeFileEntryParser(new FTPFileEntryParser[]
  139. {
  140. new OS400FTPEntryParser(),
  141. new UnixFTPEntryParser()
  142. });
  143. }
  144. }