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.text.ParseException;
019:
020: import org.apache.commons.net.ftp.FTPClientConfig;
021: import org.apache.commons.net.ftp.FTPFile;
022:
023: /**
024: * Implementation of FTPFileEntryParser and FTPFileListParser for OS2 Systems.
025: *
026: * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
027: * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
028: * @version $Id: OS2FTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
029: * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
030: */
031: public class OS2FTPEntryParser extends
032: ConfigurableFTPFileEntryParserImpl
033:
034: {
035:
036: private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy HH:mm"; //11-09-01 12:30
037: /**
038: * this is the regular expression used by this parser.
039: */
040: private static final String REGEX = "(\\s+|[0-9]+)\\s*"
041: + "(\\s+|[A-Z]+)\\s*" + "(DIR|\\s+)\\s*"
042: + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
043: + "(\\S.*)";
044:
045: /**
046: * The default constructor for a OS2FTPEntryParser object.
047: *
048: * @exception IllegalArgumentException
049: * Thrown if the regular expression is unparseable. Should not be seen
050: * under normal conditions. It it is seen, this is a sign that
051: * <code>REGEX</code> is not a valid regular expression.
052: */
053: public OS2FTPEntryParser() {
054: this (null);
055: }
056:
057: /**
058: * This constructor allows the creation of an OS2FTPEntryParser object
059: * with something other than the default configuration.
060: *
061: * @param config The {@link FTPClientConfig configuration} object used to
062: * configure this parser.
063: * @exception IllegalArgumentException
064: * Thrown if the regular expression is unparseable. Should not be seen
065: * under normal conditions. It it is seen, this is a sign that
066: * <code>REGEX</code> is not a valid regular expression.
067: * @since 1.4
068: */
069: public OS2FTPEntryParser(FTPClientConfig config) {
070: super (REGEX);
071: configure(config);
072: }
073:
074: /**
075: * Parses a line of an OS2 FTP server file listing and converts it into a
076: * usable format in the form of an <code> FTPFile </code> instance. If the
077: * file listing line doesn't describe a file, <code> null </code> is
078: * returned, otherwise a <code> FTPFile </code> instance representing the
079: * files in the directory is returned.
080: * <p>
081: * @param entry A line of text from the file listing
082: * @return An FTPFile instance corresponding to the supplied entry
083: */
084: public FTPFile parseFTPEntry(String entry) {
085:
086: FTPFile f = new FTPFile();
087: if (matches(entry)) {
088: String size = group(1);
089: String attrib = group(2);
090: String dirString = group(3);
091: String datestr = group(4) + " " + group(5);
092: String name = group(6);
093: try {
094: f.setTimestamp(super .parseTimestamp(datestr));
095: } catch (ParseException e) {
096: return null; // this is a parsing failure too.
097: }
098:
099: //is it a DIR or a file
100: if (dirString.trim().equals("DIR")
101: || attrib.trim().equals("DIR")) {
102: f.setType(FTPFile.DIRECTORY_TYPE);
103: } else {
104: f.setType(FTPFile.FILE_TYPE);
105: }
106:
107: //set the name
108: f.setName(name.trim());
109:
110: //set the size
111: f.setSize(Long.parseLong(size.trim()));
112:
113: return (f);
114: }
115: return null;
116:
117: }
118:
119: /**
120: * Defines a default configuration to be used when this class is
121: * instantiated without a {@link FTPClientConfig FTPClientConfig}
122: * parameter being specified.
123: * @return the default configuration for this parser.
124: */
125: protected FTPClientConfig getDefaultConfiguration() {
126: return new FTPClientConfig(FTPClientConfig.SYST_OS2,
127: DEFAULT_DATE_FORMAT, null, null, null, null);
128: }
129:
130: }
|