001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp.parser;
017:
018: import java.util.Calendar;
019:
020: import org.apache.commons.net.ftp.FTPFile;
021:
022: /**
023: * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce.
024: * Here is a sample of the sort of output line this parser processes:
025: * "-C--E-----FTP B QUA1I1 18128 41 Aug 12 13:56 QUADTEST"
026: * <P><B>
027: * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the
028: * DefaultFTPParserFactory by classname. It will not be chosen
029: * by the autodetection scheme.
030: * </B>
031: * @version $Id: EnterpriseUnixFTPEntryParser.java 165675 2005-05-02 20:09:55Z rwinston $
032: * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
033: * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
034: * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
035: */
036: public class EnterpriseUnixFTPEntryParser extends
037: RegexFTPFileEntryParserImpl {
038:
039: /**
040: * months abbreviations looked for by this parser. Also used
041: * to determine <b>which</b> month has been matched by the parser.
042: */
043: private static final String MONTHS = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
044:
045: /**
046: * this is the regular expression used by this parser.
047: */
048: private static final String REGEX = "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
049: + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
050: + "(\\S*)\\s*"
051: + "(\\S+)\\s*"
052: + "(\\S*)\\s*"
053: + "(\\d*)\\s*"
054: + "(\\d*)\\s*"
055: + MONTHS
056: + "\\s*"
057: + "((?:[012]\\d*)|(?:3[01]))\\s*"
058: + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
059: + "(\\S*)(\\s*.*)";
060:
061: /**
062: * The sole constructor for a EnterpriseUnixFTPEntryParser object.
063: *
064: */
065: public EnterpriseUnixFTPEntryParser() {
066: super (REGEX);
067: }
068:
069: /**
070: * Parses a line of a unix FTP server file listing and converts it into a
071: * usable format in the form of an <code> FTPFile </code> instance. If
072: * the file listing line doesn't describe a file, <code> null </code> is
073: * returned, otherwise a <code> FTPFile </code> instance representing the
074: * files in the directory is returned.
075: *
076: * @param entry A line of text from the file listing
077: * @return An FTPFile instance corresponding to the supplied entry
078: */
079: public FTPFile parseFTPEntry(String entry) {
080:
081: FTPFile file = new FTPFile();
082: file.setRawListing(entry);
083:
084: if (matches(entry)) {
085: String usr = group(14);
086: String grp = group(15);
087: String filesize = group(16);
088: String mo = group(17);
089: String da = group(18);
090: String yr = group(20);
091: String hr = group(21);
092: String min = group(22);
093: String name = group(23);
094:
095: file.setType(FTPFile.FILE_TYPE);
096: file.setUser(usr);
097: file.setGroup(grp);
098: try {
099: file.setSize(Long.parseLong(filesize));
100: } catch (NumberFormatException e) {
101: // intentionally do nothing
102: }
103:
104: Calendar cal = Calendar.getInstance();
105: cal.set(Calendar.MILLISECOND, 0);
106: cal.set(Calendar.SECOND, 0);
107: cal.set(Calendar.MINUTE, 0);
108: cal.set(Calendar.HOUR_OF_DAY, 0);
109: try {
110:
111: int pos = MONTHS.indexOf(mo);
112: int month = pos / 4;
113: if (yr != null) {
114: // it's a year
115: cal.set(Calendar.YEAR, Integer.parseInt(yr));
116: } else {
117: // it must be hour/minute or we wouldn't have matched
118: int year = cal.get(Calendar.YEAR);
119:
120: // if the month we're reading is greater than now, it must
121: // be last year
122: if (cal.get(Calendar.MONTH) < month) {
123: year--;
124: }
125: cal.set(Calendar.YEAR, year);
126: cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr));
127: cal.set(Calendar.MINUTE, Integer.parseInt(min));
128: }
129: cal.set(Calendar.MONTH, month);
130: cal.set(Calendar.DATE, Integer.parseInt(da));
131: file.setTimestamp(cal);
132: } catch (NumberFormatException e) {
133: // do nothing, date will be uninitialized
134: }
135: file.setName(name);
136:
137: return file;
138: }
139: return null;
140: }
141: }
|