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. * Implementation of FTPFileEntryParser and FTPFileListParser for NT Systems.
  21. *
  22. * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
  23. * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
  24. * @version $Id: NTFTPEntryParser.java,v 1.16 2004/05/04 22:44:05 scohen Exp $
  25. * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
  26. */
  27. public class NTFTPEntryParser extends RegexFTPFileEntryParserImpl
  28. {
  29. /**
  30. * this is the regular expression used by this parser.
  31. */
  32. private static final String REGEX =
  33. "((?:0[1-9])|(?:1[0-2]))-"
  34. + "((?:0[1-9])|(?:[1-2]\\d)|(?:3[0-1]))-"
  35. + "(\\d\\d)\\s*"
  36. + "((?:0[1-9])|(?:1[012])):"
  37. + "([0-5]\\d)\\s*"
  38. + "([AP])M\\s*"
  39. + "(<DIR>)?\\s*"
  40. + "([0-9]+)?\\s+"
  41. + "(\\S.*)";
  42. /**
  43. * The sole constructor for an NTFTPEntryParser object.
  44. *
  45. * @exception IllegalArgumentException
  46. * Thrown if the regular expression is unparseable. Should not be seen
  47. * under normal conditions. It it is seen, this is a sign that
  48. * <code>REGEX</code> is not a valid regular expression.
  49. */
  50. public NTFTPEntryParser()
  51. {
  52. super(REGEX);
  53. }
  54. /**
  55. * Parses a line of an NT FTP server file listing and converts it into a
  56. * usable format in the form of an <code> FTPFile </code> instance. If the
  57. * file listing line doesn't describe a file, <code> null </code> is
  58. * returned, otherwise a <code> FTPFile </code> instance representing the
  59. * files in the directory is returned.
  60. * <p>
  61. * @param entry A line of text from the file listing
  62. * @return An FTPFile instance corresponding to the supplied entry
  63. */
  64. public FTPFile parseFTPEntry(String entry)
  65. {
  66. FTPFile f = new FTPFile();
  67. f.setRawListing(entry);
  68. if (matches(entry))
  69. {
  70. String mo = group(1);
  71. String da = group(2);
  72. String yr = group(3);
  73. String hr = group(4);
  74. String min = group(5);
  75. String ampm = group(6);
  76. String dirString = group(7);
  77. String size = group(8);
  78. String name = group(9);
  79. if (null == name || name.equals(".") || name.equals(".."))
  80. {
  81. return (null);
  82. }
  83. f.setName(name);
  84. //convert all the calendar stuff to ints
  85. int month = new Integer(mo).intValue() - 1;
  86. int day = new Integer(da).intValue();
  87. int year = new Integer(yr).intValue() + 2000;
  88. int hour = new Integer(hr).intValue();
  89. int minutes = new Integer(min).intValue();
  90. // Y2K stuff? this will break again in 2080 but I will
  91. // be sooooo dead anyways who cares.
  92. // SMC - IS NT's directory date REALLY still not Y2K-compliant?
  93. if (year > 2080)
  94. {
  95. year -= 100;
  96. }
  97. Calendar cal = Calendar.getInstance();
  98. cal.clear();
  99. //set the calendar
  100. cal.set(Calendar.YEAR, year);
  101. cal.set(Calendar.DATE, day);
  102. cal.set(Calendar.MONTH, month);
  103. int ap = Calendar.AM;
  104. if ("P".equals(ampm))
  105. {
  106. ap = Calendar.PM;
  107. if (hour != 12) {
  108. hour += 12;
  109. }
  110. } else if (hour == 12) {
  111. hour = 0;
  112. }
  113. cal.set(Calendar.SECOND, 0);
  114. cal.set(Calendar.MINUTE, minutes);
  115. // Using Calendar.HOUR_OF_DAY instead of Calendar.HOUR
  116. // since the latter has proven to be unreliable.
  117. // see bug 27085
  118. // cal.set(Calendar.AM_PM, ap);
  119. cal.set(Calendar.HOUR_OF_DAY, hour);
  120. cal.getTime().getTime();
  121. f.setTimestamp(cal);
  122. if ("<DIR>".equals(dirString))
  123. {
  124. f.setType(FTPFile.DIRECTORY_TYPE);
  125. f.setSize(0);
  126. }
  127. else
  128. {
  129. f.setType(FTPFile.FILE_TYPE);
  130. if (null != size)
  131. {
  132. f.setSize(new Integer(size).intValue());
  133. }
  134. }
  135. return (f);
  136. }
  137. return null;
  138. }
  139. }