1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.net.ftp.parser;
19
20 import java.text.ParseException;
21
22 import org.apache.commons.net.ftp.Configurable;
23 import org.apache.commons.net.ftp.FTPClientConfig;
24 import org.apache.commons.net.ftp.FTPFile;
25 import org.apache.commons.net.ftp.FTPFileEntryParser;
26
27 /**
28 * Implements of {@link FTPFileEntryParser} and {@link Configurable} for OS/2 Systems.
29 *
30 * @see FTPFileEntryParser Usage instructions.
31 */
32 public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
33
34 private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy HH:mm"; // 11-09-01 12:30
35 /**
36 * this is the regular expression used by this parser.
37 */
38 private static final String REGEX = "\\s*([0-9]+)\\s*" + "(\\s+|[A-Z]+)\\s*" + "(DIR|\\s+)\\s*" + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
39 + "(\\S.*)";
40
41 /**
42 * Constructs a new instance.
43 *
44 * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
45 * sign that {@code REGEX} is not a valid regular expression.
46 */
47 public OS2FTPEntryParser() {
48 this(null);
49 }
50
51 /**
52 * Constructs a new instance with something other than the default configuration.
53 *
54 * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
55 * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
56 * sign that {@code REGEX} is not a valid regular expression.
57 * @since 1.4
58 */
59 public OS2FTPEntryParser(final FTPClientConfig config) {
60 super(REGEX);
61 configure(config);
62 }
63
64 /**
65 * Gets a new default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
66 *
67 * @return the default configuration for this parser.
68 */
69 @Override
70 protected FTPClientConfig getDefaultConfiguration() {
71 return new FTPClientConfig(FTPClientConfig.SYST_OS2, DEFAULT_DATE_FORMAT, null);
72 }
73
74 /**
75 * Parses a line of an OS2 FTP server file listing and converts it into a usable format in the form of an {@code FTPFile} instance. If the file
76 * listing line doesn't describe a file, {@code null} is returned, otherwise a {@code FTPFile} instance representing the files in the
77 * directory is returned.
78 *
79 * @param entry A line of text from the file listing
80 * @return An FTPFile instance corresponding to the supplied entry
81 */
82 @Override
83 public FTPFile parseFTPEntry(final String entry) {
84
85 final FTPFile f = new FTPFile();
86 if (matches(entry)) {
87 final String size = group(1);
88 final String attrib = group(2);
89 final String dirString = group(3);
90 final String datestr = group(4) + " " + group(5);
91 final String name = group(6);
92 try {
93 f.setTimestamp(super.parseTimestamp(datestr));
94 } catch (final ParseException e) {
95 // intentionally do nothing
96 }
97
98 // is it a DIR or a file
99 if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR")) {
100 f.setType(FTPFile.DIRECTORY_TYPE);
101 } else {
102 f.setType(FTPFile.FILE_TYPE);
103 }
104
105 // set the name
106 f.setName(name.trim());
107
108 // set the size
109 f.setSize(Long.parseLong(size.trim()));
110
111 return f;
112 }
113 return null;
114
115 }
116
117 }