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.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import java.util.GregorianCalendar;
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.TimeZone;
25
26 import org.apache.commons.net.ftp.FTPFile;
27 import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class MLSxEntryParser extends FTPFileEntryParserImpl
56 {
57
58 private static final MLSxEntryParser PARSER = new MLSxEntryParser();
59
60 private static final HashMap<String, Integer> TYPE_TO_INT = new HashMap<String, Integer>();
61 static {
62 TYPE_TO_INT.put("file", Integer.valueOf(FTPFile.FILE_TYPE));
63 TYPE_TO_INT.put("cdir", Integer.valueOf(FTPFile.DIRECTORY_TYPE));
64 TYPE_TO_INT.put("pdir", Integer.valueOf(FTPFile.DIRECTORY_TYPE));
65 TYPE_TO_INT.put("dir", Integer.valueOf(FTPFile.DIRECTORY_TYPE));
66 }
67
68 private static int UNIX_GROUPS[] = {
69 FTPFile.USER_ACCESS,
70 FTPFile.GROUP_ACCESS,
71 FTPFile.WORLD_ACCESS,
72 };
73
74 private static int UNIX_PERMS[][] = {
75
76
77
78
79
80
81
82
83 };
84
85
86
87
88
89 public MLSxEntryParser()
90 {
91 super();
92 }
93
94
95 public FTPFile parseFTPEntry(String entry) {
96 String parts[] = entry.split(" ",2);
97 if (parts.length != 2) {
98 return null;
99 }
100 FTPFile file = new FTPFile();
101 file.setRawListing(entry);
102 file.setName(parts[1]);
103 String[] facts = parts[0].split(";");
104 boolean hasUnixMode = parts[0].toLowerCase(Locale.ENGLISH).contains("unix.mode=");
105 for(String fact : facts) {
106 String []factparts = fact.split("=");
107
108
109
110 if (factparts.length != 2) {
111 continue;
112 }
113 String factname = factparts[0].toLowerCase(Locale.ENGLISH);
114 String factvalue = factparts[1];
115 String valueLowerCase = factvalue.toLowerCase(Locale.ENGLISH);
116 if ("size".equals(factname)) {
117 file.setSize(Long.parseLong(factvalue));
118 }
119 else if ("sizd".equals(factname)) {
120 file.setSize(Long.parseLong(factvalue));
121 }
122 else if ("modify".equals(factname)) {
123
124 SimpleDateFormat sdf;
125 if (factvalue.contains(".")){
126 sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
127 } else {
128 sdf = new SimpleDateFormat("yyyyMMddHHmmss");
129 }
130 TimeZone GMT = TimeZone.getTimeZone("GMT");
131 sdf.setTimeZone(GMT);
132 GregorianCalendar gc = new GregorianCalendar(GMT);
133 try {
134 gc.setTime(sdf.parse(factvalue));
135 } catch (ParseException e) {
136
137 }
138 file.setTimestamp(gc);
139 }
140 else if ("type".equals(factname)) {
141 Integer intType = TYPE_TO_INT.get(valueLowerCase);
142 if (intType == null) {
143 file.setType(FTPFile.UNKNOWN_TYPE);
144 } else {
145 file.setType(intType.intValue());
146 }
147 }
148 else if (factname.startsWith("unix.")) {
149 String unixfact = factname.substring("unix.".length()).toLowerCase(Locale.ENGLISH);
150 if ("group".equals(unixfact)){
151 file.setGroup(factvalue);
152 } else if ("owner".equals(unixfact)){
153 file.setUser(factvalue);
154 } else if ("mode".equals(unixfact)){
155 int off = factvalue.length()-3;
156 for(int i=0; i < 3; i++){
157 int ch = factvalue.charAt(off+i)-'0';
158 if (ch >= 0 && ch <= 7) {
159 for(int p : UNIX_PERMS[ch]) {
160 file.setPermission(UNIX_GROUPS[i], p, true);
161 }
162 } else {
163
164 }
165 }
166 }
167 }
168 else if (!hasUnixMode && "perm".equals(factname)) {
169 doUnixPerms(file, valueLowerCase);
170 }
171 }
172 return file;
173 }
174
175
176
177
178 private void doUnixPerms(FTPFile file, String valueLowerCase) {
179 for(char c : valueLowerCase.toCharArray()) {
180
181 switch (c) {
182 case 'a':
183 file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
184 break;
185 case 'c':
186 file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
187 break;
188 case 'd':
189 file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
190 break;
191 case 'e':
192 file.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION, true);
193 break;
194 case 'f':
195
196 break;
197 case 'l':
198 file.setPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION, true);
199 break;
200 case 'm':
201 file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
202 break;
203 case 'p':
204 file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
205 break;
206 case 'r':
207 file.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION, true);
208 break;
209 case 'w':
210 file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
211 break;
212 default:
213 break;
214
215 }
216 }
217 }
218
219 public static FTPFile parseEntry(String entry) {
220 return PARSER.parseFTPEntry(entry);
221 }
222
223 public static MLSxEntryParser getInstance() {
224 return PARSER;
225 }
226 }