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 * http://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 import java.text.ParseException;
20
21 import org.apache.commons.net.ftp.FTPClientConfig;
22 import org.apache.commons.net.ftp.FTPFile;
23
24 /**
25 * Implementation of FTPFileEntryParser and FTPFileListParser for OS2 Systems.
26 *
27 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
28 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
29 * @version $Id: OS2FTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
30 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
31 */
32 public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
33
34 {
35
36 private static final String DEFAULT_DATE_FORMAT
37 = "MM-dd-yy HH:mm"; //11-09-01 12:30
38 /**
39 * this is the regular expression used by this parser.
40 */
41 private static final String REGEX =
42 "\\s*([0-9]+)\\s*"
43 + "(\\s+|[A-Z]+)\\s*"
44 + "(DIR|\\s+)\\s*"
45 + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
46 + "(\\S.*)";
47
48 /**
49 * The default constructor for a OS2FTPEntryParser object.
50 *
51 * @exception IllegalArgumentException
52 * Thrown if the regular expression is unparseable. Should not be seen
53 * under normal conditions. It it is seen, this is a sign that
54 * <code>REGEX</code> is not a valid regular expression.
55 */
56 public OS2FTPEntryParser()
57 {
58 this(null);
59 }
60
61 /**
62 * This constructor allows the creation of an OS2FTPEntryParser object
63 * with something other than the default configuration.
64 *
65 * @param config The {@link FTPClientConfig configuration} object used to
66 * configure this parser.
67 * @exception IllegalArgumentException
68 * Thrown if the regular expression is unparseable. Should not be seen
69 * under normal conditions. It it is seen, this is a sign that
70 * <code>REGEX</code> is not a valid regular expression.
71 * @since 1.4
72 */
73 public OS2FTPEntryParser(FTPClientConfig config)
74 {
75 super(REGEX);
76 configure(config);
77 }
78
79 /**
80 * Parses a line of an OS2 FTP server file listing and converts it into a
81 * usable format in the form of an <code> FTPFile </code> instance. If the
82 * file listing line doesn't describe a file, <code> null </code> is
83 * returned, otherwise a <code> FTPFile </code> instance representing the
84 * files in the directory is returned.
85 * <p>
86 * @param entry A line of text from the file listing
87 * @return An FTPFile instance corresponding to the supplied entry
88 */
89 public FTPFile parseFTPEntry(String entry)
90 {
91
92 FTPFile f = new FTPFile();
93 if (matches(entry))
94 {
95 String size = group(1);
96 String attrib = group(2);
97 String dirString = group(3);
98 String datestr = group(4)+" "+group(5);
99 String name = group(6);
100 try
101 {
102 f.setTimestamp(super.parseTimestamp(datestr));
103 }
104 catch (ParseException e)
105 {
106 // intentionally do nothing
107 }
108
109
110 //is it a DIR or a file
111 if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR"))
112 {
113 f.setType(FTPFile.DIRECTORY_TYPE);
114 }
115 else
116 {
117 f.setType(FTPFile.FILE_TYPE);
118 }
119
120
121 //set the name
122 f.setName(name.trim());
123
124 //set the size
125 f.setSize(Long.parseLong(size.trim()));
126
127 return (f);
128 }
129 return null;
130
131 }
132
133 /**
134 * Defines a default configuration to be used when this class is
135 * instantiated without a {@link FTPClientConfig FTPClientConfig}
136 * parameter being specified.
137 * @return the default configuration for this parser.
138 */
139 @Override
140 protected FTPClientConfig getDefaultConfiguration() {
141 return new FTPClientConfig(
142 FTPClientConfig.SYST_OS2,
143 DEFAULT_DATE_FORMAT,
144 null, null, null, null);
145 }
146
147 }