1 16 package org.apache.commons.net.ftp.parser; 17 import java.text.ParseException ; 18 19 import org.apache.commons.net.ftp.FTPClientConfig; 20 import org.apache.commons.net.ftp.FTPFile; 21 22 32 public class UnixFTPEntryParser extends ConfigurableFTPFileEntryParserImpl 33 { 34 38 private static final String DEFAULT_MONTHS = 39 "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"; 40 41 static final String DEFAULT_DATE_FORMAT 42 = "MMM d yyyy"; 44 static final String DEFAULT_RECENT_DATE_FORMAT 45 = "MMM d HH:mm"; 47 static final String NUMERIC_DATE_FORMAT 48 = "yyyy-MM-dd HH:mm"; 50 62 public static final FTPClientConfig NUMERIC_DATE_CONFIG = 63 new FTPClientConfig( 64 FTPClientConfig.SYST_UNIX, 65 NUMERIC_DATE_FORMAT, 66 null, null, null, null); 67 68 87 private static final String REGEX = 88 "([bcdlfmpSs-])" 89 +"(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\s+" 90 + "(\\d+)\\s+" 91 + "(\\S+)\\s+" 92 + "(?:(\\S+)\\s+)?" 93 + "(\\d+)\\s+" 94 95 98 + "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S+\\s+\\S+))\\s+" 99 100 104 + "(\\d+(?::\\d+)?)\\s+" 105 106 + "(\\S*)(\\s*.*)"; 107 108 109 117 public UnixFTPEntryParser() 118 { 119 this(null); 120 } 121 122 134 public UnixFTPEntryParser(FTPClientConfig config) 135 { 136 super(REGEX); 137 configure(config); 138 } 139 140 141 151 public FTPFile parseFTPEntry(String entry) { 152 FTPFile file = new FTPFile(); 153 file.setRawListing(entry); 154 int type; 155 boolean isDevice = false; 156 157 if (matches(entry)) 158 { 159 String typeStr = group(1); 160 String hardLinkCount = group(15); 161 String usr = group(16); 162 String grp = group(17); 163 String filesize = group(18); 164 String datestr = group(19) + " " + group(20); 165 String name = group(21); 166 String endtoken = group(22); 167 168 try 169 { 170 file.setTimestamp(super.parseTimestamp(datestr)); 171 } 172 catch (ParseException e) 173 { 174 return null; } 176 177 178 switch (typeStr.charAt(0)) 180 { 181 case 'd': 182 type = FTPFile.DIRECTORY_TYPE; 183 break; 184 case 'l': 185 type = FTPFile.SYMBOLIC_LINK_TYPE; 186 break; 187 case 'b': 188 case 'c': 189 isDevice = true; 190 case 'f': 192 case '-': 193 type = FTPFile.FILE_TYPE; 194 break; 195 default: 196 type = FTPFile.UNKNOWN_TYPE; 197 } 198 199 file.setType(type); 200 201 int g = 4; 202 for (int access = 0; access < 3; access++, g += 4) 203 { 204 file.setPermission(access, FTPFile.READ_PERMISSION, 206 (!group(g).equals("-"))); 207 file.setPermission(access, FTPFile.WRITE_PERMISSION, 208 (!group(g + 1).equals("-"))); 209 210 String execPerm = group(g + 2); 211 if (!execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0))) 212 { 213 file.setPermission(access, FTPFile.EXECUTE_PERMISSION, true); 214 } 215 else 216 { 217 file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false); 218 } 219 } 220 221 if (!isDevice) 222 { 223 try 224 { 225 file.setHardLinkCount(Integer.parseInt(hardLinkCount)); 226 } 227 catch (NumberFormatException e) 228 { 229 } 231 } 232 233 file.setUser(usr); 234 file.setGroup(grp); 235 236 try 237 { 238 file.setSize(Long.parseLong(filesize)); 239 } 240 catch (NumberFormatException e) 241 { 242 } 244 245 if (null == endtoken) 246 { 247 file.setName(name); 248 } 249 else 250 { 251 name += endtoken; 254 if (type == FTPFile.SYMBOLIC_LINK_TYPE) 255 { 256 257 int end = name.indexOf(" -> "); 258 if (end == -1) 260 { 261 file.setName(name); 262 } 263 else 264 { 265 file.setName(name.substring(0, end)); 266 file.setLink(name.substring(end + 4)); 267 } 268 269 } 270 else 271 { 272 file.setName(name); 273 } 274 } 275 return file; 276 } 277 return null; 278 } 279 280 286 protected FTPClientConfig getDefaultConfiguration() { 287 return new FTPClientConfig( 288 FTPClientConfig.SYST_UNIX, 289 DEFAULT_DATE_FORMAT, 290 DEFAULT_RECENT_DATE_FORMAT, 291 null, null, null); 292 } 293 294 295 296 297 } 298 | Popular Tags |