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
84 public FTPFile parseFTPEntry(String entry)
85 {
86
87 FTPFile file = new FTPFile();
88 file.setRawListing(entry);
89
90 if (matches(entry))
91 {
92 String usr = group(14);
93 String grp = group(15);
94 String filesize = group(16);
95 String mo = group(17);
96 String da = group(18);
97 String yr = group(20);
98 String hr = group(21);
99 String min = group(22);
100 String name = group(23);
101
102 file.setType(FTPFile.FILE_TYPE);
103 file.setUser(usr);
104 file.setGroup(grp);
105 try
106 {
107 file.setSize(Long.parseLong(filesize));
108 }
109 catch (NumberFormatException e)
110 {
111
112 }
113
114 Calendar cal = Calendar.getInstance();
115 cal.set(Calendar.MILLISECOND, 0);
116 cal.set(Calendar.SECOND,
117 0);
118 cal.set(Calendar.MINUTE,
119 0);
120 cal.set(Calendar.HOUR_OF_DAY,
121 0);
122 try
123 {
124
125 int pos = MONTHS.indexOf(mo);
126 int month = pos / 4;
127 if (yr != null)
128 {
129
130 cal.set(Calendar.YEAR,
131 Integer.parseInt(yr));
132 }
133 else
134 {
135
136 int year = cal.get(Calendar.YEAR);
137
138
139
140 if (cal.get(Calendar.MONTH) < month)
141 {
142 year--;
143 }
144 cal.set(Calendar.YEAR,
145 year);
146 cal.set(Calendar.HOUR_OF_DAY,
147 Integer.parseInt(hr));
148 cal.set(Calendar.MINUTE,
149 Integer.parseInt(min));
150 }
151 cal.set(Calendar.MONTH,
152 month);
153 cal.set(Calendar.DATE,
154 Integer.parseInt(da));
155 file.setTimestamp(cal);
156 }
157 catch (NumberFormatException e)
158 {
159
160 }
161 file.setName(name);
162
163 return file;
164 }
165 return null;
166 }
167 }