1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.ftp.parser;
19 import java.util.Calendar;
20
21 import org.apache.commons.net.ftp.FTPFile;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl
38 {
39
40
41
42
43
44 private static final String MONTHS =
45 "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
46
47
48
49
50 private static final String REGEX =
51 "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
52 + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
53 + "(\\S*)\\s*"
54 + "(\\S+)\\s*"
55 + "(\\S*)\\s*"
56 + "(\\d*)\\s*"
57 + "(\\d*)\\s*"
58 + MONTHS
59 + "\\s*"
60 + "((?:[012]\\d*)|(?:3[01]))\\s*"
61 + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
62 + "(\\S*)(\\s*.*)";
63
64
65
66
67
68 public EnterpriseUnixFTPEntryParser()
69 {
70 super(REGEX);
71 }
72
73
74
75
76
77
78
79
80
81
82
83 public FTPFile parseFTPEntry(String entry)
84 {
85
86 FTPFile file = new FTPFile();
87 file.setRawListing(entry);
88
89 if (matches(entry))
90 {
91 String usr = group(14);
92 String grp = group(15);
93 String filesize = group(16);
94 String mo = group(17);
95 String da = group(18);
96 String yr = group(20);
97 String hr = group(21);
98 String min = group(22);
99 String name = group(23);
100
101 file.setType(FTPFile.FILE_TYPE);
102 file.setUser(usr);
103 file.setGroup(grp);
104 try
105 {
106 file.setSize(Long.parseLong(filesize));
107 }
108 catch (NumberFormatException e)
109 {
110
111 }
112
113 Calendar cal = Calendar.getInstance();
114 cal.set(Calendar.MILLISECOND, 0);
115 cal.set(Calendar.SECOND,
116 0);
117 cal.set(Calendar.MINUTE,
118 0);
119 cal.set(Calendar.HOUR_OF_DAY,
120 0);
121 try
122 {
123
124 int pos = MONTHS.indexOf(mo);
125 int month = pos / 4;
126 if (yr != null)
127 {
128
129 cal.set(Calendar.YEAR,
130 Integer.parseInt(yr));
131 }
132 else
133 {
134
135 int year = cal.get(Calendar.YEAR);
136
137
138
139 if (cal.get(Calendar.MONTH) < month)
140 {
141 year--;
142 }
143 cal.set(Calendar.YEAR,
144 year);
145 cal.set(Calendar.HOUR_OF_DAY,
146 Integer.parseInt(hr));
147 cal.set(Calendar.MINUTE,
148 Integer.parseInt(min));
149 }
150 cal.set(Calendar.MONTH,
151 month);
152 cal.set(Calendar.DATE,
153 Integer.parseInt(da));
154 file.setTimestamp(cal);
155 }
156 catch (NumberFormatException e)
157 {
158
159 }
160 file.setName(name);
161
162 return file;
163 }
164 return null;
165 }
166 }