1 16 package org.apache.commons.net.ftp.parser; 17 import java.io.BufferedReader ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.text.ParseException ; 21 import java.util.StringTokenizer ; 22 23 import org.apache.commons.net.ftp.FTPClientConfig; 24 import org.apache.commons.net.ftp.FTPFile; 25 import org.apache.commons.net.ftp.FTPListParseEngine; 26 27 49 public class VMSFTPEntryParser extends ConfigurableFTPFileEntryParserImpl 50 { 51 52 private static final String DEFAULT_DATE_FORMAT 53 = "d-MMM-yyyy HH:mm:ss"; 55 58 private static final String REGEX = 59 "(.*;[0-9]+)\\s*" 60 + "(\\d+)/\\d+\\s*" 61 +"(\\S+)\\s+(\\S+)\\s+" 62 + "\\[(([0-9$A-Za-z_]+)|([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+))\\]?\\s*" 63 + "\\([a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*\\)"; 64 65 66 67 75 public VMSFTPEntryParser() 76 { 77 this(null); 78 } 79 80 92 public VMSFTPEntryParser(FTPClientConfig config) 93 { 94 super(REGEX); 95 configure(config); 96 } 97 98 99 100 114 public FTPFile[] parseFileList(InputStream listStream) throws IOException { 115 FTPListParseEngine engine = new FTPListParseEngine(this); 116 engine.readServerList(listStream); 117 return engine.getFiles(); 118 } 119 120 121 122 132 public FTPFile parseFTPEntry(String entry) 133 { 134 long longBlock = 512; 136 137 if (matches(entry)) 138 { 139 FTPFile f = new FTPFile(); 140 f.setRawListing(entry); 141 String name = group(1); 142 String size = group(2); 143 String datestr = group(3)+" "+group(4); 144 String owner = group(5); 145 try 146 { 147 f.setTimestamp(super.parseTimestamp(datestr)); 148 } 149 catch (ParseException e) 150 { 151 return null; } 153 154 155 String grp; 156 String user; 157 StringTokenizer t = new StringTokenizer (owner, ","); 158 switch (t.countTokens()) { 159 case 1: 160 grp = null; 161 user = t.nextToken(); 162 break; 163 case 2: 164 grp = t.nextToken(); 165 user = t.nextToken(); 166 break; 167 default: 168 grp = null; 169 user = null; 170 } 171 172 if (name.lastIndexOf(".DIR") != -1) 173 { 174 f.setType(FTPFile.DIRECTORY_TYPE); 175 } 176 else 177 { 178 f.setType(FTPFile.FILE_TYPE); 179 } 180 if (isVersioning()) 183 { 184 f.setName(name); 185 } 186 else 187 { 188 name = name.substring(0, name.lastIndexOf(";")); 189 f.setName(name); 190 } 191 long sizeInBytes = Long.parseLong(size) * longBlock; 194 f.setSize(sizeInBytes); 195 196 f.setGroup(grp); 197 f.setUser(user); 198 return f; 203 } 204 return null; 205 } 206 207 208 220 public String readNextEntry(BufferedReader reader) throws IOException 221 { 222 String line = reader.readLine(); 223 StringBuffer entry = new StringBuffer (); 224 while (line != null) 225 { 226 if (line.startsWith("Directory") || line.startsWith("Total")) { 227 line = reader.readLine(); 228 continue; 229 } 230 231 entry.append(line); 232 if (line.trim().endsWith(")")) 233 { 234 break; 235 } 236 line = reader.readLine(); 237 } 238 return (entry.length() == 0 ? null : entry.toString()); 239 } 240 241 protected boolean isVersioning() { 242 return false; 243 } 244 245 251 protected FTPClientConfig getDefaultConfiguration() { 252 return new FTPClientConfig( 253 FTPClientConfig.SYST_VMS, 254 DEFAULT_DATE_FORMAT, 255 null, null, null, null); 256 } 257 258 259 } 260 261 268 | Popular Tags |