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 OS2 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: OS2FTPEntryParser.java,v 1.11 2004/04/21 23:30:33 scohen Exp $
  25. * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
  26. */
  27. public class OS2FTPEntryParser extends RegexFTPFileEntryParserImpl
  28. {
  29. /**
  30. * this is the regular expression used by this parser.
  31. */
  32. private static final String REGEX =
  33. "(\\s+|[0-9]+)\\s*"
  34. + "(\\s+|[A-Z]+)\\s*"
  35. + "(DIR|\\s+)\\s*"
  36. + "((?:0[1-9])|(?:1[0-2]))-"
  37. + "((?:0[1-9])|(?:[1-2]\\d)|(?:3[0-1]))-"
  38. + "(\\d\\d)\\s*"
  39. + "(?:([0-1]\\d)|(?:2[0-3])):"
  40. + "([0-5]\\d)\\s*"
  41. + "(\\S.*)";
  42. /**
  43. * The sole constructor for a OS2FTPEntryParser 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 OS2FTPEntryParser()
  51. {
  52. super(REGEX);
  53. }
  54. /**
  55. * Parses a line of an OS2 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. if (matches(entry))
  68. {
  69. String size = group(1);
  70. String attrib = group(2);
  71. String dirString = group(3);
  72. String mo = group(4);
  73. String da = group(5);
  74. String yr = group(6);
  75. String hr = group(7);
  76. String min = group(8);
  77. String name = group(9);
  78. //is it a DIR or a file
  79. if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR"))
  80. {
  81. f.setType(FTPFile.DIRECTORY_TYPE);
  82. }
  83. else
  84. {
  85. f.setType(FTPFile.FILE_TYPE);
  86. }
  87. Calendar cal = Calendar.getInstance();
  88. //convert all the calendar stuff to ints
  89. int month = new Integer(mo).intValue() - 1;
  90. int day = new Integer(da).intValue();
  91. int year = new Integer(yr).intValue() + 2000;
  92. int hour = new Integer(hr).intValue();
  93. int minutes = new Integer(min).intValue();
  94. // Y2K stuff? this will break again in 2080 but I will
  95. // be sooooo dead anyways who cares.
  96. // SMC - IS OS2's directory date REALLY still not Y2K-compliant?
  97. if (year > 2080)
  98. {
  99. year -= 100;
  100. }
  101. //set the calendar
  102. cal.set(Calendar.SECOND, 0);
  103. cal.set(Calendar.MINUTE, minutes);
  104. cal.set(Calendar.HOUR_OF_DAY, hour);
  105. cal.set(Calendar.YEAR, year);
  106. cal.set(Calendar.DATE, day);
  107. cal.set(Calendar.MONTH, month);
  108. f.setTimestamp(cal);
  109. //set the name
  110. f.setName(name.trim());
  111. //set the size
  112. Long theSize = new Long(size.trim());
  113. theSize = new Long(String.valueOf(theSize.intValue()));
  114. f.setSize(theSize.longValue());
  115. return (f);
  116. }
  117. return null;
  118. }
  119. }