001 /* 002 * ftp4j - A pure Java FTP client library 003 * 004 * Copyright (C) 2008-2009 Carlo Pelliccia (www.sauronsoftware.it) 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU Lesser General Public License version 008 * 2.1, as published by the Free Software Foundation. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License version 2.1 along with this program. 017 * If not, see <http://www.gnu.org/licenses/>. 018 */ 019 package it.sauronsoftware.ftp4j.listparsers; 020 021 import java.util.Date; 022 import java.util.StringTokenizer; 023 024 import it.sauronsoftware.ftp4j.FTPFile; 025 import it.sauronsoftware.ftp4j.FTPListParseException; 026 import it.sauronsoftware.ftp4j.FTPListParser; 027 028 /** 029 * This parser can handle the EPLF format. 030 * 031 * @author Carlo Pelliccia 032 */ 033 public class EPLFListParser implements FTPListParser { 034 035 public FTPFile[] parse(String[] lines) throws FTPListParseException { 036 int size = lines.length; 037 FTPFile[] ret = null; 038 for (int i = 0; i < size; i++) { 039 String l = lines[i]; 040 // Validate the plus sign. 041 if (l.charAt(0) != '+') { 042 throw new FTPListParseException(); 043 } 044 // Split the facts from the filename. 045 int a = l.indexOf('\t'); 046 if (a == -1) { 047 throw new FTPListParseException(); 048 } 049 String facts = l.substring(1, a); 050 String name = l.substring(a + 1, l.length()); 051 // Parse the facts. 052 Date md = null; 053 boolean dir = false; 054 long fileSize = 0; 055 StringTokenizer st = new StringTokenizer(facts, ","); 056 while (st.hasMoreTokens()) { 057 String f = st.nextToken(); 058 int s = f.length(); 059 if (s > 0) { 060 if (s == 1) { 061 if (f.equals("/")) { 062 // This is a directory. 063 dir = true; 064 } 065 } else { 066 char c = f.charAt(0); 067 String value = f.substring(1, s); 068 if (c == 's') { 069 // Size parameter. 070 try { 071 fileSize = Long.parseLong(value); 072 } catch (Throwable t) { 073 ; 074 } 075 } else if (c == 'm') { 076 // Modified date. 077 try { 078 long m = Long.parseLong(value); 079 md = new Date(m * 1000); 080 } catch (Throwable t) { 081 ; 082 } 083 } 084 } 085 } 086 } 087 // Create the related FTPFile object. 088 if (ret == null) { 089 ret = new FTPFile[size]; 090 } 091 ret[i] = new FTPFile(); 092 ret[i].setName(name); 093 ret[i].setModifiedDate(md); 094 ret[i].setSize(fileSize); 095 ret[i].setType(dir ? FTPFile.TYPE_DIRECTORY : FTPFile.TYPE_FILE); 096 } 097 return ret; 098 } 099 100 public static void main(String[] args) throws Throwable { 101 String[] test = { "+i8388621.29609,m824255902,/,\tdev", 102 "+i8388621.44468,m839956783,r,s10376,\tRFCEPLF" }; 103 EPLFListParser parser = new EPLFListParser(); 104 FTPFile[] f = parser.parse(test); 105 for (int i = 0; i < f.length; i++) { 106 System.out.println(f[i]); 107 } 108 } 109 }