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 NT 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: NTFTPEntryParser.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 NTFTPEntryParser extends
032: ConfigurableFTPFileEntryParserImpl {
033:
034: private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy hh:mma"; //11-09-01 12:30PM
035:
036: /**
037: * this is the regular expression used by this parser.
038: */
039: private static final String REGEX = "(\\S+)\\s+(\\S+)\\s+"
040: + "(<DIR>)?\\s*" + "([0-9]+)?\\s+" + "(\\S.*)";
041:
042: /**
043: * The sole constructor for an NTFTPEntryParser object.
044: *
045: * @exception IllegalArgumentException
046: * Thrown if the regular expression is unparseable. Should not be seen
047: * under normal conditions. It it is seen, this is a sign that
048: * <code>REGEX</code> is not a valid regular expression.
049: */
050: public NTFTPEntryParser() {
051: this (null);
052: }
053:
054: /**
055: * This constructor allows the creation of an NTFTPEntryParser object
056: * with something other than the default configuration.
057: *
058: * @param config The {@link FTPClientConfig configuration} object used to
059: * configure this parser.
060: * @exception IllegalArgumentException
061: * Thrown if the regular expression is unparseable. Should not be seen
062: * under normal conditions. It it is seen, this is a sign that
063: * <code>REGEX</code> is not a valid regular expression.
064: * @since 1.4
065: */
066: public NTFTPEntryParser(FTPClientConfig config) {
067: super (REGEX);
068: configure(config);
069: }
070:
071: /**
072: * Parses a line of an NT FTP server file listing and converts it into a
073: * usable format in the form of an <code> FTPFile </code> instance. If the
074: * file listing line doesn't describe a file, <code> null </code> is
075: * returned, otherwise a <code> FTPFile </code> instance representing the
076: * files in the directory is returned.
077: * <p>
078: * @param entry A line of text from the file listing
079: * @return An FTPFile instance corresponding to the supplied entry
080: */
081: public FTPFile parseFTPEntry(String entry) {
082: FTPFile f = new FTPFile();
083: f.setRawListing(entry);
084:
085: if (matches(entry)) {
086: String datestr = group(1) + " " + group(2);
087: String dirString = group(3);
088: String size = group(4);
089: String name = group(5);
090: try {
091: f.setTimestamp(super .parseTimestamp(datestr));
092: } catch (ParseException e) {
093: return null; // this is a parsing failure too.
094: }
095:
096: if (null == name || name.equals(".") || name.equals("..")) {
097: return (null);
098: }
099: f.setName(name);
100:
101: if ("<DIR>".equals(dirString)) {
102: f.setType(FTPFile.DIRECTORY_TYPE);
103: f.setSize(0);
104: } else {
105: f.setType(FTPFile.FILE_TYPE);
106: if (null != size) {
107: f.setSize(Long.parseLong(size));
108: }
109: }
110: return (f);
111: }
112: return null;
113: }
114:
115: /**
116: * Defines a default configuration to be used when this class is
117: * instantiated without a {@link FTPClientConfig FTPClientConfig}
118: * parameter being specified.
119: * @return the default configuration for this parser.
120: */
121: public FTPClientConfig getDefaultConfiguration() {
122: return new FTPClientConfig(FTPClientConfig.SYST_NT,
123: DEFAULT_DATE_FORMAT, null, null, null, null);
124: }
125:
126: }
|