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.FTPFile;
  18. import java.util.Calendar;
  19. /**
  20. * @version $Id: OS400FTPEntryParser.java,v 1.3 2004/04/22 00:48:07 scohen Exp $
  21. */
  22. public class OS400FTPEntryParser extends RegexFTPFileEntryParserImpl
  23. {
  24. private static final String REGEX =
  25. "(\\S+)\\s+" // user
  26. + "(\\d+)\\s+" // size
  27. + "(\\d\\d)/(\\d\\d)/(\\d\\d)\\s+" // year/month/day
  28. + "([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\\s+" // hour:minutes:seconds
  29. + "(\\*\\S+)\\s+" // *STMF/*DIR
  30. + "(\\S+/?)\\s*"; // filename
  31. public OS400FTPEntryParser()
  32. {
  33. super(REGEX);
  34. }
  35. public FTPFile parseFTPEntry(String entry)
  36. {
  37. FTPFile file = new FTPFile();
  38. file.setRawListing(entry);
  39. int type;
  40. if (matches(entry))
  41. {
  42. String usr = group(1);
  43. String filesize = group(2);
  44. String yr = group(3);
  45. String mo = group(4);
  46. String da = group(5);
  47. String hr = group(6);
  48. String min = group(7);
  49. String sec = group(8);
  50. String typeStr = group(9);
  51. String name = group(10);
  52. if (typeStr.equalsIgnoreCase("*STMF"))
  53. {
  54. type = FTPFile.FILE_TYPE;
  55. }
  56. else if (typeStr.equalsIgnoreCase("*DIR"))
  57. {
  58. type = FTPFile.DIRECTORY_TYPE;
  59. }
  60. else
  61. {
  62. type = FTPFile.UNKNOWN_TYPE;
  63. }
  64. file.setType(type);
  65. file.setUser(usr);
  66. try
  67. {
  68. file.setSize(Integer.parseInt(filesize));
  69. }
  70. catch (NumberFormatException e)
  71. {
  72. // intentionally do nothing
  73. }
  74. Calendar cal = Calendar.getInstance();
  75. cal.set(Calendar.SECOND, 0);
  76. cal.set(Calendar.MINUTE, 0);
  77. cal.set(Calendar.HOUR_OF_DAY, 0);
  78. try
  79. {
  80. int year = Integer.parseInt(yr, 10);
  81. if (year < 70)
  82. {
  83. year += 2000;
  84. }
  85. else
  86. {
  87. year += 1900;
  88. }
  89. cal.set(Calendar.YEAR, year);
  90. cal.set(Calendar.MONTH, Integer.parseInt(mo, 10)-1);
  91. cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da, 10));
  92. cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr, 10));
  93. cal.set(Calendar.MINUTE, Integer.parseInt(min, 10));
  94. cal.set(Calendar.SECOND, Integer.parseInt(sec, 10));
  95. file.setTimestamp(cal);
  96. }
  97. catch (NumberFormatException e)
  98. {
  99. // do nothing, date will be uninitialized
  100. }
  101. if (name.endsWith("/"))
  102. {
  103. name = name.substring(0, name.length() - 1);
  104. }
  105. int pos = name.lastIndexOf('/');
  106. if (pos > -1)
  107. {
  108. name = name.substring(pos + 1);
  109. }
  110. file.setName(name);
  111. return file;
  112. }
  113. return null;
  114. }
  115. }