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.text.DateFormat; 022 import java.text.ParseException; 023 import java.text.SimpleDateFormat; 024 import java.util.Calendar; 025 import java.util.Date; 026 import java.util.GregorianCalendar; 027 import java.util.Locale; 028 import java.util.regex.Matcher; 029 import java.util.regex.Pattern; 030 031 import it.sauronsoftware.ftp4j.FTPFile; 032 import it.sauronsoftware.ftp4j.FTPListParseException; 033 import it.sauronsoftware.ftp4j.FTPListParser; 034 035 /** 036 * This parser can handle NetWare list responses. 037 * 038 * @author Carlo Pelliccia 039 */ 040 public class NetWareListParser implements FTPListParser { 041 042 private static final Pattern PATTERN = Pattern 043 .compile("^(d|-)\\s+\\[.{8}\\]\\s+\\S+\\s+(\\d+)\\s+" 044 + "(?:(\\w{3})\\s+(\\d{1,2}))\\s+(?:(\\d{4})|(?:(\\d{1,2}):(\\d{1,2})))\\s+" 045 + "([^\\\\/*?\"<>|]+)$"); 046 047 private static final DateFormat DATE_FORMAT = new SimpleDateFormat( 048 "MMM dd yyyy HH:mm", Locale.US); 049 050 public FTPFile[] parse(String[] lines) throws FTPListParseException { 051 int currentYear = new GregorianCalendar().get(Calendar.YEAR); 052 int size = lines.length; 053 FTPFile[] ret = new FTPFile[size]; 054 for (int i = 0; i < size; i++) { 055 Matcher m = PATTERN.matcher(lines[i]); 056 if (m.matches()) { 057 String typeString = m.group(1); 058 String sizeString = m.group(2); 059 String monthString = m.group(3); 060 String dayString = m.group(4); 061 String yearString = m.group(5); 062 String hourString = m.group(6); 063 String minuteString = m.group(7); 064 String nameString = m.group(8); 065 // Parse the data. 066 ret[i] = new FTPFile(); 067 if (typeString.equals("-")) { 068 ret[i].setType(FTPFile.TYPE_FILE); 069 } else if (typeString.equals("d")) { 070 ret[i].setType(FTPFile.TYPE_DIRECTORY); 071 } else { 072 throw new FTPListParseException(); 073 } 074 long fileSize; 075 try { 076 fileSize = Long.parseLong(sizeString); 077 } catch (Throwable t) { 078 throw new FTPListParseException(); 079 } 080 ret[i].setSize(fileSize); 081 if (dayString.length() == 1) { 082 dayString = "0" + dayString; 083 } 084 StringBuffer mdString = new StringBuffer(); 085 mdString.append(monthString); 086 mdString.append(' '); 087 mdString.append(dayString); 088 mdString.append(' '); 089 if (yearString == null) { 090 mdString.append(currentYear); 091 } else { 092 mdString.append(yearString); 093 } 094 mdString.append(' '); 095 if (hourString != null && minuteString != null) { 096 if (hourString.length() == 1) { 097 hourString = "0" + hourString; 098 } 099 if (minuteString.length() == 1) { 100 minuteString = "0" + minuteString; 101 } 102 mdString.append(hourString); 103 mdString.append(':'); 104 mdString.append(minuteString); 105 } else { 106 mdString.append("00:00"); 107 } 108 Date md; 109 try { 110 md = DATE_FORMAT.parse(mdString.toString()); 111 } catch (ParseException e) { 112 throw new FTPListParseException(); 113 } 114 ret[i].setModifiedDate(md); 115 ret[i].setName(nameString); 116 } else { 117 throw new FTPListParseException(); 118 } 119 } 120 return ret; 121 } 122 123 }