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.parser;
  17. import java.util.Calendar;
  18. import org.apache.commons.net.ftp.FTPFile;
  19. /**
  20. * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce.
  21. * Here is a sample of the sort of output line this parser processes:
  22. * "-C--E-----FTP B QUA1I1 18128 41 Aug 12 13:56 QUADTEST"
  23. * <P><B>
  24. * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the
  25. * DefaultFTPParserFactory by classname. It will not be chosen
  26. * by the autodetection scheme.
  27. * </B>
  28. * @version $Id: EnterpriseUnixFTPEntryParser.java,v 1.11 2004/04/21 23:30:33 scohen Exp $
  29. * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
  30. * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
  31. * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  32. */
  33. public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl
  34. {
  35. /**
  36. * months abbreviations looked for by this parser. Also used
  37. * to determine <b>which</b> month has been matched by the parser.
  38. */
  39. private static final String MONTHS =
  40. "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
  41. /**
  42. * this is the regular expression used by this parser.
  43. */
  44. private static final String REGEX =
  45. "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
  46. + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
  47. + "(\\S*)\\s*"
  48. + "(\\S+)\\s*"
  49. + "(\\S*)\\s*"
  50. + "(\\d*)\\s*"
  51. + "(\\d*)\\s*"
  52. + MONTHS
  53. + "\\s*"
  54. + "((?:[012]\\d*)|(?:3[01]))\\s*"
  55. + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
  56. + "(\\S*)(\\s*.*)";
  57. /**
  58. * The sole constructor for a EnterpriseUnixFTPEntryParser object.
  59. *
  60. */
  61. public EnterpriseUnixFTPEntryParser()
  62. {
  63. super(REGEX);
  64. }
  65. /**
  66. * Parses a line of a unix FTP server file listing and converts it into a
  67. * usable format in the form of an <code> FTPFile </code> instance. If
  68. * the file listing line doesn't describe a file, <code> null </code> is
  69. * returned, otherwise a <code> FTPFile </code> instance representing the
  70. * files in the directory is returned.
  71. *
  72. * @param entry A line of text from the file listing
  73. * @return An FTPFile instance corresponding to the supplied entry
  74. */
  75. public FTPFile parseFTPEntry(String entry)
  76. {
  77. FTPFile file = new FTPFile();
  78. file.setRawListing(entry);
  79. if (matches(entry))
  80. {
  81. String usr = group(14);
  82. String grp = group(15);
  83. String filesize = group(16);
  84. String mo = group(17);
  85. String da = group(18);
  86. String yr = group(20);
  87. String hr = group(21);
  88. String min = group(22);
  89. String name = group(23);
  90. file.setType(FTPFile.FILE_TYPE);
  91. file.setUser(usr);
  92. file.setGroup(grp);
  93. try
  94. {
  95. file.setSize(Integer.parseInt(filesize));
  96. }
  97. catch (NumberFormatException e)
  98. {
  99. // intentionally do nothing
  100. }
  101. Calendar cal = Calendar.getInstance();
  102. cal.set(Calendar.SECOND,
  103. 0);
  104. cal.set(Calendar.MINUTE,
  105. 0);
  106. cal.set(Calendar.HOUR_OF_DAY,
  107. 0);
  108. try
  109. {
  110. int pos = MONTHS.indexOf(mo);
  111. int month = pos / 4;
  112. if (yr != null)
  113. {
  114. // it's a year
  115. cal.set(Calendar.YEAR,
  116. Integer.parseInt(yr));
  117. }
  118. else
  119. {
  120. // it must be hour/minute or we wouldn't have matched
  121. int year = cal.get(Calendar.YEAR);
  122. // if the month we're reading is greater than now, it must
  123. // be last year
  124. if (cal.get(Calendar.MONTH) < month)
  125. {
  126. year--;
  127. }
  128. cal.set(Calendar.YEAR,
  129. year);
  130. cal.set(Calendar.HOUR_OF_DAY,
  131. Integer.parseInt(hr));
  132. cal.set(Calendar.MINUTE,
  133. Integer.parseInt(min));
  134. }
  135. cal.set(Calendar.MONTH,
  136. month);
  137. cal.set(Calendar.DATE,
  138. Integer.parseInt(da));
  139. file.setTimestamp(cal);
  140. }
  141. catch (NumberFormatException e)
  142. {
  143. // do nothing, date will be uninitialized
  144. }
  145. file.setName(name);
  146. return file;
  147. }
  148. return null;
  149. }
  150. }