001: /*
002: * Copyright 2004-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: * @version $Id: OS400FTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
025: */
026:
027: public class OS400FTPEntryParser extends
028: ConfigurableFTPFileEntryParserImpl {
029: private static final String DEFAULT_DATE_FORMAT = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
030:
031: private static final String REGEX = "(\\S+)\\s+" // user
032: + "(\\d+)\\s+" // size
033: + "(\\S+)\\s+(\\S+)\\s+" // date stuff
034: + "(\\*\\S+)\\s+" // *STMF/*DIR
035: + "(\\S+/?)\\s*"; // filename
036:
037: /**
038: * The default constructor for a OS400FTPEntryParser object.
039: *
040: * @exception IllegalArgumentException
041: * Thrown if the regular expression is unparseable. Should not be seen
042: * under normal conditions. It it is seen, this is a sign that
043: * <code>REGEX</code> is not a valid regular expression.
044: */
045: public OS400FTPEntryParser() {
046: this (null);
047: }
048:
049: /**
050: * This constructor allows the creation of an OS400FTPEntryParser object
051: * with something other than the default configuration.
052: *
053: * @param config The {@link FTPClientConfig configuration} object used to
054: * configure this parser.
055: * @exception IllegalArgumentException
056: * Thrown if the regular expression is unparseable. Should not be seen
057: * under normal conditions. It it is seen, this is a sign that
058: * <code>REGEX</code> is not a valid regular expression.
059: * @since 1.4
060: */
061: public OS400FTPEntryParser(FTPClientConfig config) {
062: super (REGEX);
063: configure(config);
064: }
065:
066: public FTPFile parseFTPEntry(String entry) {
067:
068: FTPFile file = new FTPFile();
069: file.setRawListing(entry);
070: int type;
071:
072: if (matches(entry)) {
073: String usr = group(1);
074: String filesize = group(2);
075: String datestr = group(3) + " " + group(4);
076: String typeStr = group(5);
077: String name = group(6);
078:
079: try {
080: file.setTimestamp(super .parseTimestamp(datestr));
081: } catch (ParseException e) {
082: return null; // this is a parsing failure too.
083: }
084:
085: if (typeStr.equalsIgnoreCase("*STMF")) {
086: type = FTPFile.FILE_TYPE;
087: } else if (typeStr.equalsIgnoreCase("*DIR")) {
088: type = FTPFile.DIRECTORY_TYPE;
089: } else {
090: type = FTPFile.UNKNOWN_TYPE;
091: }
092:
093: file.setType(type);
094:
095: file.setUser(usr);
096:
097: try {
098: file.setSize(Long.parseLong(filesize));
099: } catch (NumberFormatException e) {
100: // intentionally do nothing
101: }
102:
103: if (name.endsWith("/")) {
104: name = name.substring(0, name.length() - 1);
105: }
106: int pos = name.lastIndexOf('/');
107: if (pos > -1) {
108: name = name.substring(pos + 1);
109: }
110:
111: file.setName(name);
112:
113: return file;
114: }
115: return null;
116: }
117:
118: /**
119: * Defines a default configuration to be used when this class is
120: * instantiated without a {@link FTPClientConfig FTPClientConfig}
121: * parameter being specified.
122: * @return the default configuration for this parser.
123: */
124: protected FTPClientConfig getDefaultConfiguration() {
125: return new FTPClientConfig(FTPClientConfig.SYST_OS400,
126: DEFAULT_DATE_FORMAT, null, null, null, null);
127: }
128:
129: }
|